Search in sources :

Example 1 with Characters

use of javax.xml.stream.events.Characters in project uPortal by Jasig.

the class StaxEventLexicalContentHandler method endCDATA.

/* (non-Javadoc)
     * @see org.xml.sax.ext.LexicalHandler#endCDATA()
     */
@Override
public void endCDATA() throws SAXException {
    final Characters event = eventFactory.createCData(cdata.toString());
    cdata = null;
    try {
        this.consumeEvent(event);
    } catch (XMLStreamException ex) {
        throw new SAXException("Could not create CDATA: " + ex.getMessage(), ex);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Characters(javax.xml.stream.events.Characters) SAXException(org.xml.sax.SAXException)

Example 2 with Characters

use of javax.xml.stream.events.Characters in project bazel by bazelbuild.

the class XmlResourceValues method nextTag.

public static XMLEvent nextTag(XMLEventReader eventReader) throws XMLStreamException {
    while (eventReader.hasNext() && !(eventReader.peek().isEndElement() || eventReader.peek().isStartElement())) {
        XMLEvent nextEvent = eventReader.nextEvent();
        if (nextEvent.isCharacters() && !nextEvent.asCharacters().isIgnorableWhiteSpace()) {
            Characters characters = nextEvent.asCharacters();
            // TODO(corysmith): Turn into a warning with the Path is available to add to it.
            // This case is when unexpected characters are thrown into the xml. Best case, it's a
            // incorrect comment type...
            logger.fine(String.format("Invalid characters [%s] found at %s", characters.getData(), characters.getLocation().getLineNumber()));
        }
    }
    return eventReader.nextEvent();
}
Also used : Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 3 with Characters

use of javax.xml.stream.events.Characters in project hadoop by apache.

the class NodeInfo method checkConf.

public static List<String> checkConf(InputStream in) {
    List<NodeInfo> nodes = null;
    List<String> errors = new ArrayList<String>();
    try {
        nodes = parseConf(in);
        if (nodes == null) {
            errors.add("bad conf file: top-level element not <configuration>");
        }
    } catch (XMLStreamException e) {
        errors.add("bad conf file: " + e.getMessage());
    }
    if (!errors.isEmpty()) {
        return errors;
    }
    Map<String, List<Integer>> duplicatedProperties = new HashMap<String, List<Integer>>();
    for (NodeInfo node : nodes) {
        StartElement element = node.getStartElement();
        int line = element.getLocation().getLineNumber();
        if (!element.getName().equals(new QName("property"))) {
            errors.add(String.format("Line %d: element not <property>", line));
            continue;
        }
        List<XMLEvent> events = node.getXMLEventsForQName(new QName("name"));
        if (events == null) {
            errors.add(String.format("Line %d: <property> has no <name>", line));
        } else {
            String v = null;
            for (XMLEvent event : events) {
                if (event.isAttribute()) {
                    v = ((Attribute) event).getValue();
                } else {
                    Characters c = node.getElement(event.asStartElement());
                    if (c != null) {
                        v = c.getData();
                    }
                }
                if (v == null || v.isEmpty()) {
                    errors.add(String.format("Line %d: <property> has an empty <name>", line));
                }
            }
            if (v != null && !v.isEmpty()) {
                List<Integer> lines = duplicatedProperties.get(v);
                if (lines == null) {
                    lines = new ArrayList<Integer>();
                    duplicatedProperties.put(v, lines);
                }
                lines.add(node.getStartElement().getLocation().getLineNumber());
            }
        }
        events = node.getXMLEventsForQName(new QName("value"));
        if (events == null) {
            errors.add(String.format("Line %d: <property> has no <value>", line));
        }
        for (QName qName : node.getDuplicatedQNames()) {
            if (!qName.equals(new QName("source"))) {
                errors.add(String.format("Line %d: <property> has duplicated <%s>s", line, qName));
            }
        }
    }
    for (Entry<String, List<Integer>> e : duplicatedProperties.entrySet()) {
        List<Integer> lines = e.getValue();
        if (1 < lines.size()) {
            errors.add(String.format("Line %s: duplicated <property>s for %s", StringUtils.join(", ", lines), e.getKey()));
        }
    }
    return errors;
}
Also used : HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Characters(javax.xml.stream.events.Characters) ArrayList(java.util.ArrayList) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with Characters

use of javax.xml.stream.events.Characters in project uPortal by Jasig.

the class BaseXMLEventReader method getElementText.

/* (non-Javadoc)
     * @see javax.xml.stream.XMLEventReader#getElementText()
     */
@Override
public final String getElementText() throws XMLStreamException {
    XMLEvent event = this.previousEvent;
    if (event == null) {
        throw new XMLStreamException("Must be on START_ELEMENT to read next text, element was null");
    }
    if (!event.isStartElement()) {
        throw new XMLStreamException("Must be on START_ELEMENT to read next text", event.getLocation());
    }
    final StringBuilder text = new StringBuilder();
    while (!event.isEndDocument()) {
        switch(event.getEventType()) {
            case XMLStreamConstants.CHARACTERS:
            case XMLStreamConstants.SPACE:
            case XMLStreamConstants.CDATA:
                {
                    final Characters characters = event.asCharacters();
                    text.append(characters.getData());
                    break;
                }
            case XMLStreamConstants.ENTITY_REFERENCE:
                {
                    final EntityReference entityReference = (EntityReference) event;
                    final EntityDeclaration declaration = entityReference.getDeclaration();
                    text.append(declaration.getReplacementText());
                    break;
                }
            case XMLStreamConstants.COMMENT:
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                {
                    // Ignore
                    break;
                }
            default:
                {
                    throw new XMLStreamException("Unexpected event type '" + XMLStreamConstantsUtils.getEventName(event.getEventType()) + "' encountered. Found event: " + event, event.getLocation());
                }
        }
        event = this.nextEvent();
    }
    return text.toString();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) EntityDeclaration(javax.xml.stream.events.EntityDeclaration) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent) EntityReference(javax.xml.stream.events.EntityReference)

Example 5 with Characters

use of javax.xml.stream.events.Characters in project uPortal by Jasig.

the class IndentingXMLEventWriter method afterEndDocument.

/**
 * Note that a document was ended.
 */
protected void afterEndDocument() {
    depth = 0;
    final Set<StackState> state = scopeState.getFirst();
    if (state.contains(StackState.WROTE_MARKUP) && !state.contains(StackState.WROTE_DATA)) {
        // but not data
        try {
            final String indent = getLineSeparator() + StringUtils.repeat("  ", 0);
            final Characters indentEvent = xmlEventFactory.createCharacters(indent);
            wrappedWriter.add(indentEvent);
        } catch (Exception ignored) {
        }
    }
    scopeState.clear();
    // start fresh
    scopeState.push(EnumSet.noneOf(StackState.class));
}
Also used : Characters(javax.xml.stream.events.Characters) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

Characters (javax.xml.stream.events.Characters)33 XMLEvent (javax.xml.stream.events.XMLEvent)21 XMLStreamException (javax.xml.stream.XMLStreamException)20 EndElement (javax.xml.stream.events.EndElement)11 StartElement (javax.xml.stream.events.StartElement)11 XMLEventReader (javax.xml.stream.XMLEventReader)6 QName (javax.xml.namespace.QName)5 XMLInputFactory (javax.xml.stream.XMLInputFactory)5 Test (org.junit.Test)5 Attribute (javax.xml.stream.events.Attribute)4 EntityDeclaration (javax.xml.stream.events.EntityDeclaration)4 EntityReference (javax.xml.stream.events.EntityReference)4 HashMap (java.util.HashMap)3 Namespace (javax.xml.stream.events.Namespace)3 StartDocument (javax.xml.stream.events.StartDocument)3 InputStream (java.io.InputStream)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 Base64 (java.util.Base64)2 ProcessingInstruction (javax.xml.stream.events.ProcessingInstruction)2