use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.
the class MCRObjectCommands method exportMCRObject.
/**
* The method read a MCRObject and use the transformer to write the data to a file. They are any steps to handel
* errors and save the damaged data.
* <ul>
* <li>Read data for object ID in the MCRObject, add ACL's and store it as checked and transformed XML. Return true.
* </li>
* <li>If it can't find a transformer instance (no script file found) it store the checked data with ACL's native in
* the file. Warning and return true.</li>
* <li>If it get an exception while build the MCRObject, it try to read the XML blob and stor it without check and
* ACL's to the file. Warning and return true.</li>
* <li>If it get an exception while store the native data without check, ACĂ–'s and transformation it return a
* warning and false.</li>
* </ul>
*
* @param dir
* the file instance to store
* @param trans
* the XML transformer
* @param nid
* the MCRObjectID
* @return true if the store was okay (see description), else return false
* @throws TransformerException
* @throws IOException
* @throws MCRException
* @throws SAXParseException
*/
private static boolean exportMCRObject(File dir, Transformer trans, String nid) throws TransformerException, IOException, MCRException, SAXParseException {
MCRContent content;
try {
// if object do'snt exist - no exception is catched!
content = MCRXMLMetadataManager.instance().retrieveContent(MCRObjectID.getInstance(nid));
} catch (MCRException ex) {
return false;
}
File xmlOutput = new File(dir, nid + ".xml");
if (trans != null) {
FileOutputStream out = new FileOutputStream(xmlOutput);
StreamResult sr = new StreamResult(out);
Document doc = MCRXMLParserFactory.getNonValidatingParser().parseXML(content);
trans.transform(new org.jdom2.transform.JDOMSource(doc), sr);
} else {
content.sendTo(xmlOutput);
}
LOGGER.info("Object {} saved to {}.", nid, xmlOutput.getCanonicalPath());
return true;
}
use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.
the class MCRExportServlet method doGetPost.
@Override
public void doGetPost(MCRServletJob job) throws Exception {
MCRExportCollection collection = createCollection(job.getRequest());
fillCollection(job.getRequest(), collection);
MCRContent content2export = collection.getContent();
String filename = getProperty(job.getRequest(), "filename");
if (filename == null) {
filename = "export-" + System.currentTimeMillis();
}
job.getResponse().setHeader("Content-Disposition", "inline;filename=\"" + filename + "\"");
String transformerID = job.getRequest().getParameter("transformer");
job.getRequest().setAttribute("XSL.Transformer", transformerID);
getLayoutService().doLayout(job.getRequest(), job.getResponse(), content2export);
}
use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.
the class MCRBasicCommands method checkXMLFile.
/**
* The method parse and check an XML file.
*
* @param fileName
* the location of the xml file
*/
@MCRCommand(syntax = "check file {0}", help = "Checks the data file {0} against the XML Schema.", order = 160)
public static boolean checkXMLFile(String fileName) throws MCRException, SAXParseException, IOException {
if (!fileName.endsWith(".xml")) {
LOGGER.warn("{} ignored, does not end with *.xml", fileName);
return false;
}
File file = new File(fileName);
if (!file.isFile()) {
LOGGER.warn("{} ignored, is not a file.", fileName);
return false;
}
LOGGER.info("Reading file {} ...", file);
MCRContent content = new MCRFileContent(file);
MCRXMLParserFactory.getParser().parseXML(content);
LOGGER.info("The file has no XML errors.");
return true;
}
use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.
the class MCRJerseyUtil method transform.
/**
* Transforms a jdom document to a <code>MCRContent</code> via the <code>MCRLayoutService</code>.
*
* @param document
* the document to transform
* @param request
* the http request
*/
public static MCRContent transform(Document document, HttpServletRequest request) throws Exception {
MCRParameterCollector parameter = new MCRParameterCollector(request);
MCRContent result;
MCRJDOMContent source = new MCRJDOMContent(document);
MCRContentTransformer transformer = MCRLayoutService.getContentTransformer(source.getDocType(), parameter);
if (transformer instanceof MCRParameterizedTransformer) {
result = ((MCRParameterizedTransformer) transformer).transform(source, parameter);
} else {
result = transformer.transform(source);
}
return result;
}
use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.
the class MCRTransferPackagePacker method buildTar.
/**
* Builds a *.tar archive at the path for the given transfer package.
*
* @param pathToTar where to store the *.tar
* @param transferPackage the package to zip
* @throws IOException something went wrong while packing
*/
private void buildTar(Path pathToTar, MCRTransferPackage transferPackage) throws IOException {
FileOutputStream fos = new FileOutputStream(pathToTar.toFile());
try (TarArchiveOutputStream tarOutStream = new TarArchiveOutputStream(fos)) {
for (Entry<String, MCRContent> contentEntry : transferPackage.getContent().entrySet()) {
String filePath = contentEntry.getKey();
byte[] data = contentEntry.getValue().asByteArray();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Adding '{}' to {}", filePath, pathToTar.toAbsolutePath());
}
writeFile(tarOutStream, filePath, data);
writeMD5(tarOutStream, filePath + ".md5", data);
}
}
}
Aggregations