Example usage for org.w3c.dom Attr getLocalName

List of usage examples for org.w3c.dom Attr getLocalName

Introduction

In this page you can find the example usage for org.w3c.dom Attr getLocalName.

Prototype

public String getLocalName();

Source Link

Document

Returns the local part of the qualified name of this node.

Usage

From source file:org.apache.xml.security.c14n.implementations.Canonicalizer11.java

/**
 * Returns the Attr[]s to be output for the given element.
 * <br>/*from  w ww  .j  ava 2 s . c o  m*/
 * 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();
}

From source file:org.apache.xml.security.c14n.implementations.Canonicalizer11.java

/**
 * Returns the Attr[]s to be output for the given element.
 * <br>//from www.  jav a 2  s  .  c om
 * IMPORTANT: This method expects to work on a modified DOM tree, i.e. a 
 * DOM which has been prepared using 
 * {@link org.apache.xml.security.utils.XMLUtils#circumventBug2650(
 * org.w3c.dom.Document)}.
 * 
 * @param element
 * @param ns
 * @return the Attr[]s to be output
 * @throws CanonicalizationException
 */
@Override
protected Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns)
        throws CanonicalizationException {
    // result will contain the attrs which have to be output
    xmlattrStack.push(ns.getLevel());
    boolean isRealVisible = isVisibleDO(element, ns.getLevel()) == 1;
    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)) {
                //A non namespace definition node.
                if (XML_LANG_URI.equals(NUri)) {
                    if (NName.equals("id")) {
                        if (isRealVisible) {
                            // treat xml:id like any other attribute 
                            // (emit it, but don't inherit it)
                            result.add(attribute);
                        }
                    } else {
                        xmlattrStack.addXmlnsAttr(attribute);
                    }
                } else if (isRealVisible) {
                    //The node is visible add the attribute to the list of output attributes.
                    result.add(attribute);
                }
            } else if (!XML.equals(NName) || !XML_LANG_URI.equals(NValue)) {
                /* except omit namespace node with local name xml, which defines
                 * the xml prefix, if its string value is 
                 * http://www.w3.org/XML/1998/namespace.
                 */
                // add the prefix binding to the ns symb table.
                if (isVisible(attribute)) {
                    if (isRealVisible || !ns.removeMappingIfRender(NName)) {
                        // The xpath select this node output it if needed.
                        Node n = ns.addMappingAndRender(NName, NValue, attribute);
                        if (n != null) {
                            result.add((Attr) n);
                            if (C14nHelper.namespaceIsRelative(attribute)) {
                                Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() };
                                throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace",
                                        exArgs);
                            }
                        }
                    }
                } else {
                    if (isRealVisible && !XMLNS.equals(NName)) {
                        ns.removeMapping(NName);
                    } else {
                        ns.addMapping(NName, NValue, attribute);
                    }
                }
            }
        }
    }

    if (isRealVisible) {
        //The element is visible, handle the xmlns definition        
        Attr xmlns = element.getAttributeNodeNS(XMLNS_URI, XMLNS);
        Node n = null;
        if (xmlns == null) {
            //No xmlns def just get the already defined.
            n = ns.getMapping(XMLNS);
        } else if (!isVisible(xmlns)) {
            //There is a definition but the xmlns is not selected by the xpath.
            //then xmlns=""
            n = ns.addMappingAndRender(XMLNS, "", nullNode);
        }
        //output the xmlns def if needed.
        if (n != null) {
            result.add((Attr) n);
        }
        //Float all xml:* attributes of the unselected parent elements to this one. 
        xmlattrStack.getXmlnsAttr(result);
        ns.getUnrenderedNodes(result);
    }

    return result.iterator();
}

From source file:org.apache.xml.security.c14n.implementations.Canonicalizer11.java

protected void handleParent(Element e, NameSpaceSymbTable ns) {
    if (!e.hasAttributes() && e.getNamespaceURI() == null) {
        return;// www.  ja v  a 2s  .  c o  m
    }
    xmlattrStack.push(-1);
    NamedNodeMap attrs = e.getAttributes();
    int attrsLength = attrs.getLength();
    for (int i = 0; i < attrsLength; i++) {
        Attr attribute = (Attr) attrs.item(i);
        String NName = attribute.getLocalName();
        String NValue = attribute.getNodeValue();

        if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())) {
            if (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) {
                ns.addMapping(NName, NValue, attribute);
            }
        } else if (!"id".equals(NName) && XML_LANG_URI.equals(attribute.getNamespaceURI())) {
            xmlattrStack.addXmlnsAttr(attribute);
        }
    }
    if (e.getNamespaceURI() != null) {
        String NName = e.getPrefix();
        String NValue = e.getNamespaceURI();
        String Name;
        if (NName == null || NName.equals("")) {
            NName = "xmlns";
            Name = "xmlns";
        } else {
            Name = "xmlns:" + NName;
        }
        Attr n = e.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", Name);
        n.setValue(NValue);
        ns.addMapping(NName, NValue, n);
    }
}

From source file:org.apache.xml.security.utils.IdResolver.java

public static int isElement(Element el, String id, Element[] els) {
    if (!el.hasAttributes()) {
        return 0;
    }//from w ww .j a  v a 2 s .co  m
    NamedNodeMap ns = el.getAttributes();
    int elementIndex = names.indexOf(el.getNamespaceURI());
    elementIndex = (elementIndex < 0) ? namesLength : elementIndex;
    for (int length = ns.getLength(), i = 0; i < length; i++) {
        Attr n = (Attr) ns.item(i);
        String s = n.getNamespaceURI();

        int index = s == null ? elementIndex : names.indexOf(n.getNamespaceURI());
        index = (index < 0) ? namesLength : index;
        String name = n.getLocalName();
        if (name == null) {
            name = n.getName();
        }
        if (name.length() > 2) {
            continue;
        }
        String value = n.getNodeValue();
        if (name.charAt(0) == 'I') {
            char ch = name.charAt(1);
            if (ch == 'd' && value.equals(id)) {
                els[index] = el;
                if (index == 0) {
                    return 1;
                }
            } else if (ch == 'D' && value.endsWith(id)) {
                if (index != 3) {
                    index = namesLength;
                }
                els[index] = el;
            }
        } else if ("id".equals(name) && value.equals(id)) {
            if (index != 2) {
                index = namesLength;
            }
            els[index] = el;
        }
    }
    //For an element namespace search for importants
    if ((elementIndex == 3) && (el.getAttribute("OriginalRequestID").equals(id)
            || el.getAttribute("RequestID").equals(id) || el.getAttribute("ResponseID").equals(id))) {
        els[3] = el;
    } else if ((elementIndex == 4) && (el.getAttribute("AssertionID").equals(id))) {
        els[4] = el;
    } else if ((elementIndex == 5)
            && (el.getAttribute("RequestID").equals(id) || el.getAttribute("ResponseID").equals(id))) {
        els[5] = el;
    }
    return 0;
}

From source file:org.apache.xml.security.utils.XMLUtils.java

/**
 * This is the work horse for {@link #circumventBug2650}.
 *
 * @param node/*from ww w.ja  v a2 s  .  c  o m*/
 * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
 * Namespace axis resolution is not XPath compliant </A>
 */
private static void circumventBug2650internal(Node node) {
    Node parent = null;
    Node sibling = null;
    final String namespaceNs = Constants.NamespaceSpecNS;
    do {
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            Element element = (Element) node;
            if (!element.hasChildNodes()) {
                break;
            }
            if (element.hasAttributes()) {
                NamedNodeMap attributes = element.getAttributes();
                int attributesLength = attributes.getLength();

                for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {

                    if (child.getNodeType() != Node.ELEMENT_NODE) {
                        continue;
                    }
                    Element childElement = (Element) child;

                    for (int i = 0; i < attributesLength; i++) {
                        Attr currentAttr = (Attr) attributes.item(i);
                        if (!namespaceNs.equals(currentAttr.getNamespaceURI()))
                            continue;
                        if (childElement.hasAttributeNS(namespaceNs, currentAttr.getLocalName())) {
                            continue;
                        }
                        childElement.setAttributeNS(namespaceNs, currentAttr.getName(),
                                currentAttr.getNodeValue());
                    }
                }
            }
        case Node.ENTITY_REFERENCE_NODE:
            parent = node;
            sibling = node.getFirstChild();
            break;
        case Node.DOCUMENT_NODE:
            parent = node;
            sibling = node.getFirstChild();
            break;
        }
        while ((sibling == null) && (parent != null)) {
            sibling = parent.getNextSibling();
            parent = parent.getParentNode();
        }
        if (sibling == null) {
            return;
        }

        node = sibling;
        sibling = node.getNextSibling();
    } while (true);
}

From source file:org.dita.dost.util.XMLUtils.java

/**
 * Add or set attribute. Convenience method for {@link #addOrSetAttribute(AttributesImpl, String, String, String, String, String)}.
 * //from  w ww  .jav  a  2 s  .  c o  m
 * @param atts attributes
 * @param att attribute node
 */
public static void addOrSetAttribute(final AttributesImpl atts, final Node att) {
    if (att.getNodeType() != Node.ATTRIBUTE_NODE) {
        throw new IllegalArgumentException();
    }
    final Attr a = (Attr) att;
    String localName = a.getLocalName();
    if (localName == null) {
        localName = a.getName();
        final int i = localName.indexOf(':');
        if (i != -1) {
            localName = localName.substring(i + 1);
        }
    }
    addOrSetAttribute(atts, a.getNamespaceURI() != null ? a.getNamespaceURI() : NULL_NS_URI, localName,
            a.getName() != null ? a.getName() : localName, a.isId() ? "ID" : "CDATA", a.getValue());
}

From source file:org.docx4j.XmlUtils.java

/**
 * Copy a node from one DOM document to another.  Used
 * to avoid relying on an underlying implementation which might 
 * not support importNode // www  . j  a  va  2 s .  c o  m
 * (eg Xalan's org.apache.xml.dtm.ref.DTMNodeProxy).
 * 
 * WARNING: doesn't fully support namespaces!
 * 
 * @param sourceNode
 * @param destParent
 */
public static void treeCopy(Node sourceNode, Node destParent) {

    // http://osdir.com/ml/text.xml.xerces-j.devel/2004-04/msg00066.html
    // suggests the problem has been fixed?

    // source node maybe org.apache.xml.dtm.ref.DTMNodeProxy
    // (if its xslt output we are copying)
    // or com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl
    // (if its marshalled JAXB)

    log.debug("node type" + sourceNode.getNodeType());

    switch (sourceNode.getNodeType()) {

    case Node.DOCUMENT_NODE: // type 9
    case Node.DOCUMENT_FRAGMENT_NODE: // type 11

        //              log.debug("DOCUMENT:" + w3CDomNodeToString(sourceNode) );
        //              if (sourceNode.getChildNodes().getLength()==0) {
        //                 log.debug("..no children!");
        //              }

        // recurse on each child
        NodeList nodes = sourceNode.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                log.debug("child " + i + "of DOCUMENT_NODE");
                //treeCopy((DTMNodeProxy)nodes.item(i), destParent);
                treeCopy((Node) nodes.item(i), destParent);
            }
        }
        break;
    case Node.ELEMENT_NODE:

        // Copy of the node itself
        log.debug("copying: " + sourceNode.getNodeName());
        Node newChild;
        if (destParent instanceof Document) {
            newChild = ((Document) destParent).createElementNS(sourceNode.getNamespaceURI(),
                    sourceNode.getLocalName());
        } else if (sourceNode.getNamespaceURI() != null) {
            newChild = destParent.getOwnerDocument().createElementNS(sourceNode.getNamespaceURI(),
                    sourceNode.getLocalName());
        } else {
            newChild = destParent.getOwnerDocument().createElement(sourceNode.getNodeName());
        }
        destParent.appendChild(newChild);

        // .. its attributes
        NamedNodeMap atts = sourceNode.getAttributes();
        for (int i = 0; i < atts.getLength(); i++) {

            Attr attr = (Attr) atts.item(i);

            //                  log.debug("attr.getNodeName(): " + attr.getNodeName());
            //                  log.debug("attr.getNamespaceURI(): " + attr.getNamespaceURI());
            //                  log.debug("attr.getLocalName(): " + attr.getLocalName());
            //                  log.debug("attr.getPrefix(): " + attr.getPrefix());

            if (attr.getNodeName().startsWith("xmlns:")) {
                /* A document created from a dom4j document using dom4j 1.6.1's io.domWriter
                 does this ?!
                 attr.getNodeName(): xmlns:w 
                 attr.getNamespaceURI(): null
                 attr.getLocalName(): null
                 attr.getPrefix(): null
                         
                 unless i'm doing something wrong, this is another reason to
                 remove use of dom4j from docx4j
                */
                ;
                // this is a namespace declaration. not our problem
            } else if (attr.getNamespaceURI() == null) {
                //log.debug("attr.getLocalName(): " + attr.getLocalName() + "=" + attr.getValue());
                ((org.w3c.dom.Element) newChild).setAttribute(attr.getName(), attr.getValue());
            } else if (attr.getNamespaceURI().equals("http://www.w3.org/2000/xmlns/")) {
                ; // this is a namespace declaration. not our problem
            } else if (attr.getNodeName() != null) {
                // && attr.getNodeName().equals("xml:space")) {
                // restrict this fix to xml:space only, if necessary

                // Necessary when invoked from BindingTraverserXSLT,
                // com.sun.org.apache.xerces.internal.dom.AttrNSImpl
                // otherwise it was becoming w:space="preserve"!

                /* eg xml:space
                 * 
                   attr.getNodeName(): xml:space
                   attr.getNamespaceURI(): http://www.w3.org/XML/1998/namespace
                   attr.getLocalName(): space
                   attr.getPrefix(): xml
                 */

                ((org.w3c.dom.Element) newChild).setAttributeNS(attr.getNamespaceURI(), attr.getNodeName(),
                        attr.getValue());
            } else {
                ((org.w3c.dom.Element) newChild).setAttributeNS(attr.getNamespaceURI(), attr.getLocalName(),
                        attr.getValue());
            }
        }

        // recurse on each child
        NodeList children = sourceNode.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                //treeCopy( (DTMNodeProxy)children.item(i), newChild);
                treeCopy((Node) children.item(i), newChild);
            }
        }

        break;

    case Node.TEXT_NODE:

        // Where destParent is com.sun.org.apache.xerces.internal.dom.DocumentImpl,
        // destParent.getOwnerDocument() returns null.
        // #document ; com.sun.org.apache.xerces.internal.dom.DocumentImpl

        //               System.out.println(sourceNode.getNodeValue());

        //System.out.println(destParent.getNodeName() + " ; " + destParent.getClass().getName() );
        if (destParent.getOwnerDocument() == null && destParent.getNodeName().equals("#document")) {
            Node textNode = ((Document) destParent).createTextNode(sourceNode.getNodeValue());
            destParent.appendChild(textNode);
        } else {
            Node textNode = destParent.getOwnerDocument().createTextNode(sourceNode.getNodeValue());
            // Warning: If you attempt to write a single "&" character, it will be converted to &amp; 
            // even if it doesn't look like that with getNodeValue() or getTextContent()!
            // So avoid any tricks with entities! See notes in docx2xhtmlNG2.xslt
            Node appended = destParent.appendChild(textNode);

        }
        break;

    //                case Node.CDATA_SECTION_NODE:
    //                    writer.write("<![CDATA[" +
    //                                 node.getNodeValue() + "]]>");
    //                    break;
    //
    //                case Node.COMMENT_NODE:
    //                    writer.write(indentLevel + "<!-- " +
    //                                 node.getNodeValue() + " -->");
    //                    writer.write(lineSeparator);
    //                    break;
    //
    //                case Node.PROCESSING_INSTRUCTION_NODE:
    //                    writer.write("<?" + node.getNodeName() +
    //                                 " " + node.getNodeValue() +
    //                                 "?>");
    //                    writer.write(lineSeparator);
    //                    break;
    //
    //                case Node.ENTITY_REFERENCE_NODE:
    //                    writer.write("&" + node.getNodeName() + ";");
    //                    break;
    //
    //                case Node.DOCUMENT_TYPE_NODE:
    //                    DocumentType docType = (DocumentType)node;
    //                    writer.write("<!DOCTYPE " + docType.getName());
    //                    if (docType.getPublicId() != null)  {
    //                        System.out.print(" PUBLIC \"" +
    //                            docType.getPublicId() + "\" ");
    //                    } else {
    //                        writer.write(" SYSTEM ");
    //                    }
    //                    writer.write("\"" + docType.getSystemId() + "\">");
    //                    writer.write(lineSeparator);
    //                    break;
    }
}

From source file:org.exist.dom.ElementImpl.java

private Node appendChild(Txn transaction, NodeId newNodeId, NodeImplRef last, NodePath lastPath, Node child,
        StreamListener listener) throws DOMException {
    if (last == null || last.getNode() == null)
    //TODO : same test as above ? -pb
    {/*from  w w w. j av  a  2  s.  com*/
        throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, "invalid node");
    }
    final DocumentImpl owner = (DocumentImpl) getOwnerDocument();
    DBBroker broker = null;
    try {
        broker = ownerDocument.getBrokerPool().get(null);
        switch (child.getNodeType()) {
        case Node.DOCUMENT_FRAGMENT_NODE:
            appendChildren(transaction, newNodeId, null, last, lastPath, child.getChildNodes(), listener);
            return null; // TODO: implement document fragments so
        //we can return all newly appended children
        case Node.ELEMENT_NODE:
            // create new element
            final ElementImpl elem = new ElementImpl(
                    new QName(child.getLocalName() == null ? child.getNodeName() : child.getLocalName(),
                            child.getNamespaceURI(), child.getPrefix()),
                    broker.getBrokerPool().getSymbols());
            elem.setNodeId(newNodeId);
            elem.setOwnerDocument(owner);
            final NodeListImpl ch = new NodeListImpl();
            final NamedNodeMap attribs = child.getAttributes();
            int numActualAttribs = 0;
            for (int i = 0; i < attribs.getLength(); i++) {
                final Attr attr = (Attr) attribs.item(i);
                if (!attr.getNodeName().startsWith("xmlns")) {
                    ch.add(attr);
                    numActualAttribs++;
                } else {
                    final String xmlnsDecl = attr.getNodeName();
                    final String prefix = xmlnsDecl.length() == 5 ? "" : xmlnsDecl.substring(6);
                    elem.addNamespaceMapping(prefix, attr.getNodeValue());
                }
            }
            final NodeList cl = child.getChildNodes();
            for (int i = 0; i < cl.getLength(); i++) {
                final Node n = cl.item(i);
                if (n.getNodeType() != Node.ATTRIBUTE_NODE) {
                    ch.add(n);
                }
            }
            elem.setChildCount(ch.getLength());
            if (numActualAttribs != (short) numActualAttribs) {
                throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, "Too many attributes");
            }
            elem.setAttributes((short) numActualAttribs);
            lastPath.addComponent(elem.getQName());
            // insert the node
            broker.insertNodeAfter(transaction, last.getNode(), elem);
            broker.indexNode(transaction, elem, lastPath);
            broker.getIndexController().indexNode(transaction, elem, lastPath, listener);
            elem.setChildCount(0);
            last.setNode(elem);
            //process child nodes
            elem.appendChildren(transaction, newNodeId.newChild(), null, last, lastPath, ch, listener);
            broker.endElement(elem, lastPath, null);
            broker.getIndexController().endElement(transaction, elem, lastPath, listener);
            lastPath.removeLastComponent();
            return elem;
        case Node.TEXT_NODE:
            final TextImpl text = new TextImpl(newNodeId, ((Text) child).getData());
            text.setOwnerDocument(owner);
            // insert the node
            broker.insertNodeAfter(transaction, last.getNode(), text);
            broker.indexNode(transaction, text, lastPath);
            broker.getIndexController().indexNode(transaction, text, lastPath, listener);
            last.setNode(text);
            return text;
        case Node.CDATA_SECTION_NODE:
            final CDATASectionImpl cdata = new CDATASectionImpl(newNodeId, ((CDATASection) child).getData());
            cdata.setOwnerDocument(owner);
            // insert the node
            broker.insertNodeAfter(transaction, last.getNode(), cdata);
            broker.indexNode(transaction, cdata, lastPath);
            last.setNode(cdata);
            return cdata;
        case Node.ATTRIBUTE_NODE:
            final Attr attr = (Attr) child;
            final String ns = attr.getNamespaceURI();
            final String prefix = (Namespaces.XML_NS.equals(ns) ? "xml" : attr.getPrefix());
            String name = attr.getLocalName();
            if (name == null) {
                name = attr.getName();
            }
            final QName attrName = new QName(name, ns, prefix);
            final AttrImpl attrib = new AttrImpl(attrName, attr.getValue(),
                    broker.getBrokerPool().getSymbols());
            attrib.setNodeId(newNodeId);
            attrib.setOwnerDocument(owner);
            if (ns != null && attrName.compareTo(Namespaces.XML_ID_QNAME) == Constants.EQUAL) {
                // an xml:id attribute. Normalize the attribute and set its type to ID
                attrib.setValue(StringValue.trimWhitespace(StringValue.collapseWhitespace(attrib.getValue())));
                attrib.setType(AttrImpl.ID);
            } else {
                attrName.setNameType(ElementValue.ATTRIBUTE);
            }
            broker.insertNodeAfter(transaction, last.getNode(), attrib);
            broker.indexNode(transaction, attrib, lastPath);
            broker.getIndexController().indexNode(transaction, attrib, lastPath, listener);
            last.setNode(attrib);
            return attrib;
        case Node.COMMENT_NODE:
            final CommentImpl comment = new CommentImpl(((Comment) child).getData());
            comment.setNodeId(newNodeId);
            comment.setOwnerDocument(owner);
            // insert the node
            broker.insertNodeAfter(transaction, last.getNode(), comment);
            broker.indexNode(transaction, comment, lastPath);
            last.setNode(comment);
            return comment;
        case Node.PROCESSING_INSTRUCTION_NODE:
            final ProcessingInstructionImpl pi = new ProcessingInstructionImpl(newNodeId,
                    ((ProcessingInstruction) child).getTarget(), ((ProcessingInstruction) child).getData());
            pi.setOwnerDocument(owner);
            //insert the node
            broker.insertNodeAfter(transaction, last.getNode(), pi);
            broker.indexNode(transaction, pi, lastPath);
            last.setNode(pi);
            return pi;
        default:
            throw new DOMException(DOMException.INVALID_MODIFICATION_ERR,
                    "Unknown node type: " + child.getNodeType() + " " + child.getNodeName());
        }
    } catch (final EXistException e) {
        LOG.warn("Exception while appending node: " + e.getMessage(), e);
    } finally {
        if (broker != null)
            broker.release();
    }
    return null;
}

From source file:org.firesoa.common.schema.DOMInitializer.java

private static String getQualifiedName(Document doc, QName qName) {
    Element rootElement = (Element) doc.getDocumentElement();

    if (rootElement != null && !equalStrings(qName.getNamespaceURI(), rootElement.getNamespaceURI())) {
        //         Attr attrTmp = rootElement.getAttributeNodeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, "ns0");
        //         System.out.println("===========found attrTmp=="+attrTmp);

        String nsPrefix = null;/*from w w w  .  j av  a  2  s  . c o m*/
        nsPrefix = rootElement.lookupPrefix(qName.getNamespaceURI());
        if (nsPrefix == null || nsPrefix.trim().equals("")) {
            int nsNumber = 1;
            NamedNodeMap attrMap = rootElement.getAttributes();
            int length = attrMap.getLength();

            for (int i = 0; i < length; i++) {
                Attr attr = (Attr) attrMap.item(i);
                String name = attr.getName();
                if (name.startsWith(Constants.XMLNS_ATTRIBUTE)) {
                    if (attr.getValue().equals(qName.getNamespaceURI())) {
                        // Namespace?
                        nsPrefix = attr.getLocalName();
                        break;
                    }
                    nsNumber++;
                }
            }
            if (nsPrefix == null) {
                nsPrefix = "ns" + nsNumber;
            }
        }

        Attr attr = doc.createAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI,
                Constants.XMLNS_ATTRIBUTE + ":" + nsPrefix);
        attr.setValue(qName.getNamespaceURI());
        rootElement.setAttributeNode(attr);

        return nsPrefix + ":" + qName.getLocalPart();
    } else {
        return "ns0:" + qName.getLocalPart();
    }

}

From source file:org.getobjects.appserver.templates.WOxElemBuilder.java

public Map<String, WOAssociation> associationsForAttributes(NamedNodeMap _m) {
    if (_m == null)
        return null;

    int len = _m.getLength();
    Map<String, WOAssociation> assocs = new HashMap<String, WOAssociation>(len);

    for (int i = 0; i < len; i++) {
        WOAssociation assoc;/*from  ww  w. ja  va  2  s . co  m*/
        Attr attr;

        attr = (Attr) (_m.item(i));
        if ((assoc = this.associationForAttribute(attr)) != null) {
            String n;

            if ((n = attr.getLocalName()) == null)
                n = attr.getName();
            assocs.put(n, assoc);
        }
    }

    return assocs;
}