Search in sources :

Example 1 with MCRXSLTransformer

use of org.mycore.common.content.transformer.MCRXSLTransformer 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)

Example 2 with MCRXSLTransformer

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

the class MCRLayoutTransformerFactory method getStylesheets.

private static String[] getStylesheets(String id, String stylesheet) throws TransformerException, SAXException, ParserConfigurationException {
    List<String> ignore = MCRConfiguration.instance().getStrings("MCR.LayoutTransformerFactory.Default.Ignore", Collections.emptyList());
    List<String> defaults = Collections.emptyList();
    if (!ignore.contains(id)) {
        MCRXSLTransformer transformerTest = MCRXSLTransformer.getInstance(stylesheet);
        String outputMethod = transformerTest.getOutputProperties().getProperty(OutputKeys.METHOD, "xml");
        if ("xml".equals(outputMethod) && !isPDF(transformerTest.getMimeType())) {
            defaults = MCRConfiguration.instance().getStrings("MCR.LayoutTransformerFactory.Default.Stylesheets", Collections.emptyList());
        }
    }
    String[] stylesheets = new String[1 + defaults.size()];
    stylesheets[0] = stylesheet;
    for (int i = 0; i < defaults.size(); i++) {
        stylesheets[i + 1] = defaults.get(i);
    }
    return stylesheets;
}
Also used : MCRXSLTransformer(org.mycore.common.content.transformer.MCRXSLTransformer)

Aggregations

MCRXSLTransformer (org.mycore.common.content.transformer.MCRXSLTransformer)2 IOException (java.io.IOException)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 Document (org.jdom2.Document)1 MCRContent (org.mycore.common.content.MCRContent)1 MCRPathContent (org.mycore.common.content.MCRPathContent)1 MCRPath (org.mycore.datamodel.niofs.MCRPath)1 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)1