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:org.slc.sli.modeling.xmi.reader.XmiReader.java

protected static final TaggedValue readTaggedValue(final XMLStreamReader reader) throws XMLStreamException {
    assertName(XmiElementName.TAGGED_VALUE, reader);
    final Identifier id = getId(reader);
    String dataValue = "";
    Identifier tagDefinition = null;//from  w  ww  .  jav  a2s  .  c om
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(XmiElementName.TAGGED_VALUE_DOT_DATA_VALUE, reader)) {
                dataValue = assertNotNull(readTaggedValueData(reader));
            } else if (match(XmiElementName.TAGGED_VALUE_DOT_TYPE, reader)) {
                tagDefinition = assertNotNull(readTaggedValueType(reader));
            } else {
                throw new AssertionError(reader.getLocalName());
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(XmiElementName.TAGGED_VALUE, reader);
            return new TaggedValue(id, EMPTY_TAGGED_VALUE_LIST, dataValue, tagDefinition);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.xmi.reader.XmiReader.java

protected static final String readTaggedValueData(final XMLStreamReader reader) throws XMLStreamException {
    assertName(XmiElementName.TAGGED_VALUE_DOT_DATA_VALUE, reader);
    final StringBuilder sb = new StringBuilder();
    while (reader.hasNext()) {
        reader.next();/*  w ww .jav  a 2s .  c o m*/
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            throw new AssertionError(reader.getLocalName());
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(XmiElementName.TAGGED_VALUE_DOT_DATA_VALUE, reader);
            return collapseWhitespace(sb.toString());
        }
        case XMLStreamConstants.CHARACTERS: {
            sb.append(reader.getText());
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.xmi.reader.XmiReader.java

protected static final List<TaggedValue> readTaggedValueGroup(final XMLStreamReader reader) {
    final List<TaggedValue> taggedValues = new LinkedList<TaggedValue>();
    try {/*from  w w  w  .j a v a  2  s  . co m*/
        if (match(XmiElementName.MODEL_ELEMENT_DOT_TAGGED_VALUE, reader)) {
            while (reader.hasNext()) {
                reader.next();
                switch (reader.getEventType()) {
                case XMLStreamConstants.START_ELEMENT: {
                    if ("TaggedValue".equals(reader.getLocalName())) {
                        taggedValues.add(assertNotNull(readTaggedValue(reader)));
                        break;
                    } else {
                        throw new AssertionError(reader.getLocalName());
                    }
                }
                case XMLStreamConstants.END_ELEMENT: {
                    if (match(XmiElementName.MODEL_ELEMENT_DOT_TAGGED_VALUE, reader)) {
                        return taggedValues;
                    } else {
                        throw new AssertionError(reader.getLocalName());
                    }
                }
                case XMLStreamConstants.CHARACTERS: {
                    // Ignore.
                    break;
                }
                default: {
                    throw new AssertionError(reader.getEventType());
                }
                }
            }
        } else {
            throw new AssertionError(reader.getLocalName());
        }
    } catch (final XMLStreamException e) {
        LOG.warn(e.getMessage());
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.xmi.reader.XmiReader.java

protected static final Identifier readTaggedValueType(final XMLStreamReader reader) throws XMLStreamException {
    assertName(XmiElementName.TAGGED_VALUE_DOT_TYPE, reader);
    Identifier reference = null;/*from w w  w  . j a  va2s  . c o  m*/
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(XmiElementName.TAG_DEFINITION, reader)) {
                reference = assertNotNull(readIdentifier(reader));
            } else {
                throw new AssertionError(reader.getLocalName());
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(XmiElementName.TAGGED_VALUE_DOT_TYPE, reader);
            return assertNotNull(reference);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.xmi.reader.XmiReader.java

protected static final Model readXMI(final XMLStreamReader reader) {
    try {//from  www. ja  v a2s. c  om
        if ("XMI".equals(reader.getLocalName())) {
            Model model = null;
            while (reader.hasNext()) {
                reader.next();
                switch (reader.getEventType()) {
                case XMLStreamConstants.START_ELEMENT: {
                    if ("XMI.header".equals(reader.getLocalName())) {
                        skipElement(reader, false);
                        break;
                    } else if ("XMI.content".equals(reader.getLocalName())) {
                        model = readContent(reader);
                        if (model == null) {
                            throw new IllegalStateException();
                        }
                        break;
                    } else {
                        throw new XmiRuntimeException("Expecting Foo element, got: " + reader.getLocalName());
                    }
                }
                case XMLStreamConstants.END_ELEMENT: {
                    if ("XMI".equals(reader.getLocalName())) {
                        if (model == null) {
                            throw new IllegalStateException();
                        }
                        return model;
                    } else {
                        throw new AssertionError(reader.getLocalName());
                    }
                }
                case XMLStreamConstants.CHARACTERS: {
                    // Ignore.
                    break;
                }
                default: {
                    throw new AssertionError(reader.getEventType());
                }
                }
            }
        } else {
            throw new AssertionError(reader.getLocalName());
        }
    } catch (final XMLStreamException e) {
        LOG.warn(e.getMessage());
    }
    return null;
}

From source file:org.slc.sli.modeling.xmi.reader.XmiReader.java

/**
 * Skips (recursively) over the element in question. Also useful during development.
 *
 * @param reader/*from  w  ww  .ja  v  a2  s .com*/
 *            The StAX {@link XMLStreamReader}.
 */
protected static final void skipElement(final XMLStreamReader reader, final boolean check)
        throws XMLStreamException {
    if (check) {
        throw new AssertionError(reader.getName());
    }
    final String localName = reader.getLocalName();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            skipElement(reader, check);
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (localName.equals(reader.getLocalName())) {
                return;
            } else {
                throw new AssertionError(reader.getLocalName());
            }
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.springmodules.remoting.xmlrpc.stax.AbstractStaxXmlRpcParser.java

/**
 * Creates a new Object from the current element being read in the specified
 * <code>StreamReader</code>.
 * /*from ww w. j  a  va 2  s.  co m*/
 * @param reader
 *          the <code>StreamReader</code>.
 * @return the created Object.
 * @throws XmlRpcInvalidPayloadException
 *           if the element contains an unknown child.
 * @see #parseArrayElement(XMLStreamReader)
 * @see #parseStructElement(XMLStreamReader)
 */
protected final XmlRpcElement parseValueElement(XMLStreamReader reader) throws XMLStreamException {

    while (reader.hasNext()) {
        int event = reader.next();
        String localName = null;

        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            localName = reader.getLocalName();

            if (XmlRpcElementNames.ARRAY.equals(localName)) {
                return parseArrayElement(reader);

            } else if (XmlRpcElementNames.BASE_64.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcBase64(source);

            } else if (XmlRpcElementNames.BOOLEAN.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcBoolean(source);

            } else if (XmlRpcElementNames.DATE_TIME.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcDateTime(source);

            } else if (XmlRpcElementNames.DOUBLE.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcDouble(source);

            } else if (XmlRpcElementNames.I4.equals(localName) || XmlRpcElementNames.INT.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcInteger(source);

            } else if (XmlRpcElementNames.STRING.equals(localName)
                    || XmlRpcElementNames.INT.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcString(source);

            } else if (XmlRpcElementNames.STRUCT.equals(localName)) {
                return parseStructElement(reader);

            } else {
                XmlRpcParsingUtils.handleUnexpectedElementFound(localName);
            }

        case XMLStreamConstants.CHARACTERS:
            String source = reader.getText();
            return new XmlRpcString(source);
        }
    }

    // we should not reach this point.
    return null;
}

From source file:org.talend.dataprep.schema.xls.streaming.StreamingSheetReader.java

/**
 * Handles a Stream event./*from   ww  w  .  j  a va 2  s . c  om*/
 *
 * @param event
 * @throws SAXException
 */
private void handleEvent(XMLEvent event) throws SAXException {
    if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
        Characters c = event.asCharacters();
        lastContents += c.getData();
    } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
        StartElement startElement = event.asStartElement();
        String tagLocalName = startElement.getName().getLocalPart();

        if ("row".equals(tagLocalName)) {
            Attribute rowIndex = startElement.getAttributeByName(new QName("r"));
            if (firstRowIndex == -1) {
                firstRowIndex = Integer.parseInt(rowIndex.getValue());
            }
            currentRow = new StreamingRow(Integer.parseInt(rowIndex.getValue()) - 1);
        } else if ("cols".equals(tagLocalName)) {
            parsingCols = true;
        } else if ("col".equals(tagLocalName) && parsingCols) {
            colNumber = colNumber + 1;
        } else if ("c".equals(tagLocalName)) {
            Attribute ref = startElement.getAttributeByName(new QName("r"));

            String[] coord = ref.getValue().split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
            currentCell = new StreamingCell(CellReference.convertColStringToIndex(coord[0]),
                    Integer.parseInt(coord[1]) - 1);
            setFormatString(startElement, currentCell);

            Attribute type = startElement.getAttributeByName(new QName("t"));
            if (type != null) {
                currentCell.setType(type.getValue());
            } else {
                currentCell.setType("n");
            }

            Attribute style = startElement.getAttributeByName(new QName("s"));
            if (style != null) {
                String indexStr = style.getValue();
                try {
                    int index = Integer.parseInt(indexStr);
                    currentCell.setCellStyle(stylesTable.getStyleAt(index));
                } catch (NumberFormatException nfe) {
                    LOGGER.warn("Ignoring invalid style index {}", indexStr);
                }
            }
            // we store the dimension as well to revert with this method when cols not found
            // can happen see xlsx attached here https://jira.talendforge.org/browse/TDP-1957
            // <dimension ref="A1:B60"/>
        } else if ("dimension".equals(tagLocalName)) {
            Attribute attribute = startElement.getAttributeByName(new QName("ref"));
            if (attribute != null) {
                this.dimension = attribute.getValue();
            }
        }

        // Clear contents cache
        lastContents = "";
    } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) {
        EndElement endElement = event.asEndElement();
        String tagLocalName = endElement.getName().getLocalPart();

        if ("v".equals(tagLocalName) || "t".equals(tagLocalName)) {
            currentCell.setRawContents(unformattedContents());
            currentCell.setContents(formattedContents());
        } else if ("row".equals(tagLocalName) && currentRow != null) {
            rowCache.add(currentRow);
        } else if ("c".equals(tagLocalName)) {
            currentRow.getCellMap().put(currentCell.getColumnIndex(), currentCell);
        } else if ("cols".equals(tagLocalName)) {
            parsingCols = false;
        }

    }
}

From source file:org.ut.biolab.medsavant.client.plugin.AppController.java

public AppDescriptor getDescriptorFromFile(File f) throws PluginVersionException {
    XMLStreamReader reader;/*from  w ww.j  a va 2 s .c  om*/

    try {
        JarFile jar = new JarFile(f);
        ZipEntry entry = jar.getEntry("plugin.xml");
        if (entry != null) {
            InputStream entryStream = jar.getInputStream(entry);
            reader = XMLInputFactory.newInstance().createXMLStreamReader(entryStream);
            String className = null;
            String id = null;
            String version = null;
            String sdkVersion = null;
            String name = null;
            String category = AppDescriptor.Category.UTILITY.toString();
            String currentElement = null;
            String currentText = "";
            do {
                switch (reader.next()) {
                case XMLStreamConstants.START_ELEMENT:
                    switch (readElement(reader)) {
                    case PLUGIN:
                        className = readAttribute(reader, AppDescriptor.PluginXMLAttribute.CLASS);

                        //category can be specified as an attribute or <property>.
                        category = readAttribute(reader, AppDescriptor.PluginXMLAttribute.CATEGORY);
                        break;

                    case ATTRIBUTE:
                        if ("sdk-version".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.ID))) {
                            sdkVersion = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE);
                        }
                        break;

                    case PARAMETER:
                        if ("name".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.ID))) {
                            name = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE);
                        }
                        break;

                    case PROPERTY:
                        if ("name".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.NAME))) {
                            name = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE);
                            if (name == null) {
                                currentElement = "name";
                            }
                        }

                        if ("version".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.NAME))) {
                            version = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE);
                            if (version == null) {
                                currentElement = "version";
                            }
                        }

                        if ("sdk-version"
                                .equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.NAME))) {
                            sdkVersion = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE);
                            if (sdkVersion == null) {
                                currentElement = "sdk-version";
                            }
                        }

                        if ("category".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.NAME))) {
                            category = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE);
                            if (category == null) {
                                currentElement = "category";
                            }
                        }

                        break;
                    }
                    break;

                case XMLStreamConstants.CHARACTERS:
                    if (reader.isWhiteSpace()) {
                        break;
                    } else if (currentElement != null) {
                        currentText += reader.getText().trim().replace("\t", "");
                    }
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    if (readElement(reader) == AppDescriptor.PluginXMLElement.PROPERTY) {
                        if (currentElement != null && currentText.length() > 0) {
                            if (currentElement.equals("name")) {
                                name = currentText;
                            } else if (currentElement.equals("sdk-version")) {
                                sdkVersion = currentText;
                            } else if (currentElement.equals("category")) {
                                category = currentText;
                            } else if (currentElement.equals("version")) {
                                version = currentText;
                            }
                        }
                        currentText = "";
                        currentElement = null;
                    }
                    break;

                case XMLStreamConstants.END_DOCUMENT:
                    reader.close();
                    reader = null;
                    break;
                }
            } while (reader != null);

            System.out.println(className + " " + name + " " + version);

            if (className != null && name != null && version != null) {
                return new AppDescriptor(className, version, name, sdkVersion, category, f);
            }
        }
    } catch (Exception x) {
        LOG.error("Error parsing plugin.xml from " + f.getAbsolutePath() + ": " + x);
    }
    throw new PluginVersionException(f.getName() + " did not contain a valid plugin");
}

From source file:tpt.dbweb.cat.io.TaggedTextXMLReader.java

private Iterator<TaggedText> getIterator(InputStream is, String errorMessageInfo) {

    XMLStreamReader tmpxsr = null;
    try {//from  w w  w.j a v a 2  s.  c  o m
        XMLInputFactory xif = XMLInputFactory.newInstance();
        xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
        xif.setProperty(XMLInputFactory.IS_VALIDATING, false);
        tmpxsr = xif.createXMLStreamReader(is);
    } catch (XMLStreamException | FactoryConfigurationError e) {
        e.printStackTrace();
        return null;
    }

    final XMLStreamReader xsr = tmpxsr;
    return new PeekIterator<TaggedText>() {

        @Override
        protected TaggedText internalNext() {
            ArrayList<TextSpan> openMarks = new ArrayList<>();
            StringBuilder pureTextSB = new StringBuilder();
            ArrayList<TextSpan> marks = new ArrayList<>();
            marks.add(new TextSpan(null, 0, 0));
            TaggedText tt = null;

            try {
                loop: while (xsr.hasNext()) {
                    xsr.next();
                    int event = xsr.getEventType();
                    switch (event) {
                    case XMLStreamConstants.START_ELEMENT:
                        if ("articles".equals(xsr.getLocalName())) {
                        } else if ("article".equals(xsr.getLocalName())) {
                            tt = new TaggedText();
                            for (int i = 0; i < xsr.getAttributeCount(); i++) {
                                if ("id".equals(xsr.getAttributeLocalName(i))) {
                                    tt.id = xsr.getAttributeValue(i);
                                }
                                tt.info().put(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i));
                            }

                        } else if ("mark".equals(xsr.getLocalName())) {
                            TextSpan tr = new TextSpan(null, pureTextSB.length(), pureTextSB.length());
                            for (int i = 0; i < xsr.getAttributeCount(); i++) {
                                tr.info().put(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i));
                            }

                            openMarks.add(tr);
                        } else if ("br".equals(xsr.getLocalName())) {
                            // TODO: how to propagate tags from the input to the output?
                        } else {
                            log.warn("ignore tag " + xsr.getLocalName());
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        if ("mark".equals(xsr.getLocalName())) {

                            // search corresponding <mark ...>
                            TextSpan tr = openMarks.remove(openMarks.size() - 1);
                            if (tr == null) {
                                log.warn("markend at " + xsr.getLocation().getCharacterOffset()
                                        + " has no corresponding mark tag");
                                break;
                            }

                            tr.end = pureTextSB.length();
                            marks.add(tr);

                        } else if ("article".equals(xsr.getLocalName())) {
                            tt.text = StringUtils.stripEnd(pureTextSB.toString().trim(), " \t\n");
                            pureTextSB = new StringBuilder();

                            tt.mentions = new ArrayList<>();
                            for (TextSpan mark : marks) {

                                String entity = mark.info().get("entity");
                                if (entity == null) {
                                    entity = mark.info().get("annotation");
                                }
                                if (entity != null) {
                                    EntityMention e = new EntityMention(tt.text, mark.start, mark.end, entity);
                                    String minMention = mark.info().get("min");
                                    String mention = e.getMention();
                                    if (minMention != null && !"".equals(minMention)) {
                                        Pattern p = Pattern.compile(Pattern.quote(minMention));
                                        Matcher m = p.matcher(mention);
                                        if (m.find()) {
                                            TextSpan min = new TextSpan(e.text, e.start + m.start(),
                                                    e.start + m.end());
                                            e.min = min;
                                            if (m.find()) {
                                                log.warn("found " + minMention + " two times in \"" + mention
                                                        + "\"");
                                            }
                                        } else {
                                            String prefix = Utility.findLongestPrefix(mention, minMention);
                                            log.warn("didn't find min mention '" + minMention + "' in text '"
                                                    + mention + "', longest prefix found: '" + prefix
                                                    + "' in article " + tt.id);
                                        }
                                    }

                                    mark.info().remove("min");
                                    mark.info().remove("entity");
                                    if (mark.info().size() > 0) {
                                        e.info().putAll(mark.info());
                                    }
                                    tt.mentions.add(e);
                                }
                            }
                            openMarks.clear();
                            marks.clear();
                            break loop;
                        }
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        String toadd = xsr.getText();
                        if (pureTextSB.length() == 0) {
                            toadd = StringUtils.stripStart(toadd, " \t\n");
                        }
                        if (toadd.contains("thanks")) {
                            log.info("test");
                        }
                        pureTextSB.append(toadd);
                        break;
                    }

                }
            } catch (XMLStreamException e) {
                log.error("{}", errorMessageInfo);
                throw new RuntimeException(e);
            }
            if (tt != null && tt.mentions != null) {
                tt.mentions.sort(null);
            }
            return tt;
        }
    };
}