Example usage for org.jdom2.input.sax XMLReaderSAX2Factory XMLReaderSAX2Factory

List of usage examples for org.jdom2.input.sax XMLReaderSAX2Factory XMLReaderSAX2Factory

Introduction

In this page you can find the example usage for org.jdom2.input.sax XMLReaderSAX2Factory XMLReaderSAX2Factory.

Prototype

public XMLReaderSAX2Factory(boolean validate) 

Source Link

Document

The required details for SAX2.0-based XMLReader creation.

Usage

From source file:com.hack23.cia.service.external.common.impl.XmlAgentImpl.java

License:Apache License

/**
 * Sets the name space on xml stream.//from  w  ww . j a  va2  s . c  o m
 *
 * @param in
 *            the in
 * @param nameSpace
 *            the name space
 * @return the source
 * @throws Exception
 *             the exception
 */
private static Source setNameSpaceOnXmlStream(final InputStream in, final String nameSpace) throws Exception {
    final SAXBuilder sb = new SAXBuilder(new XMLReaderSAX2Factory(false));
    final Document doc = sb.build(in);
    doc.getRootElement().setNamespace(Namespace.getNamespace(nameSpace));
    return new JDOMSource(doc);
}

From source file:com.soulgalore.velocity.MergeXMLWithVelocity.java

License:Apache License

String merge(String xml, String template, String[] extraXMLs) throws JDOMException, IOException {

    final File xmlFile = new File(xml);
    final SAXBuilder b = new SAXBuilder(new XMLReaderSAX2Factory(false));
    final Document doc = b.build(xmlFile);
    context.put(CONTEXT_DOCUMENT, doc);/*w w  w . j a v a  2s  . c  o m*/

    // TODO Old legacy naming, make this cleaner in the future
    int name = 2;
    for (String extraXML : extraXMLs) {
        final File xmlFileExtra = new File(extraXML);
        final Document doc2 = b.build(xmlFileExtra);
        context.put(CONTEXT_DOCUMENT + name, doc2);
        name++;
    }

    final StringWriter writer = new StringWriter();
    final Template fromTemplate = ve.getTemplate(template);
    fromTemplate.merge(context, writer);
    return writer.toString();

}

From source file:edu.unc.lib.deposit.normalize.VocabularyEnforcementJob.java

License:Apache License

@Override
public void runJob() {
    Model model = getWritableModel();/*from  ww w  . j a v a  2s.c om*/

    // Get the list of all objects being ingested in this job
    List<String> resourcePIDs = new ArrayList<>();
    Bag deposit = model.getBag(getDepositPID().getURI());
    walkChildrenDepthFirst(deposit, resourcePIDs, true);

    SAXBuilder sb = new SAXBuilder(new XMLReaderSAX2Factory(false));

    // Vocabulary mappings need to be resolved against the destination since they are not in the hierarchy yet
    PID destinationPID = new PID(getDepositStatus().get(DepositField.containerId.name()));

    for (String resourcePID : resourcePIDs) {
        PID pid = new PID(resourcePID);
        File modsFile = new File(getDescriptionDir(), pid.getUUID() + ".xml");

        // Check if the resource has a description
        if (modsFile.exists()) {
            try {
                Document modsDoc = sb.build(modsFile);

                // Update the MODS document to use approved terms when possible if the vocabularies support remapping
                log.debug("Updating document terms for {} within destination {}", pid, destinationPID);
                boolean modified = updateDocumentTerms(destinationPID, modsDoc.getRootElement());

                // Update the mods document if it was changed
                if (modified) {
                    try (FileOutputStream fos = new FileOutputStream(modsFile)) {
                        new XMLOutputter(Format.getPrettyFormat()).output(modsDoc.getDocument(), fos);
                    }
                }

                // Capture any invalid affiliations as relations
                log.debug("Adding invalid terms for {} within destination {}", pid, destinationPID);
                addInvalidTerms(pid, destinationPID, modsDoc.getRootElement(), model);

            } catch (JDOMException | IOException e) {
                log.error("Failed to parse description file {}", modsFile.getAbsolutePath(), e);
            }
        }
    }
}

From source file:edu.unc.lib.dl.xml.DepartmentOntologyUtil.java

License:Apache License

/**
 * Parses a SKOS XML vocabulary located at filePath and populates a lookup index labels and alternative labels
 * referencing the authoritative version.
 *
 * @param ontologyURL/*from   w  w w.  j av a 2 s  .  co  m*/
 * @throws Exception
 */
private void parseVocabulary(byte[] content) throws Exception {
    departments = new HashMap<String, DepartmentConcept>();

    log.debug("Parsing and building Department vocabulary from {}", getVocabularyURI());

    SAXBuilder sb = new SAXBuilder(new XMLReaderSAX2Factory(false));
    Document skosDoc = sb.build(new ByteArrayInputStream(content));

    // Extract all of the concepts and store them to an index
    List<?> concepts = skosDoc.getRootElement().getChildren("Concept", SKOS_NS);
    Map<String, DepartmentConcept> tempDepts = new HashMap<String, DepartmentConcept>(concepts.size());
    for (Object conceptObj : concepts) {
        DepartmentConcept dept = new DepartmentConcept((Element) conceptObj);
        tempDepts.put(cleanLabel(dept.getIdentifier()), dept);
    }

    // Expand out all the alternative labels into an index and resolve references
    for (Iterator<Entry<String, DepartmentConcept>> deptIt = tempDepts.entrySet().iterator(); deptIt
            .hasNext();) {
        Entry<String, DepartmentConcept> deptEntry = deptIt.next();

        DepartmentConcept dept = deptEntry.getValue();

        // Check if this concept should be ignored in favor of a preferred concept
        if (dept.prefLabel != null) {
            if (departments.containsKey(dept.prefLabel)) {
                // The preferred concept has already been indexed, grab extra labels from this concept and reindex pref
                DepartmentConcept prefDept = departments.get(dept.prefLabel);
                prefDept.merge(dept);
                addLabels(prefDept);
            } else {
                // Since the preferred concept isn't indexed yet, just need to merge labels into it
                DepartmentConcept prefDept = tempDepts.get(dept.prefLabel);
                if (prefDept == null) {
                    log.warn("Preferred label {} referencing a concept which is not present", dept.prefLabel);
                } else {
                    prefDept.merge(dept);
                }
            }
            continue;
        }

        String identifier = cleanLabel(dept.identifier);
        if (departments.containsKey(identifier)
                && dept.identifier.equals(departments.get(identifier).identifier)) {
            log.error("Illegal state, multiple concepts share the identifier {}, ignoring duplicate",
                    identifier);
        } else {
            departments.put(identifier, dept);
        }

        addLabels(dept);
    }
}

From source file:io.sitespeed.jenkins.xml.impl.XMLToPageJDOM.java

License:Open Source License

public Page get(File pageXML) throws IOException {

    final SAXBuilder b = new SAXBuilder(new XMLReaderSAX2Factory(false));
    Document doc;/*from ww w . j a v  a  2  s .  c  o  m*/
    try {
        doc = b.build(pageXML);
    } catch (JDOMException e) {
        throw new IOException(e);
    }

    int numOfHosts = doc.getRootElement().getChild("g").getChild("ydns").getChild("components")
            .getChildren("item").size();

    String url = doc.getRootElement().getChildText("curl");
    Integer score = new Integer(doc.getRootElement().getChildText("o"));

    return new Page(url, score, getRules(doc), numOfHosts, getAssetsSize(doc), getNumberOfAssets(doc));
}

From source file:io.sitespeed.jenkins.xml.impl.XMLToPageTimingsJDOM.java

License:Open Source License

public PageTimings get(File browserTimeXML) throws IOException {

    final SAXBuilder b = new SAXBuilder(new XMLReaderSAX2Factory(false));
    Document doc;/*from www  .j  av a 2 s  .  c  om*/
    try {
        doc = b.build(browserTimeXML);
    } catch (JDOMException e) {
        throw new IOException(e);
    }

    return new PageTimings(getPageData("actualUrl", doc), getPageData("browserName", doc),
            getPageData("browserVersion", doc), doc.getRootElement().getChild("runs").getChildren("run").size(),
            getMeasurements(doc));
}

From source file:io.sitespeed.jenkins.xml.impl.XMLToSummaryJDOM.java

License:Open Source License

public SiteSummary get(File summaryXML) throws IOException {

    final SAXBuilder b = new SAXBuilder(new XMLReaderSAX2Factory(false));
    Document doc;//  w w w .j av a2 s.c o  m
    try {
        doc = b.build(summaryXML);
    } catch (JDOMException e) {
        throw new IOException(e);
    }

    Map<String, HashMap<String, String>> values = new HashMap<String, HashMap<String, String>>();
    // TODO today the cache time is in seconds, probably should be converted to minutes?
    for (Element metric : doc.getRootElement().getChild("metrics").getChildren()) {
        String name = metric.getName();
        name = fixBrowserKey(name);
        HashMap<String, String> the = new HashMap<String, String>();
        for (Element valueType : metric.getChildren()) {
            the.put(valueType.getName(), valueType.getValue());
        }
        values.put(name, the);
    }

    int pages = new Integer(doc.getRootElement().getChild("pages").getValue());

    return new SiteSummary(values, pages);
}