Example usage for org.dom4j Document getXMLEncoding

List of usage examples for org.dom4j Document getXMLEncoding

Introduction

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

Prototype

String getXMLEncoding();

Source Link

Document

Return the encoding of this document, as part of the XML declaration This is null when unspecified or when it is not known (such as when the Document was created in memory) or when the implementation does not support this operation.

Usage

From source file:ch.javasoft.xml.config.XmlPrint.java

License:BSD License

/**
 * Prints the xml declaration (the first line of an xml file)
 * @param doc      the xml document//from   w  w w .j a va  2  s  .  c  om
 * @param writer   the writer to use for the output
 */
protected void printDeclaration(Document doc, PrintWriter writer) {
    String encoding = doc.getXMLEncoding();

    // Assume 1.0 version
    writer.write("<?xml version=\"1.0\"");
    if (encoding == null || encoding.equals("UTF8")) {
        writer.write(" encoding=\"UTF-8\"");
    } else {
        writer.write(" encoding=\"" + encoding + "\"");
    }
    writer.write("?>");
    writer.println();
}

From source file:com.glaf.jbpm.deploy.MxJbpmProcessDeployer.java

License:Apache License

public ProcessDefinition deploy(JbpmContext jbpmContext, byte[] zipBytes) {
    SAXReader xmlReader = new SAXReader();
    JpdlXmlReader jpdlReader = new JpdlXmlReader();
    Document doc = null;
    InputStream inputStream = null;
    ZipInputStream zipInputStream = null;
    ProcessDefinition processDefinition = null;
    Map<String, byte[]> zipMap = null;
    try {/*from   w  w w  .ja va 2 s.c  o m*/
        zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipBytes));
        zipMap = ZipUtils.getZipBytesMap(zipInputStream);
        zipInputStream.close();
        zipInputStream = null;
        if (zipMap != null) {
            if (zipMap.get("processdefinition.xml") != null) {
                byte[] bytes = zipMap.get("processdefinition.xml");
                inputStream = new ByteArrayInputStream(bytes);
                doc = xmlReader.read(inputStream);
                jpdlReader.checkTaskNames(doc);
                this.reconfigProcessDefinition(jbpmContext, doc);
                Element root = doc.getRootElement();
                String encoding = doc.getXMLEncoding();
                String processName = root.attributeValue("name");

                try {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
                    String ret = formatter.format(new Date());
                    String filename = SystemProperties.getConfigRootPath() + "/deploy/" + processName + ret
                            + ".zip";
                    // ?
                    FileUtils.save(filename, zipBytes);
                } catch (Exception ex) {
                    if (LogUtils.isDebug()) {
                        logger.debug(ex);
                        ex.printStackTrace();
                    }
                }

                byte[] pdBytes = Dom4jUtils.getBytesFromPrettyDocument(doc, encoding);
                zipMap.put("processdefinition.xml", pdBytes);

                // ??
                byte[] newZipBytes = ZipUtils.toZipBytes(zipMap);
                zipInputStream = new ZipInputStream(new ByteArrayInputStream(newZipBytes));
                processDefinition = ProcessDefinition.parseParZipInputStream(zipInputStream);
                jbpmContext.deployProcessDefinition(processDefinition);
                zipInputStream.close();
                zipInputStream = null;

                String processDefinitionId = String.valueOf(processDefinition.getId());

                logger.debug("processDefinitionId:" + processDefinitionId);
                logger.debug("processName:" + processName);
                Map<String, Task> taskMap = processDefinition.getTaskMgmtDefinition().getTasks();
                if (taskMap != null && taskMap.size() > 0) {
                    Iterator<String> iter = taskMap.keySet().iterator();
                    while (iter.hasNext()) {
                        String taskName = iter.next();
                        logger.debug("taskName:" + taskName);
                    }
                }

                try {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
                    String ret = formatter.format(new Date());
                    String filename = SystemProperties.getConfigRootPath() + "/deploy/" + processName + ret
                            + "_repack.zip";
                    // ??
                    FileUtils.save(filename, newZipBytes);
                } catch (Exception ex) {
                    if (LogUtils.isDebug()) {
                        logger.debug(ex);
                    }
                }
            }

            if (zipMap.get("process.cfg.xml") != null) {
                byte[] x_bytes = zipMap.get("process.cfg.xml");
                if (x_bytes != null) {
                    JbpmExtensionReader reader = new JbpmExtensionReader();
                    List<Extension> extensions = reader.readTasks(new ByteArrayInputStream(x_bytes));
                    if (extensions != null && extensions.size() > 0) {

                        JbpmExtensionManager jbpmExtensionManager = ProcessContainer.getContainer()
                                .getJbpmExtensionManager();
                        jbpmExtensionManager.reconfig(jbpmContext, extensions);
                    }
                }
            }
        }
        return processDefinition;
    } catch (Throwable ex) {
        if (LogUtils.isDebug()) {
            logger.debug(ex);
            ex.printStackTrace();
        }
        throw new JbpmException(ex);
    } finally {
        try {
            if (zipInputStream != null) {
                zipInputStream.close();
                zipInputStream = null;
            }
        } catch (Exception ex) {
            if (LogUtils.isDebug()) {
                logger.debug(ex);
            }
        }
        try {
            if (inputStream != null) {
                inputStream.close();
                inputStream = null;
            }
        } catch (Exception ex) {
            if (LogUtils.isDebug()) {
                logger.debug(ex);
            }
        }
    }
}

From source file:com.mg.framework.support.ui.UIProducer.java

License:Open Source License

public static Document performDocument(Document document, RuntimeMacrosLoader runtimeMacrosLoader) {
    logger.debug("Original form descriptor:\n".concat(document.asXML()));
    Document result = DocumentFactory.getInstance().createDocument(document.getXMLEncoding());
    result.setDocType(document.getDocType());
    result.setRootElement(copyElement(document.getRootElement(), runtimeMacrosLoader));
    result.getRootElement().addNamespace(document.getRootElement().getNamespacePrefix(),
            document.getRootElement().getNamespaceURI());
    try {//from  w  ww.jav  a 2 s.  c o  m
        //?   ?,  ?  ? namespaces
        result = DocumentHelper.parseText(result.asXML());
        logger.debug("Finished form descriptor:\n".concat(result.asXML()));
        return result;
    } catch (DocumentException e) {
        throw new ApplicationException(e);
    }
}

From source file:de.codecentric.multitool.xml.XmlLibrary.java

License:Apache License

/**
 * Konvertiert einen XML-String in ein XML-Document
 * /*  w w w .j  ava  2s  .c  o  m*/
 * | ${xmlDoc} = | Get Xml From String | ${xmlString} |
 */
public Document getXmlFromString(String xmlString) {
    try {
        Document result = null;
        SAXReader reader = new SAXReader();
        InputSource source = new InputSource(new StringReader(xmlString));
        source.setEncoding(XML_ENCODING);
        result = reader.read(source);
        if (result.getXMLEncoding() == null)
            result.setXMLEncoding(XML_ENCODING);
        return result;
    } catch (DocumentException e) {
        throw new RuntimeException("Der String " + xmlString + " kann nicht geparst werden" + e);
    }
}

From source file:mesquite.lib.XMLUtil.java

License:Open Source License

public static String getDocumentAsXMLString(Document doc, boolean escapeText) {
    try {//w w w  . j a  va  2 s .  co  m
        String encoding = doc.getXMLEncoding();

        if (encoding == null)
            encoding = "UTF-8";

        Writer osw = new StringWriter();
        OutputFormat opf = new OutputFormat("  ", true, encoding);
        XMLWriter writer = new XMLWriter(osw, opf);
        writer.setEscapeText(escapeText);
        writer.write(doc);
        writer.close();
        return osw.toString();
    } catch (IOException e) {
        MesquiteMessage.warnProgrammer("XML Document could not be returned as string.");
    }
    return null;
}

From source file:mesquite.lib.XMLUtil.java

License:Open Source License

public static String getDocumentAsXMLString2(Document doc) {
    try {/*w w  w  .  j  av a 2s .  co m*/
        String encoding = doc.getXMLEncoding();

        //if (encoding == null)
        //   encoding = "UTF-8";

        Writer osw = new StringWriter();
        OutputFormat opf = new OutputFormat("  ", true);
        XMLWriter writer = new XMLWriter(osw, opf);
        writer.write(doc);
        writer.close();
        return osw.toString();
    } catch (IOException e) {
        MesquiteMessage.warnProgrammer("XML Document could not be returned as string.");
    }
    return null;
}

From source file:net.sf.jguard.core.util.XMLUtils.java

License:Open Source License

/**
 * write the updated configuration to the XML file in the UTF-8 format.
 *
 * @param url      URL of the file to write
 * @param document dom4j Document//w ww .j  ava 2 s  .  c  om
 * @throws IOException
 */
public static void write(URL url, Document document) throws IOException {
    OutputFormat outFormat = OutputFormat.createPrettyPrint();
    if (document.getXMLEncoding() != null) {
        outFormat.setEncoding(document.getXMLEncoding());
    } else {
        outFormat.setEncoding(UTF_8);
    }
    XMLWriter out = new XMLWriter(new BufferedOutputStream(new FileOutputStream(url.getPath())), outFormat);
    out.write(document);
    out.flush();
    out.close();
}

From source file:net.unicon.toro.installer.tools.MergeConfiguration.java

License:Open Source License

private void saveXml(Document doc, File saveTo) throws IOException {
    String xmlEncoding = doc.getXMLEncoding();
    OutputFormat format = new OutputFormat("    ", true, xmlEncoding);
    format.setTrimText(true);//  w w w.  j  a  v  a  2s .  c o m
    XMLWriter writer = new XMLWriter(new FileWriter(saveTo), format);
    writer.write(doc);
    writer.close();
}

From source file:net.unicon.toro.installer.tools.MergeConfiguration.java

License:Open Source License

private void mergeXmlChanges(Element el, File path) throws Exception {
    System.out.println("Merging xml changes to " + path.getAbsolutePath());
    Utils.instance().backupFile(path, true);

    SAXReader reader = new SAXReader();
    reader.setEntityResolver(new DTDResolver(target));
    Document doc = reader.read(new URL("file:" + path.getAbsolutePath()));
    Element source = doc.getRootElement();

    //saveXml(doc, new File("/tmp/before."+path.getName()));

    String xmlEncoding = doc.getXMLEncoding();

    if (log.isDebugEnabled()) {
        StringBuffer sb = new StringBuffer();
        sb.append('\n').append("children root: ").append(source.getPath()).append('\n');
        outputElement(source, sb, 1);//from w w w  .  j a va2  s .  c o m
        log.debug(sb.toString());
    }

    processElementValueReplacements(el, source);
    processNodeReplacements(el, source);
    //processAddToNodes(el, source);
    processNodeAddOrReplace(el, source, path);
    processNodeRemovals(el, source);

    //saveXml(doc, new File("/tmp/after."+path.getName()));
    saveXml(doc, path);
}

From source file:nl.knaw.dans.common.fedora.fox.DatastreamVersion.java

License:Apache License

public String getXmlContentString() {
    Element xmlEl = xmlContent.getElement();
    Document doc = xmlEl.getDocument();
    String encoding = "UTF-8";
    if (doc != null)
        doc.getXMLEncoding();

    Writer osw = new StringWriter();
    OutputFormat opf = new OutputFormat("  ", true, encoding);
    XMLWriter writer = new XMLWriter(osw, opf);
    try {/*from  w  w  w .  j  av a2s  . co m*/
        writer.write(xmlEl);
        writer.close();
    } catch (IOException e) {
        return "";
    }
    return osw.toString();
}