Example usage for javax.xml.stream XMLStreamConstants CHARACTERS

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

Introduction

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

Prototype

int CHARACTERS

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

Click Source Link

Document

Indicates an event is characters

Usage

From source file:di.uniba.it.tee2.wiki.WikipediaDumpIterator.java

@Override
public WikiPage next() {
    WikiPage page = new WikiPage();
    try {// w  w w. j a  v  a2 s.  c  o  m
        StringBuilder wikimediaText = new StringBuilder();
        StringBuilder title = new StringBuilder();
        int wikiID = -1;
        int revisionID = -1;
        boolean finishedParsingArticle = false;
        boolean inrevision = false;
        char lastElement = 'n';
        while (!finishedParsingArticle && xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {
            case XMLStreamConstants.START_ELEMENT:
                switch (xmlStreamReader.getLocalName()) {
                case "title":
                    lastElement = 't';
                    break;
                case "text":
                    lastElement = 'c';
                    break;
                case "revision":
                    inrevision = true;
                    break;
                case "id":
                    lastElement = 'd';
                    break;
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                switch (xmlStreamReader.getLocalName()) {
                case "page":
                    finishedParsingArticle = true;
                    break;
                case "title":
                    lastElement = 'n';
                    break;
                case "text":
                    lastElement = 'n';
                    break;
                case "id":
                    lastElement = 'n';
                    break;
                case "revision":
                    inrevision = false;
                    break;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                if (lastElement == 't') {
                    title.append(xmlStreamReader.getText());
                } else if (lastElement == 'c') {
                    wikimediaText.append(xmlStreamReader.getText());
                } else if (lastElement == 'd') {
                    if (inrevision && revisionID == -1) {
                        revisionID = Integer.parseInt(xmlStreamReader.getText());
                    } else if (wikiID == -1) {
                        wikiID = Integer.parseInt(xmlStreamReader.getText());
                    }
                }
                break;
            }
        }

        page.setWikiID(wikiID);
        page.setRevisionID(revisionID);
        page.setTitle(title.toString());
        try {
            ParsedPage parsedPage = parser.parse(wikimediaText.toString());
            page.setParsedPage(parsedPage);
        } catch (Exception ex) {
            Logger.getLogger(WikipediaDumpIterator.class.getName()).log(Level.WARNING,
                    "Error to parse page: " + page.getTitle(), ex);
        }
    } catch (XMLStreamException ex) {
        Logger.getLogger(WikipediaDumpIterator.class.getName()).log(Level.WARNING, "Error reading XML stream",
                ex);
    }
    return page;
}

From source file:com.norconex.collector.http.sitemap.impl.StandardSitemapResolver.java

private void parseLocation(InputStream is, HttpClient httpClient, SitemapURLAdder sitemapURLAdder,
        Set<String> resolvedLocations, String location) throws XMLStreamException {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
    XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(is);
    ParseState parseState = new ParseState();

    String locationDir = StringUtils.substringBeforeLast(location, "/");
    int event = xmlReader.getEventType();
    while (true) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            String tag = xmlReader.getLocalName();
            parseStartElement(parseState, tag);
            break;
        case XMLStreamConstants.CHARACTERS:
            String value = xmlReader.getText();
            if (parseState.sitemapIndex && parseState.loc) {
                resolveLocation(value, httpClient, sitemapURLAdder, resolvedLocations);
                parseState.loc = false;/*www .  j  a  v a2s  .  c  o  m*/
            } else if (parseState.baseURL != null) {
                parseCharacters(parseState, value);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            tag = xmlReader.getLocalName();
            parseEndElement(sitemapURLAdder, parseState, locationDir, tag);
            break;
        }
        if (!xmlReader.hasNext()) {
            break;
        }
        event = xmlReader.next();
    }
}

From source file:fr.inria.oak.paxquery.pact.io.XmlNavTreePatternInputFormat.java

@Override
public Record nextRecord(Record record) throws IOException {
    if (this.pactRecordsIterator != null) {
        if (this.pactRecordsIterator.hasNext()) {
            if (this.attachDocumentID) {
                record.addField(new StringValue(this.documentID));
                RecordOperations.concatenate(record, this.pactRecordsIterator.next());
            } else
                this.pactRecordsIterator.next().copyTo(record);

            //return true;
            return record;
        }/*w  w w .j a  v  a  2 s  .c o  m*/

        this.pactRecordsIterator = null;
        this.extractor.getRecords().clear();
    }

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

            if (this.extractor.getRecords().size() != 0) {
                this.pactRecordsIterator = this.extractor.getRecords().iterator();
                if (this.attachDocumentID) {
                    record.addField(new StringValue(this.documentID));
                    RecordOperations.concatenate(record, this.pactRecordsIterator.next());
                } else
                    this.pactRecordsIterator.next().copyTo(record);

                //return true;
                return record;
            }

        }

        //return false;
        return null;
    } catch (XMLStreamException e) {
        logger.error("XMLStreamException", e);
        //return false;
        return null;
    }
}

From source file:microsoft.exchange.webservices.data.core.EwsXmlReader.java

/**
 * Reads the specified node type.// w w w  . java2  s.c o  m
 *
 * @param keepWhiteSpace Do not remove whitespace characters if true
 * @throws ServiceXmlDeserializationException  the service xml deserialization exception
 * @throws XMLStreamException the XML stream exception
 */
private void read(boolean keepWhiteSpace) throws ServiceXmlDeserializationException, XMLStreamException {
    // The caller to EwsXmlReader.Read expects
    // that there's another node to
    // read. Throw an exception if not true.
    while (true) {
        if (!xmlReader.hasNext()) {
            throw new ServiceXmlDeserializationException("Unexpected end of XML document.");
        } else {
            XMLEvent event = xmlReader.nextEvent();
            if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
                Characters characters = (Characters) event;
                if (!keepWhiteSpace)
                    if (characters.isIgnorableWhiteSpace() || characters.isWhiteSpace()) {
                        continue;
                    }
            }
            this.prevEvent = this.presentEvent;
            this.presentEvent = event;
            break;
        }
    }
}

From source file:de.ii.xtraplatform.ogc.api.gml.parser.GMLParser.java

private void parseFeature(SMInputCursor cursor) throws XMLStreamException {

    analyzer.analyzeFeatureStart(null, cursor.getNsUri(), cursor.getLocalName());

    featureDepth = cursor.getParentCount();

    for (int i = 0; i < cursor.getAttrCount(); i++) {
        analyzer.analyzeAttribute(cursor.getAttrNsUri(i), cursor.getAttrLocalName(i), cursor.getAttrValue(i));
    }/*from ww  w . jav a 2  s.  c om*/

    SMFlatteningCursor feature;
    StringBuilder text = null;
    if (parseText) {
        feature = (SMFlatteningCursor) cursor.descendantCursor().advance();
        text = new StringBuilder();
    } else {
        feature = (SMFlatteningCursor) cursor.descendantElementCursor().advance();
    }

    while (feature.readerAccessible()) {
        if (feature.getCurrEventCode() == XMLStreamConstants.START_ELEMENT) {
            boolean nil = false;

            for (int i = 0; i < feature.getAttrCount(); i++) {
                if (feature.getAttrNsUri(i).equals(XSI.getNS(XSI.VERSION.DEFAULT))
                        && feature.getAttrLocalName(i).equals(XSI.getWord(XSI.VOCABULARY.NIL))
                        && feature.getAttrValue(i).equals(XSI.getWord(XSI.VOCABULARY.NIL_TRUE))) {
                    nil = true;
                }
                analyzer.analyzeAttribute(feature.getAttrNsUri(i), feature.getAttrLocalName(i),
                        feature.getAttrValue(i));
            }

            analyzer.analyzePropertyStart(feature.getNsUri(), feature.getLocalName(),
                    feature.getParentCount() - featureDepth, feature, nil);

        } else if (feature.getCurrEventCode() == XMLStreamConstants.END_ELEMENT) {
            if (parseText && text.length() > 0) {
                analyzer.analyzePropertyText(feature.getNsUri(), feature.getLocalName(),
                        feature.getParentCount() - featureDepth, text.toString());
                text = new StringBuilder();
            }
            analyzer.analyzePropertyEnd(feature.getNsUri(), feature.getLocalName(),
                    feature.getParentCount() - featureDepth);
        } else if (parseText && (feature.getCurrEventCode() == XMLStreamConstants.CHARACTERS)) {
            text.append(feature.getText().trim());
        }

        feature = (SMFlatteningCursor) feature.advance();
    }

    analyzer.analyzeFeatureEnd();
}

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 ww.ja  v a2s .  c  om*/

    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.norconex.collector.http.sitemap.impl.DefaultSitemapResolver.java

private void parseLocation(InputStream is, DefaultHttpClient httpClient, SitemapURLStore sitemapURLStore,
        Set<String> resolvedLocations, String location) throws XMLStreamException {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
    XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(is);
    ParseState parseState = new ParseState();

    String locationDir = StringUtils.substringBeforeLast(location, "/");
    int event = xmlReader.getEventType();
    while (true) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            String tag = xmlReader.getLocalName();
            parseStartElement(parseState, tag);
            break;
        case XMLStreamConstants.CHARACTERS:
            String value = xmlReader.getText();
            if (parseState.sitemapIndex && parseState.loc) {
                resolveLocation(value, httpClient, sitemapURLStore, resolvedLocations);
                parseState.loc = false;//from  ww  w. j  av  a 2  s  . com
            } else if (parseState.baseURL != null) {
                parseCharacters(parseState, value);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            tag = xmlReader.getLocalName();
            parseEndElement(sitemapURLStore, parseState, locationDir, tag);
            break;
        }
        if (!xmlReader.hasNext()) {
            break;
        }
        event = xmlReader.next();
    }
}

From source file:davmail.exchange.dav.ExchangeDavMethod.java

protected String getTagContent(XMLStreamReader reader) throws XMLStreamException {
    String value = null;/*from   w ww.j a  va2 s.  com*/
    String tagLocalName = reader.getLocalName();
    while (reader.hasNext() && !((reader.getEventType() == XMLStreamConstants.END_ELEMENT)
            && tagLocalName.equals(reader.getLocalName()))) {
        reader.next();
        if (reader.getEventType() == XMLStreamConstants.CHARACTERS) {
            value = reader.getText();
        }
    }
    // empty tag
    if (!reader.hasNext()) {
        throw new XMLStreamException("End element for " + tagLocalName + " not found");
    }
    return value;
}

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  .ja v a2 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;
}

From source file:ca.efendi.datafeeds.messaging.FtpSubscriptionMessageListener.java

private void parse(FtpSubscription ftpSubscription, final InputStream is) throws XMLStreamException {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
    factory.setProperty(XMLInputFactory.IS_COALESCING, true);
    final XMLStreamReader reader = factory.createXMLStreamReader(is, "UTF-8");
    CJProduct product = null;/* w ww. j  ava 2  s  .c  om*/
    String tagContent = null;
    //final ServiceContext serviceContext = new ServiceContext();

    //ServiceContext serviceContext = ServiceContextFactory.getInstance(
    //        BlogsEntry.class.getName(), actionRequest);

    //serviceContext.setScopeGroupId(20159);
    while (reader.hasNext()) {
        final int event = reader.next();
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            //tagContent = "";
            if ("product".equals(reader.getLocalName())) {
                product = _cjProductLocalService.createCJProduct(0);
            }
            break;
        case XMLStreamConstants.CHARACTERS:
            //tagContent += reader.getText().trim();
            tagContent = reader.getText().trim();
            break;
        case XMLStreamConstants.END_ELEMENT:
            switch (reader.getLocalName()) {
            case "product":
                try {

                    _log.warn("refreshing document...");
                    _cjProductLocalService.refresh(ftpSubscription, product);
                } catch (final SystemException e) {
                    _log.error(e);
                } catch (final PortalException e) {
                    _log.error(e);
                }
                break;
            case "programname":
                product.setProgramName(tagContent);
                break;
            case "programurl":
                product.setProgramUrl(tagContent);
                break;
            case "catalogname":
                product.setCatalogName(tagContent);
                break;
            case "lastupdated":
                product.setLastUpdated(tagContent);
                break;
            case "name":
                product.setName(tagContent);
                break;
            case "keywords":
                product.setKeywords(tagContent);
                break;
            case "description":
                product.setDescription(tagContent);
                break;
            case "sku":
                product.setSku(tagContent);
                break;
            case "manufacturer":
                product.setManufacturer(tagContent);
                break;
            case "manufacturerid":
                product.setManufacturerId(tagContent);
                break;
            case "currency":
                product.setCurrency(tagContent);
                break;
            case "price":
                product.setPrice(tagContent);
                break;
            case "buyurl":
                product.setBuyUrl(tagContent);
                break;
            case "impressionurl":
                product.setImpressionUrl(tagContent);
                break;
            case "imageurl":
                product.setImageUrl(tagContent);
                break;
            case "instock":
                product.setInStock(tagContent);
                break;
            }
            break;
        case XMLStreamConstants.START_DOCUMENT:
            break;
        }
    }
}