Example usage for org.jdom2 Document Document

List of usage examples for org.jdom2 Document Document

Introduction

In this page you can find the example usage for org.jdom2 Document Document.

Prototype

public Document() 

Source Link

Document

Creates a new empty document.

Usage

From source file:org.goobi.production.export.ExportXmlLog.java

License:Open Source License

/**
 * This method transforms the xml log using a xslt file and opens a new window with the output file
 * /*from  w  ww  .j  a  va 2 s. c o m*/
 * @param out ServletOutputStream
 * @param doc the xml document to transform
 * @param filename the filename of the xslt
 * @throws XSLTransformException
 * @throws IOException
 */

public void XmlTransformation(OutputStream out, Document doc, String filename)
        throws XSLTransformException, IOException {
    Document docTrans = new Document();
    if (filename != null && filename.equals("")) {
        XSLTransformer transformer;
        transformer = new XSLTransformer(filename);
        docTrans = transformer.transform(doc);
    } else {
        docTrans = doc;
    }
    Format format = Format.getPrettyFormat();
    format.setEncoding("utf-8");
    XMLOutputter xmlOut = new XMLOutputter(format);

    xmlOut.output(docTrans, out);

}

From source file:org.goobi.production.export.ExportXmlLog.java

License:Open Source License

/**
 * This method exports the production metadata for al list of processes as a single file to a given stream.
 * //from w  ww.j  a  v  a2  s .com
 * @param processList
 * @param outputStream
 * @param xslt
 */

public void startExport(List<Process> processList, OutputStream outputStream, String xslt) {
    Document answer = new Document();
    Element root = new Element("processes");
    answer.setRootElement(root);
    Namespace xmlns = Namespace.getNamespace("http://www.goobi.io/logfile");

    Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.addNamespaceDeclaration(xsi);
    root.setNamespace(xmlns);
    Attribute attSchema = new Attribute("schemaLocation", "http://www.goobi.io/logfile" + " XML-logfile.xsd",
            xsi);
    root.setAttribute(attSchema);
    for (Process p : processList) {
        Document doc = createDocument(p, false);
        Element processRoot = doc.getRootElement();
        processRoot.detach();
        root.addContent(processRoot);
    }

    XMLOutputter outp = new XMLOutputter();
    outp.setFormat(Format.getPrettyFormat());

    try {

        outp.output(answer, outputStream);
    } catch (IOException e) {

    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                outputStream = null;
            }
        }
    }

}

From source file:org.jumpmind.metl.core.runtime.component.XmlFormatter.java

License:Open Source License

private void createXml(ISendMessageCallback callback) {

    Document generatedXml = new Document();
    Stack<DocElement> parentStack = new Stack<DocElement>();
    ArrayList<String> outboundPayload = new ArrayList<String>();

    for (Message msg : messagesToProcess) {
        processMsgEntities(parentStack, msg, generatedXml);
    }// w w w .j  a  va2  s .  c o  m
    XMLOutputter xmlOutputter = new XMLOutputter();
    Format format = null;
    if (xmlFormat.equals(COMPACT_FORMAT)) {
        format = Format.getCompactFormat();
    } else if (xmlFormat.equals(RAW_FORMAT)) {
        format = Format.getRawFormat();
    } else {
        format = Format.getPrettyFormat();
    }
    xmlOutputter.setFormat(format);
    outboundPayload.add(xmlOutputter.outputString(generatedXml));
    callback.sendTextMessage(null, outboundPayload);
}

From source file:org.kitodo.docket.ExportXmlLog.java

License:Open Source License

/**
 * This method exports the production metadata for al list of processes as a
 * single file to a given stream.// w  w  w.j  av a 2s .  c o m
 *
 * @param docketDataList
 *            a list of Docket data
 * @param outputStream
 *            The output stream, to write the docket to.
 */

void startMultipleExport(Iterable<DocketData> docketDataList, OutputStream outputStream) {
    Document answer = new Document();
    Element root = new Element("processes");
    answer.setRootElement(root);
    Namespace xmlns = Namespace.getNamespace(NAMESPACE);

    Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.addNamespaceDeclaration(xsi);
    root.setNamespace(xmlns);
    Attribute attSchema = new Attribute("schemaLocation", NAMESPACE + " XML-logfile.xsd", xsi);
    root.setAttribute(attSchema);
    for (DocketData docketData : docketDataList) {
        Document doc = createDocument(docketData, false);
        Element processRoot = doc.getRootElement();
        processRoot.detach();
        root.addContent(processRoot);
    }

    XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat());

    try {
        outp.output(answer, outputStream);
    } catch (IOException e) {
        logger.error("Generating XML Output failed.", e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                logger.error("Closing the output stream failed.", e);
            }
        }
    }

}

From source file:org.mule.tools.apikit.output.MuleConfigGenerator.java

License:Open Source License

private Document getDocument(API api) throws IOException, JDOMException {
    SAXBuilder saxBuilder = new SAXBuilder(XMLReaders.NONVALIDATING);
    Document doc;/*from   ww  w.  j a v a 2 s  . c  om*/
    File xmlFile = api.getXmlFile(rootDirectory);
    if (!xmlFile.exists() || xmlFile.length() == 0) {
        xmlFile.getParentFile().mkdirs();
        doc = new Document();
        doc.setRootElement(new MuleScope().generate());
    } else {
        InputStream xmlInputStream = new FileInputStream(xmlFile);
        doc = saxBuilder.build(xmlInputStream);
    }
    return doc;
}

From source file:org.mycore.common.MCRUtils.java

License:Open Source License

/**
 * The method wrap the org.jdom2.Element in a org.jdom2.Document and write it to a file.
 * //from ww  w. j  av  a  2s  .  co m
 * @param elm
 *            the JDOM Document
 * @param xml
 *            the File instance
 * @deprecated use {@link MCRJDOMContent#sendTo(Path, java.nio.file.CopyOption...)}
 */
@Deprecated
public static void writeElementToFile(Element elm, File xml) {
    writeJDOMToFile(new Document().addContent(elm), xml);
}

From source file:org.mycore.common.MCRUtils.java

License:Open Source License

/**
 * The method wrap the org.jdom2.Element in a org.jdom2.Document and write it to Sysout.
 * //from   www.  j a va  2s  . c o m
 * @param elm
 *            the JDOM Document
 * @deprecated use {@link MCRJDOMContent#sendTo(java.io.OutputStream)}
 */
@Deprecated
public static void writeElementToSysout(Element elm) {
    writeJDOMToSysout(new Document().addContent(elm));
}

From source file:org.mycore.restapi.v1.MCRRestAPIClassifications.java

License:Open Source License

/**
 *
 * @param info - a Jersey Context Object for URI
 *     Possible values are: json | xml (required)
 *//*from  w  w w.j a v a  2  s. c  o m*/
@GET
@Path("/")
@Produces({ MediaType.TEXT_XML + ";charset=UTF-8", MediaType.APPLICATION_JSON + ";charset=UTF-8" })
public Response listClassifications(@Context UriInfo info,
        @QueryParam("format") @DefaultValue("json") String format) {
    if (FORMAT_XML.equals(format)) {
        StringWriter sw = new StringWriter();

        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        Document docOut = new Document();
        Element eRoot = new Element("mycoreclassifications");
        docOut.setRootElement(eRoot);

        for (MCRCategory cat : DAO.getRootCategories()) {
            eRoot.addContent(
                    new Element("mycoreclass").setAttribute("ID", cat.getId().getRootID()).setAttribute("href",
                            info.getAbsolutePathBuilder().path(cat.getId().getRootID()).build().toString()));
        }
        try {
            xout.output(docOut, sw);
            return Response.ok(sw.toString()).type("application/xml; charset=UTF-8").build();
        } catch (IOException e) {
            //ToDo
        }
    }

    if (FORMAT_JSON.equals(format)) {
        StringWriter sw = new StringWriter();
        try {
            JsonWriter writer = new JsonWriter(sw);
            writer.setIndent("    ");
            writer.beginObject();
            writer.name("mycoreclass");
            writer.beginArray();
            for (MCRCategory cat : DAO.getRootCategories()) {
                writer.beginObject();
                writer.name("ID").value(cat.getId().getRootID());
                writer.name("href")
                        .value(info.getAbsolutePathBuilder().path(cat.getId().getRootID()).build().toString());
                writer.endObject();
            }
            writer.endArray();
            writer.endObject();

            writer.close();

            return Response.ok(sw.toString()).type("application/json; charset=UTF-8").build();
        } catch (IOException e) {
            //toDo
        }
    }
    return Response.status(Status.BAD_REQUEST).build();
}

From source file:org.mycore.restapi.v1.MCRRestAPIMessages.java

License:Open Source License

/**
 * returns a single messages entry.//from  w  w  w . j  a va 2 s  .co m
 * 
 * @param info - a Jersey Context Object for URI
 * @param format 
 *     Possible values are: props (default) | json | xml (required)
 */
@GET
@Path("/{value}")
@Produces({ MediaType.TEXT_XML + ";charset=UTF-8", MediaType.APPLICATION_JSON + ";charset=UTF-8",
        MediaType.TEXT_PLAIN + ";charset=UTF-8" })
public Response getMessage(@Context UriInfo info, @PathParam("value") String key,
        @QueryParam("lang") @DefaultValue("de") String lang,
        @QueryParam("format") @DefaultValue("text") String format) {

    Locale locale = Locale.forLanguageTag(lang);
    String result = MCRTranslation.translate(key, locale);
    try {
        if (FORMAT_PROPERTY.equals(format)) {
            return Response.ok(key + "=" + result).type("text/plain; charset=ISO-8859-1").build();
        }
        if (FORMAT_XML.equals(format)) {
            Document doc = new Document();
            Element root = new Element("entry");
            root.setAttribute("key", key);
            root.setText(result);
            doc.addContent(root);
            StringWriter sw = new StringWriter();
            XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
            outputter.output(doc, sw);
            return Response.ok(sw.toString()).type("application/xml; charset=UTF-8").build();
        }
        if (FORMAT_JSON.equals(format)) {
            StringWriter sw = new StringWriter();
            JsonWriter writer = new JsonWriter(sw);
            writer.setIndent("    ");
            writer.beginObject();
            writer.name(key);
            writer.value(result);
            writer.endObject();
            writer.close();
            return Response.ok(sw.toString()).type("application/json; charset=UTF-8").build();
        }
        //text only
        return Response.ok(result).type("text/plain; charset=UTF-8").build();
    } catch (IOException e) {
        //toDo
    }
    return Response.status(Status.BAD_REQUEST).build();
}

From source file:org.onosproject.provider.netconf.flow.impl.XmlBuilder.java

License:Apache License

public String buildAclRequestXml(AccessList accessList) {
    Document doc = new Document();
    Namespace namespaceRpc = Namespace.getNamespace("urn:ietf:params:xml:ns:netconf:base:1.0");
    Namespace accessNamespaceRpc = Namespace.getNamespace("urn:ietf:params:xml:ns:yang:ietf-acl");
    doc.setRootElement(new Element("rpc", namespaceRpc).setAttribute("message-id", "101"));

    /**//  ww w. j a va2s. co m
     * Access list elements of given ACL model.
     */
    Element access = new Element("access-list", accessNamespaceRpc);
    access.addContent(new Element("acl-name", accessNamespaceRpc).setText(accessList.getAclName()));
    // access.addContent(accessEntries);

    if (!accessList.getAccessListEntries().isEmpty() && accessList.getAccessListEntries() != null) {
        for (int accessEntryIntVlu = 0; accessEntryIntVlu < accessList.getAccessListEntries()
                .size(); accessEntryIntVlu++) {
            access.addContent(getAccessEntries(accessEntryIntVlu, accessList, accessNamespaceRpc));
        }
    }

    /**
     * edit-config operation for given ACL model.
     */
    Element editConfig = new Element("edit-config", namespaceRpc);
    editConfig.addContent(new Element("target", namespaceRpc).addContent(new Element("running", namespaceRpc)));
    editConfig
            .addContent(new Element("config", Namespace.getNamespace("urn:ietf:params:xml:ns:netconf:base:1.0"))
                    .addContent(access));

    doc.getRootElement().addContent(editConfig);
    XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
    String outputString = xmlOutputter.outputString(doc);

    return outputString;
}