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:org.joy.config.Configuration.java

public void save() {
    try {//from   w w w  .ja v a 2 s  . c o m
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element root = doc.createElement("configuration");
        doc.appendChild(root);

        Element classpathEle = doc.createElement("classpath");
        root.appendChild(classpathEle);
        for (String s : classPathEntries) {
            Element e = doc.createElement("entry");
            e.appendChild(doc.createTextNode(s));
            classpathEle.appendChild(e);
        }

        Element connectionsEle = doc.createElement("connections");
        root.appendChild(connectionsEle);
        for (DatabaseElement d : connectionHistory) {
            writeDatabase(connectionsEle, d);
        }

        Element e = doc.createElement("tagertProject");
        e.appendChild(doc.createTextNode(tagertProject));
        root.appendChild(e);

        e = doc.createElement("basePackage");
        e.appendChild(doc.createTextNode(basePackage));
        root.appendChild(e);

        e = doc.createElement("moduleName");
        e.appendChild(doc.createTextNode(moduleName));
        root.appendChild(e);

        Element templatesEle = doc.createElement("templates");
        root.appendChild(templatesEle);
        for (TemplateElement t : templates) {
            writeTemplate(templatesEle, t);
        }

        // Write the file
        DOMSource ds = new DOMSource(doc);
        StreamResult sr = new StreamResult(new File(configurationFile));
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.STANDALONE, "yes");
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        t.transform(ds, sr);
    } catch (Exception e) {
        LOGGER.info(e.getMessage(), e);
    }
}

From source file:de.e7o.caldav.caldav.CaldavServer.java

public String[][] requestEntriesByTime(String startTime, String endTime, String filterType) {
    Document d = newDocument();
    Element root = d.createElement("C:calendar-query");
    Element p, q;//w w  w.  j a  v a  2 s.  c  om

    Connection c;

    prepareRootElement(root);
    d.appendChild(root);

    p = d.createElement("D:prop");
    root.appendChild(p);
    p.appendChild(d.createElement("D:getetag"));
    p.appendChild(d.createElement("C:calendar-data"));

    p = d.createElement("C:filter");
    root.appendChild(p);
    q = d.createElement("C:comp-filter");
    q.setAttribute("name", "VCALENDAR");
    p.appendChild(q);
    p = d.createElement("C:comp-filter");
    p.setAttribute("name", filterType);
    q.appendChild(p);
    q = d.createElement("C:time-range");
    q.setAttribute("start", startTime);
    q.setAttribute("end", endTime);
    p.appendChild(q);

    try {
        c = newRawConnection();
        return parseMultigetEntries(c.request("", "REPORT", documentToString(d), depth1));
    } catch (Exception e) {
        ErrorHelper.addErrMessage(e);
        return null;
    }
}

From source file:com.codebutler.farebot.card.Card.java

public Element toXML() throws Exception {
    try {/*  w  w  w  .  java  2s .c  o  m*/
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.newDocument();

        Element element = doc.createElement("card");
        element.setAttribute("type", String.valueOf(getCardType().toInteger()));
        element.setAttribute("id", Utils.getHexString(mTagId, null));
        element.setAttribute("scanned_at", Long.toString(mScannedAt.getTime()));
        doc.appendChild(element);

        return doc.getDocumentElement();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.cxf.fediz.service.idp.protocols.TrustedIdpSAMLProtocolHandler.java

@Override
public URL mapSignInRequest(RequestContext context, Idp idp, TrustedIdp trustedIdp) {

    try {/*  www . j av  a 2s .c  o  m*/
        Document doc = DOMUtils.createDocument();
        doc.appendChild(doc.createElement("root"));
        // Create the AuthnRequest
        AuthnRequest authnRequest = authnRequestBuilder.createAuthnRequest(null, idp.getRealm(),
                idp.getIdpUrl().toString());

        boolean signRequest = isPropertyConfigured(trustedIdp, SIGN_REQUEST, true);
        if (signRequest) {
            authnRequest.setDestination(trustedIdp.getUrl());
        }
        Element authnRequestElement = OpenSAMLUtil.toDom(authnRequest, doc);
        String authnRequestEncoded = encodeAuthnRequest(authnRequestElement);

        String urlEncodedRequest = URLEncoder.encode(authnRequestEncoded, "UTF-8");

        UriBuilder ub = UriBuilder.fromUri(trustedIdp.getUrl());

        ub.queryParam(SSOConstants.SAML_REQUEST, urlEncodedRequest);

        String wctx = context.getFlowScope().getString(FederationConstants.PARAM_CONTEXT);
        if (wctx != null) {
            ub.queryParam(SSOConstants.RELAY_STATE, wctx);
        }
        if (signRequest) {
            signRequest(urlEncodedRequest, wctx, idp, ub);
        }

        // Store the Request ID
        String authnRequestId = authnRequest.getID();
        WebUtils.putAttributeInExternalContext(context, SAML_SSO_REQUEST_ID, authnRequestId);

        HttpServletResponse response = WebUtils.getHttpServletResponse(context);
        response.addHeader("Cache-Control", "no-cache, no-store");
        response.addHeader("Pragma", "no-cache");

        return ub.build().toURL();
    } catch (MalformedURLException ex) {
        LOG.error("Invalid Redirect URL for Trusted Idp", ex);
        throw new IllegalStateException("Invalid Redirect URL for Trusted Idp");
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Invalid Redirect URL for Trusted Idp", ex);
        throw new IllegalStateException("Invalid Redirect URL for Trusted Idp");
    } catch (Exception ex) {
        LOG.error("Invalid Redirect URL for Trusted Idp", ex);
        throw new IllegalStateException("Invalid Redirect URL for Trusted Idp");
    }
}

From source file:hoot.services.controllers.osm.MapResource.java

/**
 * Writes the query response to an XML document
 *
 * @param results/*from w w w .  j  a v  a  2s  .  c  om*/
 *            query results; a mapping of element IDs to records, grouped by
 *            element type
 * @param queryBounds
 *            bounds of the query
 * @param multiLayerUniqueElementIds
 *            if true, IDs are prepended with <map id>_<first letter of the
 *            element type>_; this setting activated is not compatible with
 *            standard OSM clients (specific to Hootenanny iD)
 * @return an XML document
 */
private static Document writeResponse(java.util.Map<ElementType, java.util.Map<Long, Tuple>> results,
        BoundingBox queryBounds, boolean multiLayerUniqueElementIds, long mapId, Connection connection) {
    Document responseDoc = null;
    try {
        responseDoc = XmlDocumentBuilder.create();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Error creating XmlDocumentBuilder!", e);
    }

    Element elementRootXml = OsmResponseHeaderGenerator.getOsmDataHeader(responseDoc);
    responseDoc.appendChild(elementRootXml);

    if (!results.isEmpty()) {
        elementRootXml.appendChild(queryBounds.toXml(elementRootXml));

        for (ElementType elementType : ElementType.values()) {
            if (elementType != ElementType.Changeset) {
                java.util.Map<Long, Tuple> resultsForType = results.get(elementType);
                if (resultsForType != null) {
                    for (java.util.Map.Entry<Long, Tuple> entry : resultsForType.entrySet()) {
                        Tuple record = entry.getValue();

                        hoot.services.models.osm.Element element = ElementFactory.create(elementType, record,
                                connection, mapId);

                        // the query that sent this in should have
                        // already handled filtering out invisible elements

                        Users usersTable = record.get(QUsers.users);
                        Element elementXml = element.toXml(elementRootXml, usersTable.getId(),
                                usersTable.getDisplayName(), multiLayerUniqueElementIds, true);
                        elementRootXml.appendChild(elementXml);
                    }
                }
            }
        }
    }

    return responseDoc;
}

From source file:de.e7o.caldav.caldav.CaldavServer.java

public String requestCTag() {
    Document d = newDocument();
    Element root = d.createElement("D:propfind");
    Element p;//from   w ww  .  j  a v  a  2s.  co  m

    Connection c;
    NodeList nl;

    prepareRootElement(root);
    d.appendChild(root);

    p = d.createElement("D:prop");
    root.appendChild(p);
    p.appendChild(d.createElement("CS:getctag"));

    try {
        c = newRawConnection();
        d = db.parse(c.request("", "PROPFIND", documentToString(d), depth0));
        //nl = xpath("//getctag", d);
        nl = findNodes("getctag", d);
        if (nl.getLength() == 0) {
            ErrorHelper.addErrMessage("No ctag specified");
            return null;
        } else {
            return getNodeContent(nl.item(0));
        }
    } catch (Exception e) {
        ErrorHelper.addErrMessage(e);
        return null;
    }
}

From source file:de.e7o.caldav.caldav.CaldavServer.java

public String[][] requestEntriesByUID(String... uid) {
    Document d = newDocument();
    Element root = d.createElement("calendar-multiget");
    Element p;//from  www. ja v  a 2s.  co m

    Connection c;

    int i;

    prepareRootElement(root);
    d.appendChild(root);

    p = d.createElement("D:prop");
    root.appendChild(p);
    p.appendChild(d.createElement("D:getetag"));
    p.appendChild(d.createElement("C:calendar-data"));

    for (i = 0; i < uid.length; i++) {
        p = d.createElement("D:href");
        //p.setTextContent(baseUri+uid[i]+".ics");
        XmlHelper.setTextContent(p, baseUri + uid[i] + ".ics");
        root.appendChild(p);
    }

    try {
        c = newRawConnection();
        return parseMultigetEntries(c.request("", "REPORT", documentToString(d), depth1));
    } catch (Exception e) {
        ErrorHelper.addErrMessage(e);
        return null;
    }
}

From source file:com.photon.phresco.plugins.xcode.Instrumentation.java

private void generateXMLReport(String location) {

    try {//  w ww  . j  a  va2 s.c  o  m
        String startTime = "";
        int total, pass, fail;
        total = pass = fail = 0;
        config = new XMLPropertyListConfiguration(location);
        ArrayList list = (ArrayList) config.getRoot().getChild(0).getValue();
        String key;

        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        Document doc = docBuilder.newDocument();

        Element root = doc.createElement(XMLConstants.TESTSUITES_NAME);
        doc.appendChild(root);
        Element testSuite = doc.createElement(XMLConstants.TESTSUITE_NAME);
        testSuite.setAttribute(XMLConstants.NAME, "FunctionalTestSuite");
        root.appendChild(testSuite);

        for (Object object : list) {

            XMLPropertyListConfiguration config = (XMLPropertyListConfiguration) object;

            startTime = config.getRoot().getChild(2).getValue().toString();

            break;
        }

        for (Object object : list) {

            XMLPropertyListConfiguration config = (XMLPropertyListConfiguration) object;

            ConfigurationNode con = config.getRoot().getChild(0);
            key = con.getName();

            if (key.equals(XMLConstants.LOGTYPE) && con.getValue().equals(XMLConstants.PASS)) {
                pass++;
                total++;
                Element child1 = doc.createElement(XMLConstants.TESTCASE_NAME);
                child1.setAttribute(XMLConstants.NAME, (String) config.getRoot().getChild(1).getValue());
                child1.setAttribute(XMLConstants.RESULT, (String) con.getValue());

                String endTime = config.getRoot().getChild(2).getValue().toString();

                long differ = getTimeDiff(startTime, endTime);
                startTime = endTime;
                child1.setAttribute(XMLConstants.TIME, differ + "");
                testSuite.appendChild(child1);
            } else if (key.equals(XMLConstants.LOGTYPE) && con.getValue().equals(XMLConstants.ERROR)) {
                fail++;
                total++;
                Element child1 = doc.createElement(XMLConstants.TESTCASE_NAME);
                child1.setAttribute(XMLConstants.NAME, (String) config.getRoot().getChild(1).getValue());
                child1.setAttribute(XMLConstants.RESULT, (String) con.getValue());

                String endTime = config.getRoot().getChild(2).getValue().toString();

                long differ = getTimeDiff(startTime, endTime);
                startTime = endTime;
                child1.setAttribute(XMLConstants.TIME, differ + "");
                testSuite.appendChild(child1);
            }

        }

        testSuite.setAttribute(XMLConstants.TESTS, String.valueOf(total));
        testSuite.setAttribute(XMLConstants.SUCCESS, String.valueOf(pass));
        testSuite.setAttribute(XMLConstants.FAILURES, String.valueOf(fail));

        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        File file = new File(project.getBasedir().getAbsolutePath() + File.separator + xmlResult);
        Writer bw = new BufferedWriter(new FileWriter(file));
        StreamResult result = new StreamResult(bw);
        DOMSource source = new DOMSource(doc);
        trans.transform(source, result);

    } catch (Exception e) {
        getLog().error("Interrupted while generating XML file");
    }
}

From source file:net.sf.dsig.xmldsig.XmldsigStrategy.java

@Override
public String signPlaintext(String plaintext, PrivateKey privateKey, X509Certificate[] certificateChain)
        throws Exception {
    Document contentDocument = builder.newDocument();

    Element valueElem = contentDocument.createElement("value");
    valueElem.setTextContent(plaintext);
    contentDocument.appendChild(valueElem);

    return signInternal(contentDocument, "plaintext", privateKey, certificateChain);
}

From source file:com.bluexml.xforms.controller.mapping.MappingToolSearch.java

/**
 * Builds the XForms instance document for a search form.
 * /*from ww w.  j av a2s .c om*/
 * @param formName
 * @return
 */
public Document getInstanceSearch(String formName) {
    Document formInstance = documentBuilder.newDocument();
    Element rootElement = formInstance.createElement("root");
    formInstance.appendChild(rootElement);

    SearchFormType sfType = getSearchFormType(formName);
    if (sfType != null) {
        Element formElt = formInstance.createElement(sfType.getName());
        rootElement.appendChild(formElt);

        for (SearchFieldType fieldType : sfType.getField()) {
            Element topElt = getSearchFieldNode(formInstance, fieldType);
            formElt.appendChild(topElt);
        }
    }

    return formInstance;
}