Example usage for org.w3c.dom Document createTextNode

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

Introduction

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

Prototype

public Text createTextNode(String data);

Source Link

Document

Creates a Text node given the specified string.

Usage

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.
 */// w ww. j a  v 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  a v a2  s .c om
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 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.
 *//* www . ja v a 2s.  c o 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:DomUtils.java

/**
 * Add literal text to the supplied element.
 * @param element Target DOM Element./*w  w  w .  ja v  a  2 s. co  m*/
 * @param literalText Literal text to be added.
 */
public static void addLiteral(Element element, String literalText) {

    Document document = element.getOwnerDocument();
    Text literal = document.createTextNode(literalText);
    element.appendChild(literal);
}

From source file:com.occamlab.te.parsers.ImageParser.java

private static void processBufferedImage(BufferedImage buffimage, String formatName, NodeList nodes)
        throws Exception {
    HashMap<Object, Object> bandMap = new HashMap<Object, Object>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getLocalName().equals("subimage")) {
                Element e = (Element) node;
                int x = Integer.parseInt(e.getAttribute("x"));
                int y = Integer.parseInt(e.getAttribute("y"));
                int w = Integer.parseInt(e.getAttribute("width"));
                int h = Integer.parseInt(e.getAttribute("height"));
                processBufferedImage(buffimage.getSubimage(x, y, w, h), formatName, e.getChildNodes());
            } else if (node.getLocalName().equals("checksum")) {
                CRC32 checksum = new CRC32();
                Raster raster = buffimage.getRaster();
                DataBufferByte buffer;
                if (node.getParentNode().getLocalName().equals("subimage")) {
                    WritableRaster outRaster = raster.createCompatibleWritableRaster();
                    buffimage.copyData(outRaster);
                    buffer = (DataBufferByte) outRaster.getDataBuffer();
                } else {
                    buffer = (DataBufferByte) raster.getDataBuffer();
                }//from   w w  w  . j a v a2  s. c o  m
                int numbanks = buffer.getNumBanks();
                for (int j = 0; j < numbanks; j++) {
                    checksum.update(buffer.getData(j));
                }
                Document doc = node.getOwnerDocument();
                node.appendChild(doc.createTextNode(Long.toString(checksum.getValue())));
            } else if (node.getLocalName().equals("count")) {
                String band = ((Element) node).getAttribute("bands");
                String sample = ((Element) node).getAttribute("sample");
                if (sample.equals("all")) {
                    bandMap.put(band, null);
                } else {
                    HashMap<Object, Object> sampleMap = (HashMap<Object, Object>) bandMap.get(band);
                    if (sampleMap == null) {
                        if (!bandMap.containsKey(band)) {
                            sampleMap = new HashMap<Object, Object>();
                            bandMap.put(band, sampleMap);
                        }
                    }
                    sampleMap.put(Integer.decode(sample), new Integer(0));
                }
            } else if (node.getLocalName().equals("transparentNodata")) { // 2011-08-24
                                                                          // PwD
                String transparentNodata = checkTransparentNodata(buffimage, node);
                node.setTextContent(transparentNodata);
            }
        }
    }

    Iterator bandIt = bandMap.keySet().iterator();
    while (bandIt.hasNext()) {
        String band_str = (String) bandIt.next();
        int band_indexes[];
        if (buffimage.getType() == BufferedImage.TYPE_BYTE_BINARY
                || buffimage.getType() == BufferedImage.TYPE_BYTE_GRAY) {
            band_indexes = new int[1];
            band_indexes[0] = 0;
        } else {
            band_indexes = new int[band_str.length()];
            for (int i = 0; i < band_str.length(); i++) {
                if (band_str.charAt(i) == 'A')
                    band_indexes[i] = 3;
                if (band_str.charAt(i) == 'B')
                    band_indexes[i] = 2;
                if (band_str.charAt(i) == 'G')
                    band_indexes[i] = 1;
                if (band_str.charAt(i) == 'R')
                    band_indexes[i] = 0;
            }
        }

        Raster raster = buffimage.getRaster();
        java.util.HashMap sampleMap = (java.util.HashMap) bandMap.get(band_str);
        boolean addall = (sampleMap == null);
        if (sampleMap == null) {
            sampleMap = new java.util.HashMap();
            bandMap.put(band_str, sampleMap);
        }

        int minx = raster.getMinX();
        int maxx = minx + raster.getWidth();
        int miny = raster.getMinY();
        int maxy = miny + raster.getHeight();
        int bands[][] = new int[band_indexes.length][raster.getWidth()];

        for (int y = miny; y < maxy; y++) {
            for (int i = 0; i < band_indexes.length; i++) {
                raster.getSamples(minx, y, maxx, 1, band_indexes[i], bands[i]);
            }
            for (int x = minx; x < maxx; x++) {
                int sample = 0;
                for (int i = 0; i < band_indexes.length; i++) {
                    sample |= bands[i][x] << ((band_indexes.length - i - 1) * 8);
                }

                Integer sampleObj = new Integer(sample);

                boolean add = addall;
                if (!addall) {
                    add = sampleMap.containsKey(sampleObj);
                }
                if (add) {
                    Integer count = (Integer) sampleMap.get(sampleObj);
                    if (count == null) {
                        count = new Integer(0);
                    }
                    count = new Integer(count.intValue() + 1);
                    sampleMap.put(sampleObj, count);
                }
            }
        }
    }

    Node node = nodes.item(0);
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getLocalName().equals("count")) {
                String band = ((Element) node).getAttribute("bands");
                String sample = ((Element) node).getAttribute("sample");
                HashMap sampleMap = (HashMap) bandMap.get(band);
                Document doc = node.getOwnerDocument();
                if (sample.equals("all")) {
                    Node parent = node.getParentNode();
                    Node prevSibling = node.getPreviousSibling();
                    Iterator sampleIt = sampleMap.keySet().iterator();
                    Element countnode = null;
                    int digits;
                    String prefix;
                    switch (buffimage.getType()) {
                    case BufferedImage.TYPE_BYTE_BINARY:
                        digits = 1;
                        prefix = "";
                        break;
                    case BufferedImage.TYPE_BYTE_GRAY:
                        digits = 2;
                        prefix = "0x";
                        break;
                    default:
                        prefix = "0x";
                        digits = band.length() * 2;
                    }
                    while (sampleIt.hasNext()) {
                        countnode = doc.createElementNS(node.getNamespaceURI(), "count");
                        Integer sampleInt = (Integer) sampleIt.next();
                        Integer count = (Integer) sampleMap.get(sampleInt);
                        if (band.length() > 0) {
                            countnode.setAttribute("bands", band);
                        }
                        countnode.setAttribute("sample", prefix + HexString(sampleInt.intValue(), digits));
                        Node textnode = doc.createTextNode(count.toString());
                        countnode.appendChild(textnode);
                        parent.insertBefore(countnode, node);
                        if (sampleIt.hasNext()) {
                            if (prevSibling != null && prevSibling.getNodeType() == Node.TEXT_NODE) {
                                parent.insertBefore(prevSibling.cloneNode(false), node);
                            }
                        }
                    }
                    parent.removeChild(node);
                    node = countnode;
                } else {
                    Integer count = (Integer) sampleMap.get(Integer.decode(sample));
                    if (count == null)
                        count = new Integer(0);
                    Node textnode = doc.createTextNode(count.toString());
                    node.appendChild(textnode);
                }
            }
        }
        node = node.getNextSibling();
    }
}

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.
 *///  w w w .j  av a  2 s .  com
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.meltmedia.cadmium.core.util.WarUtils.java

/**
 * Adds a shiro environment listener to load the shiro config file.
 * @param doc The xml DOM document to create the new xml elements with.
 * @param root The xml Element node to add the listener to.
 *///  www .  jav a  2 s.  c o m
public static void addListener(Document doc, Element root) {
    Element listener = doc.createElement("listener");
    Element listenerClass = doc.createElement("listener-class");
    listener.appendChild(listenerClass);
    listenerClass.appendChild(doc.createTextNode("org.apache.shiro.web.env.EnvironmentLoaderListener"));

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

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * This method sets a text node to the given DOM element.
 *
 * @param document  root document// w  w  w.  ja va 2 s  .co m
 * @param parentDom parent node
 * @param text      text to be added
 */
public static void setTextNode(final Document document, final Element parentDom, final String text) {

    final Text textNode = document.createTextNode(text);
    parentDom.appendChild(textNode);
}

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

private static Element feed(final AtomFeed feed) throws ParserConfigurationException {
    final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
    final Document doc = builder.newDocument();

    final Element feedElem = doc.createElement(ODataConstants.ATOM_ELEM_FEED);
    feedElem.setAttribute(XMLConstants.XMLNS_ATTRIBUTE, ODataConstants.NS_ATOM);
    feedElem.setAttribute(ODataConstants.XMLNS_METADATA, ODataConstants.NS_METADATA);
    feedElem.setAttribute(ODataConstants.XMLNS_DATASERVICES, ODataConstants.NS_DATASERVICES);
    feedElem.setAttribute(ODataConstants.XMLNS_GML, ODataConstants.NS_GML);
    feedElem.setAttribute(ODataConstants.XMLNS_GEORSS, ODataConstants.NS_GEORSS);
    if (feed.getBaseURI() != null) {
        feedElem.setAttribute(ODataConstants.ATTR_XMLBASE, feed.getBaseURI().toASCIIString());
    }//from  w w  w. j av  a 2s.com
    doc.appendChild(feedElem);

    if (StringUtils.isNotBlank(feed.getTitle())) {
        final Element title = doc.createElement(ODataConstants.ATOM_ELEM_TITLE);
        title.appendChild(doc.createTextNode(feed.getTitle()));
        feedElem.appendChild(title);
    }

    if (StringUtils.isNotBlank(feed.getSummary())) {
        final Element summary = doc.createElement(ODataConstants.ATOM_ELEM_SUMMARY);
        summary.appendChild(doc.createTextNode(feed.getSummary()));
        feedElem.appendChild(summary);
    }

    for (AtomEntry entry : feed.getEntries()) {
        feedElem.appendChild(doc.importNode(entry(entry), true));
    }

    return feedElem;
}

From source file:com.viettel.ws.client.JDBCUtil.java

public static Element createRowElement(Object obj, Document doc) {
    Element row = doc.createElement("Row");
    Class cls = obj.getClass();//from w  w w . jav  a2 s . co  m
    Field[] fieldArr = cls.getDeclaredFields();
    SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yyyy");

    for (int i = 0; i < fieldArr.length; i++) {
        try {
            String fieldName = fieldArr[i].getName();
            String getMethodName = "get" + UpcaseFirst(fieldName);
            Method getMethod = cls.getMethod(getMethodName);

            Object value = getMethod.invoke(obj);
            if (fieldArr[i].getType().equals(Date.class)) {
                value = fm.format(value);
            }

            Element node = doc.createElement(convertToOrigin(fieldName));
            node.appendChild(doc.createTextNode(value.toString()));
            row.appendChild(node);

        } catch (Exception ex) {
            LogUtil.addLog(ex);//binhnt sonar a160901
            continue;
        }
    }
    return row;
}