Search in sources :

Example 81 with Content

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

the class MCRSimpleFCTDetector method addRule.

/**
 * Adds a detection rule from the file content type definition XML file. The
 * detector parses the <rules> element provided with each content type
 * in the file content types XML definition.
 *
 * @param type
 *            the file content type the rule is for
 * @param xRules
 *            the rules XML element containing the rules for detecting that
 *            type
 */
public void addRule(MCRFileContentType type, Element xRules) {
    Vector<MCRDetectionRule> rules = new Vector<>();
    rulesTable.put(type, rules);
    typesList.add(type);
    try {
        List extensions = xRules.getChildren("extension");
        for (Object extension : extensions) {
            Element elem = (Element) extension;
            double score = elem.getAttribute("score").getDoubleValue();
            String ext = elem.getTextTrim();
            rules.addElement(new MCRExtensionRule(ext, score));
        }
        List patterns = xRules.getChildren("pattern");
        for (Object pattern1 : patterns) {
            Element elem = (Element) pattern1;
            double score = elem.getAttribute("score").getDoubleValue();
            int offset = elem.getAttribute("offset").getIntValue();
            String format = elem.getAttributeValue("format");
            String pattern = elem.getTextTrim();
            rules.addElement(new MCRPatternRule(pattern, format, offset, score));
        }
        List doctypes = xRules.getChildren("doctype");
        for (Object doctype1 : doctypes) {
            Element elem = (Element) doctype1;
            double score = elem.getAttribute("score").getDoubleValue();
            String doctype = elem.getTextTrim();
            rules.addElement(new MCRDoctypeRule(doctype, score));
        }
        List strings = xRules.getChildren("string");
        for (Object string1 : strings) {
            Element elem = (Element) string1;
            double score = elem.getAttribute("score").getDoubleValue();
            String string = elem.getTextTrim();
            rules.addElement(new MCRStringRule(string, score));
        }
    } catch (Exception exc) {
        String msg = "Error parsing detection rules for file content type " + type.getLabel();
        throw new MCRConfigurationException(msg, exc);
    }
}
Also used : Element(org.jdom2.Element) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) List(java.util.List) Vector(java.util.Vector)

Example 82 with Content

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

the class MCRFile method setContentFrom.

/**
 * Sets the content of this file from a JDOM xml document.
 *
 * @param xml
 *            the JDOM xml document that should be stored as file content
 */
public void setContentFrom(Document xml) {
    Objects.requireNonNull(xml, "jdom xml document is null");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        new XMLOutputter().output(xml, baos);
        baos.close();
    } catch (IOException ignored) {
    }
    setContentFrom(baos.toByteArray());
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Example 83 with Content

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

the class MCRObject method createXML.

/**
 * This method creates a XML stream for all object data.
 *
 * @exception MCRException
 *                if the content of this class is not valid
 * @return a JDOM Document with the XML data of the object as byte array
 */
@Override
public final Document createXML() throws MCRException {
    try {
        Document doc = super.createXML();
        Element elm = doc.getRootElement();
        elm.addContent(mcr_struct.createXML());
        elm.addContent(mcr_metadata.createXML());
        elm.addContent(mcr_service.createXML());
        return doc;
    } catch (MCRException exc) {
        throw new MCRException("The content of '" + mcr_id + "' is invalid.", exc);
    }
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 84 with Content

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

the class MCRWebsiteWriteProtection method getConfiguration.

private static Element getConfiguration() {
    // try to get file
    File configFolder = new File(CONFIG_FOLDER_PATH);
    if (!configFolder.exists()) {
        configFolder.mkdirs();
    }
    // file exist?, return it's content
    if (CONFIG_FILE.exists()) {
        Element config = null;
        // try to get from cache
        if (cacheValid()) {
            config = configCache;
        } else {
            SAXBuilder builder = new SAXBuilder();
            try {
                config = builder.build(CONFIG_FILE).getRootElement();
                // update cache
                updateCache(config);
            } catch (JDOMException | IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return config;
    } else {
        // create XML
        Element config = configToJDOM(false, " ");
        setConfiguration(config);
        return config;
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) File(java.io.File)

Example 85 with Content

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

the class MCRMetaAccessRule method createXML.

/**
 * This method create a XML stream for all data in this class, defined by
 * the MyCoRe XML MCRMetaAccessRule definition for the given subtag.
 *
 * @exception MCRException
 *                if the content of this class is not valid
 * @return a JDOM Element with the XML MCRMetaAccessRule part
 */
@Override
public org.jdom2.Element createXML() throws MCRException {
    Element elm = super.createXML();
    elm.setAttribute("permission", permission);
    elm.addContent(condition.clone());
    return elm;
}
Also used : Element(org.jdom2.Element)

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