Search in sources :

Example 1 with MCRPathContent

use of org.mycore.common.content.MCRPathContent in project mycore by MyCoRe-Org.

the class MCRBasketPersistence method retrieveBasket.

/**
 * Retrieves a basket from an XML file in the given derivate.
 */
public static MCRBasket retrieveBasket(String derivateID) throws Exception {
    MCRPath file = getBasketFile(derivateID);
    Document xml = new MCRPathContent(file).asXML();
    MCRBasket basket = new MCRBasketXMLParser().parseXML(xml);
    basket.setDerivateID(derivateID);
    return basket;
}
Also used : MCRPathContent(org.mycore.common.content.MCRPathContent) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document)

Example 2 with MCRPathContent

use of org.mycore.common.content.MCRPathContent in project mycore by MyCoRe-Org.

the class MCRDerivateContentTransformerServlet method getContent.

@Override
public MCRContent getContent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String pathInfo = req.getPathInfo();
    if (pathInfo.startsWith("/")) {
        pathInfo = pathInfo.substring(1);
    }
    String[] pathTokens = pathInfo.split("/");
    String derivate = pathTokens[0];
    String path = pathInfo.substring(derivate.length());
    LOGGER.debug("Derivate : {}", derivate);
    LOGGER.debug("Path : {}", path);
    MCRPath mcrPath = MCRPath.getPath(derivate, path);
    MCRContent pc = new MCRPathContent(mcrPath);
    FileTime lastModifiedTime = Files.getLastModifiedTime(mcrPath);
    MCRFrontendUtil.writeCacheHeaders(resp, (long) CACHE_TIME, lastModifiedTime.toMillis(), true);
    try {
        return getLayoutService().getTransformedContent(req, resp, pc);
    } catch (TransformerException | SAXException e) {
        throw new IOException("could not transform content", e);
    }
}
Also used : MCRPathContent(org.mycore.common.content.MCRPathContent) FileTime(java.nio.file.attribute.FileTime) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRContent(org.mycore.common.content.MCRContent) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 3 with MCRPathContent

use of org.mycore.common.content.MCRPathContent in project mycore by MyCoRe-Org.

the class MCRMetsResolver method resolve.

@Override
public Source resolve(String href, String base) throws TransformerException {
    String id = href.substring(href.indexOf(":") + 1);
    LOGGER.debug("Reading METS for ID {}", id);
    MCRObjectID objId = MCRObjectID.getInstance(id);
    if (!objId.getTypeId().equals("derivate")) {
        String derivateID = getDerivateFromObject(id);
        if (derivateID == null) {
            return new JDOMSource(new Element("mets", Namespace.getNamespace("mets", "http://www.loc.gov/METS/")));
        }
        id = derivateID;
    }
    MCRPath metsPath = MCRPath.getPath(id, "/mets.xml");
    try {
        if (Files.exists(metsPath)) {
            // ignoreNodes.add(metsFile);
            return new MCRPathContent(metsPath).getSource();
        }
        Document mets = MCRMETSGeneratorFactory.create(MCRPath.getPath(id, "/")).generate().asDocument();
        return new JDOMSource(mets);
    } catch (Exception e) {
        throw new TransformerException(e);
    }
}
Also used : Element(org.jdom2.Element) MCRPathContent(org.mycore.common.content.MCRPathContent) JDOMSource(org.jdom2.transform.JDOMSource) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException)

Example 4 with MCRPathContent

use of org.mycore.common.content.MCRPathContent in project mycore by MyCoRe-Org.

the class MCRMetsCommands method validateSelectedMets.

@MCRCommand(syntax = "validate selected mets", help = "validates all mets.xml of selected derivates", order = 10)
public static void validateSelectedMets() {
    List<String> selectedObjectIDs = MCRObjectCommands.getSelectedObjectIDs();
    for (String objectID : selectedObjectIDs) {
        LOGGER.info("Validate mets.xml of {}", objectID);
        MCRPath metsFile = MCRPath.getPath(objectID, "/mets.xml");
        if (Files.exists(metsFile)) {
            try {
                MCRContent content = new MCRPathContent(metsFile);
                InputStream metsIS = content.getInputStream();
                METSValidator mv = new METSValidator(metsIS);
                List<ValidationException> validationExceptionList = mv.validate();
                if (validationExceptionList.size() > 0) {
                    invalidMetsQueue.add(objectID);
                }
                for (ValidationException validationException : validationExceptionList) {
                    LOGGER.error(validationException.getMessage());
                }
            } catch (IOException e) {
                LOGGER.error("Error while reading mets.xml of {}", objectID, e);
            } catch (JDOMException e) {
                LOGGER.error("Error while parsing mets.xml of {}", objectID, e);
            }
        }
    }
}
Also used : ValidationException(org.mycore.mets.validator.validators.ValidationException) InputStream(java.io.InputStream) MCRPathContent(org.mycore.common.content.MCRPathContent) METSValidator(org.mycore.mets.validator.METSValidator) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) JDOMException(org.jdom2.JDOMException) MCRContent(org.mycore.common.content.MCRContent) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 5 with MCRPathContent

use of org.mycore.common.content.MCRPathContent in project mycore by MyCoRe-Org.

the class MCRDerivateCommands method transformXMLMatchingPatternWithStylesheet.

@MCRCommand(syntax = "transform xml matching file name pattern {0} in derivate {1} with stylesheet {2}", help = "Finds all files in Derivate {1} which match the pattern {0} (the complete path with regex: or glob:*.xml syntax) and transforms them with stylesheet {2}")
public static void transformXMLMatchingPatternWithStylesheet(String pattern, String derivate, String stylesheet) throws IOException {
    MCRXSLTransformer transformer = new MCRXSLTransformer(stylesheet);
    MCRPath derivateRoot = MCRPath.getPath(derivate, "/");
    PathMatcher matcher = derivateRoot.getFileSystem().getPathMatcher(pattern);
    Files.walkFileTree(derivateRoot, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (matcher.matches(file)) {
                LOGGER.info("The file {} matches the pattern {}", file, pattern);
                MCRContent sourceContent = new MCRPathContent(file);
                MCRContent resultContent = transformer.transform(sourceContent);
                try {
                    Document source = sourceContent.asXML();
                    Document result = resultContent.asXML();
                    LOGGER.info("Transforming complete!");
                    if (!MCRXMLHelper.deepEqual(source, result)) {
                        LOGGER.info("Writing result..");
                        resultContent.sendTo(file, StandardCopyOption.REPLACE_EXISTING);
                    } else {
                        LOGGER.info("Result and Source is the same..");
                    }
                } catch (JDOMException | SAXException e) {
                    throw new IOException("Error while processing file : " + file, e);
                }
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) PathMatcher(java.nio.file.PathMatcher) MCRPathContent(org.mycore.common.content.MCRPathContent) MCRXSLTransformer(org.mycore.common.content.transformer.MCRXSLTransformer) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRPathContent (org.mycore.common.content.MCRPathContent)9 MCRPath (org.mycore.datamodel.niofs.MCRPath)9 MCRContent (org.mycore.common.content.MCRContent)7 Document (org.jdom2.Document)5 IOException (java.io.IOException)4 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)3 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)3 TransformerException (javax.xml.transform.TransformerException)2 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)2 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)2 InputStream (java.io.InputStream)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 PathMatcher (java.nio.file.PathMatcher)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 FileTime (java.nio.file.attribute.FileTime)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Element (org.jdom2.Element)1