Search in sources :

Example 26 with DocType

use of org.jdom2.DocType in project pcgen by PCGen.

the class TravelMethodFactory method load.

// ### Factory methods ###
public static Vector<TravelMethod> load(File datadir) {
    //Create a new list for the travel methods
    Vector<TravelMethod> tms = new Vector<>();
    File path = new File(datadir, DIR_TRAVELMETHODS);
    if (path.isDirectory()) {
        File[] dataFiles = path.listFiles(new XMLFilter());
        SAXBuilder builder = new SAXBuilder();
        for (int i = 0; i < dataFiles.length; i++) {
            try {
                Document methodSet = builder.build(dataFiles[i]);
                DocType dt = methodSet.getDocType();
                if (dt.getElementName().equals(XML_ELEMENT_TRAVEL)) {
                    //Do work here
                    TravelMethod tm = TravelMethodFactory.create(methodSet);
                    tms.add(tm);
                }
            } catch (Exception e) {
                Logging.errorPrint(e.getMessage(), e);
            }
        }
    } else {
        //$NON-NLS-1$
        Logging.errorPrintLocalised("in_plugin_overland_noDatafile", path.getPath());
    }
    return tms;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) XMLFilter(plugin.overland.gui.XMLFilter) Document(org.jdom2.Document) Vector(java.util.Vector) File(java.io.File) DocType(org.jdom2.DocType) ParseException(java.text.ParseException)

Example 27 with DocType

use of org.jdom2.DocType in project cas by apereo.

the class AbstractSamlObjectBuilder method toDom.

private static org.w3c.dom.Document toDom(final Document doc) throws Exception {
    LOGGER.trace("Creating document from: [{}]", doc);
    val xmlOutputter = new XMLOutputter();
    val elemStrWriter = new StringWriter();
    xmlOutputter.output(doc, elemStrWriter);
    val xmlBytes = elemStrWriter.toString().getBytes(Charset.defaultCharset());
    val dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    dbf.setFeature("http://apache.org/xml/features/validation/schema/normalized-value", false);
    dbf.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
    dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
    dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    return dbf.newDocumentBuilder().parse(new ByteArrayInputStream(xmlBytes));
}
Also used : lombok.val(lombok.val) XMLOutputter(org.jdom2.output.XMLOutputter) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 28 with DocType

use of org.jdom2.DocType in project cas by apereo.

the class AbstractSamlObjectBuilder method constructDocumentFromXml.

/**
 * Construct document from xml.
 *
 * @param xmlString the xml string
 * @return the document
 */
@SuppressWarnings("java:S2755")
public static Document constructDocumentFromXml(final String xmlString) {
    LOGGER.trace("Attempting to construct an instance of Document from xml: [{}]", xmlString);
    try {
        val builder = new SAXBuilder();
        builder.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        builder.setFeature("http://xml.org/sax/features/external-general-entities", false);
        builder.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        return builder.build(new ByteArrayInputStream(xmlString.getBytes(Charset.defaultCharset())));
    } catch (final Exception e) {
        LoggingUtils.error(LOGGER, e);
        return null;
    }
}
Also used : lombok.val(lombok.val) SAXBuilder(org.jdom2.input.SAXBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 29 with DocType

use of org.jdom2.DocType in project jena by apache.

the class GMLReader method newSAXBuilder.

// ---- XXE safe SAXBuilder
private static SAXBuilder newSAXBuilder() {
    SAXBuilder builder = new SAXBuilder();
    builder.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    builder.setFeature("http://xml.org/sax/features/external-general-entities", false);
    builder.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    builder.setExpandEntities(false);
    return builder;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder)

Example 30 with DocType

use of org.jdom2.DocType 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 &lt;rules&gt; 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)

Aggregations

Document (org.jdom2.Document)26 DocType (org.jdom2.DocType)25 Element (org.jdom2.Element)21 SAXBuilder (org.jdom2.input.SAXBuilder)6 File (java.io.File)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 ParseException (java.text.ParseException)2 Vector (java.util.Vector)2 JLabel (javax.swing.JLabel)2 ProgDebugger (jmri.progdebugger.ProgDebugger)2 lombok.val (lombok.val)2 XMLFilter (plugin.overland.gui.XMLFilter)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 List (java.util.List)1 DataConversionException (org.jdom2.DataConversionException)1