Search in sources :

Example 96 with Attr

use of org.w3c.dom.Attr in project j2objc by google.

the class DOM3TreeWalker method serializeAttList.

/**
     * Serializes the Attr Nodes of an Element.
     * 
     * @param node The OwnerElement whose Attr Nodes are to be serialized.
     */
protected void serializeAttList(Element node) throws SAXException {
    NamedNodeMap atts = node.getAttributes();
    int nAttrs = atts.getLength();
    for (int i = 0; i < nAttrs; i++) {
        Node attr = atts.item(i);
        String localName = attr.getLocalName();
        String attrName = attr.getNodeName();
        String attrPrefix = attr.getPrefix() == null ? "" : attr.getPrefix();
        String attrValue = attr.getNodeValue();
        // Determine the Attr's type.
        String type = null;
        if (fIsLevel3DOM) {
            type = ((Attr) attr).getSchemaTypeInfo().getTypeName();
        }
        type = type == null ? "CDATA" : type;
        String attrNS = attr.getNamespaceURI();
        if (attrNS != null && attrNS.length() == 0) {
            attrNS = null;
            // we must remove prefix for this attribute
            attrName = attr.getLocalName();
        }
        boolean isSpecified = ((Attr) attr).getSpecified();
        boolean addAttr = true;
        boolean applyFilter = false;
        boolean xmlnsAttr = attrName.equals("xmlns") || attrName.startsWith("xmlns:");
        // well-formed=true
        if ((fFeatures & WELLFORMED) != 0) {
            isAttributeWellFormed(attr);
        }
        // Step 3. Attribute
        if ((fFeatures & NAMESPACES) != 0 && !xmlnsAttr) {
            // If the Attr has a namespace URI
            if (attrNS != null) {
                attrPrefix = attrPrefix == null ? "" : attrPrefix;
                String declAttrPrefix = fNSBinder.getPrefix(attrNS);
                String declAttrNS = fNSBinder.getURI(attrPrefix);
                // conflict: attribute has a prefix that conflicts with a binding
                if ("".equals(attrPrefix) || "".equals(declAttrPrefix) || !attrPrefix.equals(declAttrPrefix)) {
                    // more prefixes
                    if (declAttrPrefix != null && !"".equals(declAttrPrefix)) {
                        // pick the prefix that was found and change attribute's
                        // prefix and nodeName.
                        attrPrefix = declAttrPrefix;
                        if (declAttrPrefix.length() > 0) {
                            attrName = declAttrPrefix + ":" + localName;
                        } else {
                            attrName = localName;
                        }
                    } else {
                        // declaration
                        if (attrPrefix != null && !"".equals(attrPrefix) && declAttrNS == null) {
                            // declare this prefix
                            if ((fFeatures & NAMESPACEDECLS) != 0) {
                                fSerializer.addAttribute(XMLNS_URI, attrPrefix, XMLNS_PREFIX + ":" + attrPrefix, "CDATA", attrNS);
                                fNSBinder.declarePrefix(attrPrefix, attrNS);
                                fLocalNSBinder.declarePrefix(attrPrefix, attrNS);
                            }
                        } else {
                            // find a prefix following the pattern "NS" +index
                            // (starting at 1)
                            // make sure this prefix is not declared in the current
                            // scope.
                            int counter = 1;
                            attrPrefix = "NS" + counter++;
                            while (fLocalNSBinder.getURI(attrPrefix) != null) {
                                attrPrefix = "NS" + counter++;
                            }
                            // change attribute's prefix and Name
                            attrName = attrPrefix + ":" + localName;
                            // Add the xmlns declaration attribute
                            if ((fFeatures & NAMESPACEDECLS) != 0) {
                                fSerializer.addAttribute(XMLNS_URI, attrPrefix, XMLNS_PREFIX + ":" + attrPrefix, "CDATA", attrNS);
                                fNSBinder.declarePrefix(attrPrefix, attrNS);
                                fLocalNSBinder.declarePrefix(attrPrefix, attrNS);
                            }
                        }
                    }
                }
            } else {
                // Attr has no localName
                if (localName == null) {
                    // DOM Level 1 node!
                    String msg = Utils.messages.createMessage(MsgKey.ER_NULL_LOCAL_ELEMENT_NAME, new Object[] { attrName });
                    if (fErrorHandler != null) {
                        fErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_ERROR, msg, MsgKey.ER_NULL_LOCAL_ELEMENT_NAME, null, null, null));
                    }
                } else {
                // uri=null and no colon
                // attr has no namespace URI and no prefix
                // no action is required, since attrs don't use default
                }
            }
        }
        // What about default xmlns attributes???? check for xmlnsAttr
        if ((((fFeatures & DISCARDDEFAULT) != 0) && isSpecified) || ((fFeatures & DISCARDDEFAULT) == 0)) {
            applyFilter = true;
        } else {
            addAttr = false;
        }
        if (applyFilter) {
            // or namespace decl attributes
            if (fFilter != null && (fFilter.getWhatToShow() & NodeFilter.SHOW_ATTRIBUTE) != 0) {
                if (!xmlnsAttr) {
                    short code = fFilter.acceptNode(attr);
                    switch(code) {
                        case NodeFilter.FILTER_REJECT:
                        case NodeFilter.FILTER_SKIP:
                            addAttr = false;
                            break;
                        //fall through..
                        default:
                    }
                }
            }
        }
        // if the node is a namespace node
        if (addAttr && xmlnsAttr) {
            // If namespace-declarations=true, add the node , else don't add it
            if ((fFeatures & NAMESPACEDECLS) != 0) {
                // The namespace may have been fixed up, in that case don't add it.
                if (localName != null && !"".equals(localName)) {
                    fSerializer.addAttribute(attrNS, localName, attrName, type, attrValue);
                }
            }
        } else if (addAttr && !xmlnsAttr) {
            // attempt to add a xmlns attr for the prefixed attribute
            if (((fFeatures & NAMESPACEDECLS) != 0) && (attrNS != null)) {
                fSerializer.addAttribute(attrNS, localName, attrName, type, attrValue);
            } else {
                fSerializer.addAttribute("", localName, attrName, type, attrValue);
            }
        }
        // 
        if (xmlnsAttr && ((fFeatures & NAMESPACEDECLS) != 0)) {
            int index;
            // Use "" instead of null, as Xerces likes "" for the 
            // name of the default namespace.  Fix attributed 
            // to "Steven Murray" <smurray@ebt.com>.
            String prefix = (index = attrName.indexOf(":")) < 0 ? "" : attrName.substring(index + 1);
            if (!"".equals(prefix)) {
                fSerializer.namespaceAfterStartElement(prefix, attrValue);
            }
        }
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) Attr(org.w3c.dom.Attr)

Example 97 with Attr

use of org.w3c.dom.Attr in project j2objc by google.

the class DTMNodeProxy method getAttributeNodeNS.

/**
   *
   * @param namespaceURI
   * @param localName
   *
   *
   * @see org.w3c.dom.Element
   */
public final Attr getAttributeNodeNS(String namespaceURI, String localName) {
    Attr retAttr = null;
    int n = dtm.getAttributeNode(node, namespaceURI, localName);
    if (n != DTM.NULL)
        retAttr = (Attr) dtm.getNode(n);
    return retAttr;
}
Also used : Attr(org.w3c.dom.Attr)

Example 98 with Attr

use of org.w3c.dom.Attr in project j2objc by google.

the class ElementImpl method setAttributeNS.

public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException {
    Attr attr = getAttributeNodeNS(namespaceURI, qualifiedName);
    if (attr == null) {
        attr = document.createAttributeNS(namespaceURI, qualifiedName);
        setAttributeNodeNS(attr);
    }
    attr.setValue(value);
}
Also used : Attr(org.w3c.dom.Attr)

Example 99 with Attr

use of org.w3c.dom.Attr in project gradle by gradle.

the class IvyPluginPublishingRules method createIvyMarkerPublication.

private void createIvyMarkerPublication(PluginDeclaration declaration, final IvyPublication mainPublication, PublicationContainer publications) {
    String pluginId = declaration.getId();
    IvyPublication publication = publications.create(declaration.getName() + "PluginMarkerIvy", IvyPublication.class);
    publication.setOrganisation(pluginId);
    publication.setModule(pluginId + PLUGIN_MARKER_SUFFIX);
    publication.descriptor(new Action<IvyModuleDescriptorSpec>() {

        @Override
        public void execute(IvyModuleDescriptorSpec descriptor) {
            descriptor.withXml(new Action<XmlProvider>() {

                @Override
                public void execute(XmlProvider xmlProvider) {
                    Element root = xmlProvider.asElement();
                    Document document = root.getOwnerDocument();
                    Node dependencies = root.getElementsByTagName("dependencies").item(0);
                    Node dependency = dependencies.appendChild(document.createElement("dependency"));
                    Attr org = document.createAttribute("org");
                    org.setValue(mainPublication.getOrganisation());
                    dependency.getAttributes().setNamedItem(org);
                    Attr name = document.createAttribute("name");
                    name.setValue(mainPublication.getModule());
                    dependency.getAttributes().setNamedItem(name);
                    Attr rev = document.createAttribute("rev");
                    rev.setValue(mainPublication.getRevision());
                    dependency.getAttributes().setNamedItem(rev);
                }
            });
        }
    });
}
Also used : Action(org.gradle.api.Action) XmlProvider(org.gradle.api.XmlProvider) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) IvyModuleDescriptorSpec(org.gradle.api.publish.ivy.IvyModuleDescriptorSpec) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) IvyPublication(org.gradle.api.publish.ivy.IvyPublication)

Example 100 with Attr

use of org.w3c.dom.Attr in project XobotOS by xamarin.

the class ElementImpl method setAttributeNS.

public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException {
    Attr attr = getAttributeNodeNS(namespaceURI, qualifiedName);
    if (attr == null) {
        attr = document.createAttributeNS(namespaceURI, qualifiedName);
        setAttributeNodeNS(attr);
    }
    attr.setValue(value);
}
Also used : Attr(org.w3c.dom.Attr)

Aggregations

Attr (org.w3c.dom.Attr)385 Element (org.w3c.dom.Element)178 NamedNodeMap (org.w3c.dom.NamedNodeMap)160 Document (org.w3c.dom.Document)134 Node (org.w3c.dom.Node)126 NodeList (org.w3c.dom.NodeList)80 DOMException (org.w3c.dom.DOMException)30 ArrayList (java.util.ArrayList)25 DocumentBuilder (javax.xml.parsers.DocumentBuilder)16 HashMap (java.util.HashMap)12 Text (org.w3c.dom.Text)12 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)11 CanonicalizationException (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException)10 RubyString (org.jruby.RubyString)10 SAML2Exception (com.sun.identity.saml2.common.SAML2Exception)8 File (java.io.File)8 QName (javax.xml.namespace.QName)8 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)7 ParseException (java.text.ParseException)6 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)6