Java Code Examples for org.w3c.dom.ls.LSParser#setFilter()

The following examples show how to use org.w3c.dom.ls.LSParser#setFilter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringBeanService.java    From citrus-admin with Apache License 2.0 6 votes vote down vote up
/**
 * Finds bean definition element by id and type in Spring application context and
 * performs unmarshalling in order to return JaxB object.
 * @param project
 * @param id
 * @param type
 * @return
 */
public <T> T getBeanDefinition(File configFile, Project project, String id, Class<T> type) {
    LSParser parser = XMLUtils.createLSParser();

    GetSpringBeanFilter filter = new GetSpringBeanFilter(id, type);
    parser.setFilter(filter);

    List<File> configFiles = new ArrayList<>();
    configFiles.add(configFile);
    configFiles.addAll(getConfigImports(configFile, project));

    for (File file : configFiles) {
        parser.parseURI(file.toURI().toString());

        if (filter.getBeanDefinition() != null) {
            return createJaxbObjectFromElement(filter.getBeanDefinition());
        }
    }

    return null;
}
 
Example 2
Source File: SpringBeanService.java    From citrus-admin with Apache License 2.0 6 votes vote down vote up
/**
 * Finds all bean definition elements by type and attribute values in Spring application context and
 * performs unmarshalling in order to return a list of JaxB object.
 * @param project
 * @param type
 * @param attributes
 * @return
 */
public <T> List<T> getBeanDefinitions(File configFile, Project project, Class<T> type, Map<String, String> attributes) {
    List<T> beanDefinitions = new ArrayList<T>();

    List<File> importedFiles = getConfigImports(configFile, project);
    for (File importLocation : importedFiles) {
        beanDefinitions.addAll(getBeanDefinitions(importLocation, project, type, attributes));
    }

    LSParser parser = XMLUtils.createLSParser();

    GetSpringBeansFilter filter = new GetSpringBeansFilter(type, attributes);
    parser.setFilter(filter);
    parser.parseURI(configFile.toURI().toString());

    for (Element element : filter.getBeanDefinitions()) {
        beanDefinitions.add(createJaxbObjectFromElement(element));
    }

    return beanDefinitions;
}
 
Example 3
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning
 * with state, input and output values orientation
 * for public Document parse(LSInput is),
 * <br><b>pre-conditions</b>: set filter that REJECTs any CHILD* node,
 * <br><b>is</b>: xml1
 * <br><b>output</b>: XML document with ELEMNENT1 and ELEMENT2 only.
 */
@Test
public void testfilter0001() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            if (enode.getNodeName().startsWith("CHILD")) {
                return FILTER_REJECT;
            }
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<?xml version=\"1.0\"?><ROOT><ELEMENT1></ELEMENT1><ELEMENT2>test1</ELEMENT2></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }

    System.out.println("OKAY");
}
 
Example 4
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 node, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: XML document with CHILD1 and ELEMENT2 only.
 */
@Test
public void testFilter0002() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            if (enode.getNodeName().startsWith("ELEMENT1")) {
                return FILTER_SKIP;
            }
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<?xml version=\"1.0\"?><ROOT><CHILD1/><CHILD1><COC1/></CHILD1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }
    System.out.println("OKAY");
}
 
Example 5
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 node, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: XML document with ELEMENT1 only.
 */
@Test
public void testFilter0003() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            if (enode.getNodeName().startsWith("ELEMENT2")) {
                return FILTER_INTERRUPT;
            }
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }
    System.out.println("OKAY");
}
 
Example 6
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that accepts all, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: full XML document.
 */
@Test
public void testFilter0004() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }
    System.out.println("OKAY");
}
 
Example 7
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that REJECTs all, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: empty XML document.
 */
@Test
public void testFilter0005() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            return FILTER_REJECT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    Document doc = parser.parse(getXmlSource(xml1));
    NodeList children = doc.getDocumentElement().getChildNodes();
    if (children.getLength() != 0) {
        Assert.fail("Not all children skipped");
    }
    System.out.println("OKAY");
}
 
Example 8
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that SKIPs all, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: empty XML document.
 */
@Test
public void testFilter0006() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            return FILTER_SKIP;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    Document doc = parser.parse(getXmlSource(xml1));
    NodeList children = doc.getDocumentElement().getChildNodes();
    if (children.getLength() != 0) {
        Assert.fail("Not all children skipped");
    }
    System.out.println("OKAY");
}
 
Example 9
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that REJECTs any CHILD* start element, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: XML document with ELEMENT1 and ELEMENT2 only.
 */
@Test
public void testFilter0007() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            if (elt.getTagName().startsWith("CHILD")) {
                return FILTER_REJECT;
            }
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<?xml version=\"1.0\"?><ROOT><ELEMENT1></ELEMENT1><ELEMENT2>test1</ELEMENT2></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }
    System.out.println("OKAY");
}
 
Example 10
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 start element, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: XML document with CHILD1 and ELEMENT2 only.
 */
@Test
public void testFilter0008() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            if (elt.getTagName().equals("ELEMENT1")) {
                return FILTER_SKIP;
            }
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<?xml version=\"1.0\"?><ROOT><CHILD1/><CHILD1><COC1/></CHILD1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }
    System.out.println("OKAY");
}
 
Example 11
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 start element, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: XML document with ELEMENT1 only.
 */
@Test
public void testFilter0009() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser!");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            if (elt.getTagName().startsWith("ELEMENT2")) {
                return FILTER_INTERRUPT;
            }
            return FILTER_ACCEPT;
        }

        public short acceptNode(Node enode) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    String expected = "<ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1></ROOT>";
    Document doc = parser.parse(getXmlSource(xml1));
    if (!match(expected, doc)) {
        Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
    }
    System.out.println("OKAY");
}
 
Example 12
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that REJECTs all start element, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: empty XML document.
 */
@Test
public void testFilter0010() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_REJECT;
        }

        public short acceptNode(Node enode) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    Document doc = parser.parse(getXmlSource(xml1));
    NodeList children = doc.getDocumentElement().getChildNodes();
    if (children.getLength() != 0) {
        Assert.fail("Not all children skipped");
    }
    System.out.println("OKAY");
}
 
Example 13
Source File: LSParserTCKTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state, input and output values
 * orientation for public Document parse(LSInput is), <br>
 * <b>pre-conditions</b>: set filter that SKIPs all, <br>
 * <b>is</b>: xml1 <br>
 * <b>output</b>: empty XML document.
 */
@Test
public void testFilter0011() {
    LSParser parser = createLSParser();
    if (parser == null) {
        Assert.fail("Unable to create LSParser");
    }
    // set filter
    parser.setFilter(new LSParserFilter() {
        public short startElement(Element elt) {
            return FILTER_SKIP;
        }

        public short acceptNode(Node enode) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return NodeFilter.SHOW_ALL;
        }
    });
    Document doc = parser.parse(getXmlSource(xml1));
    NodeList children = doc.getDocumentElement().getChildNodes();
    if (children.getLength() != 1) {
        Assert.fail("Not all Element nodes skipped");
    }
    System.out.println("OKAY");
}
 
Example 14
Source File: SpringBeanService.java    From citrus-admin with Apache License 2.0 5 votes vote down vote up
/**
 * Reads file import locations from Spring bean application context.
 * @param project
 * @return
 */
public List<File> getConfigImports(File configFile, Project project) {
    LSParser parser = XMLUtils.createLSParser();

    GetSpringImportsFilter filter = new GetSpringImportsFilter(configFile);
    parser.setFilter(filter);
    parser.parseURI(configFile.toURI().toString());

    return filter.getImportedFiles();
}
 
Example 15
Source File: DOML3InputSourceFactoryImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public InputSource newInputSource(String filename) throws Exception {
    // Create DOMImplementationLS, and DOM L3 LSParser
    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
    DocumentBuilder bldr = fact.newDocumentBuilder();
    DOMImplementationLS impl = (DOMImplementationLS) bldr.getDOMImplementation();
    LSParser domparser = impl.createLSParser(MODE_SYNCHRONOUS, null);
    domparser.setFilter(new MyDOMBuilderFilter());

    // Parse the xml document to create the DOM Document using
    // the DOM L3 LSParser and a LSInput (formerly LSInputSource)
    Document doc = null;
    LSInput src = impl.createLSInput();
    // register the input file with the input source...
    String systemId = filenameToURL(filename);
    src.setSystemId(systemId);
    try (Reader reader = new FileReader(filename)) {
        src.setCharacterStream(reader);
        src.setEncoding("UTF-8");
        doc = domparser.parse(src);
    }

    // Use DOM L3 LSSerializer (previously called a DOMWriter)
    // to serialize the xml doc DOM to a file stream.
    String tmpCatalog = Files.createTempFile(Paths.get(USER_DIR), "catalog.xml", null).toString();

    LSSerializer domserializer = impl.createLSSerializer();
    domserializer.setFilter(new MyDOMWriterFilter());
    domserializer.getNewLine();
    DOMConfiguration config = domserializer.getDomConfig();
    config.setParameter("xml-declaration", Boolean.TRUE);
    String result = domserializer.writeToString(doc);
    try (FileWriter os = new FileWriter(tmpCatalog, false)) {
        os.write(result);
        os.flush();
    }

    // Return the Input Source created from the Serialized DOM L3 Document.
    InputSource catsrc = new InputSource(new InputStreamReader(new ByteArrayInputStream(Files.readAllBytes(Paths.get(tmpCatalog)))));
    catsrc.setSystemId(systemId);
    return catsrc;
}