Search in sources :

Example 46 with Content

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;
}
Also used : JDOMSource(org.jdom2.transform.JDOMSource) MCRException(org.mycore.common.MCRException) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) File(java.io.File)

Example 47 with Content

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));
}
Also used : Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) File(java.io.File)

Example 48 with Content

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;
}
Also used : Element(org.jdom2.Element)

Example 49 with Content

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);
}
Also used : Element(org.jdom2.Element)

Example 50 with Content

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);
    }
}
Also used : Schema(javax.xml.validation.Schema) MCRBaseContent(org.mycore.common.content.MCRBaseContent) JDOMSource(org.jdom2.transform.JDOMSource) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) Document(org.jdom2.Document) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) JDOMException(org.jdom2.JDOMException) MCRContent(org.mycore.common.content.MCRContent) SAXException(org.xml.sax.SAXException)

Aggregations

Element (org.jdom2.Element)77 Document (org.jdom2.Document)27 IOException (java.io.IOException)18 JDOMException (org.jdom2.JDOMException)16 File (java.io.File)11 MCRException (org.mycore.common.MCRException)11 Content (org.jdom2.Content)10 MCRContent (org.mycore.common.content.MCRContent)10 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)10 XMLOutputter (org.jdom2.output.XMLOutputter)9 SAXBuilder (org.jdom2.input.SAXBuilder)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Attribute (org.jdom2.Attribute)6 MCRPath (org.mycore.datamodel.niofs.MCRPath)6 SAXException (org.xml.sax.SAXException)6 JsonElement (com.google.gson.JsonElement)5 Color (java.awt.Color)4 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)4 MCRObject (org.mycore.datamodel.metadata.MCRObject)4