Example usage for org.dom4j.io DocumentResult DocumentResult

List of usage examples for org.dom4j.io DocumentResult DocumentResult

Introduction

In this page you can find the example usage for org.dom4j.io DocumentResult DocumentResult.

Prototype

public DocumentResult() 

Source Link

Usage

From source file:com.collabnet.ccf.schemageneration.XSLTInitialMFDGeneratorScriptGenerator.java

License:Apache License

/**
 * Applies the transform to the Dom4J document
 * //from   w ww. j  av a 2s  . c o  m
 * @param transformer
 * @param d
 *            the document to transform
 * @param targetSchemaName 
 * @param sourceSchemaName 
 * 
 * @return an array containing a single XML string representing the
 *         transformed document
 * @throws TransformerException
 *             thrown if an XSLT runtime error happens during transformation
 */
private static Document transform(Transformer transformer, Document d, String sourceSchemaName,
        String targetSchemaName) throws TransformerException {
    DocumentSource source = new DocumentSource(d);
    DocumentResult result = new DocumentResult();
    transformer.setParameter("sourceSchema", sourceSchemaName);
    transformer.setParameter("targetSchema", targetSchemaName);
    transformer.transform(source, result);
    return result.getDocument();
}

From source file:com.glaf.core.util.Dom4jUtils.java

License:Apache License

/**
 * ??/*from  w w  w .j  a  va  2 s .c  o m*/
 * 
 * @param document
 *            Document
 * @param stylesheet
 *            String
 * @return Document
 * @throws Exception
 */
public static Document styleDocument(Document document, String stylesheet) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
    DocumentSource source = new DocumentSource(document);
    DocumentResult result = new DocumentResult();
    transformer.transform(source, result);
    Document doc = result.getDocument();
    return doc;
}

From source file:com.thoughtworks.studios.shine.semweb.grddl.GRDDLTransformer.java

License:Apache License

public Graph transform(Document inputDoc, TempGraphFactory graphFactory) throws GrddlTransformException {
    DocumentResult result = new DocumentResult();
    try {/*from   w  ww . ja  v  a  2 s  .c om*/
        DocumentSource source = new DocumentSource(inputDoc);
        transformer.transform(source, result);
        // TODO: likely need to optimize with some sort of streaming document reader here
        Graph graph = graphFactory.createTempGraph();
        graph.addTriplesFromRDFXMLAbbrev(new StringReader(result.getDocument().asXML()));
        return graph;
    } catch (ShineRuntimeException e) {
        LOGGER.error("Could not convert to a graph. The document was: \n" + result.getDocument().asXML(), e);
        throw e;
    } catch (TransformerException e) {
        LOGGER.warn("Could not perform grddl transform. The document was: \n" + result.getDocument().asXML(),
                e);
        throw new GrddlTransformException(e);
    }
}

From source file:com.thoughtworks.studios.shine.xunit.NUnitRDFizer.java

License:Apache License

public Graph importFile(String parentURI, Document document) throws GrddlTransformException {
    DocumentResult result = new DocumentResult();
    DocumentSource source = new DocumentSource(document);
    try {/*from ww  w. j a  va 2s. com*/
        transformer.transform(source, result);
        return jUnitRDFizer.importFile(parentURI, result.getDocument());
    } catch (TransformerException e) {
        throw new ShineRuntimeException(e);
    }
}

From source file:com.xpn.xwiki.objects.BaseElement.java

License:Open Source License

protected Element toXML() {
    DocumentResult domResult = new DocumentResult();

    try {//from  w  w  w  . j a va2  s.  c  om
        Utils.getComponent(XWikiDocumentFilterUtils.class).exportEntity(this,
                new DefaultResultOutputTarget(domResult));
    } catch (Exception e) {
        LOGGER.error("Failed to serialize element to XML", e);

        return null;
    }

    return domResult.getDocument().getRootElement();
}

From source file:com.zimbra.soap.JaxbUtil.java

License:Open Source License

/**
 * JAXB marshaling creates XML which makes heavy use of namespace prefixes.  Historical Zimbra SOAP XML
 * has generally not used them inside the SOAP body.  JAXB uses randomly assigned prefixes such as "ns2" which
 * makes the XML ugly and verbose.  If {@link removePrefixes} is set then all namespace prefix usage is
 * expunged from the XML.//from w w w.j  ava  2  s.co  m
 *
 * @param o - associated JAXB class.  <b>MUST</b> have an @XmlRootElement annotation
 * @param factory - e.g. XmlElement.mFactory or JSONElement.mFactory
 * @param removePrefixes - If true then remove namespace prefixes from unmarshalled XML.
 * @param useContextMarshaller - Set true if Object is a JAXB Request or Response listed in {@code MESSAGE_CLASSES}
 */
public static Element jaxbToElement(Object o, Element.ElementFactory factory, boolean removePrefixes,
        boolean useContextMarshaller) throws ServiceException {
    if (o == null) {
        return null;
    }
    if (Element.JSONElement.mFactory.equals(factory)) {
        return JacksonUtil.jaxbToJSONElement(o);
    }
    try {
        Marshaller marshaller;
        if (useContextMarshaller) {
            marshaller = getContext().createMarshaller();
        } else {
            marshaller = createMarshaller(o.getClass());
        }
        // marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        DocumentResult dr = new DocumentResult();
        marshaller.marshal(o, dr);
        Document theDoc = dr.getDocument();
        org.dom4j.Element rootElem = theDoc.getRootElement();
        if (removePrefixes) {
            JaxbUtil.removeNamespacePrefixes(rootElem);
        }
        return Element.convertDOM(rootElem, factory);
    } catch (Exception e) {
        throw ServiceException.FAILURE("Unable to convert " + o.getClass().getName() + " to Element", e);
    }
}

From source file:com.zimbra.soap.JaxbUtil.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Element jaxbToNamedElement(String name, String namespace, Object o,
        Element.ElementFactory factory) throws ServiceException {
    if (Element.JSONElement.mFactory.equals(factory)) {
        return JacksonUtil.jaxbToJSONElement(o, org.dom4j.QName.get(name, namespace));
    }//from   www.  j a va 2s .  co  m
    try {
        Marshaller marshaller = createMarshaller(o.getClass());
        // marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        DocumentResult dr = new DocumentResult();
        marshaller.marshal(new JAXBElement(new QName(namespace, name), o.getClass(), o), dr);
        Document theDoc = dr.getDocument();
        org.dom4j.Element rootElem = theDoc.getRootElement();
        return Element.convertDOM(rootElem, factory);
    } catch (Exception e) {
        throw ServiceException.FAILURE("Unable to convert " + o.getClass().getName() + " to Element", e);
    }
}

From source file:core.harvesters.Harvester.java

private void createStatistics() {

    if (metadataFormat.equals("dc")) {
        int i = 0;
        for (Record record : records) {
            observer.analayzingRecords(i);

            String license = getMetadataValue("rights", record);
            String formatExtension = getMetadataValue("format", record);
            String date = getMetadataValue("date", record);

            if (licenses.containsKey(license)) {
                int newValue = licenses.get(license) + 1;
                licenses.remove(license);
                licenses.put(license, newValue);
            } else {
                licenses.put(license, 1);
            }//from   w  w w.  ja v a 2 s .c o  m

            if (formatExtensions.containsKey(formatExtension)) {
                int newValue = formatExtensions.get(formatExtension) + 1;
                formatExtensions.remove(formatExtension);
                formatExtensions.put(formatExtension, newValue);
            } else {
                formatExtensions.put(formatExtension, 1);
            }

            if (dates.containsKey(date)) {
                int newValue = dates.get(date) + 1;
                dates.remove(date);
                dates.put(date, newValue);
            } else {
                dates.put(date, 1);
            }

            i++;
        }
    } else if (metadataFormat.equals("p3dm")) {
        int i = 0;
        for (Record record : records) {
            observer.analayzingRecords(i);

            String license = getElement("LICENSE", record).attributeValue("NAME");
            String formatExtension = getElement("MODELFILES", record).element("MODELFILE")
                    .elementText("EXTENSION");
            String date = getElement("DATES", record).element("DATEAVAILABLE").getText();

            if (licenses.containsKey(license)) {
                int newValue = licenses.get(license) + 1;
                licenses.remove(license);
                licenses.put(license, newValue);
            } else {
                licenses.put(license, 1);
            }

            if (formatExtensions.containsKey(formatExtension)) {
                int newValue = formatExtensions.get(formatExtension) + 1;
                formatExtensions.remove(formatExtension);
                formatExtensions.put(formatExtension, newValue);
            } else {
                formatExtensions.put(formatExtension, 1);
            }

            if (dates.containsKey(date)) {
                int newValue = dates.get(date) + 1;
                dates.remove(date);
                dates.put(date, newValue);
            } else {
                dates.put(date, 1);
            }

            i++;
        }
    } else {
        return;
    }

    statisticsXml = DocumentHelper.createDocument();
    Element root = statisticsXml.addElement("StatisticalData");
    root.addAttribute("harvesterUrl", url);
    root.addElement("DocumentCount").setText(String.valueOf(documentCount));

    Element licensesElement = root.addElement("Licenses");
    for (Entry<String, Integer> e : this.licenses.entrySet()) {
        Element elem = licensesElement.addElement("License");
        elem.addAttribute("id", e.getKey());
        elem.setText(e.getValue().toString());
    }

    PieChart pie = new PieChart("Licenses overview", licenses);
    File fileLicensesPie = new File(
            new StringBuilder().append(savePath).append(File.separator).append("licenses.png").toString());
    pie.generateImage(300, 250, fileLicensesPie);

    Element datesElement = root.addElement("Dates");
    for (Entry<String, Integer> e : this.dates.entrySet()) {
        Element elem = datesElement.addElement("Date");
        elem.addAttribute("id", e.getKey());
        elem.setText(e.getValue().toString());
    }

    Histogram histoDates = new Histogram("\"Documents added on\" overview", dates, 0);
    File histoDatesFile = new File(
            new StringBuilder().append(savePath).append(File.separator).append("dates.png").toString());
    histoDates.generateImage(500, 500, histoDatesFile);

    Element formatsElement = root.addElement("Formats");
    for (Entry<String, Integer> e : this.formatExtensions.entrySet()) {
        Element elem = formatsElement.addElement("Format");
        elem.addAttribute("id", e.getKey());
        elem.setText(e.getValue().toString());
    }

    Histogram histoFormats = new Histogram("Documents format overview", formatExtensions, 1);
    File histoFormatsFile = new File(
            new StringBuilder().append(savePath).append(File.separator).append("formats.png").toString());
    histoFormats.generateImage(500, 500, histoFormatsFile);

    // load the transformer using JAXP
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {
        transformer = factory.newTransformer(
                new StreamSource(getClass().getResourceAsStream("/src/main/resources/statistics.xsl")));
    } catch (TransformerConfigurationException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

    // now lets style the given document
    DocumentSource source = new DocumentSource(statisticsXml);
    DocumentResult result = new DocumentResult();
    try {
        transformer.transform(source, result);
    } catch (TransformerException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // return the transformed document
    statisticsHtml = result.getDocument();
}

From source file:cz.mzk.editor.server.fedora.utils.DocumentTransformer.java

License:Open Source License

public static Document transformDocument(Document sourceDocument, Document xslt) {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/*  ww w .  j  a va 2  s  .com*/
        transformer = transformerFactory.newTransformer(new DocumentSource(xslt));
    } catch (TransformerConfigurationException ex) {
        logger.log(Level.SEVERE, null, ex);
        return null;
    }
    Document finalDoc = null;
    if (null != transformer) {
        DocumentSource source = new DocumentSource(sourceDocument);
        DocumentResult result = new DocumentResult();
        try {
            transformer.transform(source, result);
        } catch (TransformerException ex) {
            logger.log(Level.SEVERE, null, ex);
            return null;
        }
        finalDoc = result.getDocument();
    }
    return finalDoc;
}

From source file:datasource.XMLTools.java

License:Open Source License

public static void transformToHtml(Document doc, String xslfilename, String htmlfilename) {
    try {/*from w ww .j av  a 2 s  .  c  o  m*/
        // load the transformer using JAXP
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory
                .newTransformer(new StreamSource(System.out.getClass().getResource(xslfilename).getFile()));

        // now lets style the given document
        DocumentResult result = new DocumentResult();
        transformer.transform(new DocumentSource(doc), result);

        // write to file
        writeToXML(result.getDocument(), htmlfilename);

    } catch (Exception x) {
        System.out.println(x.getMessage());
    }
}