use of javax.xml.stream.events.Characters in project spring-framework by spring-projects.
the class AbstractXMLEventReader method getElementText.
@Override
public String getElementText() throws XMLStreamException {
checkIfClosed();
if (!peek().isStartElement()) {
throw new XMLStreamException("Not at START_ELEMENT");
}
StringBuilder builder = new StringBuilder();
while (true) {
XMLEvent event = nextEvent();
if (event.isEndElement()) {
break;
} else if (!event.isCharacters()) {
throw new XMLStreamException("Unexpected event [" + event + "] in getElementText()");
}
Characters characters = event.asCharacters();
if (!characters.isIgnorableWhiteSpace()) {
builder.append(event.asCharacters().getData());
}
}
return builder.toString();
}
use of javax.xml.stream.events.Characters in project uPortal by Jasig.
the class IndentingXMLEventWriter method beforeEndElement.
/**
* Prepare to end an element, by writing a new line and indentation.
*/
protected void beforeEndElement() {
final Set<StackState> state = scopeState.getFirst();
// but not data
if (depth > 0 && state.contains(StackState.WROTE_MARKUP) && !state.contains(StackState.WROTE_DATA)) {
final String indent = this.getIndent(depth - 1, indentSize);
final Characters indentEvent = xmlEventFactory.createCharacters(indent);
try {
wrappedWriter.add(indentEvent);
} catch (XMLStreamException e) {
// Ignore exceptions caused by indentation
}
}
}
use of javax.xml.stream.events.Characters in project uPortal by Jasig.
the class XMLEventBufferReader method getElementText.
@Override
public 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 cxf by apache.
the class StaxUtils method readDocElement.
public static Node readDocElement(Document doc, Node parent, XMLEvent ev, StreamToDOMContext context) throws XMLStreamException {
switch(ev.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
{
context.incrementCount();
Element e;
StartElement startElem = ev.asStartElement();
QName name = startElem.getName();
if (!StringUtils.isEmpty(name.getPrefix())) {
e = doc.createElementNS(name.getNamespaceURI(), name.getPrefix() + ":" + name.getLocalPart());
} else {
e = doc.createElementNS(name.getNamespaceURI(), name.getLocalPart());
}
e = (Element) parent.appendChild(e);
if (context.isRecordLoc()) {
context.setRecordLoc(addLocation(doc, e, startElem.getLocation(), context.isRecordLoc()));
}
if (context.isRepairing() && !isDeclared(e, name.getNamespaceURI(), name.getPrefix())) {
declare(e, name.getNamespaceURI(), name.getPrefix());
}
context.pushToStack(parent);
if (context.isThreshold() && innerElementLevelThreshold != -1 && context.getStackSize() >= innerElementLevelThreshold) {
throw new DepthExceededStaxException("reach the innerElementLevelThreshold:" + innerElementLevelThreshold);
}
if (context.isThreshold() && innerElementCountThreshold != -1 && context.getCount() >= innerElementCountThreshold) {
throw new DepthExceededStaxException("reach the innerElementCountThreshold:" + innerElementCountThreshold);
}
parent = e;
break;
}
case XMLStreamConstants.END_ELEMENT:
if (context.isStackEmpty()) {
return parent;
}
parent = context.popFromStack();
if (parent instanceof Document || parent instanceof DocumentFragment) {
return parent;
}
break;
case XMLStreamConstants.NAMESPACE:
Namespace ns = (Namespace) ev;
declare((Element) parent, ns.getNamespaceURI(), ns.getPrefix());
break;
case XMLStreamConstants.ATTRIBUTE:
Attribute at = (Attribute) ev;
QName qname = at.getName();
String attName = qname.getLocalPart();
String attPrefix = qname.getPrefix();
if (attPrefix != null && attPrefix.length() > 0) {
attName = attPrefix + ":" + attName;
}
Attr attr = doc.createAttributeNS(qname.getNamespaceURI(), attName);
attr.setValue(at.getValue());
((Element) parent).setAttributeNode(attr);
break;
case XMLStreamConstants.CHARACTERS:
if (parent != null) {
Characters characters = ev.asCharacters();
context.setRecordLoc(addLocation(doc, parent.appendChild(doc.createTextNode(characters.getData())), characters.getLocation(), context.isRecordLoc()));
}
break;
case XMLStreamConstants.COMMENT:
if (parent != null) {
parent.appendChild(doc.createComment(((javax.xml.stream.events.Comment) ev).getText()));
}
break;
case XMLStreamConstants.CDATA:
Characters characters = ev.asCharacters();
context.setRecordLoc(addLocation(doc, parent.appendChild(doc.createCDATASection(characters.getData())), characters.getLocation(), context.isRecordLoc()));
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
parent.appendChild(doc.createProcessingInstruction(((ProcessingInstruction) ev).getTarget(), ((ProcessingInstruction) ev).getData()));
break;
case XMLStreamConstants.ENTITY_REFERENCE:
javax.xml.stream.events.EntityReference er = (javax.xml.stream.events.EntityReference) ev;
parent.appendChild(doc.createEntityReference(er.getName()));
break;
default:
break;
}
return parent;
}
use of javax.xml.stream.events.Characters in project wso2-synapse by wso2.
the class SimpleXMLEventFactoryTest method testCreateSpace.
@Test
public void testCreateSpace() throws XMLStreamException {
Characters event = factory.createSpace(" ");
verify(event, XMLStreamConstants.CHARACTERS, " ");
Assert.assertTrue(event.isWhiteSpace());
Assert.assertFalse(event.isIgnorableWhiteSpace());
}
Aggregations