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.weloveastrid.rmilk.api.Invoker.java

public Element invoke(boolean repeat, Param... params) throws ServiceException {
    long timeSinceLastInvocation = System.currentTimeMillis() - lastInvocation;
    if (timeSinceLastInvocation < INVOCATION_INTERVAL) {
        // In order not to invoke the RTM service too often
        try {//  w w w . j a  v  a  2 s.c o  m
            Thread.sleep(INVOCATION_INTERVAL - timeSinceLastInvocation);
        } catch (InterruptedException e) {
            return null;
        }
    }

    // We compute the URI
    final StringBuffer requestUri = computeRequestUri(params);
    HttpResponse response = null;

    final HttpGet request = new HttpGet("http://" //$NON-NLS-1$
            + ServiceImpl.SERVER_HOST_NAME + requestUri.toString());
    final String methodUri = request.getRequestLine().getUri();

    Element result;
    try {
        Log.i(TAG, "Executing the method:" + methodUri); //$NON-NLS-1$
        response = httpClient.execute(request);
        lastInvocation = System.currentTimeMillis();

        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.e(TAG, "Method failed: " + response.getStatusLine()); //$NON-NLS-1$

            // Tim: HTTP error. Let's wait a little bit
            if (!repeat) {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    // ignore
                }
                response.getEntity().consumeContent();
                return invoke(true, params);
            }

            throw new ServiceInternalException("method failed: " + response.getStatusLine());
        }

        final Document responseDoc = builder.parse(response.getEntity().getContent());
        final Element wrapperElt = responseDoc.getDocumentElement();
        if (!wrapperElt.getNodeName().equals("rsp")) {
            throw new ServiceInternalException(
                    "unexpected response returned by RTM service: " + wrapperElt.getNodeName());
        } else {
            String stat = wrapperElt.getAttribute("stat");
            if (stat.equals("fail")) {
                Node errElt = wrapperElt.getFirstChild();
                while (errElt != null
                        && (errElt.getNodeType() != Node.ELEMENT_NODE || !errElt.getNodeName().equals("err"))) {
                    errElt = errElt.getNextSibling();
                }
                if (errElt == null) {
                    throw new ServiceInternalException(
                            "unexpected response returned by RTM service: " + wrapperElt.getNodeValue());
                } else {
                    if (SERVICE_UNAVAILABLE_CODE.equals(((Element) errElt).getAttribute("code")) && !repeat) {
                        try {
                            Thread.sleep(1500);
                        } catch (InterruptedException e) {
                            // ignore
                        }
                        return invoke(true, params);
                    }

                    throw new ServiceException(Integer.parseInt(((Element) errElt).getAttribute("code")),
                            ((Element) errElt).getAttribute("msg"));
                }
            } else {
                Node dataElt = wrapperElt.getFirstChild();
                while (dataElt != null && (dataElt.getNodeType() != Node.ELEMENT_NODE
                        || dataElt.getNodeName().equals("transaction") == true)) {
                    try {
                        Node nextSibling = dataElt.getNextSibling();
                        if (nextSibling == null) {
                            break;
                        } else {
                            dataElt = nextSibling;
                        }
                    } catch (IndexOutOfBoundsException exception) {
                        // Some implementation may throw this exception,
                        // instead of returning a null sibling
                        break;
                    }
                }
                if (dataElt == null) {
                    throw new ServiceInternalException(
                            "unexpected response returned by RTM service: " + wrapperElt.getNodeValue());
                } else {
                    result = (Element) dataElt;
                }
            }
        }
    } catch (IOException e) {
        throw new ServiceInternalException("Error making connection: " + e.getMessage(), e);
    } catch (SAXException e) {
        // repeat call if possible.
        if (!repeat)
            return invoke(true, params);
        else
            throw new ServiceInternalException("Error parsing response. " + "Please try sync again!", e);
    } finally {
        httpClient.getConnectionManager().closeExpiredConnections();
    }

    return result;
}

From source file:de.elbe5.base.data.XmlData.java

public String getText(Element node) {
    if (node.hasChildNodes()) {
        Node child = node.getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.TEXT_NODE) {
                return child.getNodeValue();
            }//  www .  j  av a  2  s  . c o  m
            child = child.getNextSibling();
        }
    }
    return "";
}

From source file:de.elbe5.base.data.XmlData.java

public String getCData(Node node) {
    if (node.hasChildNodes()) {
        Node child = node.getFirstChild();
        while (child != null) {
            if (child instanceof CDATASection) {
                return ((CDATASection) child).getData();
            }// ww w.  j a v a2  s. c  om
            child = child.getNextSibling();
        }
    }
    return "";
}

From source file:de.elbe5.base.data.XmlData.java

public Node findSubElement(Node parent, String localName) {
    if (parent == null) {
        return null;
    }/*  w  w w. jav a  2 s .com*/
    Node child = parent.getFirstChild();
    while (child != null) {
        if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getLocalName().equals(localName))) {
            return child;
        }
        child = child.getNextSibling();
    }
    return null;
}

From source file:com.l2jfree.gameserver.model.skills.conditions.ConditionParser.java

private Condition parseLogicNot(Node n, Object template) {
    Condition cond = null;//from   ww w.  java 2  s  . co  m
    for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) {
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (cond != null)
                throw new IllegalStateException("Full <not> condition");

            cond = parseExistingConditionInsideLogic(n, template);
        }
    }

    if (cond == null)
        throw new IllegalStateException("Empty <not> condition");

    return new ConditionLogicNot(cond);
}

From source file:com.amalto.core.history.accessor.ManyFieldAccessor.java

private Element getCollectionItemNode() {
    if (cachedCollectionItemNode != null) {
        return cachedCollectionItemNode;
    }/*from  www .  j a v a 2s  . c o  m*/
    Element collectionItemNode = null;
    Node node = parent.getNode();
    if (node == null) {
        throw new IllegalStateException("Could not find parent node in document.");
    }
    NodeList children = node.getChildNodes();
    if (index > children.getLength()) {
        return null;
    }
    int currentIndex = 0;
    Node current = node.getFirstChild();
    while (current != null) {
        if (fieldName.equals(current.getNodeName())) {
            if (index == currentIndex) {
                collectionItemNode = (Element) current;
                break;
            } else {
                currentIndex++;
            }
        }
        current = current.getNextSibling();
    }
    cachedCollectionItemNode = collectionItemNode;
    return collectionItemNode;
}

From source file:com.l2jfree.gameserver.model.skills.conditions.ConditionParser.java

private Condition parseLogicOr(Node n, Object template) {
    ConditionLogicOr cond = new ConditionLogicOr();
    for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) {
        if (n.getNodeType() == Node.ELEMENT_NODE)
            cond.add(parseExistingConditionInsideLogic(n, template));
    }//  ww w  .  j a va2  s .com

    return cond.getCanonicalCondition();
}

From source file:com.l2jfree.gameserver.model.skills.conditions.ConditionParser.java

private Condition parseLogicAnd(Node n, Object template) {
    ConditionLogicAnd cond = new ConditionLogicAnd();
    for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) {
        if (n.getNodeType() == Node.ELEMENT_NODE)
            cond.add(parseExistingConditionInsideLogic(n, template));
    }/*from  w w  w.ja v a2s .  c o  m*/

    return cond.getCanonicalCondition();
}

From source file:eu.prestoprime.p4gui.connection.AccessConnection.java

public static List<Event> getEvents(P4Service service, String id) {
    final String PREMIS_NS = "http://www.loc.gov/standards/premis/v1";

    DIP dip = AccessConnection.getDIP(service, id);
    NodeList eventNodes = ((Document) dip.getContent()).getDocumentElement().getElementsByTagNameNS(PREMIS_NS,
            "event");
    logger.debug("Found " + eventNodes.getLength() + " PREMIS events...");

    List<Event> events = new ArrayList<>();
    for (int i = 0; i < eventNodes.getLength(); i++) {
        Node eventNode = eventNodes.item(i);
        do {/*from ww w  .  j a  va 2  s . c  o  m*/
            if (eventNode instanceof Element) {
                Node typeNode = ((Element) eventNode).getElementsByTagNameNS(PREMIS_NS, "eventType").item(0);
                Node dateTimeNode = ((Element) eventNode).getElementsByTagNameNS(PREMIS_NS, "eventDateTime")
                        .item(0);
                Node detailNode = ((Element) eventNode).getElementsByTagNameNS(PREMIS_NS, "eventDetail")
                        .item(0);

                String type = typeNode.getTextContent();
                Calendar dateTime;
                try {
                    dateTime = DatatypeFactory.newInstance()
                            .newXMLGregorianCalendar(dateTimeNode.getTextContent()).toGregorianCalendar();
                } catch (Exception e) {
                    dateTime = new GregorianCalendar();
                }
                String detail = detailNode.getTextContent();

                logger.debug("Found event " + type + "...");

                Event event = new Event(type, dateTime, detail);
                events.add(event);
            }
        } while ((eventNode = eventNode.getNextSibling()) != null);
    }
    return events;
}

From source file:com.l2jfree.gameserver.model.skills.conditions.ConditionParser.java

private Condition parseCondition(Node n, Object template, boolean force, boolean onlyFirst) {
    Condition cond = null;//from  w  ww  .  ja v  a  2  s  . c o  m
    for (; n != null; n = n.getNextSibling()) {
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (cond != null)
                throw new IllegalStateException("Full condition");

            else if ("and".equalsIgnoreCase(n.getNodeName()))
                cond = parseLogicAnd(n, template);

            else if ("or".equalsIgnoreCase(n.getNodeName()))
                cond = parseLogicOr(n, template);

            else if ("not".equalsIgnoreCase(n.getNodeName()))
                cond = parseLogicNot(n, template);

            else if ("player".equalsIgnoreCase(n.getNodeName()))
                cond = parsePlayerCondition(n, template);

            else if ("target".equalsIgnoreCase(n.getNodeName()))
                cond = parseTargetCondition(n, template);

            else if ("using".equalsIgnoreCase(n.getNodeName()))
                cond = parseUsingCondition(n, template);

            else if ("game".equalsIgnoreCase(n.getNodeName()))
                cond = parseGameCondition(n, template);

            else
                throw new IllegalStateException("Unrecognized condition <" + n.getNodeName() + ">");

            if (onlyFirst)
                return cond;
        }
    }

    if (force && cond == null)
        throw new IndexOutOfBoundsException("Empty condition");

    return cond;
}