use of org.jdom2.Content 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.jdom2.Content in project mycore by MyCoRe-Org.
the class MCRCommandLineInterface method getURI.
/**
* Reads XML content from URIResolver and sends output to a local file.
*/
public static void getURI(String uri, String file) throws Exception {
Element resolved = MCRURIResolver.instance().resolve(uri);
Element cloned = resolved.clone();
new MCRJDOMContent(cloned).sendTo(new File(file));
}
use of org.jdom2.Content in project mycore by MyCoRe-Org.
the class MCRBasketXMLBuilder method buildXML.
/**
* Builds an XML representation of a basket entry.
* Note that setContent() or resolveContent() must have been called before
* if XML content of the basket entry's object should be included.
*/
public Element buildXML(MCRBasketEntry entry) {
Element xml = new Element("entry");
xml.setAttribute("id", entry.getID());
xml.setAttribute("uri", entry.getURI());
if (addContent) {
Element content = entry.getContent();
if (content != null)
xml.addContent(content.clone());
}
String comment = entry.getComment();
if (comment != null)
xml.addContent(new Element("comment").setText(comment));
return xml;
}
use of org.jdom2.Content in project mycore by MyCoRe-Org.
the class MCRPathXML method addString.
private static void addString(Element parent, String itemName, String content, boolean preserve) {
if (content == null) {
return;
}
Element child = new Element(itemName).addContent(content);
if (preserve) {
child.setAttribute("space", "preserve", MCRConstants.XML_NAMESPACE);
}
parent.addContent(child);
}
use of org.jdom2.Content in project mycore by MyCoRe-Org.
the class MCRDOIRegistrationService method transformToDatacite.
protected Document transformToDatacite(MCRDigitalObjectIdentifier doi, MCRBase mcrBase) throws MCRPersistentIdentifierException {
MCRObjectID id = mcrBase.getId();
MCRBaseContent content = new MCRBaseContent(mcrBase);
try {
MCRContent transform = MCRContentTransformerFactory.getTransformer(this.transformer).transform(content);
Document dataciteDocument = transform.asXML();
insertDOI(dataciteDocument, doi);
Schema dataciteSchema = loadDataciteSchema();
try {
dataciteSchema.newValidator().validate(new JDOMSource(dataciteDocument));
} catch (SAXException e) {
String translatedInformation = MCRTranslation.translate(TRANSLATE_PREFIX + ERR_CODE_1_2);
throw new MCRPersistentIdentifierException("The document " + id + " does not generate well formed Datacite!", translatedInformation, ERR_CODE_1_2, e);
}
return dataciteDocument;
} catch (IOException | JDOMException | SAXException e) {
throw new MCRPersistentIdentifierException("Could not transform the content of " + id + " with the transformer " + transformer, e);
}
}
Aggregations