Search in sources :

Example 56 with Content

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

the class MCRXSL2XMLTransformer method getTransformedContent.

@Override
protected MCRContent getTransformedContent(MCRContent source, XMLReader reader, TransformerHandler transformerHandler) throws IOException, SAXException {
    JDOMResult result = new JDOMResult();
    transformerHandler.setResult(result);
    // Parse the source XML, and send the parse events to the
    // TransformerHandler.
    reader.parse(source.getInputSource());
    Document resultDoc = getDocument(result);
    if (resultDoc == null) {
        throw new MCRConfigurationException("Stylesheets " + Arrays.asList(templateSources) + " does not return any content for " + source.getSystemId());
    }
    return new MCRJDOMContent(resultDoc);
}
Also used : JDOMResult(org.jdom2.transform.JDOMResult) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) Document(org.jdom2.Document)

Example 57 with Content

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

the class MCRDynamicURIResolver method resolveVariablesFromElement.

/**
 * This method runs through the whole content of the startElement and
 * tries to resolve all variables in texts and attributes.
 *
 * @param startElement where to start to resolve the variables
 * @param variablesMap a map of all variables
 */
protected void resolveVariablesFromElement(Element startElement, Hashtable<String, String> variablesMap) {
    Iterator<Element> it = startElement.getDescendants(Filters.element());
    MCRTextResolver varResolver = new MCRTextResolver(variablesMap);
    while (it.hasNext()) {
        Element element = it.next();
        // text
        String text = element.getText();
        if (text != null && !text.equals("") && text.contains("{")) {
            element.setText(varResolver.resolve(text));
        }
        // attributes
        for (Attribute attrib : element.getAttributes()) {
            String attribValue = attrib.getValue();
            if (attribValue.contains("{")) {
                attrib.setValue(varResolver.resolve(attribValue));
            }
        }
    }
}
Also used : Attribute(org.jdom2.Attribute) MCRTextResolver(org.mycore.common.MCRTextResolver) Element(org.jdom2.Element)

Example 58 with Content

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

the class MCRCompressServlet method sendObject.

private void sendObject(MCRObjectID id, MCRServletJob job, T container) throws Exception {
    MCRContent content = MCRXMLMetadataManager.instance().retrieveContent(id);
    if (content == null) {
        throw new FileNotFoundException("Could not find object: " + id);
    }
    long lastModified = MCRXMLMetadataManager.instance().getLastModified(id);
    HttpServletRequest req = job.getRequest();
    byte[] metaDataContent = getMetaDataContent(content, req);
    sendMetadataCompressed("metadata.xml", metaDataContent, lastModified, container);
    // zip all derivates
    List<Element> li = content.asXML().getRootElement().getChild("structure").getChild("derobjects").getChildren("derobject");
    for (Element el : li) {
        if (el.getAttributeValue("inherited").equals("0")) {
            String ownerID = el.getAttributeValue("href", XLINK_NAMESPACE);
            // here the access check is tested only against the derivate
            if (MCRAccessManager.checkPermission(ownerID, PERMISSION_READ) && MCRXMLFunctions.isDisplayedEnabledDerivate(ownerID)) {
                sendDerivate(MCRObjectID.getInstance(ownerID), null, container);
            }
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Element(org.jdom2.Element) FileNotFoundException(java.io.FileNotFoundException) MCRContent(org.mycore.common.content.MCRContent)

Example 59 with Content

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

the class MyCoReWebPageProvider method addSection.

/**
 * Adds a section to the MyCoRe webpage
 * @param title the title of the section
 * @param content list of content added to the section
 * @param lang the language of the section specified by a language key.
 * @return added section
 */
public Element addSection(String title, List<Content> content, String lang) {
    Element section = new Element(XML_SECTION);
    if (lang != null) {
        section.setAttribute(XML_LANG, lang, Namespace.XML_NAMESPACE);
    }
    if (title != null && !title.equals("")) {
        section.setAttribute(XML_TITLE, title);
    }
    section.addContent(content);
    this.xml.getRootElement().addContent(section);
    return section;
}
Also used : Element(org.jdom2.Element)

Example 60 with Content

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

the class MCRMetsSave method saveMets.

/**
 * Saves the content of the given document to file, if no mets present and then adds the file to
 * the derivate with the given id. The name of the file depends on property
 * 'MCR.Mets.Filename'. If this property has not been set 'mets.xml' is used
 * as a default filename.
 *
 * @param overwrite
 *          if true existing mets-file will be overwritten
 * @param validate
 *          if true the document will be validated before its stored
 * @return
 *          true if the given document was successfully saved, otherwise false
 */
public static synchronized boolean saveMets(Document document, MCRObjectID derivateId, boolean overwrite, boolean validate) {
    // add the file to the existing derivate in ifs
    MCRPath metsFile = getMetsFile(derivateId.toString());
    if (metsFile == null) {
        metsFile = createMetsFile(derivateId.toString());
    } else if (!overwrite) {
        return false;
    }
    if (validate && !Mets.isValid(document)) {
        LOGGER.warn("Storing mets.xml for {} failed cause the given document was invalid.", derivateId);
        return false;
    }
    try (OutputStream metsOut = Files.newOutputStream(metsFile)) {
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        xout.output(document, metsOut);
        LOGGER.info("Storing file content from \"{}\" to derivate \"{}\"", getMetsFileName(), derivateId);
    } catch (Exception e) {
        LOGGER.error(e);
        return false;
    }
    return true;
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) OutputStream(java.io.OutputStream) MCRPath(org.mycore.datamodel.niofs.MCRPath) URISyntaxException(java.net.URISyntaxException) JDOMException(org.jdom2.JDOMException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

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