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;
}
});
}
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;
}
Aggregations