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.bstek.dorado.view.config.XmlDocumentPreprocessor.java

private List<Element> getImportContent(Element importElement, PreparseContext context) throws Exception {
    String src = importElement.getAttribute("src");

    if (StringUtils.isNotEmpty(src)) {
        Expression expression = expressionHandler.compile(src);
        if (expression != null) {
            src = (String) expression.evaluate();
        }/*from  w ww  . ja v a 2s .  c o m*/
    }

    if (StringUtils.isEmpty(src)) {
        throw new IllegalArgumentException("Import src undefined");
    }

    int i = src.lastIndexOf('#');
    if (i < 0) {
        throw new IllegalArgumentException("[groupId/componentId] missed");
    }

    String viewName = src.substring(0, i);
    String groupId = src.substring(i + 1);
    Assert.notEmpty(viewName, "Import viewName undefined.");
    Assert.notEmpty(groupId, "Import groupId/componentId undefined.");

    ViewConfigInfo viewConfigInfo = getViewConfigModelInfo(viewName);
    if (viewConfigInfo == null) {
        throw new XmlParseException("Import view not found [" + src + "].", context);
    }

    Document document = (Document) viewConfigInfo.getConfigModel();
    List<Element> importContent = null;
    Element groupStartElement = getGroupStartElement(document, groupId, context);
    if (groupStartElement != null) {
        importContent = new ArrayList<Element>();
        String nodeName = groupStartElement.getNodeName();
        if (nodeName.equals(XmlConstants.GROUP_START)) {
            boolean groupEndFound = false;
            Node node = groupStartElement.getNextSibling();
            while (true) {
                node = node.getNextSibling();
                if (node == null)
                    break;
                if (node instanceof Element) {
                    Element element = (Element) node;
                    nodeName = element.getNodeName();
                    if (nodeName.equals(XmlConstants.GROUP_END)) {
                        groupEndFound = true;
                        break;
                    } else if (nodeName.equals(XmlConstants.GROUP_START)) {
                        throw new IllegalArgumentException("Nesting <GroupStart> not supported.");
                    } else {
                        importContent.add(element);
                    }
                }
            }
            if (!groupEndFound) {
                throw new IllegalArgumentException("<GroupEnd> not found for [" + groupId + "].");
            }
        } else if (nodeName.equals(XmlConstants.GROUP_END) || nodeName.equals(XmlConstants.PLACE_HOLDER)
                || nodeName.equals(XmlConstants.PLACE_HOLDER_START)
                || nodeName.equals(XmlConstants.PLACE_HOLDER_END)) {
            // do nothing
        } else {
            importContent.add(groupStartElement);
        }
    }

    Resource dependentResource = viewConfigInfo.getResource();
    if (dependentResource != null) {
        context.getDependentResources().add(dependentResource);
    }
    return importContent;
}

From source file:greenfoot.export.mygame.MyGameClient.java

private List<String> parseTagListXmlElement(Element element) {
    List<String> tags = new ArrayList<String>();

    Node child = element.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            element = (Element) child;
            if (element.getTagName().equals("tag")) {
                tags.add(element.getTextContent());
            }/*from   w ww .  jav a  2  s  . c o m*/
        }
        child = child.getNextSibling();
    }

    return tags;
}

From source file:DOMWriter.java

/** Writes the specified node, recursively. */
public void write(Node node) {

    // is there anything to do?
    if (node == null) {
        return;/*from   w w  w .  j  a v  a  2 s.c  o m*/
    }

    short type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        Document document = (Document) node;
        fXML11 = "1.1".equals(getVersion(document));
        if (!fCanonical) {
            if (fXML11) {
                fOut.println("<?xml version=\"1.1\" encoding=\"UTF-8\"?>");
            } else {
                fOut.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            }
            fOut.flush();
            write(document.getDoctype());
        }
        write(document.getDocumentElement());
        break;
    }

    case Node.DOCUMENT_TYPE_NODE: {
        DocumentType doctype = (DocumentType) node;
        fOut.print("<!DOCTYPE ");
        fOut.print(doctype.getName());
        String publicId = doctype.getPublicId();
        String systemId = doctype.getSystemId();
        if (publicId != null) {
            fOut.print(" PUBLIC '");
            fOut.print(publicId);
            fOut.print("' '");
            fOut.print(systemId);
            fOut.print('\'');
        } else if (systemId != null) {
            fOut.print(" SYSTEM '");
            fOut.print(systemId);
            fOut.print('\'');
        }
        String internalSubset = doctype.getInternalSubset();
        if (internalSubset != null) {
            fOut.println(" [");
            fOut.print(internalSubset);
            fOut.print(']');
        }
        fOut.println('>');
        break;
    }

    case Node.ELEMENT_NODE: {
        fOut.print('<');
        fOut.print(node.getNodeName());
        Attr attrs[] = sortAttributes(node.getAttributes());
        for (int i = 0; i < attrs.length; i++) {
            Attr attr = attrs[i];
            fOut.print(' ');
            fOut.print(attr.getNodeName());
            fOut.print("=\"");
            normalizeAndPrint(attr.getNodeValue(), true);
            fOut.print('"');
        }
        fOut.print('>');
        fOut.flush();

        Node child = node.getFirstChild();
        while (child != null) {
            write(child);
            child = child.getNextSibling();
        }
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: {
        if (fCanonical) {
            Node child = node.getFirstChild();
            while (child != null) {
                write(child);
                child = child.getNextSibling();
            }
        } else {
            fOut.print('&');
            fOut.print(node.getNodeName());
            fOut.print(';');
            fOut.flush();
        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        if (fCanonical) {
            normalizeAndPrint(node.getNodeValue(), false);
        } else {
            fOut.print("<![CDATA[");
            fOut.print(node.getNodeValue());
            fOut.print("]]&gt;");
        }
        fOut.flush();
        break;
    }

    case Node.TEXT_NODE: {
        normalizeAndPrint(node.getNodeValue(), false);
        fOut.flush();
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: {
        fOut.print("<?");
        fOut.print(node.getNodeName());
        String data = node.getNodeValue();
        if (data != null && data.length() > 0) {
            fOut.print(' ');
            fOut.print(data);
        }
        fOut.print("?>");
        fOut.flush();
        break;
    }

    case Node.COMMENT_NODE: {
        if (!fCanonical) {
            fOut.print("<!--");
            String comment = node.getNodeValue();
            if (comment != null && comment.length() > 0) {
                fOut.print(comment);
            }
            fOut.print("-->");
            fOut.flush();
        }
    }
    }

    if (type == Node.ELEMENT_NODE) {
        fOut.print("</");
        fOut.print(node.getNodeName());
        fOut.print('>');
        fOut.flush();
    }

}

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

private synchronized Element getSubSection(Element eRootSection, String sSectionType) {
    assert eRootSection != null : "Suplied root section is empty";

    if (eRootSection.hasChildNodes()) {
        Node nTemp = eRootSection.getFirstChild();
        while (nTemp != null) {
            if (nTemp.getNodeType() == Node.ELEMENT_NODE && nTemp.getNodeName().equals(sSectionType)) {
                return (Element) nTemp;
            }/*from  ww w .  ja v  a  2s .  c om*/
            nTemp = nTemp.getNextSibling();
        }
    }
    return null;
}

From source file:edu.wpi.margrave.MCommunicator.java

private static Document handleAddVocabFact(Node margraveCommandNode, Node childNode) {
    String vname = getVocabName(margraveCommandNode);
    Node secondChildNode = childNode.getNextSibling(); // WORRY Shouldn't be hardcoded in!!
    String addType = secondChildNode.getNodeName();
    writeToLog("addType: " + addType + "\n");

    if (addType.equalsIgnoreCase("SUBSORT")) {
        String parent = getSubSortParent(margraveCommandNode);
        String child = getSubSortChild(margraveCommandNode);
        return MEnvironment.addSubsort(vname, parent, child);
    } else if (addType.equalsIgnoreCase("SORT")) {
        String sortName = getSortName(margraveCommandNode);
        return MEnvironment.addSort(vname, sortName);
    } else if (addType.equalsIgnoreCase("SORT-WITH-CHILDREN")) {
        String sortName = getAttributeOfChildNodeOrNode(margraveCommandNode, "SORT-WITH-CHILDREN", "name");

        List<String> childnames = getListElements(margraveCommandNode, "SORT-WITH-CHILDREN", "name");
        return MEnvironment.addSortWithSubs(vname, sortName, childnames);
    } else if (addType.equalsIgnoreCase("PREDICATE")) {
        String sName = getPredicateName(margraveCommandNode);
        List<String> constr = getRelationsList(margraveCommandNode);
        writeToLog("Adding Predicate\n");
        return MEnvironment.addPredicate(vname, sName, constr);
    }//  ww w . j  a  va 2  s  . c  o  m

    else if (addType.equalsIgnoreCase("CONSTANT")) {
        String sName = getAttributeOfChildNodeOrNode(secondChildNode, "CONSTANT", "name");
        String sSort = getAttributeOfChildNodeOrNode(secondChildNode, "CONSTANT", "type");
        writeToLog("Adding constant " + sName + " : " + sSort + "\n");
        return MEnvironment.addConstant(vname, sName, sSort);
    } else if (addType.equalsIgnoreCase("FUNCTION")) {
        String sName = getAttributeOfChildNodeOrNode(secondChildNode, "FUNCTION", "name");
        List<String> constr = getListElements(secondChildNode, "RELATIONS", "name");
        writeToLog("Adding function " + sName + " : " + constr + "\n");
        //System.err.println("Adding function "+sName+" : "+constr+"\n");
        return MEnvironment.addFunction(vname, sName, constr);
    }

    else if (addType.equalsIgnoreCase("CONSTRAINT")) {
        Node constraintNode = secondChildNode; //Just for clarity

        String constraintType = getConstraintType(constraintNode);

        // FORMULA is special.
        if (constraintType.equalsIgnoreCase("FORMULA")) {
            return MEnvironment.addConstraintFormula(vname,
                    exploreHelper(constraintNode.getFirstChild(), new ArrayList<String>()).fmla);
        }

        List<String> relations = getRelationsList(constraintNode);

        assert (relations != null);
        String firstRelation = relations.get(0);

        if (constraintType.equalsIgnoreCase("SINGLETON")) {
            return MEnvironment.addConstraintSingleton(vname, firstRelation);
        } else if (constraintType.equalsIgnoreCase("SINGLETON-ALL")) {
            return MEnvironment.addConstraintSingletonAll(vname, firstRelation);
        } else if (constraintType.equalsIgnoreCase("ATMOSTONE")) {
            return MEnvironment.addConstraintAtMostOne(vname, firstRelation);
        } else if (constraintType.equalsIgnoreCase("DISJOINT")) {
            assert (relations.size() == 2);
            String secondRelation = relations.get(1);
            return MEnvironment.addConstraintDisjoint(vname, firstRelation, secondRelation);
        }

        else if (constraintType.equalsIgnoreCase("SUBSET")) {
            assert (relations.size() == 2);
            String secondRelation = relations.get(1);
            return MEnvironment.addConstraintSubset(vname, firstRelation, secondRelation);
        } else if (constraintType.equalsIgnoreCase("CONSTANTS-NEQ")) {
            assert (relations.size() == 2);
            String secondRelation = relations.get(1);
            return MEnvironment.addConstraintConstantsNeq(vname, firstRelation, secondRelation);
        } else if (constraintType.equalsIgnoreCase("CONSTANTS-COVER")) {
            return MEnvironment.addConstraintConstantsCover(vname, firstRelation);
        } else if (constraintType.equalsIgnoreCase("CONSTANTS-NEQ-ALL")) {
            MCommunicator.writeToLog("\nAdding constraint constants-neq-all: " + firstRelation);
            return MEnvironment.addConstraintConstantsNeqAll(vname, firstRelation);
        } else if (constraintType.equalsIgnoreCase("ATMOSTONE-ALL")) {
            return MEnvironment.addConstraintAtMostOneAll(vname, firstRelation);
        } else if (constraintType.equalsIgnoreCase("NONEMPTY")) {
            return MEnvironment.addConstraintNonempty(vname, firstRelation);
        } else if (constraintType.equalsIgnoreCase("NONEMPTY-ALL")) {
            return MEnvironment.addConstraintNonemptyAll(vname, firstRelation);
        } else if (constraintType.equalsIgnoreCase("ABSTRACT")) {
            return MEnvironment.addConstraintAbstract(vname, firstRelation);
        } else if (constraintType.equalsIgnoreCase("ABSTRACT-ALL")) {
            return MEnvironment.addConstraintAbstractAll(vname, firstRelation);
        } else if (constraintType.equalsIgnoreCase("TOTAL-FUNCTION")) {
            return MEnvironment.addConstraintTotalFunction(vname, firstRelation);
        } else if (constraintType.equalsIgnoreCase("TOTAL-RELATION")) {
            return MEnvironment.addConstraintTotalRelation(vname, firstRelation);
        } else if (constraintType.equalsIgnoreCase("PARTIAL-FUNCTION")) {
            return MEnvironment.addConstraintPartialFunction(vname, firstRelation);
        } else {
            // Unknown constraint type; throw an exception
            return MEnvironment.errorResponse(MEnvironment.sUnknown, MEnvironment.sConstraint, constraintType);
        }
    } else {
        return MEnvironment.errorResponse(MEnvironment.sCommand, MEnvironment.sNotExpected, addType);
    }
}

From source file:com.amalto.core.save.context.BeforeSaving.java

public void save(SaverSession session, DocumentSaverContext context) {
    // Invoke the beforeSaving
    MutableDocument updateReportDocument = context.getUpdateReportDocument();
    if (updateReportDocument == null) {
        throw new IllegalStateException("Update report is missing."); //$NON-NLS-1$
    }//from  w  w  w  .j  av  a 2 s. c o m
    OutputReport outputreport = session.getSaverSource().invokeBeforeSaving(context, updateReportDocument);

    if (outputreport != null) { // when a process was found
        String errorCode;
        message = outputreport.getMessage();
        if (!validateFormat(message))
            throw new BeforeSavingFormatException(message);
        try {
            Document doc = Util.parse(message);
            // handle output_report
            String xpath = "//report/message"; //$NON-NLS-1$
            Node errorNode;
            synchronized (XPATH) {
                errorNode = (Node) XPATH.evaluate(xpath, doc, XPathConstants.NODE);
            }
            errorCode = null;
            if (errorNode instanceof Element) {
                Element errorElement = (Element) errorNode;
                errorCode = errorElement.getAttribute("type"); //$NON-NLS-1$
                Node child = errorElement.getFirstChild();
                if (child instanceof org.w3c.dom.Text) {
                    message = child.getTextContent();
                } else {
                    message = ""; //$NON-NLS-1$   
                }
            }

            if (!"info".equals(errorCode)) { //$NON-NLS-1$
                throw new BeforeSavingErrorException(message);
            }

            // handle output_item
            if (outputreport.getItem() != null) {
                xpath = "//exchange/item"; //$NON-NLS-1$
                SkipAttributeDocumentBuilder builder = new SkipAttributeDocumentBuilder(
                        SaverContextFactory.DOCUMENT_BUILDER, false);
                doc = builder.parse(new InputSource(new StringReader(outputreport.getItem())));
                Node item;
                synchronized (XPATH) {
                    item = (Node) XPATH.evaluate(xpath, doc, XPathConstants.NODE);
                }
                if (item != null && item instanceof Element) {
                    Node node = null;
                    Node current = item.getFirstChild();
                    while (current != null) {
                        if (current instanceof Element) {
                            node = current;
                            break;
                        }
                        current = item.getNextSibling();
                    }
                    if (node != null) {
                        // set back the modified item by the process
                        DOMDocument document = new DOMDocument(node, context.getUserDocument().getType(),
                                context.getDataCluster(), context.getDataModelName());
                        context.setUserDocument(document);
                        context.setDatabaseDocument(null);
                        // Redo a set of actions and security checks.
                        // TMDM-4599: Adds UpdateReport phase so a new update report is generated based on latest changes.
                        next = new ID(
                                new GenerateActions(new Security(new UpdateReport(new ApplyActions(next)))));
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Exception occurred during before saving phase.", e); //$NON-NLS-1$
        }
    }

    next.save(session, context);
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection.java

/**
 * Returns the elements whose associated host objects are available through this collection.
 * @return the elements whose associated host objects are available through this collection
 *///from  w  w w.ja  v a2 s .c  o m
protected List<Object> computeElements() {
    final List<Object> response;
    if (node_ != null) {
        if (xpath_ != null) {
            response = XPathUtils.getByXPath(node_, xpath_);
        } else {
            response = new ArrayList<Object>();
            Node node = node_.getFirstChild();
            while (node != null) {
                response.add(node);
                node = node.getNextSibling();
            }
        }
    } else {
        response = new ArrayList<Object>();
    }

    final boolean isXmlPage = node_ != null && node_.getOwnerDocument() instanceof XmlPage;

    final boolean isIE = getBrowserVersion().hasFeature(BrowserVersionFeatures.GENERATED_45);

    for (int i = 0; i < response.size(); i++) {
        final DomNode element = (DomNode) response.get(i);

        //IE: XmlPage ignores all empty text nodes
        if (isIE && isXmlPage && element instanceof DomText
                && ((DomText) element).getNodeValue().trim().length() == 0) { //and 'xml:space' is 'default'
            final Boolean xmlSpaceDefault = isXMLSpaceDefault(element.getParentNode());
            if (xmlSpaceDefault != Boolean.FALSE) {
                response.remove(i--);
                continue;
            }
        }
        for (DomNode parent = element.getParentNode(); parent != null; parent = parent.getParentNode()) {
            if (parent instanceof HtmlNoScript) {
                response.remove(i--);
                break;
            }
        }
    }

    return response;
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test/*from  w ww  .  ja  v a  2s  .  co m*/
public void testSetValueNoChildren() {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    String value = "test";
    element.setValue(value);
    assertEquals(value, element.getValue());
    Node child = element.getFirstChild();
    assertTrue(child instanceof Text);
    assertEquals(value, ((Text) child).getValue());
    assertNull(child.getNextSibling());
}

From source file:com.verisign.epp.codec.gen.EPPUtil.java

/**
 * Gets the next sibling Element of a provided Element.
 * //from w ww . j a  v a 2 s . c  om
 * @param aElement
 *            Element to start scan for the next Element.
 * @return Found DOM Element Node if found; <code>null</code> otherwise.
 */
public static Element getNextElementSibling(Element aElement) {
    Element theSibling = null;
    Node theCurrNode = aElement;

    while ((theCurrNode != null) && (theSibling == null)) {
        theCurrNode = theCurrNode.getNextSibling();

        if ((theCurrNode != null) && (theCurrNode.getNodeType() == Node.ELEMENT_NODE)) {
            theSibling = (Element) theCurrNode;
        }
    }

    return theSibling;
}