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:com.joliciel.frenchTreebank.search.XmlPatternSearchImpl.java

private PhraseMemberNode traverse(Node node, int depth) {
    {//from   w w  w . j a v  a  2 s.c  om
        LOG.debug(depth + " " + node.getNodeName());
        PhraseMemberNode xmlPatternNode = null;
        Element element = (Element) node;
        String tag = node.getNodeName();
        if (tag == "phrase") {
            String phraseTypes = element.getAttribute("type");
            String functionCodes = element.getAttribute("fct");
            String existsString = element.getAttribute("exists");
            boolean exists = existsString == null || !(existsString.equals("no"));
            LOG.debug(
                    "Phrase type: " + phraseTypes + ", fct = " + functionCodes + ", exists = " + existsString);
            xmlPatternNode = new PhraseNode(phraseTypes, functionCodes, exists, phraseCounter++);
        } else if (tag == "w") {
            String categoryCode = element.getAttribute("cat");
            String subCategoryCode = element.getAttribute("subcat");
            String lemma = element.getAttribute("lemma");
            String word = element.getTextContent();
            String existsString = element.getAttribute("exists");
            boolean exists = existsString == null || !(existsString.equals("no"));
            LOG.debug("w: cat=" + categoryCode + ", subcat = " + subCategoryCode + ", lemma=" + lemma
                    + ", word=" + word + ", exists = " + existsString);
            xmlPatternNode = new WordNode(categoryCode, subCategoryCode, lemma, word, exists,
                    phraseUnitCounter++);
        } else {
            String phraseTypes = node.getNodeName();
            String functionCodes = element.getAttribute("fct");
            String existsString = element.getAttribute("exists");
            boolean exists = existsString == null || !(existsString.equals("no"));
            LOG.debug(
                    "Phrase type: " + phraseTypes + ", fct = " + functionCodes + ", exists = " + existsString);
            xmlPatternNode = new PhraseNode(phraseTypes, functionCodes, exists, phraseCounter++);
        }

        if (xmlPatternNode instanceof PhraseNode) {
            if (node.hasChildNodes()) {
                Node child = node.getFirstChild();
                while (child != null) {
                    if (child.getNodeType() == Node.ELEMENT_NODE) {
                        PhraseNode phraseNode = (PhraseNode) xmlPatternNode;
                        PhraseMemberNode childNode = this.traverse(child, depth + 1);
                        phraseNode.addNode(childNode);
                        childNode.setParent(phraseNode);
                    }
                    child = child.getNextSibling();
                }
            }
        } else if (xmlPatternNode instanceof WordNode) {
            if (node.hasChildNodes()) {
                Node child = node.getFirstChild();
                while (child != null) {
                    if (child.getNodeType() == Node.ELEMENT_NODE) {
                        WordNode wordNode = (WordNode) xmlPatternNode;
                        ComponentWordNode childNode = this.getComponentWordNode(child);
                        wordNode.addNode(childNode);
                        childNode.setParent(wordNode);
                    }
                    child = child.getNextSibling();
                }
            }
        }
        return xmlPatternNode;
    }
}

From source file:XMLConfig.java

/** Returns the value as specified by the DOM path.
 * @param path DOM path/* ww  w  . j  a v  a  2  s.  co  m*/
 * @param root node where the search should start
 * @return list of values.
 */
public List<String> getMultiple(String path, Node root) {
    List<Node> accum = getNodes(path, root);
    List<String> strings = new LinkedList<String>();
    for (Node n : accum) {
        if (n instanceof Attr) {
            strings.add(n.getNodeValue());
        } else {
            Node child;
            String acc = "";
            child = n.getFirstChild();
            while (child != null) {
                if (child.getNodeName().equals("#text")) {
                    acc += " " + child.getNodeValue();
                } else if (child.getNodeName().equals("#comment")) {
                    // ignore
                } else {
                    throw new XMLConfigException("Node " + n.getNodeName() + " contained node "
                            + child.getNodeName() + ", but should only contain #text and #comment.");
                }
                child = child.getNextSibling();
            }
            strings.add(acc.trim());
        }
    }
    return strings;
}

From source file:Main.java

/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node./*from w  w  w. j a  va  2 s  .  c  o m*/
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start = src;
    Node parent = src;
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl) element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + node.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:org.apache.cxf.cwiki.SiteExporter.java

public void loadPages() throws Exception {
    Document doc = DOMUtils.newDocument();
    Element el = doc.createElementNS(SOAPNS, "ns1:getPages");
    Element el2 = doc.createElement("in0");
    el.appendChild(el2);//  w  w w  . j av  a 2 s  .c  om
    el2.setTextContent(loginToken);
    el2 = doc.createElement("in1");
    el.appendChild(el2);
    el2.setTextContent(spaceKey);
    doc.appendChild(el);
    doc = getDispatch().invoke(doc);

    Set<String> allPages = new CopyOnWriteArraySet<String>(pages.keySet());
    Set<Page> newPages = new CopyOnWriteArraySet<Page>();
    List<Future<?>> futures = new ArrayList<Future<?>>(allPages.size());

    // XMLUtils.printDOM(doc.getDocumentElement());

    Node nd = doc.getDocumentElement().getFirstChild().getFirstChild();
    while (nd != null) {
        if (nd instanceof Element) {
            futures.add(loadPage((Element) nd, allPages, newPages));
        }
        nd = nd.getNextSibling();
    }
    for (Future<?> f : futures) {
        //wait for all the pages to be done
        f.get();
    }
    for (Page p : newPages) {
        //pages have been added, need to check
        checkForChildren(p);
    }
    for (String id : allPages) {
        //these pages have been deleted
        Page p = pages.remove(id);
        checkForChildren(p);

        File file = new File(outputDir, p.createFileName());
        if (file.exists()) {
            callSvn("rm", file.getAbsolutePath());
            svnCommitMessage.append("Deleted: " + file.getName() + "\n");
        }
        if (file.exists()) {
            file.delete();
        }
    }
    while (checkIncludes()) {
        // nothing
    }

}

From source file:jef.tools.XMLUtils.java

/**
 * ??//from w  w w .ja  va2s  .  c o  m
 * 
 * @param node
 *            
 * @param tagName
 *            ????
 * @return ?
 */
public static Element firstSibling(Node node, String tagName) {
    Node p = node.getNextSibling();
    while (p != null) {
        if (p.getNodeType() == Node.ELEMENT_NODE) {
            if (StringUtils.isEmpty(tagName) || p.getNodeName().equals(tagName))
                return (Element) p;
        }
        p = p.getNextSibling();
    }
    return null;
}

From source file:com.collabnet.tracker.core.PTrackerWebServicesClient.java

/**
 * This is a check to ensure that invalid next page calls are not sent to the server.
 * @param pageInfo//from w ww . ja va  2s  .c o m
 * @throws Exception
 */
public void validateNextPage(Node pageInfo, String altQueryRef) throws Exception {
    Node child = pageInfo.getFirstChild();
    String msg = "Next page does not have a valid query reference";
    String name, value;
    while (child != null) {
        name = child.getNodeName();
        value = child.getTextContent();
        if (name.contains("queryReference")) {
            if (value == null || value.length() < 1) {
                if (altQueryRef == null)
                    throw new Exception(msg);
                child.setTextContent(altQueryRef);
            }
            return;
        }
        child = child.getNextSibling();
    }
    throw new Exception(msg);
}

From source file:Main.java

@SuppressWarnings("null")
public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;//  ww  w  . jav a  2s . co m
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                /*
                 if (domimpl && !attr.getSpecified()) {
                 ((Attr) element.getAttributeNode(attrName)).setSpecified(false);
                 }
                 */
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + node.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null && dest != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:com.alfaariss.oa.util.configuration.ConfigurationManager.java

/**
 * Saves the configuration.//from   w  ww. ja v  a  2  s.  c  o  m
 * @see IConfigurationManager#saveConfiguration()
 */
public synchronized void saveConfiguration() throws ConfigurationException {
    boolean bFound = false;
    Date dNow = null;
    StringBuffer sbComment = null;
    Element elRoot = null;
    Node nCurrent = null;
    Node nComment = null;
    String sValue = null;
    try {
        // add date to configuration
        dNow = new Date(System.currentTimeMillis());

        sbComment = new StringBuffer(" Configuration changes saved on ");
        sbComment.append(DateFormat.getDateInstance().format(dNow));
        sbComment.append(". ");

        elRoot = _oDomDocument.getDocumentElement();
        nCurrent = elRoot.getFirstChild();
        while (!bFound && nCurrent != null) // all elements
        {
            if (nCurrent.getNodeType() == Node.COMMENT_NODE) {
                // check if it's a "save changes" comment
                sValue = nCurrent.getNodeValue();
                if (sValue.trim().startsWith("Configuration changes saved on")) {
                    // overwrite message
                    nCurrent.setNodeValue(sbComment.toString());
                    bFound = true;
                }
            }
            nCurrent = nCurrent.getNextSibling();
        }
        if (!bFound) // no comment found: adding new
        {
            // create new comment node
            nComment = _oDomDocument.createComment(sbComment.toString());
            // insert comment before first node
            elRoot.insertBefore(nComment, elRoot.getFirstChild());
        }
        _oConfigHandler.saveConfiguration(_oDomDocument);
    } catch (ConfigurationException e) {
        throw e;
    } catch (Exception e) {
        _logger.fatal("Internal error", e);
        throw new ConfigurationException(SystemErrors.ERROR_INTERNAL);
    }
}

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

@Override
public final void normalize() {

    Document document = getOwnerDocument();
    if (document != null) {

        // merge adjacent text nodes until there is only one left
        Node child = getFirstChild();
        while (child != null) {

            if (child instanceof Text) {

                Node next = child.getNextSibling();
                if (next != null && next instanceof Text) {

                    String text1 = child.getNodeValue();
                    String text2 = next.getNodeValue();

                    // create new text node
                    Text newText = document.createTextNode(text1.concat(text2));

                    removeChild(child);//from   w  ww.j av  a2s .  c om
                    insertBefore(newText, next);
                    removeChild(next);

                    child = newText;

                } else {

                    // advance to next node
                    child = next;
                }

            } else {

                // advance to next node
                child = child.getNextSibling();

            }
        }

        // recursively normalize child nodes
        if (hasChildNodes()) {

            Node currentChild = getFirstChild();
            while (currentChild != null) {

                currentChild.normalize();
                currentChild = currentChild.getNextSibling();
            }
        }
    }

}

From source file:org.apache.cxf.cwiki.SiteExporter.java

public void loadBlog() throws Exception {
    System.out.println("Loading Blog entries for " + spaceKey);
    Document doc = DOMUtils.createDocument();
    Element el = doc.createElementNS(SOAPNS, "ns1:getBlogEntries");
    Element el2 = doc.createElement("in0");
    el.appendChild(el2);//from  w w w.j a  v  a 2  s. c  om
    el2.setTextContent(loginToken);
    el2 = doc.createElement("in1");
    el.appendChild(el2);
    el2.setTextContent(spaceKey);
    doc.appendChild(el);
    doc = getDispatch().invoke(doc);

    Map<String, BlogEntrySummary> oldBlog = new ConcurrentHashMap<String, BlogEntrySummary>(blog);

    Node nd = doc.getDocumentElement().getFirstChild().getFirstChild();
    while (nd != null) {
        if (nd instanceof Element) {
            BlogEntrySummary entry = new BlogEntrySummary((Element) nd);
            entry.setVersion(getBlogVersion(entry.id));
            BlogEntrySummary oldEntry = blog.put(entry.getId(), entry);
            System.out.println("Found Blog entry for " + entry.getTitle() + " " + entry.getPath());

            if (oldEntry == null || oldEntry.getVersion() != entry.getVersion()) {
                System.out.println("   and it's modified");
                modifiedBlog.add(entry);
            } else {
                System.out.println("   but it's not modified");
            }
            oldBlog.remove(entry.getId());
        }
        nd = nd.getNextSibling();
    }

    for (String id : oldBlog.keySet()) {
        //these pages have been deleted
        BlogEntrySummary p = blog.remove(id);
        File file = new File(outputDir, p.getPath());
        if (file.exists()) {
            callSvn("rm", file.getAbsolutePath());
            svnCommitMessage.append("Deleted: " + file.getName() + "\n");
        }
        if (file.exists()) {
            file.delete();
        }
    }
}