Example usage for org.w3c.dom NamedNodeMap getLength

List of usage examples for org.w3c.dom NamedNodeMap getLength

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap getLength.

Prototype

public int getLength();

Source Link

Document

The number of nodes in this map.

Usage

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

public static void handleElement(Element elt, JSONObject obj, boolean ignoreStepIds, boolean useType)
        throws JSONException {
    String key = elt.getTagName();
    JSONObject value = new JSONObject();
    NodeList nl = elt.getChildNodes();

    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);//from w  ww  . j a v a 2  s . c o m

        if (node instanceof Element) {
            Element child = (Element) node;
            Object childValue = useType ? getValue(child, ignoreStepIds, useType) : null;

            if (childValue != null) {
                value.accumulate(child.getTagName(), childValue);
            } else {
                handleElement(child, value, ignoreStepIds, useType);
            }
        }
    }

    JSONObject attr = new JSONObject();
    NamedNodeMap nnm = elt.getAttributes();

    for (int i = 0; i < nnm.getLength(); i++) {
        Node node = nnm.item(i);
        if (ignoreStepIds && (node.getNodeName() != "step_id")) {
            attr.accumulate(node.getNodeName(), node.getNodeValue());
        }
    }

    if (value.length() == 0) {
        String content = elt.getTextContent();
        if (attr.length() == 0) {
            obj.accumulate(key, content);
        } else {
            value.accumulate("text", content);
        }
    }

    if (attr.length() != 0) {
        value.accumulate("attr", attr);
    }

    if (value.length() != 0) {
        obj.accumulate(key, value);
    }
}

From source file:com.centeractive.ws.builder.soap.XmlUtils.java

public static boolean hasContentAttributes(Element elm) {
    NamedNodeMap attributes = elm.getAttributes();
    for (int c = 0; c < attributes.getLength(); c++) {
        Node item = attributes.item(c);
        String ns = item.getNamespaceURI();
        if (!ns.equals(Constants.XML_NS)
        // && !ns.equals( Constants.XSI_NS ) && !ns.equals(
        // Constants.XSI_NS_2000 )
        // && !ns.equals( Constants.XSD_NS )
        )/*from   www .  ja v a  2  s  .c o  m*/
            return true;
    }

    return false;
}

From source file:com.websystique.springmvc.youtube_api.Function.java

@SuppressWarnings({ "static-access", "unchecked" })

private String printNote(NodeList nodeList) {
    List<VideoSub> listsub = new ArrayList<VideoSub>();
    List<VideoSub> savelistsub = new ArrayList<VideoSub>();
    for (int count = 0; count < nodeList.getLength(); count++) {
        VideoSub v = new VideoSub();
        Node tempNode = nodeList.item(count);
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            v.setTextsb(replace_all(replacetext(tempNode.getTextContent().replace("...", "."))));
            if (tempNode.hasAttributes()) {
                NamedNodeMap nodeMap = tempNode.getAttributes();
                for (int i = 0; i < nodeMap.getLength(); i++) {
                    Node node = nodeMap.item(i);
                    if (i == 0) {
                        v.setEnd(node.getNodeValue());
                    } else {
                        v.setStart(node.getNodeValue());
                    }//from  w  w w.  j  a v  a2s  .  co m
                }
            }
        }
        listsub.add(v);
    }
    String savetext = "";
    double sodu = 0;
    double savedur = 0;
    String dutext = "";
    double start = 0;
    String kq = "";
    boolean flag = true;// vo lan dau
    boolean flagchamcuoi = false;// vo lan dau
    for (int i = 0; i < listsub.size(); i++) {
        String cau = listsub.get(i).getTextsb();
        if (flag || flagchamcuoi == true) {
            start = Double.parseDouble(listsub.get(i).getStart());
            savedur = Double.parseDouble(listsub.get(i).getEnd());
            flag = false;
            flagchamcuoi = false;
        }

        double dur = Double.parseDouble(listsub.get(i).getEnd());
        if (sodu != 0) {
            savetext += dutext;
            start = Double.parseDouble(listsub.get(i).getStart());
            start -= sodu;
            System.out.println(sodu + "So du" + start);
            dutext = "";
            sodu = 0;
        }
        if (cau.contains("####") == true) {
            float sogiay = (float) dur;
            float phantram = 0;
            int socau = 0;
            int sotu = demsotu(cau);
            String[] savecau = new String[100];
            int[] savesotu = new int[1000];
            double[] phantramsotu = new double[1000];
            for (String retval : cau.split("####")) { // split("\\|;\\s*|\\.\\s*|\\?\\s*")
                if (retval != "" && retval.length() > 0) {
                    savecau[socau] = retval;
                    savesotu[socau] = (1 + StringUtils.countMatches(retval.trim(), " "));
                    double pt = 0.0;
                    double so1 = (double) savesotu[socau];
                    double so2 = (double) sotu;
                    pt = so1 / so2;
                    phantramsotu[socau] = pt * sogiay;
                    socau++;
                }
            }

            // *******************************
            char[] array = cau.toCharArray();
            // save cau 1
            VideoSub cau1 = new VideoSub();
            cau1.setTextsb((savetext + " " + savecau[0]).replace("\n", " ").replace("'", " ").trim());
            cau1.setStart(start + "");
            String end = (savedur + phantramsotu[0]) + "";
            cau1.setEnd(end + "");
            if (savecau[0] != ".")
                savelistsub.add(cau1);
            // add vao
            savetext = "";
            // cap nhat start
            start += Double.parseDouble(cau1.getEnd());

            savedur = 0;
            // save cau giua
            for (int i1 = 1; i1 < socau - 1; i1++) {
                // add vao
                VideoSub Cau2 = new VideoSub();
                Cau2.setTextsb(savecau[i1].replace("\n", " ").replace("'", " ").trim());
                Cau2.setStart(start + "");
                Cau2.setEnd(phantramsotu[i1] + "");
                if (savecau[i1] != ".")
                    savelistsub.add(Cau2);

                start += phantramsotu[i1];
            }

            // save cau cuoi
            if (array[cau.length() - 5] == '.' || array[cau.length() - 5] == '?'
                    || array[cau.length() - 5] == ';') {
                flagchamcuoi = true;

            }

            System.out.println(cau);
            if (array[cau.length() - 5] == '.' || array[cau.length() - 5] == ';'
                    || array[cau.length() - 5] == '?') {
                if (socau - 1 != 0) {
                    VideoSub Cau3 = new VideoSub();
                    Cau3.setTextsb(savecau[socau - 1].replace("\n", " ").replace("'", "-").trim());
                    Cau3.setStart(start + "");
                    Cau3.setEnd(phantramsotu[socau - 1] + "");
                    if (savecau[socau - 1] != ".")
                        savelistsub.add(Cau3);

                    start += phantramsotu[socau - 1];
                }
            } else {
                dutext = savecau[socau - 1];
                sodu = phantramsotu[socau - 1];
            }
        } else {
            savetext += " " + cau;
            savedur += dur;
        }
    }
    // ================Xuat File Json===============
    JSONArray jsonArray = new JSONArray();
    for (int i = 0; i < savelistsub.size(); i++) {
        JSONObject obj = new JSONObject();
        if (savelistsub.get(i).getTextsb() != ".") {
            String startadd = savelistsub.get(i).getStart();
            String duradd = savelistsub.get(i).getEnd();
            String textadd = savelistsub.get(i).getTextsb();
            obj.put("start", startadd);
            obj.put("dur", duradd);
            obj.put("text", textadd);
            jsonArray.add(obj);
        }

    }
    String out_String = jsonArray.toJSONString();
    out_String = out_String.toString();

    // ================Xuat File Json===============

    return out_String;
}

From source file:XMLReader.java

/** Writes node and all child nodes into System.out
 * @param node XML node from from XML tree wrom which will output statement start
 * @param indent number of spaces used to indent output
 *//*from   w w  w.  j a  v a  2s.  c o m*/
private void writeDocumentToOutput(Node node, int indent) {
    // get element name
    String nodeName = node.getNodeName();
    // get element value
    String nodeValue = getElementValue(node);
    // get attributes of element
    NamedNodeMap attributes = node.getAttributes();
    System.out.println(getIndentSpaces(indent) + "NodeName: " + nodeName + ", NodeValue: " + nodeValue);
    for (int i = 0; i < attributes.getLength(); i++) {
        Node attribute = attributes.item(i);
        System.out.println(getIndentSpaces(indent + 2) + "AttributeName: " + attribute.getNodeName()
                + ", attributeValue: " + attribute.getNodeValue());
    }
    // write all child nodes recursively
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            writeDocumentToOutput(child, indent + 2);
        }
    }
}

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

private static void copyNodeWithoutNamespace(Document document, Node parentNode, Node sourceNode) {
    Node destinationNode;/*  ww  w  .j a v  a  2  s . c om*/
    if (sourceNode instanceof Document) {
        destinationNode = parentNode;
    } else {
        if (sourceNode instanceof Element) {
            String localName = XMLUtils.getLocalName(sourceNode);
            destinationNode = document.createElement(localName);

            // Copy attributes
            NamedNodeMap attributes = sourceNode.getAttributes();
            for (int i = 0; i < attributes.getLength(); i++) {
                Node sourceAttribute = attributes.item(i);

                String prefix = XMLUtils.getPrefix(sourceAttribute);

                if (!prefix.equalsIgnoreCase("xmlns")) {
                    ((Element) destinationNode).setAttribute(XMLUtils.getLocalName(sourceAttribute),
                            sourceAttribute.getNodeValue());
                }
            }
        } else {
            destinationNode = document.importNode(sourceNode, false);
        }

        parentNode.appendChild(destinationNode);
    }

    NodeList childNodes = sourceNode.getChildNodes();
    int len = childNodes.getLength();
    for (int i = 0; i < len; i++) {
        XMLUtils.copyNodeWithoutNamespace(document, destinationNode, childNodes.item(i));
    }
}

From source file:edu.eurac.commul.pepperModules.mmax2.Salt2MMAXMapping.java

public static SAnnotationStringValueMatchCondition parseSAnnotationStringValueMatchCondition(Node node) {
    NamedNodeMap attributes = node.getAttributes();
    Node stringValueAttributeNode = attributes.getNamedItem(SANN_STRING_VALUE_REGEXP);
    String stringValueRegExp = null;
    if (stringValueAttributeNode != null) {
        stringValueRegExp = stringValueAttributeNode.getNodeValue();
        attributes.removeNamedItem(SANN_STRING_VALUE_REGEXP);
    } else {//from w  w w. ja va 2s .  c o m
        throw new PepperModuleException("'" + SANN_STRING_VALUE_REGEXP
                + "' attribute not found on SAnnotation String Value Match Condition '" + node + "'");
    }
    if (attributes.getLength() != 0) {
        throw new PepperModuleException(
                "Additional unexpected attributes found on SAnnotation String Value Match Condition '" + node
                        + "'");
    }

    return new SAnnotationStringValueMatchCondition(Pattern.compile(stringValueRegExp));
}

From source file:com.diversityarrays.dalclient.DalUtil.java

/**
 * <p>/*  ww  w .  j a  va  2  s.  c om*/
 * Extract the &lt;Error Message="..." /&gt; from the XML response.
 * In fact it will retrieve all of the attributes from the 'Error' element
 * because some DAL responses use the attribute name as part of the message!
 * <p>
 * Note that if the Message attribute is missing, this will still produce
 * an error so it is <b>not</b> the same as just calling:
 * <code>
 * getElementAttributeValue(doc, "Error", "Message");
 * </code>
 * <br>
 * Any other attributes of the <i>Error</i> element will also be returned. This caters
 * for diagnostic errors which are returned by some of the DAL "update" operations.
 * @param doc
 * @return error message as a String or null
 */
static public String getXmlDalErrorMessage(Document doc) {
    String result = null;
    NodeList errorElements = doc.getElementsByTagName(DALClient.TAG_ERROR);
    if (errorElements.getLength() > 0) {
        Node item = errorElements.item(0);
        NamedNodeMap attributes = item.getAttributes();
        if (attributes == null) {
            result = String.format("No attributes available in '%s' element", //$NON-NLS-1$
                    DALClient.TAG_ERROR);
        } else {
            // We will get them *all*.
            StringBuilder sb = new StringBuilder();
            String sep = ""; //$NON-NLS-1$
            int nAttributes = attributes.getLength();
            for (int ai = 0; ai < nAttributes; ++ai) {
                Node attr = attributes.item(ai);
                if (attr != null) {
                    sb.append(sep).append(attr.getNodeName()).append('=').append(attr.getTextContent());
                    sep = ", "; //$NON-NLS-1$
                }
            }
            result = sb.toString();
        }
    }
    return result;
}

From source file:com.bstek.dorado.config.xml.DispatchableXmlParser.java

/**
 * ??<br>/*from w  w w. j a v  a  2  s  .  c  om*/
 * XML(Attribute)???
 * <code><Property name="xxx">XXXX</Property></code>
 * ??
 * 
 * @param element
 *            XML
 * @param context
 *            ?
 * @return ?Map???
 *         ??????
 * @throws Exception
 */
protected Map<String, Object> parseProperties(Element element, ParseContext context) throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();

    for (Element propertyElement : DomUtils.getChildrenByTagName(element, XmlConstants.PROPERTY)) {
        String name = propertyElement.getAttribute(XmlConstants.ATTRIBUTE_NAME);
        if (StringUtils.isNotEmpty(name)) {
            properties.put(name, propertyElement);
        }
    }

    NamedNodeMap attributes = element.getAttributes();
    int attributeNum = attributes.getLength();
    for (int i = 0; i < attributeNum; i++) {
        Node node = attributes.item(i);
        String property = node.getNodeName();
        properties.put(property, node);
    }

    for (Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator(); it.hasNext();) {
        Map.Entry<String, Object> entry = it.next();
        String property = entry.getKey();
        Node node = (Node) entry.getValue();
        Object value = parseProperty(property, node, context);
        if (value != ConfigUtils.IGNORE_VALUE) {
            entry.setValue(value);
        } else {
            it.remove();
        }
    }
    return properties;
}

From source file:spec.schema.SchemaCurrent_TO_2008_02_Test.java

/**
 * Checks if the <code>Image</code> tag was correctly transformed.
 * //from www . jav  a  2s . c  o  m
 * @param destNode The node from the transformed file.
 * @param srcNode The Image node from the source file
 */
private void checkImageNode(Node destNode, Node srcNode) {
    assertNotNull(destNode);
    assertNotNull(srcNode);
    NamedNodeMap attributesSrc = srcNode.getAttributes();
    String nameSrc = "";
    String idSrc = "";
    Node n;
    String name;
    for (int i = 0; i < attributesSrc.getLength(); i++) {
        n = attributesSrc.item(i);
        name = n.getNodeName();
        if (name.equals(XMLWriter.ID_ATTRIBUTE))
            idSrc = n.getNodeValue();
        else if (name.equals(XMLWriter.NAME_ATTRIBUTE))
            nameSrc = n.getNodeValue();
    }

    // compare the stored values for ID and Name attributes
    // to those on the output node
    NamedNodeMap attributes = destNode.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        n = attributes.item(i);
        if (n != null) {
            name = n.getNodeName();
            if (name.equals(XMLWriter.ID_ATTRIBUTE))
                assertEquals(n.getNodeValue(), idSrc);
            else if (name.equals(XMLWriter.NAME_ATTRIBUTE))
                assertEquals(n.getNodeValue(), nameSrc);
        }
    }

    // Find the pixels node in the input image node
    // (if more than one last will be found)
    Node pixelsNode = null;
    NodeList list = srcNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        n = list.item(i);
        if (n != null) {
            name = n.getNodeName();
            if (name.contains(XMLWriter.PIXELS_TAG))
                pixelsNode = n;
        }
    }
    // Find the pixels node in the output image node
    // (if more than one this will be incorrect)
    list = destNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        n = list.item(i);
        if (n != null) {
            name = n.getNodeName();
            if (name.contains(XMLWriter.PIXELS_TAG))
                //  - compare the found node to the input node stored above
                checkPixelsNode(n, pixelsNode);
        }
    }
}

From source file:DOMWriter.java

/** Returns a sorted list of attributes. */
protected Attr[] sortAttributes(NamedNodeMap attrs) {

    int len = (attrs != null) ? attrs.getLength() : 0;
    Attr array[] = new Attr[len];
    for (int i = 0; i < len; i++) {
        array[i] = (Attr) attrs.item(i);
    }//  w w  w  .j a v  a  2  s  .c  om
    for (int i = 0; i < len - 1; i++) {
        String name = array[i].getNodeName();
        int index = i;
        for (int j = i + 1; j < len; j++) {
            String curName = array[j].getNodeName();
            if (curName.compareTo(name) < 0) {
                name = curName;
                index = j;
            }
        }
        if (index != i) {
            Attr temp = array[i];
            array[i] = array[index];
            array[index] = temp;
        }
    }

    return array;

}