Search in sources :

Example 91 with Content

use of org.jdom2.Content in project mycore by MyCoRe-Org.

the class MCRFieldBaseValue method buildXML.

/**
 * Builds a XML representation of this field's value
 *
 * @return a 'field' element with attribute 'name' and the value as element
 *         content
 */
public Element buildXML() {
    Element eField = new Element("field", MCRConstants.MCR_NAMESPACE);
    eField.setAttribute("name", getFieldName());
    eField.addContent(value);
    return eField;
}
Also used : Element(org.jdom2.Element)

Example 92 with Content

use of org.jdom2.Content in project mycore by MyCoRe-Org.

the class MCRSolrTransformerInputDocumentFactory method getDocument.

/* (non-Javadoc)
     * @see org.mycore.solr.index.document.MCRSolrInputDocumentFactory#getDocument(org.mycore.datamodel.metadata.MCRObjectID, org.mycore.common.content.MCRContent)
     */
@Override
public SolrInputDocument getDocument(MCRObjectID id, MCRContent content) throws SAXException, IOException {
    // we need no parameter for searchfields - hopefully
    try {
        SolrInputDocument document;
        if (isJAXBTransformer) {
            MCRParameterCollector param = new MCRParameterCollector();
            @SuppressWarnings("unchecked") MCRXSL2JAXBTransformer<MCRSolrInputDocumentList> jaxbTransformer = (MCRXSL2JAXBTransformer<MCRSolrInputDocumentList>) transformer;
            MCRSolrInputDocumentList input = jaxbTransformer.getJAXBObject(content, param);
            document = MCRSolrInputDocumentGenerator.getSolrInputDocument(input.getDoc().iterator().next());
        } else {
            MCRContent result = transformer.transform(content);
            document = MCRSolrInputDocumentGenerator.getSolrInputDocument(result.asXML().getRootElement());
        }
        return document;
    } catch (TransformerConfigurationException | JAXBException | JDOMException | ParserConfigurationException e) {
        throw new IOException(e);
    }
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) MCRContent(org.mycore.common.content.MCRContent) MCRSolrInputDocumentList(org.mycore.solr.index.document.jaxb.MCRSolrInputDocumentList) SolrInputDocument(org.apache.solr.common.SolrInputDocument) MCRXSL2JAXBTransformer(org.mycore.common.content.transformer.MCRXSL2JAXBTransformer) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 93 with Content

use of org.jdom2.Content in project mycore by MyCoRe-Org.

the class MCRXSL2XMLTransformer method getDocument.

private Document getDocument(JDOMResult result) {
    Document resultDoc = result.getDocument();
    if (resultDoc == null) {
        // Sometimes a transformation produces whitespace strings
        // JDOM would produce a empty document if it detects those
        // So we remove them, if they exists.
        List<Content> transformResult = result.getResult();
        int origSize = transformResult.size();
        Iterator<Content> iterator = transformResult.iterator();
        while (iterator.hasNext()) {
            Content content = iterator.next();
            if (content instanceof Text) {
                String trimmedText = ((Text) content).getTextTrim();
                if (trimmedText.length() == 0) {
                    iterator.remove();
                }
            }
        }
        if (transformResult.size() < origSize) {
            JDOMFactory f = result.getFactory();
            if (f == null) {
                f = new DefaultJDOMFactory();
            }
            resultDoc = f.document(null);
            resultDoc.setContent(transformResult);
        }
    }
    return resultDoc;
}
Also used : DefaultJDOMFactory(org.jdom2.DefaultJDOMFactory) MCRContent(org.mycore.common.content.MCRContent) Content(org.jdom2.Content) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) JDOMFactory(org.jdom2.JDOMFactory) DefaultJDOMFactory(org.jdom2.DefaultJDOMFactory) Text(org.jdom2.Text) Document(org.jdom2.Document)

Example 94 with Content

use of org.jdom2.Content in project mycore by MyCoRe-Org.

the class MCRToPrettyXML method transform.

@Override
public MCRContent transform(MCRContent source) throws IOException {
    MCRXMLContent content;
    try {
        content = (source instanceof MCRXMLContent ? (MCRXMLContent) source : new MCRJDOMContent(source.asXML()));
    } catch (JDOMException | SAXException e) {
        throw new IOException(e);
    }
    if (content != source) {
        content.setName(source.getName());
        content.setLastModified(source.lastModified());
    }
    content.setFormat(Format.getPrettyFormat().setEncoding(getEncoding()));
    return content;
}
Also used : MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRXMLContent(org.mycore.common.content.MCRXMLContent) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) SAXException(org.xml.sax.SAXException)

Example 95 with Content

use of org.jdom2.Content in project mycore by MyCoRe-Org.

the class MCRTransferPackage method getContent.

/**
 * Returns the content for this transfer package. You have to call {@link #build()}
 * before you can retrieve this data.
 *
 * @return a map where key = filename; value = MCRContent
 */
public Map<String, MCRContent> getContent() throws IOException {
    Map<String, MCRContent> content = new HashMap<>();
    // config
    content.put(IMPORT_CONFIG_FILENAME, new MCRJDOMContent(buildImportConfiguration()));
    // objects
    for (MCRObject object : this.objects) {
        String fileName = CONTENT_PATH + object.getId() + ".xml";
        Document xml = object.createXML();
        content.put(fileName, new MCRJDOMContent(xml));
    }
    // file containers
    for (MCRTransferPackageFileContainer fc : this.fileContainers) {
        // derivate
        MCRObjectID derivateId = fc.getDerivateId();
        MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateId);
        Document derivateXML = derivate.createXML();
        String folder = CONTENT_PATH + fc.getName();
        String derivateFileName = folder + "/" + fc.getName() + ".xml";
        content.put(derivateFileName, new MCRJDOMContent(derivateXML));
        // files of derivate
        for (MCRFile file : fc.getFiles()) {
            content.put(folder + file.getAbsolutePath(), file.getContent());
        }
    }
    // classifications
    for (String classId : this.classifications) {
        Document classification = MCRClassificationUtils.asDocument(classId);
        String path = CLASS_PATH + classId + ".xml";
        content.put(path, new MCRJDOMContent(classification));
    }
    return content;
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRObject(org.mycore.datamodel.metadata.MCRObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent)

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