Example usage for javax.xml.stream StreamFilter StreamFilter

List of usage examples for javax.xml.stream StreamFilter StreamFilter

Introduction

In this page you can find the example usage for javax.xml.stream StreamFilter StreamFilter.

Prototype

StreamFilter

Source Link

Usage

From source file:org.eclipse.koneki.protocols.omadm.client.basic.DMBasicSession.java

private void readMessage(final InputStream in) throws XMLStreamException {
    final XMLStreamReader reader = this.dmClient.createXMLStreamReader(in, ENCODING, new StreamFilter() {

        @Override/*  w  w  w  .j av  a2  s  .  c om*/
        public boolean accept(final XMLStreamReader reader) {
            return !reader.isWhiteSpace() && !reader.isStandalone();
        }

    });

    jumpToStartTag(reader, "SyncHdr"); //$NON-NLS-1$

    readSyncHdr(reader);
    reader.nextTag();

    readSyncBody(reader);
    reader.nextTag();

    reader.close();
}

From source file:org.jboss.rusheye.parser.Parser.java

private void parseFile(File file, boolean tmpfile) {
    VisualSuite visualSuite = null;/*from  w  w  w.  j  a  v a2s  .com*/
    try {
        XMLValidationSchemaFactory schemaFactory = XMLValidationSchemaFactory
                .newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA);
        URL schemaURL = getClass().getClassLoader().getResource("org/jboss/rusheye/visual-suite.xsd");
        XMLValidationSchema schema = schemaFactory.createSchema(schemaURL);

        XMLInputFactory2 factory = (XMLInputFactory2) XMLInputFactory.newInstance();

        StreamFilter filter = new StreamFilter() {
            @Override
            public boolean accept(XMLStreamReader reader) {
                return reader.isStartElement();
            }
        };

        XMLStreamReader2 reader = factory.createXMLStreamReader(file);
        XMLStreamReader2 filteredReader = new Stax2FilteredStreamReader(reader, filter);

        reader.validateAgainst(schema);

        // EventFilter filter = new EventFilter() {
        // @Override
        // public boolean accept(XMLEvent reader) {
        // return reader.isStartElement();
        // }
        // };
        //
        // XMLEventReader reader = factory.createXMLEventReader(file);
        // XMLEventReader filteredReader = factory.createFilteredReader(reader, filter);

        JAXBContext ctx = JAXBContext.newInstance(VisualSuite.class.getPackage().getName());
        Unmarshaller um = ctx.createUnmarshaller();

        UnmarshallerMultiListener listener = new UnmarshallerMultiListener();
        um.setListener(listener);

        // skip parsing of the first element - visual-suite
        filteredReader.nextTag();

        visualSuite = new VisualSuite();
        handler.setVisualSuite(visualSuite);
        handler.getContext().invokeListeners().onSuiteStarted(visualSuite);

        listener.registerListener(new UniqueIdentityChecker(handler.getContext()));

        while (filteredReader.hasNext()) {
            try {
                // go on the start of the next tag
                filteredReader.nextTag();

                Object o = um.unmarshal(reader);
                if (o instanceof GlobalConfiguration) {
                    GlobalConfiguration globalConfiguration = (GlobalConfiguration) o;
                    handler.getContext().setCurrentConfiguration(globalConfiguration);
                    visualSuite.setGlobalConfiguration(globalConfiguration);
                    handler.getContext().invokeListeners().onConfigurationReady(visualSuite);

                    RetriverInjector retriverInjector = new RetriverInjector(this);
                    for (Mask mask : globalConfiguration.getMasks()) {
                        retriverInjector.afterUnmarshal(mask, null);
                    }
                    listener.registerListener(retriverInjector);
                }
                if (o instanceof Test) {
                    Test test = (Test) o;
                    handler.getContext().setCurrentConfiguration(test);
                    handler.getContext().setCurrentTest(test);
                    for (Pattern pattern : test.getPatterns()) {
                        handler.getContext().invokeListeners().onPatternReady(test, pattern);
                    }
                    Test testWrapped = ConfigurationCompiler.wrap(test, visualSuite.getGlobalConfiguration());
                    handler.getContext().invokeListeners().onTestReady(testWrapped);
                }
            } catch (WstxParsingException e) {
                // intentionally blank - wrong end of document detection
            }

        }
    } catch (XMLStreamException e) {
        throw handleParsingException(e, e);
    } catch (JAXBException e) {
        throw handleParsingException(e, e.getLinkedException());
    } finally {
        if (visualSuite != null && handler.getContext() != null) {
            handler.getContext().invokeListeners().onSuiteReady(visualSuite);
        }
        if (tmpfile) {
            FileUtils.deleteQuietly(file);
        }
    }
}