Example usage for org.w3c.dom Element appendChild

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

Introduction

In this page you can find the example usage for org.w3c.dom Element 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.photon.phresco.service.util.ServerUtil.java

/**
 * To create pom.xml file for artifact upload
 * /*from w w w.  ja  v a  2  s  . c  om*/
 * @param info
 * @return
 * @throws PhrescoException
 */
public static File createPomFile(ArtifactGroup info) throws PhrescoException {
    FileWriter writer = null;
    File pomFile = getPomFile();
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder domBuilder = domFactory.newDocumentBuilder();

        org.w3c.dom.Document newDoc = domBuilder.newDocument();
        Element rootElement = newDoc.createElement(ServerConstants.POM_PROJECT);
        newDoc.appendChild(rootElement);

        Element modelVersion = newDoc.createElement(ServerConstants.POM_MODELVERSION);
        modelVersion.setTextContent(ServerConstants.POM_MODELVERSION_VAL);
        rootElement.appendChild(modelVersion);

        Element groupId = newDoc.createElement(ServerConstants.POM_GROUPID);
        groupId.setTextContent(info.getGroupId());
        rootElement.appendChild(groupId);

        Element artifactId = newDoc.createElement(ServerConstants.POM_ARTIFACTID);
        artifactId.setTextContent(info.getArtifactId());
        rootElement.appendChild(artifactId);

        Element version = newDoc.createElement(ServerConstants.POM_VERSION);
        version.setTextContent(info.getVersions().get(0).getVersion());
        rootElement.appendChild(version);

        Element packaging = newDoc.createElement(ServerConstants.POM_PACKAGING);
        packaging.setTextContent(info.getPackaging());
        rootElement.appendChild(packaging);

        Element description = newDoc.createElement(ServerConstants.POM_DESC);
        description.setTextContent(ServerConstants.POM_DESC_VAL);
        rootElement.appendChild(description);

        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ServerConstants.POM_OMMIT);
        trans.setOutputProperty(OutputKeys.INDENT, ServerConstants.POM_DESC);

        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(newDoc);
        trans.transform(source, result);
        String xmlString = sw.toString();
        writer = new FileWriter(pomFile);
        writer.write(xmlString);
        writer.close();
    } catch (Exception e) {
        throw new PhrescoException(e);
    } finally {
        Utility.closeStream(writer);
    }

    return pomFile;
}

From source file:Main.java

public static Element beanToXml(Document document, Object obj)
        throws ParserConfigurationException, InvocationTargetException, IllegalAccessException {
    String name = obj.getClass().getName();
    Element rootElement = document.createElement(name);
    Method[] methods = obj.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        String methodName = method.getName();
        String fieldName = analyzeFieldName(methodName);
        Element fieldElement = document.createElement(fieldName);
        if (methodName.startsWith("get") && !"getClass".equals(methodName)) {
            Object value = method.invoke(obj);
            if (value != null && !"".equals(value.toString())) {
                fieldElement.setTextContent(value.toString());
            } else {
                continue;
            }/*  w  w  w  .  j a  v a2s  .c o  m*/
        } else {
            continue;
        }
        rootElement.appendChild(fieldElement);
    }
    return rootElement;
}

From source file:com.meltmedia.cadmium.core.util.WarUtils.java

/**
 * Adds a context parameter to a web.xml file to override where the shiro environment class. 
 * @param doc The xml DOM document to create the new xml elements with.
 * @param root The xml Element node to add the context param to.
 *//*ww  w  . j av a  2  s. c  o m*/
public static void addEnvContextParam(Document doc, Element root) {
    Element ctxParam = doc.createElement("context-param");
    Element paramName = doc.createElement("param-name");
    paramName.appendChild(doc.createTextNode("shiroEnvironmentClass"));
    ctxParam.appendChild(paramName);
    Element paramValue = doc.createElement("param-value");
    paramValue.appendChild(doc.createTextNode("com.meltmedia.cadmium.servlets.shiro.WebEnvironment"));
    ctxParam.appendChild(paramValue);

    addRelativeTo(root, ctxParam, "listener", false);
}

From source file:com.meltmedia.cadmium.core.util.WarUtils.java

/**
 * Adds a context parameter to a web.xml file to override where the shiro config location is to be loaded from. 
 * The location loaded from will be represented by the "com.meltmedia.cadmium.contentRoot" system property.
 * @param doc The xml DOM document to create the new xml elements with.
 * @param root The xml Element node to add the context param to.
 */// w  w  w  .  j  av  a2  s. co m
public static void addContextParam(Document doc, Element root) {
    Element ctxParam = doc.createElement("context-param");
    Element paramName = doc.createElement("param-name");
    paramName.appendChild(doc.createTextNode("shiroConfigLocations"));
    ctxParam.appendChild(paramName);
    Element paramValue = doc.createElement("param-value");
    paramValue.appendChild(doc.createTextNode(
            "file:" + new File(System.getProperty("com.meltmedia.cadmium.contentRoot"), "shiro.ini")
                    .getAbsoluteFile().getAbsolutePath()));
    ctxParam.appendChild(paramValue);

    addRelativeTo(root, ctxParam, "listener", false);
}

From source file:com.meltmedia.cadmium.core.util.WarUtils.java

/**
 * Adds the shiro filter to a web.xml file.
 * @param doc The xml DOM document to create the new xml elements with.
 * @param root The xml Element node to add the filter to.
 *///from  w  w  w  .j  av  a  2 s. c  o  m
public static void addFilter(Document doc, Element root) {
    Element filter = doc.createElement("filter");
    Element filterName = doc.createElement("filter-name");
    filterName.appendChild(doc.createTextNode("ShiroFilter"));
    filter.appendChild(filterName);
    Element filterClass = doc.createElement("filter-class");
    filterClass.appendChild(doc.createTextNode("org.apache.shiro.web.servlet.ShiroFilter"));
    filter.appendChild(filterClass);

    addRelativeTo(root, filter, "filter", true);
}

From source file:com.meltmedia.cadmium.core.util.WarUtils.java

/**
 * Adds the filter mapping for the shiro filter to a web.xml file.
 * @param doc The xml DOM document to create the new xml elements with.
 * @param root The xml Element node to add the filter mapping to.
 *//*from   w  w w  .j  av a  2s. co m*/
public static void addFilterMapping(Document doc, Element root) {
    Element filterMapping = doc.createElement("filter-mapping");
    Element filterName = doc.createElement("filter-name");
    filterName.appendChild(doc.createTextNode("ShiroFilter"));
    filterMapping.appendChild(filterName);
    Element urlPattern = doc.createElement("url-pattern");
    urlPattern.appendChild(doc.createTextNode("/*"));
    filterMapping.appendChild(urlPattern);

    addDispatchers(doc, filterMapping, "REQUEST", "FORWARD", "INCLUDE", "ERROR");

    addRelativeTo(root, filterMapping, "filter-mapping", true);
}

From source file:com.meltmedia.cadmium.core.util.WarUtils.java

/**
 * Adds dispatchers for each item in the vargs parameter names to a 
 * filter mapping element of a web.xml file.
 * @param doc The xml DOM document to create the new xml elements with.
 * @param filterMapping The filter mapping element to append to from a web.xml document.
 * @param names The names of the dispatchers to add.
 *///ww w  . ja  v  a  2 s  . co m
public static void addDispatchers(Document doc, Element filterMapping, String... names) {
    if (names != null) {
        for (String name : names) {
            Element dispatcher = doc.createElement("dispatcher");
            dispatcher.appendChild(doc.createTextNode(name));
            filterMapping.appendChild(dispatcher);
        }
    }
}

From source file:com.msopentech.odatajclient.engine.data.atom.AtomSerializer.java

private static void setLinks(final Element entry, final List<AtomLink> links)
        throws ParserConfigurationException {
    for (AtomLink link : links) {
        final Element linkElem = entry.getOwnerDocument().createElement(ODataConstants.ATOM_ELEM_LINK);

        linkElem.setAttribute(ODataConstants.ATTR_REL, link.getRel());
        linkElem.setAttribute(ODataConstants.ATTR_TITLE, link.getTitle());
        linkElem.setAttribute(ODataConstants.ATTR_HREF, link.getHref());

        if (StringUtils.isNotBlank(link.getType())) {
            linkElem.setAttribute(ODataConstants.ATTR_TYPE, link.getType());
        }//from   w  ww. ja va  2  s  .com

        if (link.getInlineEntry() != null || link.getInlineFeed() != null) {
            final Element inline = entry.getOwnerDocument().createElement(ODataConstants.ATOM_ELEM_INLINE);
            linkElem.appendChild(inline);

            if (link.getInlineEntry() != null) {
                inline.appendChild(
                        entry.getOwnerDocument().importNode(entry((AtomEntry) link.getInlineEntry()), true));
            }
            if (link.getInlineFeed() != null) {
                inline.appendChild(
                        entry.getOwnerDocument().importNode(feed((AtomFeed) link.getInlineFeed()), true));
            }
        }

        entry.appendChild(linkElem);
    }
}

From source file:Main.java

/**
 * Converts a map to XML, where parent is the parent element, and every
 * other element is of the form <key>value</key> or <listElementName>listvalue</listElementName>
 * for each list index.  The list elements MUST be Strings if they are to
 * have text nodes.  But, they may also be another Map with key/value
 * pairs./*from w ww . j  ava  2s  .c o  m*/
 * <p/>
 * We assume that every key is an actual XML compatible element name; i.e. a
 * String object.  The values will be XML encoded automatically.
 *
 * @param elements        the elements, whether a list of elements or a map
 *                        of key/value pairs
 * @param parentElement   the parent element to append the new child to
 * @param document        the XML document to create new elements in
 * @param listElementName the name for the element, for every element in the
 *                        List
 *
 * @throws ParserConfigurationException if a configuration error occurs
 */
public static void mapToNode(final Object elements, final Element parentElement, final Document document,
        final String listElementName) throws ParserConfigurationException {
    Object value;
    if (elements instanceof Map) {
        final Map map = (Map) elements;
        final Iterator it = map.keySet().iterator();
        Element tmp;

        while (it.hasNext()) {
            final String key = (String) it.next();

            value = map.get(key);
            if (value instanceof Map) {
                tmp = document.createElement(key);
                mapToNode(value, tmp, document, null);
                parentElement.appendChild(tmp);
            } else if (value instanceof List) {
                mapToNode(value, parentElement, document, key);
            } else {
                tmp = document.createElement(key);
                if (value != null) { // null elements don't get in
                    tmp.appendChild(document.createTextNode((String) value));
                    parentElement.appendChild(tmp);
                }
            }
        }
    } else if (elements instanceof List) {
        if (listElementName == null || "".equals(listElementName.trim())) {
            throw new IllegalArgumentException(
                    "listElementName can never be null if a list is passed " + "in for elements");
        }
        final List list = (List) elements;
        for (int index = 0; index < list.size(); index++) {
            final Object element = list.get(index);
            if (element instanceof String) { // text node
                final String text = (String) list.get(index);
                final Element tmp = document.createElement(listElementName);
                tmp.appendChild(document.createTextNode(text));
                parentElement.appendChild(tmp);
            } else if (element instanceof Map) { // sub elements that have key/value pairs, or key/List pairs
                final Element tmp = document.createElement(listElementName);
                parentElement.appendChild(tmp);
                mapToNode(element, tmp, document, null);
            } else if (element instanceof List) {
                throw new IllegalArgumentException(
                        "List not supported " + "inside of List, cannot determine element name");
            }
        }
    } else {
        throw new IllegalArgumentException("unsupported class type for " + "mapToXML");
    }
}

From source file:XMLUtils.java

/**
 * Sets the text value for a given element.
 * @param el/*from ww w . j a  va  2s .co  m*/
 * @param value
 */
public static void setText(Element el, String value) {
    // remove the children if already exist
    while (el.getFirstChild() != null) {
        el.removeChild(el.getFirstChild());
    }
    if (value == null) {
        value = "";
    }
    Text txt = el.getOwnerDocument().createTextNode(value);
    el.appendChild(txt);
}