Example usage for org.w3c.dom DocumentFragment getFirstChild

List of usage examples for org.w3c.dom DocumentFragment getFirstChild

Introduction

In this page you can find the example usage for org.w3c.dom DocumentFragment getFirstChild.

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:Main.java

static public String getFragmentText(DocumentFragment elm) {
    Node node = elm.getFirstChild();
    if (node != null && node.getNodeType() == Node.TEXT_NODE)
        return node.getNodeValue();

    return null;/*from   w w w .java2s .  c  om*/
}

From source file:ee.ria.xroad.proxy.util.MetaserviceTestUtil.java

/** Merge xroad-specific {@link SoapHeader} to the generic {@link SOAPHeader}
 * @param header// ww w .  j  ava  2 s.  co  m
 * @param xrHeader
 * @throws JAXBException
 * @throws ParserConfigurationException
 * @throws SOAPException
 */
public static void mergeHeaders(SOAPHeader header, SoapHeader xrHeader)
        throws JAXBException, ParserConfigurationException, SOAPException {

    Document document = documentBuilderFactory.newDocumentBuilder().newDocument();
    final DocumentFragment documentFragment = document.createDocumentFragment();
    // marshalling on the header would add the xroad header as a child of the header
    // (i.e. two nested header elements)
    marshaller.marshal(xrHeader, documentFragment);

    Document headerDocument = header.getOwnerDocument();
    Node xrHeaderElement = documentFragment.getFirstChild();

    assertTrue("test setup failed: assumed had header element but did not",
            xrHeaderElement.getNodeType() == Node.ELEMENT_NODE
                    && xrHeaderElement.getLocalName().equals("Header"));

    final NamedNodeMap attributes = xrHeaderElement.getAttributes();

    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            final Attr attribute = (Attr) attributes.item(i);
            header.setAttributeNodeNS((Attr) headerDocument.importNode(attribute, false));
        }
    }

    final NodeList childNodes = xrHeaderElement.getChildNodes();

    if (childNodes != null) {
        for (int i = 0; i < childNodes.getLength(); i++) {
            final Node node = childNodes.item(i);
            header.appendChild(headerDocument.importNode(node, true));
        }
    }

}

From source file:com.netspective.sparx.util.xml.XmlSource.java

public void inheritElement(Element srcElement, Element destElem, Set excludeElems, String inheritedFromNode) {
    NamedNodeMap inhAttrs = srcElement.getAttributes();
    for (int i = 0; i < inhAttrs.getLength(); i++) {
        Node attrNode = inhAttrs.item(i);
        final String nodeName = attrNode.getNodeName();
        if (!excludeElems.contains(nodeName) && destElem.getAttribute(nodeName).equals(""))
            destElem.setAttribute(nodeName, attrNode.getNodeValue());
    }/*from   w  w w . j  av  a2  s . com*/

    DocumentFragment inheritFragment = xmlDoc.createDocumentFragment();
    NodeList inhChildren = srcElement.getChildNodes();
    for (int i = inhChildren.getLength() - 1; i >= 0; i--) {
        Node childNode = inhChildren.item(i);

        // only add if there isn't an attribute overriding this element
        final String nodeName = childNode.getNodeName();
        if (destElem.getAttribute(nodeName).length() == 0 && (!excludeElems.contains(nodeName))) {
            Node cloned = childNode.cloneNode(true);
            if (inheritedFromNode != null && cloned.getNodeType() == Node.ELEMENT_NODE)
                ((Element) cloned).setAttribute("_inherited-from", inheritedFromNode);
            inheritFragment.insertBefore(cloned, inheritFragment.getFirstChild());
        }
    }

    destElem.insertBefore(inheritFragment, destElem.getFirstChild());
}

From source file:org.eclipse.swordfish.internal.core.integration.nmr.SwordfishExchangeListener.java

private void processOutgoingRequestHeaders(Exchange exchange) {
    EndpointMetadata<?> metadata = (EndpointMetadata<?>) exchange
            .getProperty(EndpointMetadata.ENDPOINT_METADATA);
    if (metadata == null) {
        return;/*from w ww  .j  a va 2s.  com*/
    }
    DocumentFragment policy = metadata.toXml();

    // create wsa:ReplyTo SOAP header containing traded policy
    Document doc = XmlUtil.getDocumentBuilder().newDocument();
    Element replyTo = doc.createElementNS(JbiConstants.WSA_NS, JbiConstants.WSA_REPLY_TO_NAME);
    replyTo.setPrefix(JbiConstants.WSA_PREFIX);

    Element addr = doc.createElementNS(JbiConstants.WSA_NS, JbiConstants.WSA_ADDRESS_NAME);
    addr.setPrefix(JbiConstants.WSA_PREFIX);
    addr.appendChild(doc.createTextNode(JbiConstants.WSA_ANONYMOUS));
    replyTo.appendChild(addr);

    Element refParams = doc.createElementNS(JbiConstants.WSA_NS, JbiConstants.WSA_REFERENCE_PARAMS_NAME);
    refParams.setPrefix(JbiConstants.WSA_PREFIX);
    Node policyNode = doc.importNode(policy, true);
    refParams.appendChild(policyNode);
    replyTo.appendChild(refParams);

    DocumentFragment replyToHeader = doc.createDocumentFragment();
    replyToHeader.appendChild(replyTo);

    Map<QName, DocumentFragment> headers = new HashMap<QName, DocumentFragment>();

    QName policyKey = new QName(policy.getFirstChild().getNamespaceURI(),
            policy.getFirstChild().getLocalName());
    headers.put(policyKey, policy);
    headers.put(JbiConstants.WSA_REPLY_TO_QNAME, replyToHeader);

    exchange.getIn(false).setHeader(JbiConstants.SOAP_HEADERS, headers);
}

From source file:org.eclipse.swordfish.internal.core.integration.nmr.SwordfishExchangeListener.java

@SuppressWarnings("unchecked")
private void processOutgoingResponseHeaders(Exchange exchange) {
    Message inMessage = exchange.getIn(false);
    Message outMessage = exchange.getOut(false);

    if (inMessage == null || outMessage == null) {
        LOG.debug("Skip processing of SOAP headers for outgoing response.");
        return;// w w w  . j a v a 2 s  . c  o  m
    }

    Map<QName, DocumentFragment> outHeaders = (Map<QName, DocumentFragment>) outMessage
            .getHeader(JbiConstants.SOAP_HEADERS);
    if (outHeaders == null) {
        outHeaders = new HashMap<QName, DocumentFragment>();
    }

    Map<QName, DocumentFragment> inHeaders = (Map<QName, DocumentFragment>) inMessage
            .getHeader(JbiConstants.SOAP_HEADERS);
    if (inHeaders != null && inHeaders.containsKey(JbiConstants.WSA_REPLY_TO_QNAME)) {
        // include all elements from wsa:ReferenceParameters
        // to SOAP headers of outgoing message
        DocumentFragment replyToFrag = inHeaders.get(JbiConstants.WSA_REPLY_TO_QNAME);
        Node refParams = XPathUtil.getElementByName(replyToFrag, JbiConstants.WSA_REFERENCE_PARAMS_QNAME);
        NodeList params = refParams.getChildNodes();

        for (int i = 0; i < params.getLength(); i++) {
            DocumentFragment fragment = XmlUtil.wrapWithDocumentFragment(params.item(i));
            Node fragmentNode = fragment.getFirstChild();
            QName fragmentName = new QName(fragmentNode.getNamespaceURI(), fragmentNode.getLocalName());
            outHeaders.put(fragmentName, fragment);
        }
    }
    outMessage.setHeader(JbiConstants.SOAP_HEADERS, outHeaders);
}

From source file:org.eclipse.swordfish.plugins.planner.policy.PolicyAssertionHintExtractor.java

@SuppressWarnings("unchecked")
private Policy getPolicy(MessageExchange messageExchange) {
    Policy policy = null;/*from  ww w.  j a v a2s  .  co  m*/
    try {
        NormalizedMessage message = getNormalizedMessage(messageExchange);

        Map<QName, Node> headers = (Map<QName, Node>) message.getProperty(JbiConstants.SOAP_HEADERS);
        if (headers == null) {
            return null;
        }

        DocumentFragment policyFragment = (DocumentFragment) headers.get(POLICY_HEADER);
        if (policyFragment == null) {
            return null;
        }

        Element policyElement = (Element) policyFragment.getFirstChild();
        if (!Constants.ELEM_POLICY.equals(policyElement.getLocalName())
                || !Constants.URI_POLICY_NS.equals(policyElement.getNamespaceURI())) {
            return null;
        }
        return policyExtractor.extractPolicy(policyElement);
    } catch (Exception ex) {
        LOG.warn(ex.getMessage(), ex);
    }
    return policy;
}

From source file:org.nuxeo.ecm.platform.forms.layout.descriptors.WidgetDescriptor.java

@XContent("selectOptions")
public void setSelectOptions(DocumentFragment selectOptionsDOM) {
    XMap xmap = new XMap();
    xmap.register(WidgetSelectOptionDescriptor.class);
    xmap.register(WidgetSelectOptionsDescriptor.class);
    Node p = selectOptionsDOM.getFirstChild();
    List<WidgetSelectOption> options = new ArrayList<WidgetSelectOption>();
    while (p != null) {
        if (p.getNodeType() == Node.ELEMENT_NODE) {
            Object desc = xmap.load((Element) p);
            if (desc instanceof WidgetSelectOptionDescriptor) {
                options.add(((WidgetSelectOptionDescriptor) desc).getWidgetSelectOption());
            } else if (desc instanceof WidgetSelectOptionsDescriptor) {
                options.add(((WidgetSelectOptionsDescriptor) desc).getWidgetSelectOption());
            } else {
                log.error("Unknown resolution of select option");
            }//from ww  w .  ja  va 2 s.co  m
        }
        p = p.getNextSibling();
    }
    selectOptions = options.toArray(new WidgetSelectOption[0]);
}

From source file:org.nuxeo.ecm.webengine.security.GuardDescriptor.java

@XContent
protected void setGuards(DocumentFragment content) {
    Node node = content.getFirstChild();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String name = node.getNodeName();
            if ("guard".equals(name)) {
                NamedNodeMap map = node.getAttributes();
                Node aId = map.getNamedItem("id");
                Node aType = map.getNamedItem("type");
                if (aId == null) {
                    throw new IllegalArgumentException("id is required");
                }//from  www  .  ja  v a  2 s  .  com
                String id = aId.getNodeValue();
                if (aType == null) {
                    throw new IllegalArgumentException("type is required");
                } else {
                    //String value = node.getTextContent().trim();
                    //guards.put(id, new ScriptGuard(value));
                    //TODO: compound guard
                }
                String type = aType.getNodeValue();
                if ("permission".equals(type)) {
                    String value = node.getTextContent().trim();
                    guards.put(id, new PermissionGuard(value));
                } else if ("isAdministrator".equals(type)) {
                    String value = node.getTextContent().trim();
                    guards.put(id, new IsAdministratorGuard(value));
                } else if ("facet".equals(type)) {
                    String value = node.getTextContent().trim();
                    guards.put(id, new FacetGuard(value));
                } else if ("type".equals(type)) {
                    String value = node.getTextContent().trim();
                    guards.put(id, new TypeGuard(value));
                } else if ("schema".equals(type)) {
                    String value = node.getTextContent().trim();
                    guards.put(id, new SchemaGuard(value));
                } else if ("user".equals(type)) {
                    String value = node.getTextContent().trim();
                    guards.put(id, new UserGuard(value));
                } else if ("group".equals(type)) {
                    String value = node.getTextContent().trim();
                    guards.put(id, new GroupGuard(value));
                } else if ("script".equals(type)) {
                    Node engineNode = map.getNamedItem("engine");
                    if (engineNode == null) {
                        throw new IllegalArgumentException("Must specify an engine attribute on script guards");
                    }
                    String value = node.getTextContent().trim();
                    guards.put(id, new ScriptGuard(engineNode.getNodeValue(), value));
                } else if ("expression".equals(type)) {
                    String value = node.getTextContent().trim();
                    try {
                        guards.put(id, PermissionService.getInstance().parse(value, guards));
                    } catch (ParseException e) {
                        log.error(e, e);
                    }
                } else { // the type should be a guard factory
                    String value = node.getTextContent().trim();
                    try {
                        Class<?> factory = Class.forName(type);
                        Guard guard = ((GuardFactory) factory.newInstance()).newGuard(value);
                        guards.put(id, guard);
                    } catch (Exception e) {
                        log.error(e, e); //TODO should throw a DeployException
                    }
                }
            }
        }
        node = node.getNextSibling();
    }
}

From source file:org.structr.web.entity.dom.DOMNode.java

@Override
public Node insertBefore(final Node newChild, final Node refChild) throws DOMException {

    // according to DOM spec, insertBefore with null refChild equals appendChild
    if (refChild == null) {

        return appendChild(newChild);
    }//from  ww  w  .j ava2  s. com

    checkWriteAccess();

    checkSameDocument(newChild);
    checkSameDocument(refChild);

    checkHierarchy(newChild);
    checkHierarchy(refChild);

    if (newChild instanceof DocumentFragment) {

        // When inserting document fragments, we must take
        // care of the special case that the nodes already
        // have a NEXT_LIST_ENTRY relationship coming from
        // the document fragment, so we must first remove
        // the node from the document fragment and then
        // add it to the new parent.
        final DocumentFragment fragment = (DocumentFragment) newChild;
        Node currentChild = fragment.getFirstChild();

        while (currentChild != null) {

            // save next child in fragment list for later use
            Node savedNextChild = currentChild.getNextSibling();

            // remove child from document fragment
            fragment.removeChild(currentChild);

            // insert child into new parent
            insertBefore(currentChild, refChild);

            // next
            currentChild = savedNextChild;
        }

    } else {

        final Node _parent = newChild.getParentNode();
        if (_parent != null) {

            _parent.removeChild(newChild);
        }

        try {

            // do actual tree insertion here
            treeInsertBefore((DOMNode) newChild, (DOMNode) refChild);

        } catch (FrameworkException frex) {

            if (frex.getStatus() == 404) {

                throw new DOMException(DOMException.NOT_FOUND_ERR, frex.getMessage());

            } else {

                throw new DOMException(DOMException.INVALID_STATE_ERR, frex.getMessage());
            }
        }

        // allow parent to set properties in new child
        handleNewChild(newChild);
    }

    return refChild;
}