Example usage for org.w3c.dom DocumentFragment getChildNodes

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

Introduction

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

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:org.apache.nutch.parse.html.HtmlParser.java

private DocumentFragment parseNeko(InputSource input) throws Exception {
    DOMFragmentParser parser = new DOMFragmentParser();
    try {// www . j  a  v  a2s. co  m
        parser.setFeature("http://cyberneko.org/html/features/augmentations", true);
        parser.setProperty("http://cyberneko.org/html/properties/default-encoding", defaultCharEncoding);
        parser.setFeature("http://cyberneko.org/html/features/scanner/ignore-specified-charset", true);
        parser.setFeature("http://cyberneko.org/html/features/balance-tags/ignore-outside-content", false);
        parser.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true);
        parser.setFeature("http://cyberneko.org/html/features/report-errors", LOG.isTraceEnabled());
    } catch (SAXException e) {
    }
    // convert Document to DocumentFragment
    HTMLDocumentImpl doc = new HTMLDocumentImpl();
    doc.setErrorChecking(false);
    DocumentFragment res = doc.createDocumentFragment();
    DocumentFragment frag = doc.createDocumentFragment();
    parser.parse(input, frag);
    res.appendChild(frag);

    try {
        while (true) {
            frag = doc.createDocumentFragment();
            parser.parse(input, frag);
            if (!frag.hasChildNodes())
                break;
            if (LOG.isInfoEnabled()) {
                LOG.info(" - new frag, " + frag.getChildNodes().getLength() + " nodes.");
            }
            res.appendChild(frag);
        }
    } catch (Exception x) {
        x.printStackTrace(LogUtil.getWarnStream(LOG));
    }
    ;
    return res;
}

From source file:org.apache.nutch.store.readable.StoreReadable.java

private DocumentFragment parseNeko(InputSource input) throws Exception {
    System.out.println("[STORE-READABLE]----------------------------------------------------parseNeko");
    DOMFragmentParser parser = new DOMFragmentParser();
    try {// ww  w  .j  a v  a 2 s .  co  m
        parser.setFeature("http://cyberneko.org/html/features/scanner/allow-selfclosing-iframe", true);
        parser.setFeature("http://cyberneko.org/html/features/augmentations", true);
        parser.setProperty("http://cyberneko.org/html/properties/default-encoding", defaultCharEncoding);
        parser.setFeature("http://cyberneko.org/html/features/scanner/ignore-specified-charset", true);
        parser.setFeature("http://cyberneko.org/html/features/balance-tags/ignore-outside-content", false);
        parser.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true);
        parser.setFeature("http://cyberneko.org/html/features/report-errors", LOG.isTraceEnabled());
    } catch (SAXException e) {
    }
    // convert Document to DocumentFragment
    HTMLDocumentImpl doc = new HTMLDocumentImpl();
    doc.setErrorChecking(false);
    DocumentFragment res = doc.createDocumentFragment();
    DocumentFragment frag = doc.createDocumentFragment();
    parser.parse(input, frag);
    res.appendChild(frag);

    try {
        while (true) {
            frag = doc.createDocumentFragment();
            parser.parse(input, frag);
            if (!frag.hasChildNodes())
                break;
            if (LOG.isInfoEnabled()) {
                LOG.info(" - new frag, " + frag.getChildNodes().getLength() + " nodes.");
            }
            res.appendChild(frag);
        }
    } catch (Exception x) {
        LOG.error("Failed with the following Exception: ", x);
    }
    ;
    return res;
}

From source file:org.apache.servicemix.jbi.runtime.impl.AbstractComponentContext.java

/**
 * <p>/*from w ww . jav  a2  s  .  c o m*/
 * Resolve an internal JBI EPR conforming to the format defined in the JBI specification.
 * </p>
 *
 * <p>The EPR would look like:
 * <pre>
 * <jbi:end-point-reference xmlns:jbi="http://java.sun.com/xml/ns/jbi/end-point-reference"
 *      jbi:end-point-name="endpointName"
 *      jbi:service-name="foo:serviceName"
 *      xmlns:foo="urn:FooNamespace"/>
 * </pre>
 * </p>
 *
 * @author Maciej Szefler m s z e f l e r @ g m a i l . c o m
 * @param epr EPR fragment
 * @return internal service endpoint corresponding to the EPR, or <code>null</code>
 *         if the EPR is not an internal EPR or if the EPR cannot be resolved
 */
public ServiceEndpoint resolveInternalEPR(DocumentFragment epr) {
    if (epr == null) {
        throw new NullPointerException("resolveInternalEPR(epr) called with null epr.");
    }
    NodeList nl = epr.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i) {
        Node n = nl.item(i);
        if (n.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        Element el = (Element) n;
        // Namespace should be "http://java.sun.com/jbi/end-point-reference"
        if (el.getNamespaceURI() == null || !el.getNamespaceURI().equals(ServiceEndpointImpl.JBI_NAMESPACE)) {
            continue;
        }
        if (el.getLocalName() == null
                || !el.getLocalName().equals(ServiceEndpointImpl.JBI_ENDPOINT_REFERENCE)) {
            continue;
        }
        String serviceName = el.getAttributeNS(el.getNamespaceURI(), ServiceEndpointImpl.JBI_SERVICE_NAME);
        // Now the DOM pain-in-the-you-know-what: we need to come up with QName for this;
        // fortunately, there is only one place where the xmlns:xxx attribute could be, on
        // the end-point-reference element!
        QName serviceQName = DOMUtil.createQName(el, serviceName);
        String endpointName = el.getAttributeNS(el.getNamespaceURI(), ServiceEndpointImpl.JBI_ENDPOINT_NAME);
        return getEndpoint(serviceQName, endpointName);
    }
    return null;
}

From source file:org.apache.servicemix.jbi.runtime.impl.AbstractComponentContext.java

/**
 * Resolve a standard EPR understood by ServiceMix container.
 * Currently, the supported syntax is the WSA one, the address uri
 * being parsed with the following possiblities:
 *    jbi:endpoint:service-namespace/service-name/endpoint
 *    jbi:endpoint:service-namespace:service-name:endpoint
 *
 * The full EPR will look like://from   www.  ja v a  2s.  c o m
 *   <epr xmlns:wsa="http://www.w3.org/2005/08/addressing">
 *     <wsa:Address>jbi:endpoint:http://foo.bar.com/service/endpoint</wsa:Address>
 *   </epr>
 *
 * BCs should also be able to resolve such EPR but using their own URI parsing,
 * for example:
 *   <epr xmlns:wsa="http://www.w3.org/2005/08/addressing">
 *     <wsa:Address>http://foo.bar.com/myService?http.soap=true</wsa:Address>
 *   </epr>
 *
 * or
 *   <epr xmlns:wsa="http://www.w3.org/2005/08/addressing">
 *     <wsa:Address>jms://activemq/queue/FOO.BAR?persistent=true</wsa:Address>
 *   </epr>
 *
 * Note that the separator should be same as the one used in the namespace
 * depending on the namespace:
 *     http://foo.bar.com  => '/'
 *     urn:foo:bar         => ':'
 *
 * The syntax is the same as the one that can be used to specifiy a target
 * for a JBI exchange with the restriction that it only allows the
 * endpoint subprotocol to be used.
 *
 * @param epr the xml fragment to resolve
 * @return the resolved endpoint or <code>null</code>
 */
public ServiceEndpoint resolveStandardEPR(DocumentFragment epr) {
    try {
        NodeList children = epr.getChildNodes();
        for (int i = 0; i < children.getLength(); ++i) {
            Node n = children.item(i);
            if (n.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            Element elem = (Element) n;
            String[] namespaces = new String[] { WSAddressingConstants.WSA_NAMESPACE_200508,
                    WSAddressingConstants.WSA_NAMESPACE_200408, WSAddressingConstants.WSA_NAMESPACE_200403,
                    WSAddressingConstants.WSA_NAMESPACE_200303 };
            NodeList nl = null;
            for (String ns : namespaces) {
                NodeList tnl = elem.getElementsByTagNameNS(ns, WSAddressingConstants.EL_ADDRESS);
                if (tnl.getLength() == 1) {
                    nl = tnl;
                    break;
                }
            }
            if (nl != null) {
                Element address = (Element) nl.item(0);
                String uri = DOMUtil.getElementText(address);
                if (uri != null) {
                    uri = uri.trim();
                    if (uri.startsWith("endpoint:")) {
                        uri = uri.substring("endpoint:".length());
                        String[] parts = URIResolver.split3(uri);
                        return getEndpoint(new QName(parts[0], parts[1]), parts[2]);
                    } else if (uri.startsWith("service:")) {
                        uri = uri.substring("service:".length());
                        String[] parts = URIResolver.split2(uri);
                        return getEndpoint(new QName(parts[0], parts[1]), parts[1]);
                    }
                }
                // TODO should we support interface: and operation: here?
            }
        }
    } catch (Exception e) {
        LOG.debug("Unable to resolve EPR: " + e);
    }
    return null;
}

From source file:org.dita.dost.writer.ConrefPushParser.java

/**
 * The function is to judge if the pushed content type march the type of content being pushed/replaced
 * @param targetClassAttribute the class attribute of target element which is being pushed
 * @param content pushedContent//from w  w  w . j a v  a2 s. co  m
 * @return boolean: if type match, return true, else return false
 */
private boolean isPushedTypeMatch(final DitaClass targetClassAttribute, final DocumentFragment content) {
    DitaClass clazz = null;

    if (content.hasChildNodes()) {
        final NodeList nodeList = content.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                final Element elem = (Element) node;
                clazz = new DitaClass(elem.getAttribute(ATTRIBUTE_NAME_CLASS));
                break;
                // get type of the target element
            }
        }
    }

    return targetClassAttribute.matches(clazz);
}

From source file:org.dita.dost.writer.ConrefPushParser.java

/**
 * //w  w  w .ja  v a2s . c  o m
 * @param targetClassAttribute targetClassAttribute
 * @param content string
 * @return string
 */
private DocumentFragment replaceElementName(final DitaClass targetClassAttribute,
        final DocumentFragment content) {
    try {
        if (content.hasChildNodes()) {
            final NodeList nodeList = content.getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++) {
                final Node node = nodeList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    final Element elem = (Element) node;
                    final DitaClass clazz = DitaClass.getInstance(elem);
                    // get type of the target element
                    final String type = targetClassAttribute.toString()
                            .substring(1, targetClassAttribute.toString().indexOf("/")).trim();
                    if (!clazz.equals(targetClassAttribute) && targetClassAttribute.matches(clazz)) {
                        // Specializing the pushing content is not handled here
                        // but we can catch such a situation to emit a warning by comparing the class values.
                        final String targetElementName = targetClassAttribute.toString()
                                .substring(targetClassAttribute.toString().indexOf("/") + 1).trim();
                        if (elem.getAttributeNode(ATTRIBUTE_NAME_CONREF) != null) {
                            hasConref = true;
                        }
                        if (elem.getAttributeNode(ATTRIBUTE_NAME_KEYREF) != null) {
                            hasKeyref = true;
                        }
                        elem.getOwnerDocument().renameNode(elem, elem.getNamespaceURI(), targetElementName);
                        // process the child nodes of the current node
                        final NodeList nList = elem.getChildNodes();
                        for (int j = 0; j < nList.getLength(); j++) {
                            final Node subNode = nList.item(j);
                            if (subNode.getNodeType() == Node.ELEMENT_NODE) {
                                //replace the subElement Name
                                replaceSubElementName(type, (Element) subNode);
                            }
                        }
                    } else {
                        replaceSubElementName(STRING_BLANK, elem);
                    }
                }
            }
        }
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return content;
}

From source file:org.jasig.resourceserver.utils.aggr.ResourcesElementsProviderImpl.java

@Override
public NodeList getResourcesXmlFragment(HttpServletRequest request, String skinXml) {
    final DocumentFragment headFragment = getResourcesXml(request, skinXml);

    return headFragment.getChildNodes();
}