Example usage for org.w3c.dom.traversal NodeFilter FILTER_ACCEPT

List of usage examples for org.w3c.dom.traversal NodeFilter FILTER_ACCEPT

Introduction

In this page you can find the example usage for org.w3c.dom.traversal NodeFilter FILTER_ACCEPT.

Prototype

short FILTER_ACCEPT

To view the source code for org.w3c.dom.traversal NodeFilter FILTER_ACCEPT.

Click Source Link

Document

Accept the node.

Usage

From source file:DOMTreeWalkerTreeModel.java

/**
 * This main() method demonstrates the use of this class, the use of the
 * Xerces DOM parser, and the creation of a DOM Level 2 TreeWalker object.
 *///from   w w  w  . j a  va2 s .c o m
public static void main(String[] args) throws IOException, SAXException {
    // Obtain an instance of a Xerces parser to build a DOM tree.
    // Note that we are not using the JAXP API here, so this
    // code uses Apache Xerces APIs that are not standards
    DOMParser parser = new org.apache.xerces.parsers.DOMParser();

    // Get a java.io.Reader for the input XML file and
    // wrap the input file in a SAX input source
    Reader in = new BufferedReader(new FileReader(args[0]));
    InputSource input = new org.xml.sax.InputSource(in);

    // Tell the Xerces parser to parse the input source
    parser.parse(input);

    // Ask the parser to give us our DOM Document. Once we've got the DOM
    // tree, we don't have to use the Apache Xerces APIs any more; from
    // here on, we use the standard DOM APIs
    Document document = parser.getDocument();

    // If we're using a DOM Level 2 implementation, then our Document
    // object ought to implement DocumentTraversal
    DocumentTraversal traversal = (DocumentTraversal) document;

    // For this demonstration, we create a NodeFilter that filters out
    // Text nodes containing only space; these just clutter up the tree
    NodeFilter filter = new NodeFilter() {
        public short acceptNode(Node n) {
            if (n.getNodeType() == Node.TEXT_NODE) {
                // Use trim() to strip off leading and trailing space.
                // If nothing is left, then reject the node
                if (((Text) n).getData().trim().length() == 0)
                    return NodeFilter.FILTER_REJECT;
            }
            return NodeFilter.FILTER_ACCEPT;
        }
    };

    // This set of flags says to "show" all node types except comments
    int whatToShow = NodeFilter.SHOW_ALL & ~NodeFilter.SHOW_COMMENT;

    // Create a TreeWalker using the filter and the flags
    TreeWalker walker = traversal.createTreeWalker(document, whatToShow, filter, false);

    // Instantiate a TreeModel and a JTree to display it
    JTree tree = new JTree(new DOMTreeWalkerTreeModel(walker));

    // Create a frame and a scrollpane to display the tree, and pop them up
    JFrame frame = new JFrame("DOMTreeWalkerTreeModel Demo");
    frame.getContentPane().add(new JScrollPane(tree));
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    frame.setSize(500, 250);
    frame.setVisible(true);
}

From source file:com.consol.citrus.admin.service.spring.filter.AddSpringBeanFilter.java

/**
 * {@inheritDoc}/*from ww w  .  ja  v  a  2  s  .  c  om*/
 */
public short accept(Element element) {
    if (DomUtils.nodeNameEquals(element, "beans")) {
        element.appendChild(element.getOwnerDocument().createTextNode("\n    ")); //TODO make indentation configurable
        element.appendChild(element.getOwnerDocument().importNode(beanDefinition, true));
    }

    return NodeFilter.FILTER_ACCEPT;
}

From source file:com.consol.citrus.admin.service.spring.filter.GetSpringBeanFilter.java

/**
 * {@inheritDoc}/*w  w w.j a v  a  2s.  co  m*/
 */
public short accept(Element element) {
    if (DomUtils.nodeNameEquals(element, elementName) && isEqualByNamespace(element, elementNamespace)
            && (isEqualById(element, id) || isEqualByBeanName(element, id))) {
        beanDefinition = element;
    }

    return NodeFilter.FILTER_ACCEPT;
}

From source file:com.consol.citrus.admin.service.spring.filter.GetSpringBeansFilter.java

/**
 * {@inheritDoc}//from   w  w  w  .ja  va  2 s. c o  m
 */
public short accept(Element element) {
    if (DomUtils.nodeNameEquals(element, elementName) && isEqualByNamespace(element, elementNamespace)
            && isEqualByBeanAttributes(element, attributes)) {
        beanDefinitions.add(element);
    }

    return NodeFilter.FILTER_ACCEPT;
}

From source file:com.consol.citrus.admin.service.spring.filter.GetSpringImportsFilter.java

@Override
public short startElement(Element element) {
    if (DomUtils.nodeNameEquals(element, "import")) {
        String resourceLocation = element.getAttribute("resource");

        if (StringUtils.hasText(resourceLocation)) {
            if (resourceLocation.startsWith("classpath:")) {
                resourceLocation = resourceLocation.substring("classpath:".length());
            } else if (resourceLocation.startsWith("file:")) {
                resourceLocation = resourceLocation.substring("file:".length());
            }// ww  w.ja  va2s. co m

            try {
                File importedFile = new FileSystemResource(
                        parentConfigFile.getParentFile().getCanonicalPath() + File.separator + resourceLocation)
                                .getFile();

                if (importedFile.exists()) {
                    importedFiles.add(importedFile);
                }
            } catch (IOException e) {
                log.warn("Unable to resolve imported file resource location", e);
            }
        }
    }

    return NodeFilter.FILTER_ACCEPT;
}

From source file:com.consol.citrus.admin.service.spring.filter.GetSpringImportsFilter.java

@Override
public short acceptNode(Node node) {
    return NodeFilter.FILTER_ACCEPT;
}

From source file:DOM3.java

/**
 * @see org.w3c.dom.ls.LSParserFilter#acceptNode(Node)
 */
public short acceptNode(Node enode) {
    return NodeFilter.FILTER_ACCEPT;
}

From source file:DomPrintUtil.java

private Node getVisibleNextSibling(Node target, Node root) {
    if (target == root) {
        return null;
    }/* w  w w . ja v a2 s . c om*/
    Node tmpN = target.getNextSibling();
    if (null == tmpN) {
        Node tmpP = target.getParentNode();
        if (eval(tmpP) == NodeFilter.FILTER_SKIP) {
            return getVisibleNextSibling(tmpP, root);
        }
        return null;
    }
    switch (eval(tmpN)) {
    case NodeFilter.FILTER_ACCEPT:
        return tmpN;
    case NodeFilter.FILTER_SKIP:
        Node tmpC = getVisibleFirstChild(tmpN);
        if (null != tmpC) {
            return tmpC;
        }
        // case NodeFilter.FILTER_REJECT:
    default:
        return getVisibleNextSibling(tmpN, root);
    }
}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * copies all attributes from one Element to another
 *
 * @param from   - the Element which the source attributes
 * @param to     - the target Element for the Attributes
 * @param filter - a NodeFilter to apply during copy
 */// w  w  w.  ja  va 2s  .co m
public static void copyAttributes(Element from, Element to, NodeFilter filter) {
    if ((from != null) && (to != null)) {
        NamedNodeMap map = from.getAttributes();

        /* if filter is null use our own default filter, which accepts
           everything (this saves us from always check if filter is
           null */
        if (filter == null) {
            filter = new NodeFilter() {
                public short acceptNode(Node n) {
                    return NodeFilter.FILTER_ACCEPT;
                }
            };
        }

        if (map != null) {
            int len = map.getLength();

            for (int i = 0; i < len; i++) {
                Node attr = map.item(i);

                if (attr.getNodeType() == Node.ATTRIBUTE_NODE) {
                    if (filter.acceptNode(attr) == NodeFilter.FILTER_ACCEPT) {
                        to.setAttributeNS(attr.getNamespaceURI(), attr.getNodeName(), attr.getNodeValue());
                    }
                }
            }
        }
    }
}

From source file:DomPrintUtil.java

private Node getVisiblePreviousSibling(Node target, Node root) {
    if (target == root) {
        return null;
    }/*from   ww w.  j  ava 2 s.  co m*/
    Node tmpN = target.getPreviousSibling();
    if (null == tmpN) {
        Node tmpP = target.getParentNode();
        if (eval(tmpP) == NodeFilter.FILTER_SKIP) {
            return getVisiblePreviousSibling(tmpP, root);
        }
        return null;
    }
    switch (eval(tmpN)) {
    case NodeFilter.FILTER_ACCEPT:
        return tmpN;
    case NodeFilter.FILTER_SKIP:
        Node tmpC = getVisibleLastChild(tmpN);
        if (null != tmpC) {
            return tmpC;
        }
        // case NodeFilter.FILTER_REJECT:
    default:
        return getVisiblePreviousSibling(tmpN, root);
    }
}