Example usage for javax.xml.stream XMLStreamConstants START_DOCUMENT

List of usage examples for javax.xml.stream XMLStreamConstants START_DOCUMENT

Introduction

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

Prototype

int START_DOCUMENT

To view the source code for javax.xml.stream XMLStreamConstants START_DOCUMENT.

Click Source Link

Document

Indicates an event is a start document

Usage

From source file:de.huxhorn.sulky.plist.impl.PropertyListReader.java

public PropertyList read(XMLStreamReader reader) throws XMLStreamException {
    int type = reader.getEventType();

    if (XMLStreamConstants.START_DOCUMENT == type) {
        do {/*from   ww w . j  a v  a2 s .c o  m*/
            reader.next();
            type = reader.getEventType();
        } while (XMLStreamConstants.START_ELEMENT != type);
    }
    PropertyList result = new PropertyList();
    if (XMLStreamConstants.START_ELEMENT == type && PLIST_NODE.equals(reader.getLocalName())) {
        reader.nextTag();
        type = reader.getEventType();
        if (!(XMLStreamConstants.END_ELEMENT == type && PLIST_NODE.equals(reader.getLocalName()))) {
            result.setRoot(readValue(reader));
        }
        reader.require(XMLStreamConstants.END_ELEMENT, null, PLIST_NODE);
    }
    return result;
}

From source file:com.cisco.cta.taxii.adapter.ResponseTransformerTest.java

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(resp.getBody()).thenReturn(body);
    when(templates.newTransformer()).thenReturn(transformer);
    when(readerFactory.create(body)).thenReturn(responseReader);
    when(responseReader.getEventType()).thenReturn(XMLStreamConstants.START_DOCUMENT);
    responseTransformer = new ResponseTransformer(templates, logWriter, readerFactory);
    feed = new TaxiiStatus.Feed();
    feed.setName("my-feed");
}

From source file:de.tuebingen.uni.sfs.germanet.api.IliLoader.java

/**
 * Loads <code>IliRecords</code> from the specified file into this
 * <code>IliLoader</code>'s <code>GermaNet</code> object.
 * @param iliFile the file containing <code>IliRecords</code> data
 * @throws java.io.FileNotFoundException
 * @throws javax.xml.stream.XMLStreamException
 *///from ww  w . ja v  a2  s.  c om
protected void loadILI(File iliFile) throws FileNotFoundException, XMLStreamException {
    InputStream in = new FileInputStream(iliFile);
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser = factory.createXMLStreamReader(in);
    int event;
    String nodeName;
    logger.debug("Loading " + iliFile.getName() + "...");

    //Parse entire file, looking for ili record start elements
    while (parser.hasNext()) {
        event = parser.next();
        switch (event) {
        case XMLStreamConstants.START_DOCUMENT:
            namespace = parser.getNamespaceURI();
            break;
        case XMLStreamConstants.START_ELEMENT:
            nodeName = parser.getLocalName();
            if (nodeName.equals(GermaNet.XML_ILI_RECORD)) {
                IliRecord ili = processIliRecord(parser);
                germaNet.addIliRecord(ili);
            }
            break;
        }
    }
    parser.close();
    logger.debug("Done.");
}

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

/**
 * Read modes.xml and create {@code Scale instances} from those definitions.
 *///from w w w  . ja  va  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);
}

From source file:de.tuebingen.uni.sfs.germanet.api.WiktionaryLoader.java

/**
 * Loads <code>WiktionaryParaphrases</code> from the specified file into this
 * <code>WiktionaryLoader</code>'s <code>GermaNet</code> object.
 * @param wiktionaryFile the file containing <code>WiktionaryParaphrases</code> data
 * @throws java.io.FileNotFoundException
 * @throws javax.xml.stream.XMLStreamException
 *///from   w  w w  .j a  v a2s . com
protected void loadWiktionary(File wiktionaryFile) throws FileNotFoundException, XMLStreamException {
    wikiDir = wiktionaryFile;
    FilenameFilter filter = new WikiFilter(); //get only wiktionary files
    File[] wikiFiles = wikiDir.listFiles(filter);

    if (wikiFiles == null || wikiFiles.length == 0) {
        throw new FileNotFoundException(
                "Unable to load Wiktionary Paraphrases from \"" + this.wikiDir.getPath() + "\"");
    }

    for (File wikiFile : wikiFiles) {
        logger.debug("Loading " + wikiFile.getName() + "...");
        InputStream in = new FileInputStream(wikiFile);
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader parser = factory.createXMLStreamReader(in);
        int event;
        String nodeName;

        //Parse entire file, looking for Wiktionary paraphrase start elements
        while (parser.hasNext()) {
            event = parser.next();
            switch (event) {
            case XMLStreamConstants.START_DOCUMENT:
                namespace = parser.getNamespaceURI();
                break;
            case XMLStreamConstants.START_ELEMENT:
                nodeName = parser.getLocalName();
                if (nodeName.equals(GermaNet.XML_WIKTIONARY_PARAPHRASE)) {
                    WiktionaryParaphrase wiki = processWiktionaryParaphrase(parser);
                    germaNet.addWiktionaryParaphrase(wiki);
                }
                break;
            }
        }
        parser.close();
    }

    logger.debug("Done.");

}

From source file:de.tuebingen.uni.sfs.germanet.api.IliLoader.java

/**
 * Loads <code>IliRecords</code> from the specified stream into this
 * <code>IliLoader</code>'s <code>GermaNet</code> object.
 * @param inputStream the stream containing <code>IliRecords</code> data
 * @throws javax.xml.stream.XMLStreamException
 *///ww w  . j  a  v  a 2s .c  o  m
protected void loadILI(InputStream inputStream) throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser = factory.createXMLStreamReader(inputStream);
    int event;
    String nodeName;
    logger.debug("Loading input stream interLingualIndex_DE-EN.xml...");

    //Parse entire file, looking for ili record start elements
    while (parser.hasNext()) {
        event = parser.next();
        switch (event) {
        case XMLStreamConstants.START_DOCUMENT:
            namespace = parser.getNamespaceURI();
            break;
        case XMLStreamConstants.START_ELEMENT:
            nodeName = parser.getLocalName();
            if (nodeName.equals(GermaNet.XML_ILI_RECORD)) {
                IliRecord ili = processIliRecord(parser);
                germaNet.addIliRecord(ili);
            }
            break;
        }
    }
    parser.close();
    logger.debug("Done.");
}

From source file:de.tuebingen.uni.sfs.germanet.api.WiktionaryLoader.java

/**
 * Loads <code>WiktionaryParaphrases</code> from the given streams into this
 * <code>WiktionaryLoader</code>'s <code>GermaNet</code> object.
 * @param inputStreams the list of streams containing <code>WiktionaryParaphrases</code> data
 * @param xmlNames the names of the streams
 * @throws javax.xml.stream.XMLStreamException
 *//*from w ww . j  av  a2 s.c o  m*/
protected void loadWiktionary(List<InputStream> inputStreams, List<String> xmlNames) throws XMLStreamException {

    for (int i = 0; i < inputStreams.size(); i++) {
        if (xmlNames.get(i).startsWith("wiktionary")) {
            logger.debug("Loading input stream " + xmlNames.get(i) + "...");
            XMLInputFactory factory = XMLInputFactory.newInstance();
            XMLStreamReader parser = factory.createXMLStreamReader(inputStreams.get(i));
            int event;
            String nodeName;

            //Parse entire file, looking for Wiktionary paraphrase start elements
            while (parser.hasNext()) {
                event = parser.next();
                switch (event) {
                case XMLStreamConstants.START_DOCUMENT:
                    namespace = parser.getNamespaceURI();
                    break;
                case XMLStreamConstants.START_ELEMENT:
                    nodeName = parser.getLocalName();
                    if (nodeName.equals(GermaNet.XML_WIKTIONARY_PARAPHRASE)) {
                        WiktionaryParaphrase wiki = processWiktionaryParaphrase(parser);
                        germaNet.addWiktionaryParaphrase(wiki);
                    }
                    break;
                }
            }
            parser.close();
        }
    }

    logger.debug("Done.");

}

From source file:lux.search.highlight.XmlHighlighter.java

@Override
public void handleEvent(XMLStreamReader reader, int eventType) throws XMLStreamException {

    switch (eventType) {
    case XMLStreamConstants.START_DOCUMENT:
        xmlStreamReader = reader; // cache the reader so we can pull events
        super.handleEvent(reader, eventType);
        break;//from   w w  w.  j  a va 2s.c  o m

    case XMLStreamConstants.START_ELEMENT:
        super.handleEvent(reader, eventType);
        xmlStreamTokens
                .pushElement(new QName(reader.getNamespaceURI(), reader.getLocalName(), reader.getPrefix()));
        break;

    case XMLStreamConstants.END_ELEMENT:
        super.handleEvent(reader, eventType);
        xmlStreamTokens.popElement();
        break;

    case XMLStreamConstants.COMMENT:
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        super.handleEvent(reader, eventType);
        break;

    case XMLStreamConstants.CDATA:
        throw new XMLStreamException("unexpected CDATA event");

    case XMLStreamConstants.SPACE:
        super.handleEvent(reader, eventType);
        break;

    case XMLStreamConstants.CHARACTERS:
        textReader.text();
        try {
            highlightTextNode();
        } catch (IOException e) {
            throw new XMLStreamException(e);
        }
        break;

    case XMLStreamConstants.ENTITY_REFERENCE:
        throw new XMLStreamException("unexpected entity reference event");

    default:
        super.handleEvent(reader, eventType);
    }

}

From source file:com.microsoft.windowsazure.services.table.client.AtomPubParser.java

/**
 * Reserved for internal use. Parses the operation response as a collection of entities. Reads entity data from the
 * specified input stream using the specified class type and optionally projects each entity result with the
 * specified resolver into an {@link ODataPayload} containing a collection of {@link TableResult} objects. .
 * /* w w  w.jav a  2 s .c o m*/
 * @param inStream
 *            The <code>InputStream</code> to read the data to parse from.
 * @param clazzType
 *            The class type <code>T</code> implementing {@link TableEntity} for the entities returned. Set to
 *            <code>null</code> to ignore the returned entities and copy only response properties into the
 *            {@link TableResult} objects.
 * @param resolver
 *            An {@link EntityResolver} instance to project the entities into instances of type <code>R</code>. Set
 *            to <code>null</code> to return the entities as instances of the class type <code>T</code>.
 * @param opContext
 *            An {@link OperationContext} object used to track the execution of the operation.
 * @return
 *         An {@link ODataPayload} containing a collection of {@link TableResult} objects with the parsed operation
 *         response.
 * 
 * @throws XMLStreamException
 *             if an error occurs while accessing the stream.
 * @throws ParseException
 *             if an error occurs while parsing the stream.
 * @throws InstantiationException
 *             if an error occurs while constructing the result.
 * @throws IllegalAccessException
 *             if an error occurs in reflection while parsing the result.
 * @throws StorageException
 *             if a storage service error occurs.
 */
@SuppressWarnings("unchecked")
protected static <T extends TableEntity, R> ODataPayload<?> parseResponse(final InputStream inStream,
        final Class<T> clazzType, final EntityResolver<R> resolver, final OperationContext opContext)
        throws XMLStreamException, ParseException, InstantiationException, IllegalAccessException,
        StorageException {
    ODataPayload<T> corePayload = null;
    ODataPayload<R> resolvedPayload = null;
    ODataPayload<?> commonPayload = null;

    if (resolver != null) {
        resolvedPayload = new ODataPayload<R>();
        commonPayload = resolvedPayload;
    } else {
        corePayload = new ODataPayload<T>();
        commonPayload = corePayload;
    }

    final XMLStreamReader xmlr = Utility.createXMLStreamReaderFromStream(inStream);
    int eventType = xmlr.getEventType();
    xmlr.require(XMLStreamConstants.START_DOCUMENT, null, null);
    eventType = xmlr.next();

    xmlr.require(XMLStreamConstants.START_ELEMENT, null, ODataConstants.FEED);
    // skip feed chars
    eventType = xmlr.next();

    while (xmlr.hasNext()) {
        eventType = xmlr.next();

        if (eventType == XMLStreamConstants.CHARACTERS) {
            xmlr.getText();
            continue;
        }

        final String name = xmlr.getName().toString();

        if (eventType == XMLStreamConstants.START_ELEMENT) {
            if (name.equals(ODataConstants.BRACKETED_ATOM_NS + ODataConstants.ENTRY)) {
                final TableResult res = parseEntity(xmlr, clazzType, resolver, opContext);
                if (corePayload != null) {
                    corePayload.tableResults.add(res);
                }

                if (resolver != null) {
                    resolvedPayload.results.add((R) res.getResult());
                } else {
                    corePayload.results.add((T) res.getResult());
                }
            }
        } else if (eventType == XMLStreamConstants.END_ELEMENT
                && name.equals(ODataConstants.BRACKETED_ATOM_NS + ODataConstants.FEED)) {
            break;
        }
    }

    xmlr.require(XMLStreamConstants.END_ELEMENT, null, ODataConstants.FEED);
    return commonPayload;
}

From source file:org.javelin.sws.ext.bind.internal.model.ComplexTypePattern.java

@Override
public T consumeValue(XMLEventReader eventReader, UnmarshallingContext context) throws XMLStreamException {
    // first create an object to be filled (using PropertyAccessors - direct or bean) according to the content model
    T object = BeanUtils.instantiate(this.getJavaType());

    // the order is dictated by incoming events, not by the mode
    // TODO: create a property to enable strict unmarshalling - dictated by content model
    // only this (ContentModel) pattern iterates over XML Events
    XMLEvent event = null;//from w w  w .j  av  a  2 s  .  c o m
    PropertyMetadataValue<T, ?> pmv = null;

    // this loop will only handle first level of start elements and only single end element
    // deeper levels will be handled by nested patterns
    while (true) {
        boolean end = false;
        event = eventReader.peek();
        pmv = null;

        switch (event.getEventType()) {
        case XMLStreamConstants.ATTRIBUTE:
            pmv = this.consumeNestedAttribute(eventReader, context);
            break;
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
            // TODO: XMLEvent.ENTITY_REFERENCE?
            if (this.simpleContent != null) {
                pmv = this.consumeSimpleContent(eventReader, context);
                break;
            }
        case XMLStreamConstants.COMMENT:
        case XMLStreamConstants.DTD:
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.ENTITY_DECLARATION:
        case XMLStreamConstants.NOTATION_DECLARATION:
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            eventReader.nextEvent();
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            // TODO: XMLEvent.ENTITY_REFERENCE?
            eventReader.nextEvent();
            break;
        case XMLStreamConstants.START_DOCUMENT:
            // strange
            break;
        case XMLStreamConstants.START_ELEMENT:
            pmv = this.consumeNestedElement(eventReader, context);
            break;
        case XMLStreamConstants.END_ELEMENT:
            // TODO: in mixed content there will be more than one end element it this content model's level
        case XMLStreamConstants.END_DOCUMENT:
            end = true;
            break;
        }

        if (end)
            break;

        if (pmv != null)
            pmv.getMetadata().setValue(object, pmv.getValue());
    }

    return (T) object;
}