Example usage for javax.xml.stream XMLInputFactory setXMLResolver

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

Introduction

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

Prototype

public abstract void setXMLResolver(XMLResolver resolver);

Source Link

Document

The resolver that will be set on any XMLStreamReader or XMLEventReader created by this factory instance.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setXMLResolver(new XMLResolver() {
        public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace)
                throws XMLStreamException {
            System.out.println(systemID);
            return null;
        }//from  ww  w .  j  av  a 2  s .  c  o m
    });

    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream("dtd.xml"));
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);
    writer.add(reader);
    writer.flush();

}

From source file:com.wavemaker.runtime.data.parser.BaseHbmParser.java

private static void setupEntityResolver(XMLInputFactory factory) {

    factory.setXMLResolver(new XMLResolver() {

        @Override/*  w  w w  . ja  v  a  2  s. c o m*/
        public Object resolveEntity(String publicId, String systemId, String s1, String s2) {
            if (HbmConstants.HBM_SYSTEM_ID.equals(systemId)) {
                InputStream rtn = ClassLoaderUtils
                        .getResourceAsStream("com/wavemaker/tools/data/hibernate-mapping-3.0.dtd");
                if (rtn == null) {
                    // get rid of references to "tools" package
                    Log parserLogger = LogFactory.getLog("com.wavemaker.tools.data.parser");
                    parserLogger.warn("Did not find local copy of \"hibernate-mapping-3.0.dtd\"");
                    // DataServiceLoggers.parserLogger.warn( //
                    // "Did not find local copy of \"hibernate-mapping-3.0.dtd\"");
                } else {
                    return rtn;
                }
            }
            return null;
        }
    });
}

From source file:com.lucidworks.hadoop.ingest.util.EmptyEntityResolver.java

/**
 * Configures the given {@link XMLInputFactory} to not parse external entities.
 * No further configuration on is needed, all required entity resolvers are configured.
 *///from   w  w  w . ja  v  a2  s.co m
public static void configureXMLInputFactory(XMLInputFactory inputFactory) {
    // don't enable validation of DTDs:
    trySetStAXProperty(inputFactory, XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    // enable this to *not* produce parsing failure on external entities:
    trySetStAXProperty(inputFactory, XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.TRUE);
    inputFactory.setXMLResolver(EmptyEntityResolver.STAX_INSTANCE);
}

From source file:demo.SourceHttpMessageConverter.java

private Source readStAXSource(InputStream body) {
    try {/*  www  .  jav a 2  s  . c om*/
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, isSupportDtd());
        inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, isProcessExternalEntities());
        if (!isProcessExternalEntities()) {
            inputFactory.setXMLResolver(NO_OP_XML_RESOLVER);
        }
        XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body);
        return new StAXSource(streamReader);
    } catch (XMLStreamException ex) {
        throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex);
    }
}

From source file:org.modeldriven.fuml.xmi.stream.StreamReader.java

@SuppressWarnings("unchecked")
public Collection read(InputStream stream) {
    List<Object> results = new ArrayList<Object>();
    InputStream source = stream;//from   ww  w  .  j a v  a  2 s  .c  o  m
    StreamContext context = null;

    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();

        factory.setXMLResolver(new XMLResolver() {

            @Override
            public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace)
                    throws XMLStreamException {
                // TODO Auto-generated method stub
                return null;
            }

        });

        /*
        XStream xstream = new XStream(new StaxDriver() {
           protected XMLStreamReader createParser(Reader xml) throws
           XMLStreamException {
           return getInputFactory().createXMLStreamReader(xml);
           }
           protected XMLStreamReader createParser(InputStream xml) throws
           XMLStreamException {
           return getInputFactory().createXMLStreamReader(xml);
           }
           });
        */

        //factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,Boolean.FALSE);
        //factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,Boolean.TRUE);
        //set the IS_COALESCING property to true , if application desires to
        //get whole text data as one event.            
        //factory.setProperty(XMLInputFactory.IS_COALESCING , Boolean.TRUE);

        factory.setEventAllocator(new EventAllocator());

        allocator = factory.getEventAllocator();
        XMLStreamReader streamReader = factory.createXMLStreamReader(stream);

        int eventType = streamReader.getEventType();
        StreamNode node = null;
        StreamNode parent = null;
        int level = 0;
        int ignoredNodeLevel = -1;

        while (streamReader.hasNext()) {
            eventType = streamReader.next();
            //if (log.isDebugEnabled())
            //    log.debug(this.getEventTypeString(eventType));

            switch (eventType) {
            case XMLEvent.START_ELEMENT:
                level++;
                if (ignoredNodeLevel >= 0)
                    break;

                XMLEvent event = allocateXMLEvent(streamReader);
                if (level == 1) {
                    if (context != null)
                        throw new XmiException("existing context unexpected");
                    context = new StreamContext(event);
                }

                // debugging
                if (event.toString().contains("plasma:PlasmaType")) {
                    int foo = 1;
                    foo++;
                }

                node = new StreamNode(event, context);
                if (node.isIgnored()) {
                    if (log.isDebugEnabled()) {
                        Location loc = event.getLocation();
                        String msg = "start ignoring elements - level:  " + String.valueOf(level)
                                + " - line:col[" + loc.getLineNumber() + ":" + loc.getColumnNumber() + "] - ";
                        log.debug(msg);
                    }
                    ignoredNodeLevel = level;
                    break;
                }

                logEventInfo(event);
                parent = null;
                if (nodes.size() > 0) {
                    parent = nodes.peek();
                    parent.add(node);
                    node.setParent(parent);
                    if (isRootNode(node, context))
                        results.add(node);
                } else {
                    if (isRootNode(node, context))
                        results.add(node);
                }

                nodes.push(node);
                fireStreamNodeCreated(node, parent);
                break;
            case XMLEvent.END_ELEMENT:
                if (ignoredNodeLevel >= 0) {
                    if (ignoredNodeLevel == level) {
                        if (log.isDebugEnabled()) {
                            event = allocateXMLEvent(streamReader);
                            Location loc = event.getLocation();
                            String msg = "end ignoring elements - level:  " + String.valueOf(level)
                                    + " - line:col[" + loc.getLineNumber() + ":" + loc.getColumnNumber()
                                    + "] - ";
                            log.debug(msg);
                        }
                        ignoredNodeLevel = -1;
                    }
                    level--;
                    break;
                }

                level--;
                node = nodes.pop();
                parent = null;
                if (nodes.size() > 0)
                    parent = nodes.peek();
                fireStreamNodeCompleted(node, parent);
                break;
            case XMLEvent.CHARACTERS:
                if (ignoredNodeLevel >= 0)
                    break;
                node = nodes.peek();
                event = allocateXMLEvent(streamReader);
                String data = event.asCharacters().getData();
                if (data != null) {
                    data = data.trim();
                    if (data.length() > 0) {
                        if (log.isDebugEnabled())
                            log.debug("CHARACTERS: '" + data + "'");
                        if (data.length() > 0) {
                            node = nodes.peek();
                            node.addCharactersEvent(event);
                        }
                    }
                }
                break;
            default:
                if (log.isDebugEnabled()) {
                    event = allocateXMLEvent(streamReader);
                    logEventInfo(event);
                }
                break;
            }
        }
        if (results.size() > 1)
            throw new XmiException("found multiple root nodes (" + results.size() + ")");
    } catch (XMLStreamException e) {
        throw new XmiException(e);
    } finally {
        try {
            source.close();
        } catch (IOException e) {
        }
    }

    return results;
}