Example usage for javax.xml.stream XMLInputFactory IS_NAMESPACE_AWARE

List of usage examples for javax.xml.stream XMLInputFactory IS_NAMESPACE_AWARE

Introduction

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

Prototype

String IS_NAMESPACE_AWARE

To view the source code for javax.xml.stream XMLInputFactory IS_NAMESPACE_AWARE.

Click Source Link

Document

The property used to turn on/off namespace support, this is to support XML 1.0 documents, only the true setting must be supported

Usage

From source file:com.github.lindenb.jvarkit.tools.blast.BlastFilterJS.java

@Override
protected Collection<Throwable> call(String inputName) throws Exception {

    final CompiledScript compiledScript;
    Unmarshaller unmarshaller;/*from  ww  w.j  a v  a 2 s  .  c o m*/
    Marshaller marshaller;
    try {
        compiledScript = super.compileJavascript();

        JAXBContext jc = JAXBContext.newInstance("gov.nih.nlm.ncbi.blast");

        unmarshaller = jc.createUnmarshaller();
        marshaller = jc.createMarshaller();

        XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
        xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
        xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
        xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        PrintWriter pw = openFileOrStdoutAsPrintWriter();
        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        xof.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
        XMLEventWriter w = xof.createXMLEventWriter(pw);

        StreamSource src = null;
        if (inputName == null) {
            LOG.info("Reading stdin");
            src = new StreamSource(stdin());
        } else {
            LOG.info("Reading file " + inputName);
            src = new StreamSource(new File(inputName));
        }

        XMLEventReader r = xmlInputFactory.createXMLEventReader(src);

        XMLEventFactory eventFactory = XMLEventFactory.newFactory();

        SimpleBindings bindings = new SimpleBindings();
        while (r.hasNext()) {
            XMLEvent evt = r.peek();
            switch (evt.getEventType()) {
            case XMLEvent.START_ELEMENT: {
                StartElement sE = evt.asStartElement();
                Hit hit = null;
                JAXBElement<Hit> jaxbElement = null;
                if (sE.getName().getLocalPart().equals("Hit")) {
                    jaxbElement = unmarshaller.unmarshal(r, Hit.class);
                    hit = jaxbElement.getValue();
                } else {
                    w.add(r.nextEvent());
                    break;
                }

                if (hit != null) {

                    bindings.put("hit", hit);

                    boolean accept = super.evalJavaScriptBoolean(compiledScript, bindings);

                    if (accept) {
                        marshaller.marshal(jaxbElement, w);
                        w.add(eventFactory.createCharacters("\n"));
                    }
                }

                break;
            }
            case XMLEvent.SPACE:
                break;
            default: {
                w.add(r.nextEvent());
                break;
            }
            }
            r.close();
        }
        w.flush();
        w.close();
        pw.flush();
        pw.close();
        return RETURN_OK;
    } catch (Exception err) {
        return wrapException(err);
    } finally {

    }
}

From source file:ValidateStax.java

private static XMLEventReader getXMLEventReader(String filename) {
    XMLInputFactory xmlif = null;
    XMLEventReader xmlr = null;//from   w w w.  j av  a2 s  . c  om
    try {
        xmlif = XMLInputFactory.newInstance();
        xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
        xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
        xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
        xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

        FileInputStream fis = new FileInputStream(filename);
        xmlr = xmlif.createXMLEventReader(filename, fis);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return xmlr;
}

From source file:StreamSrcStAXRst.java

private static XMLEventReader getXMLEventReader(String filename) {

    XMLInputFactory xmlif = null;
    XMLEventReader xmlr = null;/*from   www .j  a va2  s .c om*/
    try {
        xmlif = XMLInputFactory.newInstance();
        xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
        xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
        xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
        xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

        FileInputStream fis = new FileInputStream(filename);
        xmlr = xmlif.createXMLEventReader(filename, fis);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return xmlr;
}

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessor.java

/**
 * This constructor gets a new {@link XMLInputFactory} instance that is reused every time
 * {@link #process(com.autonomy.aci.client.transport.AciResponseInputStream)} is called, this
 * should be faster than creating a new instance every time this method is called.
 * <p>/*from w w  w.  ja va 2  s .  com*/
 * The properties are set to the following defaults if they are not specified as system properties:
 * <table summary="">
 * <tr><th>Property</th><th>Default</th></tr>
 * <tr><td>XMLInputFactory.IS_NAMESPACE_AWARE</td><td><tt>false</tt></td></tr>
 * <tr><td>XMLInputFactory.IS_VALIDATING<tt>false</tt></td></tr>
 * <tr><td>XMLInputFactory.IS_COALESCING<tt>false</tt></td></tr>
 * <tr><td>XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES<tt>true</tt></td></tr>
 * <tr><td>XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES<tt>false</tt></td></tr>
 * <tr><td>XMLInputFactory.SUPPORT_DTD<tt>true</tt></td></tr>
 * </table>
 */
protected AbstractStAXProcessor() {
    // See if the various XMLInputFactory properties are set as system properties...
    namespaceAware = BooleanUtils.toBoolean(
            StringUtils.defaultString(System.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE), "false"));
    validating = BooleanUtils
            .toBoolean(StringUtils.defaultString(System.getProperty(XMLInputFactory.IS_VALIDATING), "false"));
    coalescing = BooleanUtils
            .toBoolean(StringUtils.defaultString(System.getProperty(XMLInputFactory.IS_COALESCING), "false"));
    replacingEntityReferences = BooleanUtils.toBoolean(StringUtils
            .defaultString(System.getProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES), "true"));
    supportingExternalEntities = BooleanUtils.toBoolean(StringUtils
            .defaultString(System.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES), "false"));
    supportDtd = BooleanUtils
            .toBoolean(StringUtils.defaultString(System.getProperty(XMLInputFactory.SUPPORT_DTD), "true"));

    // Create the XMLStreamReader factory...
    xmlInputFactory = XMLInputFactory.newInstance();
}

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessor.java

/**
 * This method firstly checks that the content type of the response is text based and can be parsed. If so, it
 * converts the <tt>AciResponseInputStream</tt> into a StAX <tt>XMLStreamReader</tt> and calls the the {@link
 * #process(javax.xml.stream.XMLStreamReader)} method that should be implemented in a subclass to do all the work.
 * @param aciResponseInputStream The ACI response to process
 * @return An object of type <tt>T</tt>
 * @throws AciErrorException  If the ACI response was an error response
 * @throws ProcessorException If an error occurred during the processing of the IDOL response
 *///from   ww  w  . j a  v  a2 s.  c  om
public T process(final AciResponseInputStream aciResponseInputStream) {
    LOGGER.trace("process() called...");

    if (!"text/xml".equalsIgnoreCase(aciResponseInputStream.getContentType())) {
        throw new ProcessorException(
                "This processor is unable to process non-text ACI responses. The content type for this response is "
                        + aciResponseInputStream.getContentType());
    }

    // Define this here so we can make sure it's closed when the processor is finished...
    XMLStreamReader xmlStreamReader = null;

    try {
        // Update the factory with the various properties as they might have changed since the last run...
        xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, namespaceAware);
        xmlInputFactory.setProperty(XMLInputFactory.IS_VALIDATING, validating);
        xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, coalescing);
        xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, replacingEntityReferences);
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
                supportingExternalEntities);
        xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, supportDtd);

        // Convert the input stream..
        xmlStreamReader = xmlInputFactory.createXMLStreamReader(aciResponseInputStream);
        return process(xmlStreamReader);
    } catch (final XMLStreamException xmlse) {
        throw new ProcessorException("Unable to convert the InputStream to a XMLStreamReader", xmlse);
    } finally {
        if (xmlStreamReader != null) {
            try {
                // This does NOT close the underlying AciResponseInputStream
                xmlStreamReader.close();
            } catch (final XMLStreamException xmlse) {
                LOGGER.error("Unable to close the XMLStreamReader.", xmlse);
            }
        }
    }
}

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessorTest.java

@Test
public void testXMLInputFactorySystemProperties() throws NoSuchFieldException, IllegalAccessException {
    AbstractStAXProcessor<?> abstractStAXProcessor = spy(AbstractStAXProcessor.class);
    final XMLInputFactory mockXmlInputFactory = mock(XMLInputFactory.class);

    final Field field = ReflectionTestUtils.getAccessibleField(AbstractStAXProcessor.class, "xmlInputFactory");
    field.set(abstractStAXProcessor, mockXmlInputFactory);

    // Check the defaults...
    abstractStAXProcessor.process(when(mock(AciResponseInputStream.class).getContentType())
            .thenReturn("text/xml").<AciResponseInputStream>getMock());
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_VALIDATING, false);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_COALESCING, false);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.SUPPORT_DTD, true);

    // Set different values via system properties...
    System.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, "true");
    System.setProperty(XMLInputFactory.IS_VALIDATING, "true");
    System.setProperty(XMLInputFactory.IS_COALESCING, "true");
    System.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, "false");
    System.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, "true");
    System.setProperty(XMLInputFactory.SUPPORT_DTD, "false");

    // Create a new spy...
    abstractStAXProcessor = spy(AbstractStAXProcessor.class);
    field.set(abstractStAXProcessor, mockXmlInputFactory);

    // Check the values have changed when set...
    abstractStAXProcessor.process(when(mock(AciResponseInputStream.class).getContentType())
            .thenReturn("text/xml").<AciResponseInputStream>getMock());
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_VALIDATING, true);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_COALESCING, true);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, true);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.SUPPORT_DTD, false);
}

From source file:sapience.injectors.stax.inject.ModelBasedStaxStreamInjector.java

/**
 * If the reference is more then a simple attribute, we have to add new XML (subtree) to the stream. We transform
 * the reference into an InputStream and invoke another SAX parsing process for it. But the parsed events are added
 * to the main XMLEventWriter. //from   w w  w .  j av  a2  s. c om
 *
 * @param w
 * @param string
 * @throws XMLStreamException 
 * @throws XMLStreamException
 */
private void createEventsForElement(XMLEventWriter w, Reference ref) throws XMLStreamException {
    XMLEventReader r = null;
    try {
        StringBuilder target = new StringBuilder(ref.getTarget().toString());

        NamespaceContext c = w.getNamespaceContext();

        ByteArrayInputStream bais = new ByteArrayInputStream(target.toString().getBytes());
        getXMLInputFactory().setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
        r = getXMLInputFactory().createXMLEventReader(bais);

        while (r.hasNext()) {
            XMLEvent e = r.nextEvent();
            switch (e.getEventType()) {
            case XMLEvent.START_DOCUMENT:
                break;
            case XMLEvent.END_DOCUMENT:
                break;
            default:
                w.add(e);
                break;
            }
        }
    } finally {
        ;

        if (r != null)
            r.close();
    }

}

From source file:sapience.injectors.stax.inject.StringBasedStaxStreamInjector.java

/**
 * If the reference is more then a simple attribute, we have to add new XML (subtree) to the stream. We transform
 * the reference into an InputStream and invoke another SAX parsing process for it. But the parsed events are added
 * to the main XMLEventWriter. //from  w  w  w  . j a v a  2s.  c om
 *
 * @param w
 * @param string
 * @throws XMLStreamException 
 * @throws XMLStreamException
 */
private void createEventsForElement(XMLEventWriter w, Reference ref) throws XMLStreamException {
    XMLEventReader r = null;
    try {
        StringBuilder target = new StringBuilder(ref.getTarget().toString());

        NamespaceContext c = w.getNamespaceContext();

        // process namespaces
        //processNamespace(target, w.getNamespaceContext());

        ByteArrayInputStream bais = new ByteArrayInputStream(target.toString().getBytes());
        this.inFac.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
        r = this.inFac.createXMLEventReader(bais);
        // start a new line

        while (r.hasNext()) {
            XMLEvent e = r.nextEvent();
            switch (e.getEventType()) {
            case XMLEvent.START_DOCUMENT:
                break;
            case XMLEvent.END_DOCUMENT:
                break;
            default:
                w.add(e);
                break;
            }
        }
    } finally {
        ;

        if (r != null)
            r.close();
    }

}

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessorTest.java

@Test
public void testXMLInputFactoryPropertyAccessors() throws NoSuchFieldException, IllegalAccessException {
    // Check for the default values...
    final AbstractStAXProcessor<?> abstractStAXProcessor = spy(AbstractStAXProcessor.class);
    assertThat(abstractStAXProcessor.isNamespaceAware(), is(false));
    assertThat(abstractStAXProcessor.isValidating(), is(false));
    assertThat(abstractStAXProcessor.isCoalescing(), is(false));
    assertThat(abstractStAXProcessor.isReplacingEntityReferences(), is(true));
    assertThat(abstractStAXProcessor.isSupportingExternalEntities(), is(false));
    assertThat(abstractStAXProcessor.isSupportDtd(), is(true));

    // Set new values via the property accessors...
    abstractStAXProcessor.setNamespaceAware(true);
    abstractStAXProcessor.setValidating(true);
    abstractStAXProcessor.setCoalescing(true);
    abstractStAXProcessor.setReplacingEntityReferences(false);
    abstractStAXProcessor.setSupportingExternalEntities(true);
    abstractStAXProcessor.setSupportDtd(false);

    final XMLInputFactory mockXmlInputFactory = mock(XMLInputFactory.class);
    final Field field = ReflectionTestUtils.getAccessibleField(AbstractStAXProcessor.class, "xmlInputFactory");
    field.set(abstractStAXProcessor, mockXmlInputFactory);

    // Check the values have changed...
    abstractStAXProcessor.process(when(mock(AciResponseInputStream.class).getContentType())
            .thenReturn("text/xml").<AciResponseInputStream>getMock());
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_VALIDATING, true);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_COALESCING, true);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, true);
    verify(mockXmlInputFactory).setProperty(XMLInputFactory.SUPPORT_DTD, false);
}

From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java

protected XMLEventReader getEventReader(final InputStream is) throws XMLStreamException {
    if (ifactory == null) {
        ifactory = XMLInputFactory.newInstance();
    }//from   w ww. j  av a 2  s .  c  o m
    ifactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
    return ifactory.createXMLEventReader(is);
}