Example usage for org.w3c.dom Node getNextSibling

List of usage examples for org.w3c.dom Node getNextSibling

Introduction

In this page you can find the example usage for org.w3c.dom Node getNextSibling.

Prototype

public Node getNextSibling();

Source Link

Document

The node immediately following this node.

Usage

From source file:org.pentaho.di.trans.steps.datagrid.DataGridMeta.java

private void readData(Node stepnode) throws KettleXMLException {
    try {/*w  ww. java 2 s  .  c  o  m*/
        Node fields = XMLHandler.getSubNode(stepnode, "fields");
        int nrfields = XMLHandler.countNodes(fields, "field");

        allocate(nrfields);

        String slength, sprecision;

        for (int i = 0; i < nrfields; i++) {
            Node fnode = XMLHandler.getSubNodeByNr(fields, "field", i);

            fieldName[i] = XMLHandler.getTagValue(fnode, "name");
            fieldType[i] = XMLHandler.getTagValue(fnode, "type");
            fieldFormat[i] = XMLHandler.getTagValue(fnode, "format");
            currency[i] = XMLHandler.getTagValue(fnode, "currency");
            decimal[i] = XMLHandler.getTagValue(fnode, "decimal");
            group[i] = XMLHandler.getTagValue(fnode, "group");
            slength = XMLHandler.getTagValue(fnode, "length");
            sprecision = XMLHandler.getTagValue(fnode, "precision");

            fieldLength[i] = Const.toInt(slength, -1);
            fieldPrecision[i] = Const.toInt(sprecision, -1);
            String emptyString = XMLHandler.getTagValue(fnode, "set_empty_string");
            setEmptyString[i] = !Const.isEmpty(emptyString) && "Y".equalsIgnoreCase(emptyString);
        }

        Node datanode = XMLHandler.getSubNode(stepnode, "data");
        // NodeList childNodes = datanode.getChildNodes();
        dataLines = new ArrayList<List<String>>();

        Node lineNode = datanode.getFirstChild();
        while (lineNode != null) {
            if ("line".equals(lineNode.getNodeName())) {
                List<String> line = new ArrayList<String>();
                Node itemNode = lineNode.getFirstChild();
                while (itemNode != null) {
                    if ("item".equals(itemNode.getNodeName())) {
                        String itemNodeValue = XMLHandler.getNodeValue(itemNode);
                        line.add(itemNodeValue != null ? itemNodeValue : StringUtils.EMPTY);
                    }
                    itemNode = itemNode.getNextSibling();
                }
                /*
                 * for (int f=0;f<nrfields;f++) { Node itemNode = XMLHandler.getSubNodeByNr(lineNode, "item", f); String item
                 * = XMLHandler.getNodeValue(itemNode); line.add(item); }
                 */
                dataLines.add(line);

            }

            lineNode = lineNode.getNextSibling();
        }
    } catch (Exception e) {
        throw new KettleXMLException("Unable to load step info from XML", e);
    }
}

From source file:org.pentaho.di.www.CarteIT.java

public static List<Node> flatten(Node parent, List<Node> nodes) {
    Node child = parent.getFirstChild();
    if (nodes == null) {
        nodes = new ArrayList<Node>();
    }//  ww w  .jav a2s. c  o m
    nodes.add(parent);
    while (child != null) {
        flatten(child, nodes);
        child = child.getNextSibling();
    }
    return nodes;
}

From source file:org.pentaho.di.www.CarteIT.java

public static void print(Node node, String indent) {
    // System.out.println(indent + node.getClass().getName());
    if (node.getNodeType() == Node.TEXT_NODE && !StringUtils.isEmpty(node.getTextContent().trim())) {
        System.out.println(node.getParentNode().getNodeName());
        System.out.println(node.getNodeName() + node.getTextContent());
    }/*  www.j  a v  a 2  s.co m*/
    Node child = node.getFirstChild();
    while (child != null) {
        print(child, indent + " ");
        child = child.getNextSibling();
    }
}

From source file:org.primaresearch.web.gwt.server.UserServiceImpl.java

/**
 * Calls the integration web service and retrieves the relevant parameters for getting the document image, the
 * page content and the permissions.//from  w  w w  . j  a  va2  s . c om
 */
private WebServiceInfo getWebServiceInfo(String webServiceUrl, String userId, String attachmentId) {
    WebServiceInfo services = new WebServiceInfo();

    //Soap request
    try {
        SimpleSoapRequest request = new SimpleSoapRequest(webServiceUrl, "getDocumentAttachmentSources");
        request.addMethodParameter("Uid", userId);
        request.addMethodParameter("Aid", attachmentId);
        request.setDEBUG(DEBUG);

        String soapResponseContent = request.send();

        InputStream is = new ByteArrayInputStream(soapResponseContent.getBytes());

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(is);

        Node root = doc.getFirstChild();
        if (root != null) {
            Node node = root.getFirstChild();
            while (node != null) {
                if ("DocumentAttachmentSources".equals(node.getNodeName())) {

                    Node param = node.getFirstChild();
                    while (param != null) {
                        if ("ImageSource".equals(param.getNodeName()))
                            services.imageWebService = param.getTextContent();
                        else if ("AttachmentSource".equals(param.getNodeName()))
                            services.pageContentWebService = param.getTextContent();

                        param = param.getNextSibling();
                    }
                    break;
                }
                node = node.getNextSibling();
            }
        }

        is.close();

    } catch (Exception exc) {
        exc.printStackTrace();
    }

    return services;
}

From source file:org.primaresearch.web.gwt.server.UserServiceImpl.java

/**
 * Calls the permission web service and retrieves the user permissions.
 */// ww w  .java  2  s .  c om
private Permissions getPermissions(String webServiceUrl, String userId, String attachmentId) {

    ArrayList<String> permissionStrings = new ArrayList<String>();

    //Soap request
    try {
        SimpleSoapRequest request = new SimpleSoapRequest(webServiceUrl, "getDocumentAttachmentPermissions");
        request.addMethodParameter("Uid", userId);
        request.addMethodParameter("Aid", attachmentId);
        String soapResponseContent = request.send();

        InputStream is = new ByteArrayInputStream(soapResponseContent.getBytes());

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(is);

        Node root = doc.getFirstChild();
        if (root != null) {
            Node node = root.getFirstChild();
            while (node != null) {
                if ("DocumentAttachmentPermissions".equals(node.getNodeName())) {

                    Node permission = node.getFirstChild();
                    while (permission != null) {

                        if ("Permission".equals(permission.getNodeName())) {
                            NamedNodeMap attrs = node.getAttributes();
                            if (attrs != null && attrs.getNamedItem("name") != null) {
                                Node attr = attrs.getNamedItem("name");
                                permissionStrings.add(attr.getNodeValue());
                            }
                        }
                        permission = permission.getNextSibling();
                    }
                }
                node = node.getNextSibling();
            }
        }
        is.close();

    } catch (Exception exc) {
        exc.printStackTrace();
        return null;
    }

    Permissions ret = new Permissions();
    ret.init(permissionStrings.toArray(new String[permissionStrings.size()]));

    return ret;
}

From source file:org.rapidcontext.app.web.WebDavRequest.java

/**
 * Finds a child node by tag name./* w w  w  .  j  a v a2 s .co  m*/
 *
 * @param parent         the parent node
 * @param name           the child node name
 *
 * @return the child node found, or
 *         null if not found
 */
private Element parseChild(Element parent, String name) {
    if (parent != null) {
        Node child = parent.getFirstChild();
        while (child != null) {
            String localName = child.getLocalName();
            if (child instanceof Element && localName.equals(name)) {
                return (Element) child;
            }
            child = child.getNextSibling();
        }
    }
    return null;
}

From source file:org.rapidcontext.app.web.WebDavRequest.java

/**
 * Parses a property find request. All properties found will be
 * added to the query properties map. If no properties node was
 * provided, all standard properties will be added.
 *
 * @param node           the properties node, or null
 * @param isCollection   the collection flag
 *//*from w w  w  . java2 s.  c  om*/
private void parsePropFind(Element node, boolean isCollection) {
    LinkedHashMap defaults;
    String name;

    defaults = isCollection ? PROPS_COLLECTION : PROPS_FILE;
    if (node == null) {
        properties.putAll(defaults);
    } else {
        Node child = node.getFirstChild();
        while (child != null) {
            if (child.getNodeType() != Node.ELEMENT_NODE) {
                // Ignore non-elements
            } else if ("DAV:".equals(child.getNamespaceURI())) {
                if (defaults.containsKey(child.getLocalName())) {
                    properties.put(child.getLocalName(), "");
                } else {
                    properties.put(child.getLocalName(), null);
                }
            } else {
                name = child.getNamespaceURI() + ":" + child.getLocalName();
                properties.put(name, null);
            }
            child = child.getNextSibling();
        }
    }
}

From source file:org.sakaiproject.content.chh.dspace.ContentHostingHandlerImplDSpace.java

private DSpaceItemInfo queryDSpaceFor(String endpoint, String base, String needle) {
    try {//from www  .j  av a  2 s. co m
        /* PARSE XML RESPONSE... */
        // This is a really horrible tree-walk but it is, at least, NOT
        // vulnerable to the whitespace in the XML being reformatted.
        // After *much* debate, we decided that this is no worse than other
        // ways of doing this!
        Document d = getDSpaceProps(endpoint, base, 1);
        if (d == null)
            return null;

        // find multistatus node
        Node node_multistatus = null;
        NodeList nl = d.getChildNodes();
        for (int j = 0; j < nl.getLength(); ++j)
            if (nl.item(j).getNodeName() != null && nl.item(j).getNodeName().equals(LAYER1)) {
                node_multistatus = nl.item(j);
                break;
            }
        if (node_multistatus == null)
            return null;

        boolean foundMatch = false; // haven't found the named node so far
        DSpaceItemInfo dii = new DSpaceItemInfo(); // the return value
        // (unless we return
        // null)
        dii.displayname = needle; // will only be returned if we find the
        // name sought
        dii.endpoint = endpoint;

        // examine each response node, looking for ones which match the
        // string sought
        nl = node_multistatus.getChildNodes();
        for (int j = 0; j < nl.getLength(); ++j) {
            // only interested in nodes with name="response"
            if (nl.item(j).getNodeName() == null || !nl.item(j).getNodeName().equals(LAYER2))
                continue;
            Node node_response = nl.item(j);
            NodeList resources = node_response.getChildNodes();

            // grab the resource handle
            String handle = null;
            for (int k = 0; k < resources.getLength(); ++k)
                if (resources.item(k).getNodeName() != null && resources.item(k).getNodeName().equals(HREF)) {
                    handle = resources.item(k).getFirstChild().getNodeValue().trim();
                    break;
                }
            if (handle == null)
                continue; // skip this resource if it
            // doesn't have an 'href' node.

            // Communities and collections have a /-imploded handle
            // hierarchy.
            // The comm/coll's own handle is the last one.
            String[] handles = handle.split("/");
            handle = handles[handles.length - 1]; // only interested in
            // the last one
            if (!handle.startsWith("bitstream_")) // this only affects
                // reading bitstreams
                // when queryDSpaceFor
                // is called for a
                // resource
                dii.handle = handle.replace("%24", "/").substring(4);
            else {
                if (handles.length > 1)
                    dii.handle = handles[handles.length - 2].replace("%24", "/").substring(4);
                else
                    continue; // a handle containing only "bitstream_x" is
                // not valid
                try {
                    int indx;
                    String numberPart = handle.substring("bitstream_".length());
                    if ((indx = numberPart.indexOf(".")) != -1)
                        numberPart = numberPart.substring(0, indx);
                    dii.bitstreamID = Integer.parseInt(numberPart);
                } catch (NumberFormatException e) {
                }
            }

            // now loop over the child nodes again looking at propstat
            // blocks...
            for (int k = 0; k < resources.getLength(); ++k) {
                if (resources.item(k).getNodeName() == null || !resources.item(k).getNodeName().equals(LAYER3))
                    continue; // only
                // want
                // 'propstat'
                // nodes

                NodeList propstats = resources.item(k).getChildNodes();

                // first check whether status is OK (ignore all HTTP status
                // codes except 200)
                // no status node is assumed to mean OK. abort this propstat
                // if status is not OK.
                boolean status_ok = true;
                for (int l = 0; l < propstats.getLength(); ++l)
                    if (propstats.item(l).getNodeName() != null
                            && propstats.item(l).getNodeName().equals(STATUS)
                            && !propstats.item(l).getFirstChild().getNodeValue().trim().equals(ST_OK)) {
                        status_ok = false;
                        break;
                    }
                if (!status_ok)
                    continue; // skip this propstat node

                // Look for child nodes with name 'prop', having nested
                // child called 'displayname'.
                // This is only complicated when the base handle is a DSpace
                // resource. There will
                // be child nodes (with handle=bitstream_<n>) and the same
                // display name as the resource.
                // We need to extract properties from the bitstream_1 child
                // (.._2 is the license).
                for (int l = 0; l < propstats.getLength(); ++l) {
                    if (propstats.item(l).getNodeName() != null
                            && propstats.item(l).getNodeName().equals(LAYER4)) {
                        boolean recordFromThisChild = false;
                        String lastmod = null, contentlength = null, contenttype = null, dstype = null;
                        NodeList properties = propstats.item(l).getChildNodes();
                        for (int m = 0; m < properties.getLength(); ++m)
                            if (properties.item(m).getNodeName() != null) {
                                if (properties.item(m).getNodeName().equals(DISPNM) && properties.item(m)
                                        .getFirstChild().getNodeValue().trim().equals(needle)) {
                                    foundMatch = true;
                                    recordFromThisChild = true;
                                }
                                if (properties.item(m).getNodeName().equals(LSTMOD))
                                    lastmod = properties.item(m).getFirstChild().getNodeValue().trim();
                                if (properties.item(m).getNodeName().equals(CNTLEN))
                                    contentlength = properties.item(m).getFirstChild().getNodeValue().trim();
                                if (properties.item(m).getNodeName().equals(CNTTYP))
                                    contenttype = properties.item(m).getFirstChild().getNodeValue().trim();
                                if (properties.item(m).getNodeName().equals(DSTYPE)) {
                                    Node n = properties.item(m).getFirstChild();
                                    while (n != null) {
                                        String s = n.getNodeName();
                                        if (!s.equals("#text")) {
                                            dstype = s;
                                            break;
                                        }
                                        n = n.getNextSibling();
                                    }
                                }
                            }
                        if (recordFromThisChild) {
                            if (lastmod != null)
                                dii.lastmodified = lastmod;
                            if (contentlength != null)
                                try {
                                    dii.contentLength = Long.parseLong(contentlength);
                                } catch (NumberFormatException nfe) {
                                }
                            if (contenttype != null)
                                dii.contentType = contenttype;
                            if (dstype != null)
                                dii.itemType = dstype;
                            if (DSRESOURCES_AS_COLLECTIONS)
                                return dii;
                        }
                    }
                }
            }
        }
        if (foundMatch)
            return dii;
    } catch (Exception e) {
        log.warn("Error in CHH DSpace mechanism: parse error: [" + e.toString() + "]");
    }
    return null; // problem talking to DSpace or named resource has gone
}

From source file:org.seagatesoft.sde.tagtreebuilder.DOMParserTagTreeBuilder.java

public TagTree buildTagTree(InputSource inputSource, boolean ignoreFormattingTags, String htmlDocument)
        throws IOException, SAXException {
    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24); // Chrome not working
    HtmlPage page = null;//from w ww .  j  a v a  2  s  .c  o m
    try {
        page = processWebPage(htmlDocument, webClient);
    } catch (Exception e) {
        System.out.println("Get page error");
    }
    TagTree tree = null;
    if (ignoreFormattingTags) {
        tagNodeCreator = new IgnoreFormattingTagsTagNodeCreator();
    } else {
        tagNodeCreator = new DefaultTagNodeCreator();
    }
    DOMParser parser = new DOMParser();
    try {
        //?  
        parser.setFeature("http://xml.org/sax/features/validation", true);
        parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
        parser.setProperty("http://cyberneko.org/html/properties/default-encoding", "UTF-8");
        parser.setFeature("http://xml.org/sax/features/namespaces", false);
        inputSource.setEncoding("UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    org.jsoup.nodes.Document doc = Jsoup.parse(page.asXml());
    File temprFile = File.createTempFile("parsedHtml", ".html");
    temprFile.deleteOnExit();
    FileOutputStream fos = new FileOutputStream(temprFile);
    fos.write(doc.html().getBytes("UTF-8"));
    fos.close();
    BufferedReader in = new BufferedReader(
            new InputStreamReader(new FileInputStream(temprFile.getAbsolutePath()), "UTF8"));
    parser.parse(new InputSource(in));
    org.w3c.dom.Document documentNode = parser.getDocument();
    //baseURI = documentNode.getBaseURI();
    //Pattern baseDirectoryPattern = Pattern.compile("^(.*/)[^/]*$");
    //Matcher matcher = baseDirectoryPattern.matcher( baseURI );

    /*// dapatkan BaseURI dari dokumen HTML ini
    if ( matcher.lookingAt() )
    {
       baseURI = matcher.group(1);
    }*/

    Node bodyNode = documentNode.getElementsByTagName("BODY").item(0);
    TagNode rootTagNode = new TagNode();
    tree = new TagTree();
    tree.setRoot(rootTagNode);
    rootTagNode.setTagElement(HTMLElements.getElement(bodyNode.getNodeName()).code);
    tree.addTagNodeAtLevel(rootTagNode);
    Node child = bodyNode.getFirstChild();

    while (child != null) {
        tagNodeCreator.createTagNodes(child, rootTagNode, tree);
        child = child.getNextSibling();
    }
    tree.assignNodeNumber();
    return tree;
}

From source file:org.sonar.api.utils.XpathParser.java

public String getElementValue(Node base) {
    if (base.getNextSibling() != null && base.getNextSibling().getNodeType() == Node.TEXT_NODE) {
        return base.getNextSibling().getNodeValue();
    } else if (base.getFirstChild() != null && base.getFirstChild().getNodeType() == Node.TEXT_NODE) {
        return base.getFirstChild().getNodeValue();
    }//from www.  j av  a2  s.c o m
    return null;
}