Package org.apache.excalibur.xml.xpath

Examples of org.apache.excalibur.xml.xpath.XPathProcessor


            if (!"".equals(select)) {

               
                DOMParser parser = null;
                XPathProcessor processor = null;
               
                try {
                    parser = (DOMParser)this.manager.lookup(DOMParser.ROLE);
                    processor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);

                    InputSource input = SourceUtil.getInputSource(source);

                    Document document = parser.parseDocument(input);
                    NodeList list = processor.selectNodeList(document, select);
                    int length = list.getLength();
                    for (int i=0; i<length; i++) {
                          IncludeXMLConsumer.includeNode((Node)list.item(i),
                                               (ContentHandler)this, 
                                               (LexicalHandler)this);
View Full Code Here


                    this.manager.release((Component)parser);
            }

            if (doc != null) {

                XPathProcessor processor = null;
                try {
                    processor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE);

                    NodeList nodelist = processor.selectNodeList(doc.getDocumentElement(), this.xpath);

                    SourceProperty property = new SourceProperty(this.propertynamespace, this.propertyname);
                    property.setValue(nodelist);

                    return property;
View Full Code Here

                    this.manager.release(parser);
                }
            }

            if (doc != null) {
                XPathProcessor processor = null;
                try {
                    processor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE);
                    NodeList nodelist = processor.selectNodeList(doc.getDocumentElement(), m_xpath);
                    SourceProperty property = new SourceProperty(m_namespace, m_propertyname);
                    property.setValue(nodelist);
                    return property;
                } catch (ServiceException se) {
                    this.getLogger().error("Could not retrieve component", se);
View Full Code Here

                    Document document = SourceUtil.toDOM(source);
                    Element element = document.getDocumentElement();

                    String xpath = bindingElm.getAttribute("xpath");
                    if (!xpath.equals("")) {
                        XPathProcessor xpathProcessor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE);
                        try {
                            Node node = xpathProcessor.selectSingleNode(document, xpath);
                            if (node == null)
                                throw new BindingException("XPath expression \"" + xpath + "\" didn't return a result.");
                            if (!(node instanceof Element))
                                throw new BindingException("XPath expression \"" + xpath + "\" did not return an element node.");
                            element = (Element)node;
View Full Code Here

        super(name);
    }

    public void testXPath() throws Exception {
        DOMParser parser = null;
        XPathProcessor processor = null;
        try {
            parser = (DOMParser)manager.lookup(DOMParser.ROLE);
            processor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE);

            Document document1 = parser.parseDocument(new InputSource(new StringReader(CONTENT1)));
            Document document2 = parser.parseDocument(new InputSource(new StringReader(CONTENT2)));

            // 1. Test single node expression
            String expr = "/test:root/test:element1";
            Node node = processor.selectSingleNode(document1, expr);
            assertNotNull("Must select <test:element1/> node, but got null", node);
            assertEquals("Must select <test:element1/> node", Node.ELEMENT_NODE, node.getNodeType());
            assertEquals("Must select <test:element1/> node", "element1", node.getLocalName());

            // 2. Test single node expression with no expected result
            expr = "/test:root/test:element3";
            node = processor.selectSingleNode(document1, expr);
            assertNull("Must be null", node);

            // 3. Test multiple node expression
            expr = "/test:root/test:*";
            NodeList list = processor.selectNodeList(document1, expr);
            assertNotNull("Must select two nodes, but got null", list);
            assertEquals("Must select two nodes", 2, list.getLength());
            assertEquals("Must select <test:element1/> node", "element1", list.item(0).getLocalName());
            assertEquals("Must select <test:element2/> node", "element2", list.item(1).getLocalName());

            // 4. Test with a namespace prefix configured in the component configuration
            expr = "count(/test:root/*)";
            Number number = processor.evaluateAsNumber(document1, expr);
            assertEquals(2, number.intValue());

            // 5. Test with a custom prefix resolver using a different document in a different namespace,
            // to be sure the custom prefix resolver is used
            number = processor.evaluateAsNumber(document2, expr, new PrefixResolver() {
                public String prefixToNamespace(String prefix)
                {
                    if (prefix.equals("test"))
                        return "http://localhost/test2";
                    return null;
                }
            });
            assertEquals(2, number.intValue());

            // 6. Test boolean
            expr = "count(/test:root/*) = 2";
            boolean bool = processor.evaluateAsBoolean(document1, expr);
            assertEquals(true, bool);

            // 7. Test expression in the root element context
            expr = "/test:root/test:element1";
            node = processor.selectSingleNode(document1.getDocumentElement(), expr);
            assertNotNull("Must select <test:element1/> node, but got null", node);
            assertEquals("Must select <test:element1/> node", Node.ELEMENT_NODE, node.getNodeType());
            assertEquals("Must select <test:element1/> node", "element1", node.getLocalName());

            // 8. Test expression in the child node context
            node = processor.selectSingleNode(document1.getDocumentElement().getFirstChild(), expr);
            assertNotNull("Must select <test:element1/> node, but got null", node);
            assertEquals("Must select <test:element1/> node", Node.ELEMENT_NODE, node.getNodeType());
            assertEquals("Must select <test:element1/> node", "element1", node.getLocalName());

        } finally {
View Full Code Here

                    Document document = SourceUtil.toDOM(source);
                    Element element = document.getDocumentElement();

                    String xpath = bindingElm.getAttribute("xpath");
                    if (!xpath.equals("")) {
                        XPathProcessor xpathProcessor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE);
                        try {
                            Node node = xpathProcessor.selectSingleNode(document, xpath);
                            if (node == null)
                                throw new BindingException("XPath expression \"" + xpath + "\" didn't return a result.");
                            if (!(node instanceof Element))
                                throw new BindingException("XPath expression \"" + xpath + "\" did not return an element node.");
                            element = (Element)node;
View Full Code Here

            if (!"".equals(select)) {

               
                DOMParser parser = null;
                XPathProcessor processor = null;
               
                try {
                    parser = (DOMParser)this.manager.lookup(DOMParser.ROLE);
                    processor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);

                    InputSource input = SourceUtil.getInputSource(source);

                    Document document = parser.parseDocument(input);
                    NodeList list = processor.selectNodeList(document, select);
                    int length = list.getLength();
                    for (int i=0; i<length; i++) {
                          IncludeXMLConsumer.includeNode(list.item(i),
                                               this, 
                                               this);
View Full Code Here

                    this.manager.release(parser);
                }
            }

            if (doc != null) {
                XPathProcessor processor = null;
                try {
                    processor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE);
                    NodeList nodelist = processor.selectNodeList(doc.getDocumentElement(), m_xpath);
                    SourceProperty property = new SourceProperty(m_namespace, m_propertyname);
                    property.setValue(nodelist);
                    return property;
                } catch (ServiceException se) {
                    this.getLogger().error("Could not retrieve component", se);
View Full Code Here

                    Document document = SourceUtil.toDOM(source);
                    Element element = document.getDocumentElement();

                    String xpath = bindingElm.getAttribute("xpath");
                    if (!xpath.equals("")) {
                        XPathProcessor xpathProcessor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE);
                        try {
                            Node node = xpathProcessor.selectSingleNode(document, xpath);
                            if (node == null)
                                throw new BindingException("XPath expression \"" + xpath + "\" didn't return a result.");
                            if (!(node instanceof Element))
                                throw new BindingException("XPath expression \"" + xpath + "\" did not return an element node.");
                            element = (Element)node;
View Full Code Here

            if (!"".equals(select)) {

               
                DOMParser parser = null;
                XPathProcessor processor = null;
               
                try {
                    parser = (DOMParser)this.manager.lookup(DOMParser.ROLE);
                    processor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);

                    InputSource input = SourceUtil.getInputSource(source);

                    Document document = parser.parseDocument(input);
                    NodeList list = processor.selectNodeList(document, select);
                    int length = list.getLength();
                    for (int i=0; i<length; i++) {
                          IncludeXMLConsumer.includeNode((Node)list.item(i),
                                               (ContentHandler)this, 
                                               (LexicalHandler)this);
View Full Code Here

TOP

Related Classes of org.apache.excalibur.xml.xpath.XPathProcessor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.