Example usage for javax.xml.stream XMLResolver XMLResolver

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

Introduction

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

Prototype

XMLResolver

Source Link

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;
        }/*ww  w.  ja va2  s  .co  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  a2s.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:org.modeldriven.fuml.xmi.stream.StreamReader.java

@SuppressWarnings("unchecked")
public Collection read(InputStream stream) {
    List<Object> results = new ArrayList<Object>();
    InputStream source = stream;//  w  w  w. jav  a2 s  . c  om
    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;
}

From source file:org.plasma.sdo.xml.StreamUnmarshaller.java

private void setup() {
    this.factory = XMLInputFactory.newInstance();
    factory.setXMLResolver(new XMLResolver() {
        public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace)
                throws XMLStreamException {
            log.debug("resolve: " + publicID + ":" + systemID + ":" + baseURI + ":" + namespace);
            return null;
        }/*from ww  w. j a  v  a 2s  . c o m*/
    });
    factory.setEventAllocator(new EventAllocator());
    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);

    allocator = factory.getEventAllocator();
}