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);
}
}
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();
}
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;
}
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();
}
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));
}
Aggregations