Java Code Examples for org.w3c.dom.Attr#getName()

The following examples show how to use org.w3c.dom.Attr#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnyTypeBeanInfo.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public void serializeAttributes(Object element, XMLSerializer target) throws SAXException {
    NamedNodeMap al = ((Element)element).getAttributes();
    int len = al.getLength();
    for( int i=0; i<len; i++ ) {
        Attr a = (Attr)al.item(i);
        // work defensively
        String uri = a.getNamespaceURI();
        if(uri==null)   uri="";
        String local = a.getLocalName();
        String name = a.getName();
        if(local==null) local = name;
        if (uri.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE) && ("nil".equals(local))) {
            isNilIncluded = true;
        }
        if(name.startsWith("xmlns")) continue;// DOM reports ns decls as attributes

        target.attribute(uri,local,a.getValue());
    }
}
 
Example 2
Source File: XMLTree.java    From pdfxtk with Apache License 2.0 6 votes vote down vote up
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
     Icon icon;
     String label;
     if (value instanceof Element) {
Element e = (Element) value;
label = "<"+e.getTagName()+">";
icon = elementIcon;
     } else if (value instanceof Attr) {
Attr a = (Attr) value;
label = a.getName()+"="+a.getValue();
icon = attributeIcon;
     } else if (value instanceof Text) {
Text t = (Text) value;
label = t.getData();
icon = textIcon;
     } else {
label = "?";
icon = null;
     }
     if (label.length() > MAX_TEXT_LENGTH) {
label = label.substring(0, MAX_TEXT_LENGTH-1-3)+"...";
     }
     return new Cell(label, icon, selected, hasFocus);
   }
 
Example 3
Source File: Locators.java    From ttt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static String getLocatorAttribute(Element element) {
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0, n = attributes.getLength(); i < n; ++i) {
        Node item = attributes.item(i);
        if (!(item instanceof Attr))
            continue;
        Attr attribute = (Attr) item;
        String nsUri = attribute.getNamespaceURI();
        String localName = attribute.getLocalName();
        if (localName == null)
            localName = attribute.getName();
        if (localName.indexOf("xmlns") == 0)
            continue;
        QName name = new QName(nsUri != null ? nsUri : "", localName);
        if (name.equals(getLocatorAttributeQName()))
            return attribute.getValue();
    }
    return null;
}
 
Example 4
Source File: HtmlPage.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Returns all namespaces defined in the root element of this page.</p>
 * <p>The default namespace has a key of an empty string.</p>
 * @return all namespaces defined in the root element of this page
 */
public Map<String, String> getNamespaces() {
    final org.w3c.dom.NamedNodeMap attributes = getDocumentElement().getAttributes();
    final Map<String, String> namespaces = new HashMap<>();
    for (int i = 0; i < attributes.getLength(); i++) {
        final Attr attr = (Attr) attributes.item(i);
        String name = attr.getName();
        if (name.startsWith("xmlns")) {
            int startPos = 5;
            if (name.length() > 5 && name.charAt(5) == ':') {
                startPos = 6;
            }
            name = name.substring(startPos);
            namespaces.put(name, attr.getValue());
        }
    }
    return namespaces;
}
 
Example 5
Source File: AnyTypeBeanInfo.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public void serializeAttributes(Object element, XMLSerializer target) throws SAXException {
    NamedNodeMap al = ((Element)element).getAttributes();
    int len = al.getLength();
    for( int i=0; i<len; i++ ) {
        Attr a = (Attr)al.item(i);
        // work defensively
        String uri = a.getNamespaceURI();
        if(uri==null)   uri="";
        String local = a.getLocalName();
        String name = a.getName();
        if(local==null) local = name;
        if (uri.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE) && ("nil".equals(local))) {
            isNilIncluded = true;
        }
        if(name.startsWith("xmlns")) continue;// DOM reports ns decls as attributes

        target.attribute(uri,local,a.getValue());
    }
}
 
Example 6
Source File: BasicFilter.java    From libreveris with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean canTolerate (Element elem,
                            Attr attr,
                            String controlValue,
                            String testValue)
{
    String attrName = attr.getName();

    // First check specific attributes
    Boolean bool = info.canTolerateAttr(
            elem.getNodeName(),
            attrName,
            controlValue,
            testValue);
    if (bool != null) {
        return bool.booleanValue();
    }

    // Then fall back on global attributes
    bool = info.canTolerateAttr(NO_ELEM, attrName, controlValue, testValue);
    if (bool != null) {
        return bool.booleanValue();
    } else {
        return false;
    }
}
 
Example 7
Source File: Documents.java    From ttt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static Map<QName,String> getAttributes(Element e) {
    Map<QName,String> attributes = new java.util.HashMap<QName,String>();
    NamedNodeMap attrs = e.getAttributes();
    for (int i = 0, n = attrs.getLength(); i < n; ++i) {
        Node node = attrs.item(i);
        if (node instanceof Attr) {
            Attr a = (Attr) node;
            String ns = a.getNamespaceURI();
            String ln;
            if ((ns == null) || (ns.length() == 0))
                ln = a.getName();
            else
                ln = a.getLocalName();
            attributes.put(new QName(ns != null ? ns : "", ln), a.getValue());
        }
    }
    return attributes;
}
 
Example 8
Source File: IIOMetadataNode.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public Attr setAttributeNode(Attr newAttr) throws DOMException {
    Element owner = newAttr.getOwnerElement();
    if (owner != null) {
        if (owner == this) {
            return null;
        } else {
            throw new DOMException(DOMException.INUSE_ATTRIBUTE_ERR,
                                   "Attribute is already in use");
        }
    }

    IIOAttr attr;
    if (newAttr instanceof IIOAttr) {
        attr = (IIOAttr)newAttr;
        attr.setOwnerElement(this);
    } else {
        attr = new IIOAttr(this,
                           newAttr.getName(),
                           newAttr.getValue());
    }

    Attr oldAttr = getAttributeNode(attr.getName());
    if (oldAttr != null) {
        removeAttributeNode(oldAttr);
    }

    attributes.add(attr);

    return oldAttr;
}
 
Example 9
Source File: TextTransformer.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void serialize(Writer w, Attr a) throws IOException {
    String ns = a.getNamespaceURI();
    String ln = a.getLocalName();
    if (ln == null)
        ln = a.getName();
    QName qn = new QName(ns, ln);
    serialize(w, qn);
    w.write('=');
    w.write('"');
    serialize(w, a.getValue());
    w.write('"');
}
 
Example 10
Source File: DOMUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** @param el 
* @return the attributes as Map<QName, String>
 */
public static Map getAttributes(Element el)
{
    Map attmap = new HashMap();
    NamedNodeMap attribs = el.getAttributes();
    for (int i = 0; i < attribs.getLength(); i++)
    {
        Attr attr = (Attr)attribs.item(i);
        String name = attr.getName();
        QName qname = resolveQName(el, name);
        String value = attr.getNodeValue();
        attmap.put(qname, value);
    }
    return attmap;
}
 
Example 11
Source File: NodeUtils.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Looks for an existing prefix for a a given namespace.
 * The prefix must start with "xmlns:". The whole prefix is returned.
 * @param attributes the list of attributes to look through
 * @param ns the namespace to find.
 * @return the found prefix or null if none is found.
 */
private static String getPrefixForNs(NamedNodeMap attributes, String ns) {
    if (attributes != null) {
        for (int i = 0, n = attributes.getLength(); i < n; i++) {
            Attr attribute = (Attr) attributes.item(i);
            if (ns.equals(attribute.getValue()) && ns.startsWith(SdkConstants.XMLNS_PREFIX)) {
                return attribute.getName();
            }
        }
    }

    return null;
}
 
Example 12
Source File: IIOMetadataNode.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public Attr setAttributeNode(Attr newAttr) throws DOMException {
    Element owner = newAttr.getOwnerElement();
    if (owner != null) {
        if (owner == this) {
            return null;
        } else {
            throw new DOMException(DOMException.INUSE_ATTRIBUTE_ERR,
                                   "Attribute is already in use");
        }
    }

    IIOAttr attr;
    if (newAttr instanceof IIOAttr) {
        attr = (IIOAttr)newAttr;
        attr.setOwnerElement(this);
    } else {
        attr = new IIOAttr(this,
                           newAttr.getName(),
                           newAttr.getValue());
    }

    Attr oldAttr = getAttributeNode(attr.getName());
    if (oldAttr != null) {
        removeAttributeNode(oldAttr);
    }

    attributes.add(attr);

    return oldAttr;
}
 
Example 13
Source File: MCRXEditorTransformer.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public void initializePostprocessor(Node postProcessorNode) {
    NamedNodeMap attributes = postProcessorNode.getAttributes();
    int attributesLength = attributes.getLength();
    HashMap<String, String> attributeMap = new HashMap<>();
    for (int i = 0; i < attributesLength; i++) {
        Attr item = (Attr) attributes.item(i); // this should be save because we called getAttributes earlier
        String attrName = item.getName();
        String attrValue = item.getValue();
        attributeMap.put(attrName, attrValue);
    }

    editorSession.getPostProcessor().setAttributes(attributeMap);
}
 
Example 14
Source File: ConfigurationResource.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
/**
 * This method will read a tree of elements and their attributes
 * 
 * @param element the root element of the tree
 */
private void readXmlElement(
                             LinkedList<String> currentElementPath,
                             Element element ) {

    //append this node element to the current path
    currentElementPath.add(element.getNodeName());

    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            readXmlElement(currentElementPath, (Element) childNode);
        }
    }

    //read all attributes
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attribute = (Attr) attributes.item(i);

        String propertyName = getCurrentXmlElementPath(currentElementPath) + attribute.getName();
        String propertyValue = attribute.getValue();

        //put in the properties table
        properties.put(propertyName, propertyValue);

        log.debug("Added property with name '" + propertyName + "' and value '" + propertyValue + "'");
    }

    //after we are done with the node, remove it from the path
    currentElementPath.removeLast();
}
 
Example 15
Source File: IIOMetadataNode.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public Attr setAttributeNode(Attr newAttr) throws DOMException {
    Element owner = newAttr.getOwnerElement();
    if (owner != null) {
        if (owner == this) {
            return null;
        } else {
            throw new DOMException(DOMException.INUSE_ATTRIBUTE_ERR,
                                   "Attribute is already in use");
        }
    }

    IIOAttr attr;
    if (newAttr instanceof IIOAttr) {
        attr = (IIOAttr)newAttr;
        attr.setOwnerElement(this);
    } else {
        attr = new IIOAttr(this,
                           newAttr.getName(),
                           newAttr.getValue());
    }

    Attr oldAttr = getAttributeNode(attr.getName());
    if (oldAttr != null) {
        removeAttributeNode(oldAttr);
    }

    attributes.add(attr);

    return oldAttr;
}
 
Example 16
Source File: CompletionContextImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Adds namespaces from the tag to this context, possibly overriding namespaces
 * from previously added tags. Tags should be added starting from the root down
 * to the context position.
 */
private void addNamespacesFrom(SyntaxElement s) {
    Node e = s.getNode();
    NamedNodeMap attrs = e.getAttributes();
    String nodePrefix = getPrefix(e.getNodeName(), false);
    String version = null;
    String xsltAttrName = null;
    
    for (int i = attrs.getLength() - 1; i >= 0; i--) {
        Node n = attrs.item(i);
        if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
            Attr a = (Attr)n;
            String attrName = a.getName();
            String value = a.getValue();
            addNamespace(attrName, value, nodePrefix);

        
            if(value.trim().equals("http://www.w3.org/1999/XSL/Transform")) { //NOI18N
                xsltAttrName = attrName;
            }
            if(CompletionUtil.getLocalNameFromTag(attrName).
                    equals("version")) { //NOI18N
                version = value.trim();
            }
        } 
    }
    
    if (xsltAttrName != null && "2.0".equals(version)) {
        String prefix = getPrefix(xsltAttrName, false);
        if (prefix == null) {
            // override nonNS location because nonNS schema is XSLT 2.0
            noNamespaceSchemaLocation = "http://www.w3.org/2007/schema-for-xslt20.xsd";
        } else {
            addSchemaLocation(prefix + " http://www.w3.org/2007/schema-for-xslt20.xsd"); //NOI18N
        }
    }
}
 
Example 17
Source File: AttrCompare.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compares two attributes based on the C14n specification.
 *
 * <UL>
 * <LI>Namespace nodes have a lesser document order position than
 *   attribute nodes.
 * <LI> An element's namespace nodes are sorted lexicographically by
 *   local name (the default namespace node, if one exists, has no
 *   local name and is therefore lexicographically least).
 * <LI> An element's attribute nodes are sorted lexicographically with
 *   namespace URI as the primary key and local name as the secondary
 *   key (an empty namespace URI is lexicographically least).
 * </UL>
 *
 * @param attr0
 * @param attr1
 * @return returns a negative integer, zero, or a positive integer as
 *   obj0 is less than, equal to, or greater than obj1
 *
 */
public int compare(Attr attr0, Attr attr1) {
    String namespaceURI0 = attr0.getNamespaceURI();
    String namespaceURI1 = attr1.getNamespaceURI();

    boolean isNamespaceAttr0 = XMLNS.equals(namespaceURI0);
    boolean isNamespaceAttr1 = XMLNS.equals(namespaceURI1);

    if (isNamespaceAttr0) {
        if (isNamespaceAttr1) {
            // both are namespaces
            String localname0 = attr0.getLocalName();
            String localname1 = attr1.getLocalName();

            if ("xmlns".equals(localname0)) {
                localname0 = "";
            }

            if ("xmlns".equals(localname1)) {
                localname1 = "";
            }

            return localname0.compareTo(localname1);
        }
        // attr0 is a namespace, attr1 is not
        return ATTR0_BEFORE_ATTR1;
    } else if (isNamespaceAttr1) {
        // attr1 is a namespace, attr0 is not
        return ATTR1_BEFORE_ATTR0;
    }

    // none is a namespace
    if (namespaceURI0 == null) {
        if (namespaceURI1 == null) {
            String name0 = attr0.getName();
            String name1 = attr1.getName();
            return name0.compareTo(name1);
        }
        return ATTR0_BEFORE_ATTR1;
    } else if (namespaceURI1 == null) {
        return ATTR1_BEFORE_ATTR0;
    }

    int a = namespaceURI0.compareTo(namespaceURI1);
    if (a != 0) {
        return a;
    }

    return (attr0.getLocalName()).compareTo(attr1.getLocalName());
}
 
Example 18
Source File: ActionMappingScanner.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Scan through Element named action.
 */
void visitElement_action(Element element) {
    String name = element.getAttribute("name");
    mapping = new DefaultActionMapping(name);
    mapping.withPlugins = withPlugins;

    NamedNodeMap attrs = element.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr = (Attr) attrs.item(i);
        String value = attr.getValue();
        switch (attr.getName()) {
            case "displayName":
                mapping.displayName = value;
                break;
            case "repeatable":
                mapping.repeatableAction = Boolean.parseBoolean(value);
                break;
            case "priority":
                try {
                    mapping.priority = Integer.parseInt(value);
                } catch (NumberFormatException ex) {
                }
                break;
        }
    }
    NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element nodeElement = (Element) node;
            switch (nodeElement.getTagName()) {
                case "reload":
                    visitElement_reload(nodeElement);
                    break;
                case "args":
                    mapping.args = visitElement_args(nodeElement);
                    break;
            }
        }
    }
    mappings.add(mapping);
}
 
Example 19
Source File: AttrCompare.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Compares two attributes based on the C14n specification.
 *
 * <UL>
 * <LI>Namespace nodes have a lesser document order position than
 *   attribute nodes.
 * <LI> An element's namespace nodes are sorted lexicographically by
 *   local name (the default namespace node, if one exists, has no
 *   local name and is therefore lexicographically least).
 * <LI> An element's attribute nodes are sorted lexicographically with
 *   namespace URI as the primary key and local name as the secondary
 *   key (an empty namespace URI is lexicographically least).
 * </UL>
 *
 * @param attr0
 * @param attr1
 * @return returns a negative integer, zero, or a positive integer as
 *   obj0 is less than, equal to, or greater than obj1
 *
 */
public int compare(Attr attr0, Attr attr1) {
    String namespaceURI0 = attr0.getNamespaceURI();
    String namespaceURI1 = attr1.getNamespaceURI();

    boolean isNamespaceAttr0 = XMLNS.equals(namespaceURI0);
    boolean isNamespaceAttr1 = XMLNS.equals(namespaceURI1);

    if (isNamespaceAttr0) {
        if (isNamespaceAttr1) {
            // both are namespaces
            String localname0 = attr0.getLocalName();
            String localname1 = attr1.getLocalName();

            if ("xmlns".equals(localname0)) {
                localname0 = "";
            }

            if ("xmlns".equals(localname1)) {
                localname1 = "";
            }

            return localname0.compareTo(localname1);
        }
        // attr0 is a namespace, attr1 is not
        return ATTR0_BEFORE_ATTR1;
    } else if (isNamespaceAttr1) {
        // attr1 is a namespace, attr0 is not
        return ATTR1_BEFORE_ATTR0;
    }

    // none is a namespace
    if (namespaceURI0 == null) {
        if (namespaceURI1 == null) {
            String name0 = attr0.getName();
            String name1 = attr1.getName();
            return name0.compareTo(name1);
        }
        return ATTR0_BEFORE_ATTR1;
    } else if (namespaceURI1 == null) {
        return ATTR1_BEFORE_ATTR0;
    }

    int a = namespaceURI0.compareTo(namespaceURI1);
    if (a != 0) {
        return a;
    }

    return (attr0.getLocalName()).compareTo(attr1.getLocalName());
}
 
Example 20
Source File: AttrCompare.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compares two attributes based on the C14n specification.
 *
 * <UL>
 * <LI>Namespace nodes have a lesser document order position than
 *   attribute nodes.
 * <LI> An element's namespace nodes are sorted lexicographically by
 *   local name (the default namespace node, if one exists, has no
 *   local name and is therefore lexicographically least).
 * <LI> An element's attribute nodes are sorted lexicographically with
 *   namespace URI as the primary key and local name as the secondary
 *   key (an empty namespace URI is lexicographically least).
 * </UL>
 *
 * @param attr0
 * @param attr1
 * @return returns a negative integer, zero, or a positive integer as
 *   obj0 is less than, equal to, or greater than obj1
 *
 */
public int compare(Attr attr0, Attr attr1) {
    String namespaceURI0 = attr0.getNamespaceURI();
    String namespaceURI1 = attr1.getNamespaceURI();

    boolean isNamespaceAttr0 = XMLNS.equals(namespaceURI0);
    boolean isNamespaceAttr1 = XMLNS.equals(namespaceURI1);

    if (isNamespaceAttr0) {
        if (isNamespaceAttr1) {
            // both are namespaces
            String localname0 = attr0.getLocalName();
            String localname1 = attr1.getLocalName();

            if ("xmlns".equals(localname0)) {
                localname0 = "";
            }

            if ("xmlns".equals(localname1)) {
                localname1 = "";
            }

            return localname0.compareTo(localname1);
        }
        // attr0 is a namespace, attr1 is not
        return ATTR0_BEFORE_ATTR1;
    } else if (isNamespaceAttr1) {
        // attr1 is a namespace, attr0 is not
        return ATTR1_BEFORE_ATTR0;
    }

    // none is a namespace
    if (namespaceURI0 == null) {
        if (namespaceURI1 == null) {
            String name0 = attr0.getName();
            String name1 = attr1.getName();
            return name0.compareTo(name1);
        }
        return ATTR0_BEFORE_ATTR1;
    } else if (namespaceURI1 == null) {
        return ATTR1_BEFORE_ATTR0;
    }

    int a = namespaceURI0.compareTo(namespaceURI1);
    if (a != 0) {
        return a;
    }

    return (attr0.getLocalName()).compareTo(attr1.getLocalName());
}