Example usage for org.w3c.dom Document appendChild

List of usage examples for org.w3c.dom Document appendChild

Introduction

In this page you can find the example usage for org.w3c.dom Document appendChild.

Prototype

public Node appendChild(Node newChild) throws DOMException;

Source Link

Document

Adds the node newChild to the end of the list of children of this node.

Usage

From source file:com.twinsoft.convertigo.engine.util.CarUtils.java

private static Document exportProject(Project project, final List<TestCase> selectedTestCases)
        throws EngineException {
    try {/*from w  ww.j  a v a2  s  . c om*/
        final Document document = XMLUtils.getDefaultDocumentBuilder().newDocument();
        //            ProcessingInstruction pi = document.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
        //            document.appendChild(pi);
        final Element rootElement = document.createElement("convertigo");

        rootElement.setAttribute("version", com.twinsoft.convertigo.engine.Version.fullProductVersion);
        rootElement.setAttribute("engine", com.twinsoft.convertigo.engine.Version.version);
        rootElement.setAttribute("beans", com.twinsoft.convertigo.beans.Version.version);
        String studioVersion = "";
        try {
            Class<?> c = Class.forName("com.twinsoft.convertigo.eclipse.Version");
            studioVersion = (String) c.getDeclaredField("version").get(null);
        } catch (Exception e) {
        } catch (Throwable th) {
        }

        rootElement.setAttribute("studio", studioVersion);
        document.appendChild(rootElement);

        new WalkHelper() {
            protected Element parentElement = rootElement;

            @Override
            protected void walk(DatabaseObject databaseObject) throws Exception {
                Element parentElement = this.parentElement;

                Element element = parentElement;
                element = databaseObject.toXml(document);
                String name = " : " + databaseObject.getName();
                try {
                    name = CachedIntrospector.getBeanInfo(databaseObject.getClass()).getBeanDescriptor()
                            .getDisplayName() + name;
                } catch (IntrospectionException e) {
                    name = databaseObject.getClass().getSimpleName() + name;
                }
                Integer depth = (Integer) document.getUserData("depth");
                if (depth == null) {
                    depth = 0;
                }

                String openpad = StringUtils.repeat("   ", depth);
                String closepad = StringUtils.repeat("   ", depth);
                parentElement.appendChild(document.createTextNode("\n"));
                parentElement.appendChild(
                        document.createComment(StringUtils.rightPad(openpad + "<" + name + ">", 150)));

                if (databaseObject instanceof TestCase && selectedTestCases.size() > 0) {
                    if (selectedTestCases.contains((TestCase) databaseObject)) {
                        parentElement.appendChild(element);
                    }
                } else {
                    parentElement.appendChild(element);
                }

                document.setUserData("depth", depth + 1, null);

                this.parentElement = element;
                super.walk(databaseObject);

                element.appendChild(document.createTextNode("\n"));
                element.appendChild(
                        document.createComment(StringUtils.rightPad(closepad + "</" + name + ">", 150)));
                document.setUserData("depth", depth, null);

                databaseObject.hasChanged = false;
                databaseObject.bNew = false;

                this.parentElement = parentElement;
            }

        }.init(project);

        return document;
    } catch (Exception e) {
        throw new EngineException("Unable to export the project \"" + project.getName() + "\".", e);
    }
}

From source file:com.xinferin.licensing.LicenceGenerator.java

/**
 * Gets an RSA Public key as an xml document
 * //from  w  w  w  .  j av a  2 s  .  c o  m
 * @param key
 * @return
 * @throws ParserConfigurationException
 * @throws UnsupportedEncodingException
 */
private Document getRSAPublicKeyAsXML(RSAPublicKey key)
        throws ParserConfigurationException, UnsupportedEncodingException {

    Document result = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element rsaKeyValue = result.createElement("RSAKeyValue");
    result.appendChild(rsaKeyValue);

    Element modulus = result.createElement("Modulus");
    rsaKeyValue.appendChild(modulus);

    byte[] modulusBytes = key.getModulus().toByteArray();
    modulusBytes = stripLeadingZeros(modulusBytes);
    modulus.appendChild(result.createTextNode(new String(Base64.encodeBase64(modulusBytes))));

    Element exponent = result.createElement("Exponent");
    rsaKeyValue.appendChild(exponent);

    byte[] exponentBytes = key.getPublicExponent().toByteArray();
    exponent.appendChild(result.createTextNode(new String(Base64.encodeBase64(exponentBytes))));

    return result;
}

From source file:com.verisign.epp.codec.signedMark.EPPEncodedSignedMark.java

/**
 * Encode the signed mark to a <code>byte[]</code>.
 * //  w  ww.  j  a va2 s .  c  o  m
 * @return Encoded signed mark
 * @throws EPPEncodeException
 *         Error encoding the signed mark
 */
public byte[] encode() throws EPPEncodeException {
    cat.debug("EPPEncodedSignedMark.encode(): enter");

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        Document doc = new DocumentImpl();
        Element root = this.encode(doc);
        doc.appendChild(root);

        TransformerFactory transFac = TransformerFactory.newInstance();
        Transformer trans = transFac.newTransformer();

        trans.transform(new DOMSource(root), new StreamResult(os));
    } catch (EPPEncodeException ex) {
        throw ex;
    } catch (Exception ex) {
        cat.error("Error encoding trademark to byte[]: " + ex);
        throw new EPPEncodeException("Error encoding trademark to byte[]");
    }

    cat.debug("EPPEncodedSignedMark.encode(): exit");
    return os.toByteArray();
}

From source file:com.evolveum.midpoint.util.DOMUtil.java

public static Document getDocument(QName rootElementName) {
    Document document = loader.newDocument();
    document.appendChild(createElement(document, rootElementName));
    return document;
}

From source file:de.mpg.mpdl.inge.citationmanager.data.FontStylesCollection.java

/**
 * Writes {@link FontStylesCollection} to xmlfile
 * //ww w.ja  v  a  2 s.c  o  m
 * @param xmlFileName
 * @throws IOException
 * @throws SAXException
 * @throws CitationStyleManagerException
 */
public void writeToXml(String xmlFileName) throws IOException, SAXException, CitationStyleManagerException {

    Document doc = XmlHelper.createDocument();

    Element root = doc.createElement("font-styles-collection");

    doc.appendChild(root);

    // here is the loop:

    for (FontStyle fs : fontStyles) {

        Element element = doc.createElement("font-style");

        element.setAttribute("def", "" + fs.getDef());
        element.setAttribute("name", fs.getName());
        element.setAttribute("font-size", "" + fs.getFontSize());
        element.setAttribute("font-name", fs.getFontName());
        element.setAttribute("is-bold", "" + fs.getIsBold());
        element.setAttribute("is-italic", "" + fs.getIsItalic());
        element.setAttribute("is-underline", "" + fs.getIsUnderline());
        element.setAttribute("is-strike-through", "" + fs.getIsStrikeThrough());
        element.setAttribute("pdf-font-name", fs.getPdfFontName());
        element.setAttribute("fore-color", fs.getForeColor());
        element.setAttribute("back-color", fs.getBackColor());
        element.setAttribute("pdf-encoding", fs.getPdfEncoding());
        element.setAttribute("is-pdf-embedded", "" + fs.getIsPdfEmbedded());
        element.setAttribute("is-pdf-simulated-bold", "" + fs.getIsPdfSimulatedBold());
        element.setAttribute("is-pdf-simulated-italic", "" + fs.getIsPdfSimulatedItalic());
        element.setAttribute("css-class", "" + fs.getCssClass());

        root.appendChild(element);
    }

    XmlHelper.output(doc, xmlFileName);

}

From source file:de.mpg.escidoc.services.citationmanager.data.FontStylesCollection.java

/**
 * Writes {@link FontStylesCollection} to xmlfile
 * @param xmlFileName/*from  w  w w  . j  a  va2 s  . c  o  m*/
 * @throws IOException
 * @throws SAXException
 * @throws CitationStyleManagerException 
 */
public void writeToXml(String xmlFileName) throws IOException, SAXException, CitationStyleManagerException {

    Document doc = XmlHelper.createDocument();

    Element root = doc.createElement("font-styles-collection");

    doc.appendChild(root);

    //here is the loop:

    for (FontStyle fs : fontStyles) {

        Element element = doc.createElement("font-style");

        element.setAttribute("def", "" + fs.getDef());
        element.setAttribute("name", fs.getName());
        element.setAttribute("font-size", "" + fs.getFontSize());
        element.setAttribute("font-name", fs.getFontName());
        element.setAttribute("is-bold", "" + fs.getIsBold());
        element.setAttribute("is-italic", "" + fs.getIsItalic());
        element.setAttribute("is-underline", "" + fs.getIsUnderline());
        element.setAttribute("is-strike-through", "" + fs.getIsStrikeThrough());
        element.setAttribute("pdf-font-name", fs.getPdfFontName());
        element.setAttribute("fore-color", fs.getForeColor());
        element.setAttribute("back-color", fs.getBackColor());
        element.setAttribute("pdf-encoding", fs.getPdfEncoding());
        element.setAttribute("is-pdf-embedded", "" + fs.getIsPdfEmbedded());
        element.setAttribute("is-pdf-simulated-bold", "" + fs.getIsPdfSimulatedBold());
        element.setAttribute("is-pdf-simulated-italic", "" + fs.getIsPdfSimulatedItalic());
        element.setAttribute("css-class", "" + fs.getCssClass());

        root.appendChild(element);
    }

    XmlHelper.output(doc, xmlFileName);

}

From source file:com.adaptris.core.services.splitter.XpathMessageSplitter.java

@Override
public List<AdaptrisMessage> splitMessage(AdaptrisMessage msg) throws CoreException {
    List<AdaptrisMessage> result = new ArrayList<AdaptrisMessage>();
    try {/*from ww w .j  ava 2  s  .  c  om*/
        NamespaceContext namespaceCtx = SimpleNamespaceContext.create(getNamespaceContext(), msg);
        DocumentBuilderFactoryBuilder factoryBuilder = documentFactoryBuilder();
        if (namespaceCtx != null) {
            factoryBuilder = documentFactoryBuilder().withNamespaceAware(true);
        }
        DocumentBuilder docBuilder = factoryBuilder.configure(DocumentBuilderFactory.newInstance())
                .newDocumentBuilder();
        XmlUtils xml = new XmlUtils(namespaceCtx,
                factoryBuilder.configure(DocumentBuilderFactory.newInstance()));
        NodeList list = resolveXpath(msg, namespaceCtx, factoryBuilder);
        String encodingToUse = evaluateEncoding(msg);
        for (int i = 0; i < list.getLength(); i++) {
            Document splitXmlDoc = docBuilder.newDocument();
            Node e = list.item(i);
            Node dup = splitXmlDoc.importNode(e, true);
            splitXmlDoc.appendChild(dup);
            AdaptrisMessage splitMsg = selectFactory(msg).newMessage("", encodingToUse);
            try (Writer writer = splitMsg.getWriter()) {
                xml.writeDocument(splitXmlDoc, writer, encodingToUse);
                copyMetadata(msg, splitMsg);
                result.add(splitMsg);
            }
        }
    } catch (Exception e) {
        throw new CoreException(e);
    } finally {

    }
    return result;
}

From source file:com.twinsoft.convertigo.engine.requesters.InternalRequester.java

protected Object addStatisticsAsText(String stats, Object result) throws UnsupportedEncodingException {
    if (result != null) {
        if (stats == null)
            stats = context.statistics.printStatistics();
        if (result instanceof Document) {
            Document document = (Document) result;
            Comment comment = document.createComment("\n" + stats);
            document.appendChild(comment);
        } else if (result instanceof byte[]) {
            String encodingCharSet = "UTF-8";
            if (context.requestedObject != null)
                encodingCharSet = context.requestedObject.getEncodingCharSet();
            String sResult = new String((byte[]) result, encodingCharSet);
            sResult += "<!--\n" + stats + "\n-->";
            result = sResult.getBytes(encodingCharSet);
        }//from  w w  w.  j  av a  2 s . c  o m
    }
    return result;
}

From source file:io.wcm.tooling.commons.contentpackagebuilder.XmlContentBuilder.java

/**
 * Build filter XML for package metadata files.
 * @param filters Filters/*ww w .j  a  va 2 s  . co  m*/
 * @return Filter XML
 */
public Document buildFilter(List<PackageFilter> filters) {
    Document doc = documentBuilder.newDocument();

    Element workspaceFilterElement = doc.createElement("workspaceFilter");
    workspaceFilterElement.setAttribute("version", "1.0");
    doc.appendChild(workspaceFilterElement);

    for (PackageFilter filter : filters) {
        Element filterElement = doc.createElement("filter");
        filterElement.setAttribute("root", filter.getRootPath());
        workspaceFilterElement.appendChild(filterElement);

        for (PackageFilterRule rule : filter.getRules()) {
            Element ruleElement = doc.createElement(rule.isInclude() ? "include" : "exclude");
            ruleElement.setAttribute("pattern", rule.getPattern());
            filterElement.appendChild(ruleElement);
        }
    }

    return doc;
}

From source file:com.action.ExportAction.java

public File exportXMLAction(File file) throws Exception {
    //document/*  w  w  w. j a va 2  s .c o  m*/
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.newDocument();
    Element reportsElement = document.createElement("reports");//
    reportsElement.setAttribute("date", (new Date()).toString());
    document.appendChild(reportsElement);
    System.out.println("exportXMLAction:" + tableCode);
    String[] ids = tableCode.split(",");
    for (String id : ids) {
        System.out.println("id:" + id);
        List<JQ_zhibiao_entity> list = Export.getDuizhaoByJqTbCode(id);
        document = Export.exportXML(reportsElement, document, list, id);
    }

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    PrintWriter pw = new PrintWriter(new FileOutputStream(file));
    StreamResult result = new StreamResult(pw);
    transformer.transform(source, result);
    pw.close();
    return file;
}