org.w3c.dom.Attr#getLocalName ( )源码实例Demo

下面列出了org.w3c.dom.Attr#getLocalName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: teamengine   文件: DomUtils.java
static public Map<QName, String> getAttributes(Node node) {
    Map<QName, String> atts = new HashMap<QName, String>();
    NamedNodeMap nnm = node.getAttributes();
    if (nnm != null) {
        for (int i = 0; i < nnm.getLength(); i++) {
            Attr att = (Attr) nnm.item(i);
            String uri = att.getBaseURI();
            String localname = att.getLocalName();
            String prefix = att.getPrefix();
            QName name;
            if (uri == null) {
                name = new QName(localname);
            } else if (prefix == null) {
                name = new QName(uri, localname);
            } else {
                name = new QName(uri, localname, prefix);
            }
            if (prefix == null
                    || !(prefix.equals("xmlns") || prefix.equals("xml"))) {
                atts.put(name, att.getValue());
            }
        }
    }
    return atts;
}
 
源代码2 项目: openjdk-8   文件: DomPostInitAction.java
public void run() {
    Set<String> declaredPrefixes = new HashSet<String>();
    for( Node n=node; n!=null && n.getNodeType()==Node.ELEMENT_NODE; n=n.getParentNode() ) {
        NamedNodeMap atts = n.getAttributes();
        if(atts==null)      continue; // broken DOM. but be graceful.
        for( int i=0; i<atts.getLength(); i++ ) {
            Attr a = (Attr)atts.item(i);
            String nsUri = a.getNamespaceURI();
            if(nsUri==null || !nsUri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI))
                continue;   // not a namespace declaration
            String prefix = a.getLocalName();
            if(prefix==null)
                continue;   // broken DOM. skip to be safe
            if(prefix.equals("xmlns")) {
                prefix = "";
            }
            String value = a.getValue();
            if(value==null)
                continue;   // broken DOM. skip to be safe
            if(declaredPrefixes.add(prefix)) {
                serializer.addInscopeBinding(value,prefix);
            }
        }
    }
}
 
源代码3 项目: openjdk-8   文件: DOMStreamReader.java
/**
 * If the given attribute is a namespace declaration for the given namespace URI,
 * return its prefix. Otherwise null.
 */
private static String getPrefixForAttr(Attr attr, String nsUri) {
    String attrName = attr.getNodeName();
    if (!attrName.startsWith("xmlns:") && !attrName.equals("xmlns"))
        return null;    // not nsdecl

    if(attr.getValue().equals(nsUri)) {
        if(attrName.equals("xmlns"))
            return "";
        String localName = attr.getLocalName();
        return (localName != null) ? localName :
            QName.valueOf(attrName).getLocalPart();
    }

    return null;
}
 
源代码4 项目: cosmo   文件: DomWriter.java
private static void writeAttribute(Attr a, XMLStreamWriter writer) throws XMLStreamException {        
    String local = a.getLocalName();
    if (local == null) {
        local = a.getNodeName();
    }
    String ns = a.getNamespaceURI();
    String value = a.getValue();

    // was handled by writing the default namespace in writeElement
    if (local.equals("xmlns")) {
        return;
    }

    if (ns != null) {
        String prefix = a.getPrefix();
        if (prefix != null) {
            writer.writeAttribute(prefix, ns, local, value);
        } else {
            writer.writeAttribute(ns, local, value);
        }
    } else {
        writer.writeAttribute(local, value);
    }
}
 
源代码5 项目: openjdk-jdk8u   文件: AnyTypeBeanInfo.java
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());
    }
}
 
源代码6 项目: openjdk-jdk9   文件: AnyTypeBeanInfo.java
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());
    }
}
 
源代码7 项目: ttt   文件: ISD.java
private static boolean isDefaultParameterValue(Attr attr) {
    String localName = attr.getLocalName();
    String value = attr.getValue();
    if ((value == null) || value.isEmpty())
        return false;
    else if (localName.equals("cellResolution"))
        return value.equals("32 15");
    else if (localName.equals("clockMode"))
        return value.equals("utc");
    else if (localName.equals("dropMode"))
        return value.equals("nonDrop");
    else if (localName.equals("frameRateMultiplier"))
        return value.equals("1 1");
    else if (localName.equals("markerMode"))
        return value.equals("discontinuous");
    else if (localName.equals("pixelAspectRatio"))
        return value.equals("1 1");
    else if (localName.equals("subFrameRate"))
        return value.equals("1");
    else if (localName.equals("tickRate"))
        return value.equals("1");
    else if (localName.equals("timeBase"))
        return value.equals("media");
    else
        return false;
}
 
源代码8 项目: hottub   文件: Internalizer.java
/**
 * Declares a new prefix on the given element and associates it
 * with the specified namespace URI.
 * <p>
 * Note that this method doesn't use the default namespace
 * even if it can.
 */
private String allocatePrefix( Element e, String nsUri ) {
    // look for existing namespaces.
    NamedNodeMap atts = e.getAttributes();
    for( int i=0; i<atts.getLength(); i++ ) {
        Attr a = (Attr)atts.item(i);
        if( Const.XMLNS_URI.equals(a.getNamespaceURI()) ) {
            if( a.getName().indexOf(':')==-1 )  continue;

            if( a.getValue().equals(nsUri) )
                return a.getLocalName();    // found one
        }
    }

    // none found. allocate new.
    while(true) {
        String prefix = "p"+(int)(Math.random()*1000000)+'_';
        if(e.getAttributeNodeNS(Const.XMLNS_URI,prefix)!=null)
            continue;   // this prefix is already allocated.

        e.setAttributeNS(Const.XMLNS_URI,"xmlns:"+prefix,nsUri);
        return prefix;
    }
}
 
源代码9 项目: TencentKona-8   文件: Internalizer.java
/**
 * Declares a new prefix on the given element and associates it
 * with the specified namespace URI.
 * <p>
 * Note that this method doesn't use the default namespace
 * even if it can.
 */
private String allocatePrefix( Element e, String nsUri ) {
    // look for existing namespaces.
    NamedNodeMap atts = e.getAttributes();
    for( int i=0; i<atts.getLength(); i++ ) {
        Attr a = (Attr)atts.item(i);
        if( Const.XMLNS_URI.equals(a.getNamespaceURI()) ) {
            if( a.getName().indexOf(':')==-1 )  continue;

            if( a.getValue().equals(nsUri) )
                return a.getLocalName();    // found one
        }
    }

    // none found. allocate new.
    while(true) {
        String prefix = "p"+(int)(Math.random()*1000000)+'_';
        if(e.getAttributeNodeNS(Const.XMLNS_URI,prefix)!=null)
            continue;   // this prefix is already allocated.

        e.setAttributeNS(Const.XMLNS_URI,"xmlns:"+prefix,nsUri);
        return prefix;
    }
}
 
源代码10 项目: openjdk-jdk8u   文件: Internalizer.java
/**
 * Declares a new prefix on the given element and associates it
 * with the specified namespace URI.
 * <p>
 * Note that this method doesn't use the default namespace
 * even if it can.
 */
private String allocatePrefix( Element e, String nsUri ) {
    // look for existing namespaces.
    NamedNodeMap atts = e.getAttributes();
    for( int i=0; i<atts.getLength(); i++ ) {
        Attr a = (Attr)atts.item(i);
        if( Const.XMLNS_URI.equals(a.getNamespaceURI()) ) {
            if( a.getName().indexOf(':')==-1 )  continue;

            if( a.getValue().equals(nsUri) )
                return a.getLocalName();    // found one
        }
    }

    // none found. allocate new.
    while(true) {
        String prefix = "p"+(int)(Math.random()*1000000)+'_';
        if(e.getAttributeNodeNS(Const.XMLNS_URI,prefix)!=null)
            continue;   // this prefix is already allocated.

        e.setAttributeNS(Const.XMLNS_URI,"xmlns:"+prefix,nsUri);
        return prefix;
    }
}
 
源代码11 项目: jdk8u60   文件: Internalizer.java
/**
 * Declares a new prefix on the given element and associates it
 * with the specified namespace URI.
 * <p>
 * Note that this method doesn't use the default namespace
 * even if it can.
 */
private String allocatePrefix( Element e, String nsUri ) {
    // look for existing namespaces.
    NamedNodeMap atts = e.getAttributes();
    for( int i=0; i<atts.getLength(); i++ ) {
        Attr a = (Attr)atts.item(i);
        if( Const.XMLNS_URI.equals(a.getNamespaceURI()) ) {
            if( a.getName().indexOf(':')==-1 )  continue;

            if( a.getValue().equals(nsUri) )
                return a.getLocalName();    // found one
        }
    }

    // none found. allocate new.
    while(true) {
        String prefix = "p"+(int)(Math.random()*1000000)+'_';
        if(e.getAttributeNodeNS(Const.XMLNS_URI,prefix)!=null)
            continue;   // this prefix is already allocated.

        e.setAttributeNS(Const.XMLNS_URI,"xmlns:"+prefix,nsUri);
        return prefix;
    }
}
 
源代码12 项目: ttt   文件: TextTransformer.java
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('"');
}
 
源代码13 项目: lams   文件: AbstractXMLObjectUnmarshaller.java
/**
 * Unmarshalls a namespace declaration attribute.
 * 
 * @param xmlObject the xmlObject to recieve the namespace decleration
 * @param attribute the namespace decleration attribute
 */
protected void unmarshallNamespaceAttribute(XMLObject xmlObject, Attr attribute) {
    if (log.isTraceEnabled()) {
        log.trace("{} is a namespace declaration, adding it to the list of namespaces on the XMLObject",
                XMLHelper.getNodeQName(attribute));
    }
    Namespace namespace;
    if(DatatypeHelper.safeEquals(attribute.getLocalName(), XMLConstants.XMLNS_PREFIX)){
        namespace = new Namespace(attribute.getValue(), null);
    }else{
        namespace = new Namespace(attribute.getValue(), attribute.getLocalName());
    }
    namespace.setAlwaysDeclare(true);
    xmlObject.getNamespaceManager().registerNamespaceDeclaration(namespace);
}
 
源代码14 项目: openjdk-8-source   文件: Canonicalizer20010315.java
/**
 * Returns the Attr[]s to be output for the given element.
 * <br>
 * The code of this method is a copy of {@link #handleAttributes(Element,
 * NameSpaceSymbTable)},
 * whereas it takes into account that subtree-c14n is -- well -- subtree-based.
 * So if the element in question isRoot of c14n, it's parent is not in the
 * node set, as well as all other ancestors.
 *
 * @param element
 * @param ns
 * @return the Attr[]s to be output
 * @throws CanonicalizationException
 */
@Override
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
    throws CanonicalizationException {
    if (!element.hasAttributes() && !firstCall) {
        return null;
    }
    // result will contain the attrs which have to be output
    final SortedSet<Attr> result = this.result;
    result.clear();

    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();

        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            String NUri = attribute.getNamespaceURI();
            String NName = attribute.getLocalName();
            String NValue = attribute.getValue();

            if (!XMLNS_URI.equals(NUri)) {
                //It's not a namespace attr node. Add to the result and continue.
                result.add(attribute);
            } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) {
                //The default mapping for xml must not be output.
                Node n = ns.addMappingAndRender(NName, NValue, attribute);

                if (n != null) {
                    //Render the ns definition
                    result.add((Attr)n);
                    if (C14nHelper.namespaceIsRelative(attribute)) {
                        Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() };
                        throw new CanonicalizationException(
                            "c14n.Canonicalizer.RelativeNamespace", exArgs
                        );
                    }
                }
            }
        }
    }

    if (firstCall) {
        //It is the first node of the subtree
        //Obtain all the namespaces defined in the parents, and added to the output.
        ns.getUnrenderedNodes(result);
        //output the attributes in the xml namespace.
        xmlattrStack.getXmlnsAttr(result);
        firstCall = false;
    }

    return result.iterator();
}
 
源代码15 项目: dragonwell8_jdk   文件: Canonicalizer11.java
/**
 * Returns the Attr[]s to be output for the given element.
 * <br>
 * The code of this method is a copy of {@link #handleAttributes(Element,
 * NameSpaceSymbTable)},
 * whereas it takes into account that subtree-c14n is -- well --
 * subtree-based.
 * So if the element in question isRoot of c14n, it's parent is not in the
 * node set, as well as all other ancestors.
 *
 * @param element
 * @param ns
 * @return the Attr[]s to be output
 * @throws CanonicalizationException
 */
@Override
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
    throws CanonicalizationException {
    if (!element.hasAttributes() && !firstCall) {
        return null;
    }
    // result will contain the attrs which have to be output
    final SortedSet<Attr> result = this.result;
    result.clear();

    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();

        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            String NUri = attribute.getNamespaceURI();
            String NName = attribute.getLocalName();
            String NValue = attribute.getValue();

            if (!XMLNS_URI.equals(NUri)) {
                // It's not a namespace attr node. Add to the result and continue.
                result.add(attribute);
            } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) {
                // The default mapping for xml must not be output.
                Node n = ns.addMappingAndRender(NName, NValue, attribute);

                if (n != null) {
                    // Render the ns definition
                    result.add((Attr)n);
                    if (C14nHelper.namespaceIsRelative(attribute)) {
                        Object exArgs[] = {element.getTagName(), NName, attribute.getNodeValue()};
                        throw new CanonicalizationException(
                            "c14n.Canonicalizer.RelativeNamespace", exArgs
                        );
                    }
                }
            }
        }
    }

    if (firstCall) {
        // It is the first node of the subtree
        // Obtain all the namespaces defined in the parents, and added to the output.
        ns.getUnrenderedNodes(result);
        // output the attributes in the xml namespace.
        xmlattrStack.getXmlnsAttr(result);
        firstCall = false;
    }

    return result.iterator();
}
 
源代码16 项目: TencentKona-8   文件: Canonicalizer20010315.java
/**
 * Returns the Attr[]s to be output for the given element.
 * <br>
 * The code of this method is a copy of {@link #handleAttributes(Element,
 * NameSpaceSymbTable)},
 * whereas it takes into account that subtree-c14n is -- well -- subtree-based.
 * So if the element in question isRoot of c14n, it's parent is not in the
 * node set, as well as all other ancestors.
 *
 * @param element
 * @param ns
 * @return the Attr[]s to be output
 * @throws CanonicalizationException
 */
@Override
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
    throws CanonicalizationException {
    if (!element.hasAttributes() && !firstCall) {
        return null;
    }
    // result will contain the attrs which have to be output
    final SortedSet<Attr> result = this.result;
    result.clear();

    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();

        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            String NUri = attribute.getNamespaceURI();
            String NName = attribute.getLocalName();
            String NValue = attribute.getValue();

            if (!XMLNS_URI.equals(NUri)) {
                //It's not a namespace attr node. Add to the result and continue.
                result.add(attribute);
            } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) {
                //The default mapping for xml must not be output.
                Node n = ns.addMappingAndRender(NName, NValue, attribute);

                if (n != null) {
                    //Render the ns definition
                    result.add((Attr)n);
                    if (C14nHelper.namespaceIsRelative(attribute)) {
                        Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() };
                        throw new CanonicalizationException(
                            "c14n.Canonicalizer.RelativeNamespace", exArgs
                        );
                    }
                }
            }
        }
    }

    if (firstCall) {
        //It is the first node of the subtree
        //Obtain all the namespaces defined in the parents, and added to the output.
        ns.getUnrenderedNodes(result);
        //output the attributes in the xml namespace.
        xmlattrStack.getXmlnsAttr(result);
        firstCall = false;
    }

    return result.iterator();
}
 
源代码17 项目: jdk8u-dev-jdk   文件: Canonicalizer11.java
/**
 * Returns the Attr[]s to be output for the given element.
 * <br>
 * The code of this method is a copy of {@link #handleAttributes(Element,
 * NameSpaceSymbTable)},
 * whereas it takes into account that subtree-c14n is -- well --
 * subtree-based.
 * So if the element in question isRoot of c14n, it's parent is not in the
 * node set, as well as all other ancestors.
 *
 * @param element
 * @param ns
 * @return the Attr[]s to be output
 * @throws CanonicalizationException
 */
@Override
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
    throws CanonicalizationException {
    if (!element.hasAttributes() && !firstCall) {
        return null;
    }
    // result will contain the attrs which have to be output
    final SortedSet<Attr> result = this.result;
    result.clear();

    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();

        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            String NUri = attribute.getNamespaceURI();
            String NName = attribute.getLocalName();
            String NValue = attribute.getValue();

            if (!XMLNS_URI.equals(NUri)) {
                // It's not a namespace attr node. Add to the result and continue.
                result.add(attribute);
            } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) {
                // The default mapping for xml must not be output.
                Node n = ns.addMappingAndRender(NName, NValue, attribute);

                if (n != null) {
                    // Render the ns definition
                    result.add((Attr)n);
                    if (C14nHelper.namespaceIsRelative(attribute)) {
                        Object exArgs[] = {element.getTagName(), NName, attribute.getNodeValue()};
                        throw new CanonicalizationException(
                            "c14n.Canonicalizer.RelativeNamespace", exArgs
                        );
                    }
                }
            }
        }
    }

    if (firstCall) {
        // It is the first node of the subtree
        // Obtain all the namespaces defined in the parents, and added to the output.
        ns.getUnrenderedNodes(result);
        // output the attributes in the xml namespace.
        xmlattrStack.getXmlnsAttr(result);
        firstCall = false;
    }

    return result.iterator();
}
 
源代码18 项目: ttt   文件: IMSC11SemanticsVerifier.java
private boolean verifyIMSCOtherAttributes(Object content, Locator locator, VerifierContext context) {
    boolean failed = false;
    NamedNodeMap attributes = context.getXMLNode(content).getAttributes();
    for (int i = 0, n = attributes.getLength(); i < n; ++i) {
        boolean failedAttribute = false;
        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);
        Model model = getModel();
        if (model.isNamespace(name.getNamespaceURI())) {
            String nsLabel;
            if (name.getNamespaceURI().indexOf(NAMESPACE_IMSC10_PREFIX) == 0)
                nsLabel = "IMSC";
            else if (name.getNamespaceURI().indexOf(NAMESPACE_EBUTT_PREFIX) == 0)
                nsLabel = "EBUTT";
            else
                nsLabel = null;
            if (nsLabel != null) {
                Reporter reporter = context.getReporter();
                String value = attribute.getValue();
                if (!model.isGlobalAttribute(name)) {
                    reporter.logError(reporter.message(locator, "*KEY*", "Unknown attribute in {0} namespace ''{1}'' not permitted on ''{2}''.",
                        nsLabel, name, context.getBindingElementName(content)));
                    failedAttribute = true;
                } else if (!model.isGlobalAttributePermitted(name, context.getBindingElementName(content))) {
                    reporter.logError(reporter.message(locator, "*KEY*", "{0} attribute ''{1}'' not permitted on ''{2}''.",
                        nsLabel, name, context.getBindingElementName(content)));
                    failedAttribute = true;
                } else if (!verifyNonEmptyOrPadded(content, name, value, locator, context)) {
                    reporter.logError(reporter.message(locator, "*KEY*", "Invalid {0} value ''{1}''.", name, value));
                    failedAttribute = true;
                } else if (nsLabel.equals("IMSC")) {
                    if (!verifyIMSCAttribute(content, locator, context, name, value)) {
                        reporter.logError(reporter.message(locator, "*KEY*", "Invalid {0} value ''{1}''.", name, value));
                        failedAttribute = true;
                    }
                } else if (nsLabel.equals("EBUTT")) {
                    if (!verifyEBUTTAttribute(content, locator, context, name, value)) {
                        reporter.logError(reporter.message(locator, "*KEY*", "Invalid {0} value ''{1}''.", name, value));
                        failedAttribute = true;
                    }
                }
            }
        }
        if (failedAttribute)
            failed = failedAttribute;
    }
    return !failed;
}
 
源代码19 项目: openjdk-jdk9   文件: AttrCompare.java
/**
 * 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());
}
 
源代码20 项目: jdk8u-jdk   文件: Canonicalizer20010315.java
/**
 * Returns the Attr[]s to be output for the given element.
 * <br>
 * The code of this method is a copy of {@link #handleAttributes(Element,
 * NameSpaceSymbTable)},
 * whereas it takes into account that subtree-c14n is -- well -- subtree-based.
 * So if the element in question isRoot of c14n, it's parent is not in the
 * node set, as well as all other ancestors.
 *
 * @param element
 * @param ns
 * @return the Attr[]s to be output
 * @throws CanonicalizationException
 */
@Override
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
    throws CanonicalizationException {
    if (!element.hasAttributes() && !firstCall) {
        return null;
    }
    // result will contain the attrs which have to be output
    final SortedSet<Attr> result = this.result;
    result.clear();

    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();

        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            String NUri = attribute.getNamespaceURI();
            String NName = attribute.getLocalName();
            String NValue = attribute.getValue();

            if (!XMLNS_URI.equals(NUri)) {
                //It's not a namespace attr node. Add to the result and continue.
                result.add(attribute);
            } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) {
                //The default mapping for xml must not be output.
                Node n = ns.addMappingAndRender(NName, NValue, attribute);

                if (n != null) {
                    //Render the ns definition
                    result.add((Attr)n);
                    if (C14nHelper.namespaceIsRelative(attribute)) {
                        Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() };
                        throw new CanonicalizationException(
                            "c14n.Canonicalizer.RelativeNamespace", exArgs
                        );
                    }
                }
            }
        }
    }

    if (firstCall) {
        //It is the first node of the subtree
        //Obtain all the namespaces defined in the parents, and added to the output.
        ns.getUnrenderedNodes(result);
        //output the attributes in the xml namespace.
        xmlattrStack.getXmlnsAttr(result);
        firstCall = false;
    }

    return result.iterator();
}