Example usage for javax.xml.stream XMLInputFactory createXMLStreamReader

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

Introduction

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

Prototype

public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream) throws XMLStreamException;

Source Link

Document

Create a new XMLStreamReader from a java.io.InputStream

Usage

From source file:Main.java

/**
 * /*from w ww .  jav a 2 s  .  c  o m*/
 * Checks indentation (over a single line - multipline text nodes is not supported)
 * 
 * @param out
 * @param indentSize
 * @return
 * @throws Exception
 */

public static boolean isIndented(String out, int indentSize) throws Exception {
    BufferedReader reader = new BufferedReader(new StringReader(out));

    boolean indentated = false;

    int level = 0;
    int line = 0;

    String string = reader.readLine();
    while (string != null) {
        int newLevel = 0;
        while (newLevel < string.length()) {
            if (!Character.isWhitespace(string.charAt(newLevel))) {
                break;
            }
            newLevel++;
        }
        if ((newLevel % indentSize) != 0) {
            throw new IllegalArgumentException("Unexpected " + newLevel + " whitespace chars at line " + line);
        }
        if (Math.abs(level - newLevel) > indentSize) {
            throw new IllegalArgumentException("Unexpected jump from " + level + " to " + newLevel
                    + " whitespace chars at line " + line + " for indenting with " + indentSize + " chars");
        }
        level = newLevel;

        string = reader.readLine();
        line++;

        if (level > 0) {
            indentated = true;
        }
    }

    if (!indentated) {
        // see if a simple xml piece
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(out));

        int elementMaxLevel = -1;
        int elementLevel = 0;
        do {
            int event = parser.next();
            if (event == XMLStreamConstants.START_ELEMENT) {
                elementLevel++;

                if (elementMaxLevel < elementLevel) {
                    elementMaxLevel = elementLevel;
                }
            } else if (event == XMLStreamConstants.END_ELEMENT) {
                elementLevel--;
            }
        } while (parser.hasNext());

        if (elementMaxLevel > 1) { // should be indentated
            return false;
        }
        return true;
    }

    return indentated;
}

From source file:edu.harvard.i2b2.eclipse.plugins.admin.utilities.ws.CRCServiceDriver.java

/**
 * Function to convert PFT requestPdo to OMElement
 * //from   w ww.ja v a2  s. c om
 * @param requestPdo   String requestPdo to send to PFT web service
 * @return An OMElement containing the PFT web service requestPdo
 */
public static OMElement getStatusPayLoad(String request) throws Exception {
    OMElement method = null;
    try {
        OMFactory fac = OMAbstractFactory.getOMFactory();

        StringReader strReader = new StringReader(request);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader reader = xif.createXMLStreamReader(strReader);

        StAXOMBuilder builder = new StAXOMBuilder(reader);
        method = builder.getDocumentElement();
        //   method.addChild(lineItem);
    } catch (FactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        log.error(e.getMessage());
        throw new Exception(e);
    }
    return method;
}

From source file:Main.java

public static XMLStreamReader createSafeReader(StreamSource source) throws XMLStreamException {
    if (source == null) {
        throw new IllegalArgumentException("The provided source cannot be null");
    }/*from   www .j ava 2 s .com*/

    XMLInputFactory xif = XMLInputFactory.newFactory();
    xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    return xif.createXMLStreamReader(source);
}

From source file:edu.harvard.i2b2.eclipse.plugins.admin.utilities.ws.OntServiceDriver.java

/**
 * Function to convert Ont requestVdo to OMElement
 * /*from  w  w  w .  j  a va 2 s.co m*/
 * @param requestVdo   String requestVdo to send to Ont web service
 * @return An OMElement containing the Ont web service requestVdo
 */
public static OMElement getOntPayLoad(String requestVdo) throws Exception {
    OMElement lineItem = null;
    try {
        StringReader strReader = new StringReader(requestVdo);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader reader = xif.createXMLStreamReader(strReader);

        StAXOMBuilder builder = new StAXOMBuilder(reader);
        lineItem = builder.getDocumentElement();
    } catch (FactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        log.error(e.getMessage());
        throw new Exception(e);
    }
    return lineItem;
}

From source file:edu.harvard.i2b2.timeline.lifelines.PDOQueryClient.java

public static OMElement getQueryPayLoad(String str) throws Exception {
    StringReader strReader = new StringReader(str);
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader reader = xif.createXMLStreamReader(strReader);

    StAXOMBuilder builder = new StAXOMBuilder(reader);
    OMElement lineItem = builder.getDocumentElement();
    // System.out.println("Line item string " + lineItem.toString());
    return lineItem;
}

From source file:Main.java

public static XMLStreamReader createXMLStreamReader(File file)
        throws FileNotFoundException, XMLStreamException {

    XMLInputFactory factory = XMLInputFactory.newInstance();
    if (factory.isPropertySupported("javax.xml.stream.isValidating")) {
        factory.setProperty("javax.xml.stream.isValidating", Boolean.TRUE);
    }//from ww  w .  j a  va  2  s  . c  om

    XMLStreamReader reader = factory.createXMLStreamReader(new FileReader(file));

    return reader;
}

From source file:com.biomeris.i2b2.export.ws.messages.MessageFactory.java

/**
 * Function creates tutorial response OMElement from xml string
 * /*from  ww  w . ja v a  2 s .co  m*/
 * @param xmlString
 * @return OMElement
 * @throws XMLStreamException
 */
public static OMElement createResponseOMElementFromString(String xmlString) throws XMLStreamException {
    OMElement returnElement = null;

    try {
        StringReader strReader = new StringReader(xmlString);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader reader = xif.createXMLStreamReader(strReader);

        StAXOMBuilder builder = new StAXOMBuilder(reader);
        returnElement = builder.getDocumentElement();
    } catch (XMLStreamException xmlStreamEx) {
        log.error("Error while converting tutorial response PDO to OMElement");
        throw xmlStreamEx;
    }

    return returnElement;
}

From source file:edu.harvard.i2b2.common.util.axis2.ServiceClient.java

public static OMElement getPayLoad(String requestPm) throws Exception {
    OMElement lineItem = null;//from ww w. j  a va  2 s.co  m
    try {
        StringReader strReader = new StringReader(requestPm);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader reader = xif.createXMLStreamReader(strReader);

        StAXOMBuilder builder = new StAXOMBuilder(reader);
        lineItem = builder.getDocumentElement();
    } catch (FactoryConfigurationError e) {
        log.error(e.getMessage());
        throw new Exception(e);
    }
    return lineItem;
}

From source file:fr.inria.oak.paxquery.pact.operators.unary.NavigationOperator.java

/**
 * /*from  w w w .j  a  v  a2  s . c  om*/
 * @param record
 * @param treePattern
 * @param collector
 */
public static void navigation(NestedMetadata inputRecordSignature, Record record, int column,
        NavigationTreePattern navigationTreePattern, Collector<Record> collector) {
    XMLInputFactory factory = XMLInputFactory.newInstance();

    try {
        StringValue value = record.getField(column, StringValue.class);
        XMLStreamReader streamReader = factory.createXMLStreamReader(new StringReader(value.getValue()));

        SingleDocumentExtractor extractor = new SingleDocumentExtractor(navigationTreePattern, streamReader);

        while (streamReader.hasNext()) {
            streamReader.next();
            if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT) {
                extractor.startElement();
            } else if (streamReader.getEventType() == XMLStreamConstants.END_ELEMENT) {
                extractor.endElement();
            } else if (streamReader.getEventType() == XMLStreamConstants.CHARACTERS) {
                extractor.characters();
            }

            if (extractor.getRecords().size() != 0) {
                Iterator<Record> pactRecordsIterator = extractor.getRecords().iterator();
                while (pactRecordsIterator.hasNext()) {
                    Record originalRecord = record.createCopy();
                    RecordOperations.concatenate(originalRecord, pactRecordsIterator.next());
                    collector.collect(originalRecord);
                }

                extractor.getRecords().clear();
            }
        }

    } catch (XMLStreamException e) {
        logger.error("XMLStreamException", e);
    }
}

From source file:com.rockhoppertech.music.midi.js.xml.ModeFactoryXMLHelper.java

/**
 * Read modes.xml and create {@code Scale instances} from those definitions.
 *///from   ww w.j  a  v a 2  s . co  m
public static void init() {
    List<Integer> intervals = null;
    Scale currentMode = null;
    String tagContent = null;
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader reader = null;
    try {
        reader = factory.createXMLStreamReader(ClassLoader.getSystemResourceAsStream("modes.xml"));
    } catch (XMLStreamException e) {
        e.printStackTrace();
        return;
    }

    try {
        while (reader.hasNext()) {
            int event = reader.next();

            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                String el = reader.getLocalName();
                logger.debug("start element '{}'", el);
                if ("mode".equals(el)) {
                    currentMode = new Scale();
                    intervals = new ArrayList<>();
                }
                if ("modes".equals(el)) {
                    modeList = new ArrayList<>();
                }
                break;

            case XMLStreamConstants.CHARACTERS:
                tagContent = reader.getText().trim();
                logger.debug("tagcontent '{}'", tagContent);
                break;

            case XMLStreamConstants.END_ELEMENT:
                switch (reader.getLocalName()) {
                case "mode":
                    // wow. both guava and commmons to get an int[] array
                    Integer[] array = FluentIterable.from(intervals).toArray(Integer.class);
                    // same as Integer[] array = intervals.toArray(new
                    // Integer[intervals.size()]);
                    int[] a = ArrayUtils.toPrimitive(array);
                    currentMode.setIntervals(a);
                    modeList.add(currentMode);
                    ScaleFactory.registerScale(currentMode);
                    break;
                case "interval":
                    logger.debug("interval '{}'", tagContent);
                    logger.debug("intervals '{}'", intervals);
                    intervals.add(Integer.parseInt(tagContent));
                    break;
                case "name":
                    currentMode.setName(tagContent);
                    break;
                }
                break;

            case XMLStreamConstants.START_DOCUMENT:
                modeList = new ArrayList<>();
                intervals = new ArrayList<>();
                break;
            }
        }
    } catch (XMLStreamException e) {
        logger.error(e.getLocalizedMessage(), e);
        e.printStackTrace();
    }

    logger.debug("mode list \n{}", modeList);
}