Example usage for org.dom4j Node accept

List of usage examples for org.dom4j Node accept

Introduction

In this page you can find the example usage for org.dom4j Node accept.

Prototype

void accept(Visitor visitor);

Source Link

Document

accept is the method used in the Visitor Pattern.

Usage

From source file:edu.scripps.fl.pubchem.web.session.PCDepositionSystemSession.java

License:Apache License

/**
 * // www  .  j  a  v a 2 s. c  o  m
 * Logs into your PubChem Deposition System account 
 * 
 * @param username
 * @param password
 * @throws Exception
 */
public void login(String username, String password) throws Exception {
    Document doc = getDocument("http://" + SITE + "/deposit/deposit.cgi");

    Node formNode = doc.selectSingleNode("//form[@name='deplogform']");
    FormControlVisitorSupport fcvs = new FormControlVisitorSupport();
    formNode.accept(fcvs);

    Set<String> set = new HashSet<String>(parameters);
    set.add("login");
    set.add("password");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    for (Map.Entry<String, String> entry : fcvs.getFormParameters().entrySet()) {
        if (!set.contains(entry.getKey()))
            params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }
    addParameters(params, "wcev:src", "logmgr", "wcev:name", "login", "wcev:data", "true", "wc:scrollx", 0,
            "wc:scrolly", 0, "login", username, "password", password);

    String page = postPage("http://" + SITE + "/deposit/deposit.cgi",
            new UrlEncodedFormEntity(params, HTTP.UTF_8));
    if (page.contains("Error while Performing an Action"))
        throw new Exception("PubChem Login Failure: Error while Performing an Action");
}

From source file:edu.scripps.fl.pubchem.web.session.PCWebSession.java

License:Apache License

/**
 * //from   w  w  w.ja v a2  s .  c  o m
 * Returns counts of active, all, inactive, inconclusive and probe
 * substances in the aid.
 * 
 * @param aid
 * @return OutComeCount
 * @throws Exception
 */
public PCOutcomeCounts getSubstanceOutcomeCounts(int aid) throws Exception {
    PCOutcomeCounts oc = null;
    Document doc = getDocument("http://" + SITE + "/assay/assay.cgi?aid=" + aid);
    Node node = doc.selectSingleNode("//div[@id='uptsub']");
    //      Node node = doc.selectSingleNode("//b[. = 'Links:']");
    //Node node = doc.selectSingleNode("//b[. = 'Substances: ']");
    if (node != null) {
        node = node.getParent();
        AppendingVisitorSupport visitor = new AppendingVisitorSupport();
        node.accept(visitor);
        String text = visitor.getText();
        Pattern sectionPattern = Pattern.compile("Tested Substances(.+)", Pattern.DOTALL | Pattern.MULTILINE);
        Matcher matcher = sectionPattern.matcher(text);
        Boolean found = matcher.find();
        if (found) {
            text = matcher.group(1);
            Pattern countPattern;
            if (text.contains("("))
                countPattern = Pattern.compile("([\\w]+)\\((\\d+)\\)");
            else
                countPattern = Pattern.compile("([\\w]+):\\s+(\\d+)");

            matcher = countPattern.matcher(text);
            oc = new PCOutcomeCounts();
            while (matcher.find()) {
                String outcome = matcher.group(1);
                int count = Integer.parseInt(matcher.group(2));
                if ("All".equalsIgnoreCase(outcome))
                    oc.all = count;
                else if (outcome.contains("All"))
                    oc.all = count;
                else if ("Active".equalsIgnoreCase(outcome))
                    oc.active = count;
                else if ("Inactive".equalsIgnoreCase(outcome))
                    oc.inactive = count;
                else if ("Inconclusive".equalsIgnoreCase(outcome))
                    oc.inconclusive = count;
                else if ("Probe".equalsIgnoreCase(outcome))
                    oc.probe = count;
            }
        }
    }
    return oc;
}

From source file:edu.scripps.fl.pubchem.web.session.WebSessionBase.java

License:Apache License

protected void addFormParts(Node formNode, MultipartEntity entity, Set<String> ignore)
        throws UnsupportedEncodingException {
    FormControlVisitorSupport fcvs = new FormControlVisitorSupport();
    formNode.accept(fcvs);
    for (Map.Entry<String, String> entry : fcvs.getFormParameters().entrySet()) {
        if (!ignore.contains(entry.getKey()))
            entity.addPart(entry.getKey(), new StringBody(entry.getValue()));
    }//from ww  w .j  a  v  a  2  s.  c  o m
}

From source file:io.mashin.oep.hpdl.XMLReadUtils.java

License:Open Source License

public static Node schemaVersionParentNode(Node node) {
    Node[] parentNode = new Node[1];
    node.accept(new VisitorSupport() {
        @Override/*from   w  w w. j a va  2 s . com*/
        public void visit(Attribute node) {
            if (node.getName().equalsIgnoreCase(XMLUtils.SCHEMA_VERSION_TAG)) {
                parentNode[0] = node.getParent();
            }
        }
    });
    return parentNode[0];
}

From source file:org.orbeon.oxf.xforms.submission.XFormsSubmissionUtils.java

License:Open Source License

/**
 * Check whether an XML sub-tree satisfies validity and required MIPs.
 *
 * @param indentedLogger        logger/*from   ww  w.  j a v a  2 s  .  c  o m*/
 * @param startNode             node to check
 * @param recurse               whether to recurse into attributes and descendant nodes
 * @return                      true iif the sub-tree passes the checks
 */
public static boolean isSatisfiesValid(final IndentedLogger indentedLogger, final Node startNode,
        boolean recurse) {

    if (recurse) {
        // Recurse into attributes and descendant nodes
        final boolean[] instanceSatisfiesValidRequired = new boolean[] { true };
        startNode.accept(new VisitorSupport() {

            public final void visit(Element element) {
                final boolean valid = checkInstanceData(element);

                instanceSatisfiesValidRequired[0] &= valid;

                if (!valid && indentedLogger.isDebugEnabled()) {
                    indentedLogger.logDebug("", "found invalid element", "element name",
                            Dom4jUtils.elementToDebugString(element));
                }
            }

            public final void visit(Attribute attribute) {
                final boolean valid = checkInstanceData(attribute);

                instanceSatisfiesValidRequired[0] &= valid;

                if (!valid && indentedLogger.isDebugEnabled()) {
                    indentedLogger.logDebug("", "found invalid attribute", "attribute name",
                            Dom4jUtils.attributeToDebugString(attribute), "parent element",
                            Dom4jUtils.elementToDebugString(attribute.getParent()));
                }
            }

            private boolean checkInstanceData(Node node) {
                return InstanceData.getValid(node);
            }
        });
        return instanceSatisfiesValidRequired[0];
    } else {
        // Just check the current node
        return InstanceData.getValid(startNode);
    }
}

From source file:org.orbeon.oxf.xml.dom4j.Dom4jUtils.java

License:Open Source License

/**
 * Go over the Node and its children and make sure that there are no two contiguous text nodes so as to ensure that
 * XPath expressions run correctly. As per XPath 1.0 (http://www.w3.org/TR/xpath):
 *
 * "As much character data as possible is grouped into each text node: a text node never has an immediately
 * following or preceding sibling that is a text node."
 *
 * dom4j Text and CDATA nodes are combined together.
 *
 * @param nodeToNormalize Node hierarchy to normalize
 * @return                the input node, normalized
 *//*w ww  . j a v  a  2 s . com*/
public static Node normalizeTextNodes(Node nodeToNormalize) {
    final List<Node> nodesToDetach = new ArrayList<Node>();
    nodeToNormalize.accept(new VisitorSupport() {
        public void visit(Element element) {
            final List children = element.content();
            Node previousNode = null;
            StringBuilder sb = null;
            for (Iterator i = children.iterator(); i.hasNext();) {
                final Node currentNode = (Node) i.next();
                if (previousNode != null) {
                    if (isTextOrCDATA(previousNode) && isTextOrCDATA(currentNode)) {
                        final CharacterData previousNodeText = (CharacterData) previousNode;
                        if (sb == null)
                            sb = new StringBuilder(previousNodeText.getText());
                        sb.append(currentNode.getText());
                        nodesToDetach.add(currentNode);
                    } else if (isTextOrCDATA(previousNode)) {
                        // Update node if needed
                        if (sb != null) {
                            previousNode.setText(sb.toString());
                        }
                        previousNode = currentNode;
                        sb = null;
                    } else {
                        previousNode = currentNode;
                        sb = null;
                    }
                } else {
                    previousNode = currentNode;
                    sb = null;
                }
            }
            // Update node if needed
            if (previousNode != null && sb != null) {
                previousNode.setText(sb.toString());
            }
        }
    });
    // Detach nodes only in the end so as to not confuse the acceptor above
    for (final Node currentNode : nodesToDetach) {
        currentNode.detach();
    }

    return nodeToNormalize;
}