Example usage for javax.xml.stream XMLInputFactory IS_REPLACING_ENTITY_REFERENCES

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

Introduction

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

Prototype

String IS_REPLACING_ENTITY_REFERENCES

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

Click Source Link

Document

Requires the parser to replace internal entity references with their replacement text and report them as characters

Usage

From source file:EntityReferenceTest.java

public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);

    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream("e.xml"));
    while (reader.hasNext()) {
        int event = reader.next();
        if (event == XMLStreamConstants.CHARACTERS)
            System.out.println(reader.getText());
        else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
            System.out.println("en: " + reader.getLocalName());
            System.out.println("er: " + reader.getText());
        }/* w  w  w  .  jav a 2s . co  m*/
    }
    inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);

    reader = inputFactory.createXMLStreamReader(new FileInputStream("e.xml"));
    while (reader.hasNext()) {
        int event = reader.next();
        if (event == XMLStreamConstants.CHARACTERS)
            System.out.println(reader.getText());
        else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
            System.out.println("en: " + reader.getLocalName());
            System.out.println("er: " + reader.getText());
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    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);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new MyFilter());

    int eventType = xmlr.getEventType();
    printEventType(eventType);//from   ww  w . j a va 2s  . c  om

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    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);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new Main());

    int eventType = xmlr.getEventType();
    printEventType(eventType);/*from w  w  w .j a va2  s .  com*/

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:StaxCursorTest.java

public static void main(String[] args) throws Exception {

    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    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_COALESCING, Boolean.FALSE);

    try {//  w w  w. j  av a2 s.c  o  m
        XMLStreamReader xmlr = xmlif.createXMLStreamReader(filename, new FileInputStream(filename));
        int eventType = xmlr.getEventType();
        printStartDocument(xmlr);
        while (xmlr.hasNext()) {
            eventType = xmlr.next();
            printStartElement(xmlr);
            printEndElement(xmlr);
            printText(xmlr);
            printPIData(xmlr);
            printComment(xmlr);
        }
    } catch (XMLStreamException ex) {
        System.out.println(ex.getMessage());
        if (ex.getNestedException() != null) {
            ex.getNestedException().printStackTrace();
        }
    }
}

From source file:MyStreamFilter.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    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);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new MyStreamFilter());

    int eventType = xmlr.getEventType();
    printEventType(eventType);/*from  w  w w.  ja v a2s.  c o  m*/

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:Main.java

protected static synchronized void initializeXMLInputFactory() {
    if (xmlInputFactory == null) {
        xmlInputFactory = XMLInputFactory.newInstance();
        xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
        xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); // This disables DTDs entirely for that factory
        xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
    }/*from  w ww  .j  av  a  2 s  .c  o m*/
}

From source file:Main.java

/**
 * 'safe' is here reflecting://from w  w w .j a  va 2 s.  c  o m
 * http://www.jorambarrez.be/blog/2013/02/19/uploading
 * -a-funny-xml-can-bring-down-your-server/ and
 * http://activiti.org/userguide/index.html#advanced.safe.bpmn.xml
 */
public static XMLInputFactory createSafeXmlInputFactory() {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    if (xif.isPropertySupported(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES)) {
        xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
    }

    if (xif.isPropertySupported(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES)) {
        xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    }

    if (xif.isPropertySupported(XMLInputFactory.SUPPORT_DTD)) {
        xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    }
    return xif;
}

From source file:Main.java

/**
 * Build a new XMLInputFactory./*from  w w  w .j  a va 2  s  . c  o  m*/
 *
 * @return XML input factory
 */
public static XMLInputFactory getXmlInputFactory() {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    return inputFactory;
}

From source file:Main.java

private static XMLEventReader getXMLEventReader(String filename) throws Exception {
    XMLInputFactory xmlif = null;
    XMLEventReader xmlr = null;/*from ww  w .j  a  v a  2  s. c  o  m*/
    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);

    return xmlr;
}

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

@After
public void tearDown() {
    System.clearProperty(XMLInputFactory.IS_NAMESPACE_AWARE);
    System.clearProperty(XMLInputFactory.IS_VALIDATING);
    System.clearProperty(XMLInputFactory.IS_COALESCING);
    System.clearProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES);
    System.clearProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES);
    System.clearProperty(XMLInputFactory.SUPPORT_DTD);
}