Example usage for org.w3c.dom Element getParentNode

List of usage examples for org.w3c.dom Element getParentNode

Introduction

In this page you can find the example usage for org.w3c.dom Element getParentNode.

Prototype

public Node getParentNode();

Source Link

Document

The parent of this node.

Usage

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

/**
 * Add a repeat section if maxOccurs > 1.
 *///  w ww.  ja va 2 s  . c om
private Element addRepeatIfNecessary(final Document xformsDocument, final Element modelSection,
        final Element formSection, final XSTypeDefinition controlType, final String pathToRoot,
        final SchemaUtil.Occurrence o) {

    // add xforms:repeat section if this element re-occurs
    if ((o.isOptional()
            && (controlType instanceof XSSimpleTypeDefinition || "anyType".equals(controlType.getName())))
            || (o.maximum == 1 && o.minimum == 1)
            || (controlType instanceof XSComplexTypeDefinition && pathToRoot.equals(""))) {
        return formSection;
    }

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("[addRepeatIfNecessary] for multiple element for type " + controlType.getName()
                + ", maxOccurs = " + o.maximum);
    }

    final Element repeatSection = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":repeat");

    //bind instead of repeat
    //repeatSection.setAttributeNS(NamespaceConstants.XFORMS_NS,NamespaceConstants.XFORMS_PREFIX + ":nodeset",pathToRoot);
    // bind -> last element in the modelSection
    Element bind = DOMUtil.getLastChildElement(modelSection);

    // ALF-9524 fix, previously we've added extra bind element, so last child is not correct for repeatable switch
    String attribute = bind.getAttribute(NamespaceConstants.XFORMS_PREFIX + ":relevant");
    if (controlType instanceof XSComplexTypeDefinition
            && ((XSComplexTypeDefinition) controlType).getDerivationMethod() == XSConstants.DERIVATION_EXTENSION
            && attribute != null && !attribute.isEmpty()) {
        bind = modelSection;
    }

    String bindId = null;

    if (bind != null && bind.getLocalName() != null && "bind".equals(bind.getLocalName())) {
        bindId = bind.getAttributeNS(null, "id");
    } else {
        LOGGER.warn("[addRepeatIfNecessary] bind not found: " + bind + " (model selection name = "
                + modelSection.getNodeName() + ")");

        //if no bind is found -> modelSection is already a bind, get its parent last child
        bind = DOMUtil.getLastChildElement(modelSection.getParentNode());

        if (bind != null && bind.getLocalName() != null && "bind".equals(bind.getLocalName())) {
            bindId = bind.getAttributeNS(null, "id");
        } else {
            LOGGER.warn("[addRepeatIfNecessary] bind really not found");
        }
    }

    repeatSection.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":bind",
            bindId);
    this.setXFormsId(repeatSection);

    //appearance=full is more user friendly
    repeatSection.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":appearance",
            "full");

    formSection.appendChild(repeatSection);

    //add a group inside the repeat?
    final Element group = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":group");
    group.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":appearance",
            "repeated");
    this.setXFormsId(group);
    repeatSection.appendChild(group);
    return group;
}

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

private void createTriggersForRepeats(final Document xformsDocument, final Element rootGroup) {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("[createTriggersForRepeats] start");

    final HashMap<String, Element> bindIdToBind = new HashMap<String, Element>();
    final NodeList binds = xformsDocument.getElementsByTagNameNS(NamespaceConstants.XFORMS_NS, "bind");
    for (int i = 0; i < binds.getLength(); i++) {
        final Element b = (Element) binds.item(i);
        bindIdToBind.put(b.getAttributeNS(null, "id"), b);
    }/* w  ww.  java2  s .co  m*/

    final NodeList repeats = xformsDocument.getElementsByTagNameNS(NamespaceConstants.XFORMS_NS, "repeat");
    final HashMap<Element, Element> bindToRepeat = new HashMap<Element, Element>();
    for (int i = 0; i < repeats.getLength(); i++) {
        Element r = (Element) repeats.item(i);

        if (LOGGER.isDebugEnabled())
            LOGGER.debug("[createTriggersForRepeats] processing repeat " + r.getAttributeNS(null, "id"));

        Element bind = bindIdToBind.get(r.getAttributeNS(NamespaceConstants.XFORMS_NS, "bind"));
        bindToRepeat.put(bind, r);

        String xpath = "";

        do {
            if (xpath.length() != 0) {
                xpath = '/' + xpath;
            }

            if (LOGGER.isDebugEnabled())
                LOGGER.debug("[createTriggersForRepeats] walking bind " + bind.getAttributeNS(null, "id"));

            String s = bind.getAttributeNS(NamespaceConstants.XFORMS_NS, "nodeset");
            s = s.replaceAll("^([^\\[]+).*$", "$1");
            if (bindToRepeat.containsKey(bind) && !r.equals(bindToRepeat.get(bind))) {
                s += "[index(\'" + bindToRepeat.get(bind).getAttributeNS(null, "id") + "\')]";
            }
            xpath = s + xpath;
            bind = ((NamespaceConstants.XFORMS_PREFIX + ":bind").equals(bind.getParentNode().getNodeName())
                    ? (Element) bind.getParentNode()
                    : null);
        } while (bind != null);
        this.createTriggersForRepeat(xformsDocument, rootGroup, r.getAttributeNS(null, "id"), xpath,
                r.getAttributeNS(NamespaceConstants.XFORMS_NS, "bind"));
    }
}

From source file:org.apache.cocoon.forms.util.DomHelper.java

public static Map getInheritedNSDeclarations(Element elm) {
    List ancestorsAndSelf = new LinkedList();
    Element current = elm;
    while (current != null) {
        ancestorsAndSelf.add(current);//  w  w w .j av  a 2  s  .c om
        Node parent = current.getParentNode();
        if (parent.getNodeType() == Node.ELEMENT_NODE)
            current = (Element) parent;
        else
            current = null;
    }

    Map nsDeclarations = null;
    ListIterator iter = ancestorsAndSelf.listIterator(ancestorsAndSelf.size());
    while (iter.hasPrevious()) {
        Element element = (Element) iter.previous();
        nsDeclarations = addLocalNSDeclarations(element, nsDeclarations);
    }

    return nsDeclarations;
}

From source file:org.apache.ode.bpel.rtrep.v1.ASSIGN.java

private Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) {
    Document doc = ptr.getOwnerDocument();
    Node parent = ptr.getParentNode();
    if (keepSrcElement) {
        Element replacement = (Element) doc.importNode(src, true);
        parent.replaceChild(replacement, ptr);
        return (lval == ptr) ? replacement : lval;
    }//  w  ww.j  av  a2  s . co  m

    Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getLocalName());
    NodeList nl = src.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i)
        replacement.appendChild(doc.importNode(nl.item(i), true));
    NamedNodeMap attrs = src.getAttributes();
    for (int i = 0; i < attrs.getLength(); ++i) {
        Attr attr = (Attr) attrs.item(i);
        if (!attr.getName().startsWith("xmlns")) {
            replacement.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true));
            // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually
            int colonIdx = attr.getValue().indexOf(":");
            if (colonIdx > 0) {
                String prefix = attr.getValue().substring(0, colonIdx);
                String attrValNs = src.lookupPrefix(prefix);
                if (attrValNs != null)
                    replacement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs);
            }
        }
    }
    parent.replaceChild(replacement, ptr);
    DOMUtils.copyNSContext(ptr, replacement);

    return (lval == ptr) ? replacement : lval;
}

From source file:org.apache.ode.bpel.rtrep.v2.ASSIGN.java

private Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) {
    Document doc = ptr.getOwnerDocument();
    Node parent = ptr.getParentNode();
    if (keepSrcElement) {
        Element replacement = (Element) doc.importNode(src, true);
        parent.replaceChild(replacement, ptr);
        return (lval == ptr) ? replacement : lval;
    }/*from   www  . j  a  va 2 s  .c o m*/

    Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getLocalName());
    if (ptr.getPrefix() != null) {
        replacement.setPrefix(ptr.getPrefix());
    }
    NodeList nl = src.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i)
        replacement.appendChild(doc.importNode(nl.item(i), true));
    NamedNodeMap attrs = src.getAttributes();
    for (int i = 0; i < attrs.getLength(); ++i) {
        Attr attr = (Attr) attrs.item(i);
        if (!attr.getName().startsWith("xmlns")) {
            replacement.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true));
            // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually
            int colonIdx = attr.getValue().indexOf(":");
            if (colonIdx > 0) {
                String prefix = attr.getValue().substring(0, colonIdx);
                String attrValNs = src.lookupPrefix(prefix);
                if (attrValNs != null)
                    replacement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs);
            }
        }
    }
    parent.replaceChild(replacement, ptr);
    DOMUtils.copyNSContext(ptr, replacement);

    return (lval == ptr) ? replacement : lval;
}

From source file:org.apache.ode.bpel.rtrep.v2.AssignHelper.java

/**
 * madars.vitolins _at gmail.com - 2009.04.17 - moved from ASSIGN here
 *//*from   w w  w. j a va  2s.  co  m*/
public Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) {
    Document doc = ptr.getOwnerDocument();
    Node parent = ptr.getParentNode();
    if (keepSrcElement) {
        Element replacement = (Element) doc.importNode(src, true);
        parent.replaceChild(replacement, ptr);
        return (lval == ptr) ? replacement : lval;
    }

    Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getLocalName());
    NodeList nl = src.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i)
        replacement.appendChild(doc.importNode(nl.item(i), true));
    NamedNodeMap attrs = src.getAttributes();
    for (int i = 0; i < attrs.getLength(); ++i) {
        Attr attr = (Attr) attrs.item(i);
        if (!attr.getName().startsWith("xmlns")) {
            replacement.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true));
            // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually
            int colonIdx = attr.getValue().indexOf(":");
            if (colonIdx > 0) {
                String prefix = attr.getValue().substring(0, colonIdx);
                String attrValNs = src.lookupPrefix(prefix);
                if (attrValNs != null)
                    replacement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs);
            }
        }
    }
    parent.replaceChild(replacement, ptr);
    DOMUtils.copyNSContext(ptr, replacement);

    return (lval == ptr) ? replacement : lval;
}

From source file:org.apache.ode.bpel.runtime.ASSIGN.java

private Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) {
    Document doc = ptr.getOwnerDocument();
    Node parent = ptr.getParentNode();
    if (keepSrcElement) {
        Element replacement = (Element) doc.importNode(src, true);
        parent.replaceChild(replacement, ptr);
        return (lval == ptr) ? replacement : lval;
    }//from ww w .  j  av  a 2 s  .co  m

    Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getTagName());
    NodeList nl = src.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i)
        replacement.appendChild(doc.importNode(nl.item(i), true));
    copyAttributes(doc, ptr, replacement);
    copyAttributes(doc, src, replacement);
    parent.replaceChild(replacement, ptr);
    DOMUtils.copyNSContext(ptr, replacement);

    return (lval == ptr) ? replacement : lval;
}

From source file:org.apache.ode.bpel.runtime.AssignHelper.java

private Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) {
    Document doc = ptr.getOwnerDocument();
    Node parent = ptr.getParentNode();
    if (keepSrcElement) {
        Element replacement = (Element) doc.importNode(src, true);
        parent.replaceChild(replacement, ptr);
        return (lval == ptr) ? replacement : lval;
    }//from w  ww. jav  a2s.c o  m

    Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getTagName());
    NodeList nl = src.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i)
        replacement.appendChild(doc.importNode(nl.item(i), true));
    NamedNodeMap attrs = src.getAttributes();
    for (int i = 0; i < attrs.getLength(); ++i) {
        Attr attr = (Attr) attrs.item(i);
        if (!attr.getName().startsWith("xmlns")) {
            replacement.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true));
            // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually
            int colonIdx = attr.getValue().indexOf(":");
            if (colonIdx > 0) {
                String prefix = attr.getValue().substring(0, colonIdx);
                String attrValNs = src.lookupPrefix(prefix);
                if (attrValNs != null)
                    replacement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs);
            }
        }
    }
    parent.replaceChild(replacement, ptr);
    DOMUtils.copyNSContext(ptr, replacement);

    return (lval == ptr) ? replacement : lval;
}

From source file:org.apache.ode.utils.DOMUtils.java

/**
 * Given a prefix and a node, return the namespace URI that the prefix has
 * been associated with. This method is useful in resolving the namespace
 * URI of attribute values which are being interpreted as QNames. If prefix
 * is null, this method will return the default namespace.
 *
 * @param context the starting node (looks up recursively from here)
 * @param prefix the prefix to find an xmlns:prefix=uri for
 *
 * @return the namespace URI or null if not found
 *//*from  w ww.ja v a  2s .  c  om*/
public static String getNamespaceURIFromPrefix(Node context, String prefix) {
    short nodeType = context.getNodeType();
    Node tempNode = null;
    switch (nodeType) {
    case Node.ATTRIBUTE_NODE: {
        tempNode = ((Attr) context).getOwnerElement();
        break;
    }
    case Node.ELEMENT_NODE: {
        tempNode = context;
        break;
    }
    default: {
        tempNode = context.getParentNode();
        break;
    }
    }
    while ((tempNode != null) && (tempNode.getNodeType() == Node.ELEMENT_NODE)) {
        Element tempEl = (Element) tempNode;
        String namespaceURI = (prefix == null) ? getAttribute(tempEl, "xmlns")
                : getAttributeNS(tempEl, NS_URI_XMLNS, prefix);
        if (namespaceURI != null) {
            return namespaceURI;
        }
        tempNode = tempEl.getParentNode();
    }
    return null;
}

From source file:org.apache.ode.utils.DOMUtils.java

/**
 * This method traverses the DOM and grabs namespace declarations
 * on parent elements with the intent of preserving them for children.  <em>Note
 * that the DOM level 3 document method {@link Element#getAttribute(java.lang.String)}
 * is not desirable in this case, as it does not respect namespace prefix
 * bindings that may affect attribute values.  (Namespaces in DOM are
 * uncategorically a mess, especially in the context of XML Schema.)</em>
 * @param el the starting element/*from  www . j a  va  2s .c o  m*/
 * @return a {@link Map} containing prefix bindings.
 */
public static Map<String, String> getParentNamespaces(Element el) {
    HashMap<String, String> pref = new HashMap<String, String>();
    Map<String, String> mine = getMyNamespaces(el);
    Node n = el.getParentNode();
    while (n != null && n.getNodeType() != Node.DOCUMENT_NODE) {
        if (n instanceof Element) {
            Element l = (Element) n;
            NamedNodeMap nnm = l.getAttributes();
            int len = nnm.getLength();
            for (int i = 0; i < len; ++i) {
                Attr a = (Attr) nnm.item(i);
                if (isNSAttribute(a)) {
                    String key = getNSPrefixFromNSAttr(a);
                    String uri = a.getValue();
                    // prefer prefix bindings that are lower down in the tree.
                    if (pref.containsKey(key) || mine.containsKey(key))
                        continue;
                    pref.put(key, uri);
                }
            }
        }
        n = n.getParentNode();
    }
    return pref;
}