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

The following examples show how to use org.w3c.dom.Attr#getLocalName() . 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: DomPostInitAction.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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);
            }
        }
    }
}
 
Example 2
Source File: Internalizer.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example 3
Source File: Internalizer.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example 4
Source File: Internalizer.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example 5
Source File: Internalizer.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example 6
Source File: ISD.java    From ttt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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;
}
 
Example 7
Source File: AnyTypeBeanInfo.java    From openjdk-jdk9 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 8
Source File: AnyTypeBeanInfo.java    From openjdk-jdk8u 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 9
Source File: DomWriter.java    From cosmo with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 10
Source File: DOMStreamReader.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: DomUtils.java    From teamengine with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 12
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 13
Source File: AbstractXMLObjectUnmarshaller.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 14
Source File: Canonicalizer20010315.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 15
Source File: Canonicalizer11.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 16
Source File: IMSC11SemanticsVerifier.java    From ttt with BSD 2-Clause "Simplified" License 4 votes vote down vote up
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;
}
 
Example 17
Source File: Canonicalizer11.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 18
Source File: Canonicalizer20010315.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 19
Source File: AttrCompare.java    From openjdk-jdk9 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 20
Source File: Canonicalizer20010315.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
}