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