Search in sources :

Example 96 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project gocd by gocd.

the class XmlDocument method createAndCacheNamespaces.

private void createAndCacheNamespaces(Ruby ruby, Node node) {
    if (node == null)
        return;
    if (node.hasAttributes()) {
        NamedNodeMap nodeMap = node.getAttributes();
        for (int i = 0; i < nodeMap.getLength(); i++) {
            Node n = nodeMap.item(i);
            if (n instanceof Attr) {
                Attr attr = (Attr) n;
                String attrName = attr.getName();
                // not sure, but need to get value always before document is referred.
                // or lose attribute value
                // don't delete this line
                String attrValue = attr.getValue();
                if (isNamespace(attrName)) {
                    // create and cache
                    XmlNamespace.createFromAttr(ruby, attr);
                }
            }
        }
    }
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        createAndCacheNamespaces(ruby, children.item(i));
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) Attr(org.w3c.dom.Attr)

Example 97 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project gocd by gocd.

the class Canonicalizer11 method handleParent.

@Override
protected void handleParent(Element e, NameSpaceSymbTable ns) {
    if (!e.hasAttributes() && e.getNamespaceURI() == null) {
        return;
    }
    xmlattrStack.push(-1);
    NamedNodeMap attrs = e.getAttributes();
    int attrsLength = attrs.getLength();
    for (int i = 0; i < attrsLength; i++) {
        Attr attribute = (Attr) attrs.item(i);
        String NName = attribute.getLocalName();
        String NValue = attribute.getNodeValue();
        if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())) {
            if (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) {
                ns.addMapping(NName, NValue, attribute);
            }
        } else if (!"id".equals(NName) && XML_LANG_URI.equals(attribute.getNamespaceURI())) {
            xmlattrStack.addXmlnsAttr(attribute);
        }
    }
    if (e.getNamespaceURI() != null) {
        String NName = e.getPrefix();
        String NValue = e.getNamespaceURI();
        String Name;
        if (NName == null || NName.equals("")) {
            NName = "xmlns";
            Name = "xmlns";
        } else {
            Name = "xmlns:" + NName;
        }
        Attr n = e.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", Name);
        n.setValue(NValue);
        ns.addMapping(NName, NValue, n);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Attr(org.w3c.dom.Attr)

Example 98 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project gocd by gocd.

the class Canonicalizer20010315 method handleParent.

@Override
protected void handleParent(Element e, NameSpaceSymbTable ns) {
    if (!e.hasAttributes() && e.getNamespaceURI() == null) {
        return;
    }
    xmlattrStack.push(-1);
    NamedNodeMap attrs = e.getAttributes();
    int attrsLength = attrs.getLength();
    for (int i = 0; i < attrsLength; i++) {
        Attr attribute = (Attr) attrs.item(i);
        String NName = attribute.getLocalName();
        String NValue = attribute.getNodeValue();
        if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())) {
            if (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) {
                ns.addMapping(NName, NValue, attribute);
            }
        } else if (XML_LANG_URI.equals(attribute.getNamespaceURI())) {
            xmlattrStack.addXmlnsAttr(attribute);
        }
    }
    if (e.getNamespaceURI() != null) {
        String NName = e.getPrefix();
        String NValue = e.getNamespaceURI();
        String Name;
        if (NName == null || NName.equals("")) {
            NName = "xmlns";
            Name = "xmlns";
        } else {
            Name = "xmlns:" + NName;
        }
        Attr n = e.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", Name);
        n.setValue(NValue);
        ns.addMapping(NName, NValue, n);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Attr(org.w3c.dom.Attr)

Example 99 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project gocd by gocd.

the class CanonicalizerBase method handleParent.

protected void handleParent(Element e, NameSpaceSymbTable ns) {
    if (!e.hasAttributes() && e.getNamespaceURI() == null) {
        return;
    }
    NamedNodeMap attrs = e.getAttributes();
    int attrsLength = attrs.getLength();
    for (int i = 0; i < attrsLength; i++) {
        Attr attribute = (Attr) attrs.item(i);
        String NName = attribute.getLocalName();
        String NValue = attribute.getNodeValue();
        if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI()) && (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue))) {
            ns.addMapping(NName, NValue, attribute);
        }
    }
    if (e.getNamespaceURI() != null) {
        String NName = e.getPrefix();
        String NValue = e.getNamespaceURI();
        String Name;
        if (NName == null || NName.equals("")) {
            NName = XMLNS;
            Name = XMLNS;
        } else {
            Name = XMLNS + ":" + NName;
        }
        Attr n = e.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", Name);
        n.setValue(NValue);
        ns.addMapping(NName, NValue, n);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Attr(org.w3c.dom.Attr)

Example 100 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project buck by facebook.

the class XmlPrettyPrinter method printOpenElementTag.

private void printOpenElementTag(int depth, Node node) {
    Element element = (Element) node;
    if (newlineBeforeElementOpen(element, depth)) {
        mOut.append(mLineSeparator);
    }
    if (indentBeforeElementOpen(element, depth)) {
        indent(depth);
    }
    mOut.append('<').append(element.getTagName());
    NamedNodeMap attributes = element.getAttributes();
    int attributeCount = attributes.getLength();
    if (attributeCount > 0) {
        // Sort the attributes
        List<Attr> attributeList = new ArrayList<Attr>();
        for (int i = 0; i < attributeCount; i++) {
            attributeList.add((Attr) attributes.item(i));
        }
        Comparator<Attr> comparator = mPrefs.getAttributeComparator();
        if (comparator != null) {
            Collections.sort(attributeList, comparator);
        }
        // Put the single attribute on the same line as the element tag?
        boolean singleLine = mPrefs.oneAttributeOnFirstLine && attributeCount == 1 || // usually just zero, one or two) on the same line
        mStyle == XmlFormatStyle.RESOURCE;
        // We also place the namespace declaration on the same line as the root element,
        // but this doesn't also imply singleLine handling; subsequent attributes end up
        // on their own lines
        boolean indentNextAttribute;
        if (singleLine || (depth == 0 && XMLNS.equals(attributeList.get(0).getPrefix()))) {
            mOut.append(' ');
            indentNextAttribute = false;
        } else {
            mOut.append(mLineSeparator);
            indentNextAttribute = true;
        }
        Attr last = attributeList.get(attributeCount - 1);
        for (Attr attribute : attributeList) {
            if (indentNextAttribute) {
                indent(depth + 1);
            }
            mOut.append(attribute.getName());
            mOut.append('=').append('"');
            XmlUtils.appendXmlAttributeValue(mOut, attribute.getValue());
            mOut.append('"');
            // immediately follow the last attribute
            if (attribute != last) {
                //$NON-NLS-1$
                mOut.append(singleLine ? " " : mLineSeparator);
                indentNextAttribute = !singleLine;
            }
        }
    }
    boolean isClosed = isEmptyTag(element);
    // element
    if (mPrefs.spaceBeforeClose && (mStyle != XmlFormatStyle.RESOURCE || isClosed) && // in <selector> files etc still treat the <item> entries as in resource files
    !TAG_ITEM.equals(element.getTagName()) && (isClosed || element.getAttributes().getLength() > 0)) {
        mOut.append(' ');
    }
    if (isClosed) {
        mOut.append('/');
    }
    mOut.append('>');
    if (newlineAfterElementOpen(element, depth, isClosed)) {
        mOut.append(mLineSeparator);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Attr(org.w3c.dom.Attr)

Aggregations

NamedNodeMap (org.w3c.dom.NamedNodeMap)993 Node (org.w3c.dom.Node)690 NodeList (org.w3c.dom.NodeList)339 Attr (org.w3c.dom.Attr)295 Element (org.w3c.dom.Element)238 Document (org.w3c.dom.Document)154 ArrayList (java.util.ArrayList)92 HashMap (java.util.HashMap)91 DocumentBuilder (javax.xml.parsers.DocumentBuilder)59 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)58 IOException (java.io.IOException)53 SAXException (org.xml.sax.SAXException)41 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)40 List (java.util.List)36 Map (java.util.Map)33 File (java.io.File)27 InputStream (java.io.InputStream)26 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)25 DOMException (org.w3c.dom.DOMException)25 ByteArrayInputStream (java.io.ByteArrayInputStream)19