use of javax.xml.stream.events.Characters in project wso2-synapse by wso2.
the class SimpleXMLEventAllocatorTest method testCharactersCData.
@Test
public void testCharactersCData() throws XMLStreamException {
XMLStreamReader reader = createXmlStreamReader("<alice>bob</alice>");
reader.next();
Characters characters = eventAllocator.allocate(reader).asCharacters();
verify(characters, XMLStreamConstants.CHARACTERS, "bob");
Assert.assertEquals("bob", characters.getData());
Assert.assertFalse(characters.isCData());
Assert.assertFalse(characters.isWhiteSpace());
Assert.assertFalse(characters.isIgnorableWhiteSpace());
reader = createXmlStreamReader("<alice><![CDATA[bob]]></alice>");
reader.next();
characters = eventAllocator.allocate(reader).asCharacters();
verify(characters, XMLStreamConstants.CDATA, "<![CDATA[bob]]>");
Assert.assertEquals("bob", characters.getData());
Assert.assertTrue(characters.isCData());
reader = createXmlStreamReader("<alice><![CDATA[ ]]></alice>");
reader.next();
characters = eventAllocator.allocate(reader).asCharacters();
verify(characters, XMLStreamConstants.CDATA, "<![CDATA[ ]]>");
Assert.assertEquals(" ", characters.getData());
Assert.assertTrue(characters.isWhiteSpace());
Assert.assertFalse(characters.isIgnorableWhiteSpace());
}
use of javax.xml.stream.events.Characters in project wso2-synapse by wso2.
the class SimpleXMLEventFactoryTest method testCreateCharacters.
@Test
public void testCreateCharacters() throws XMLStreamException {
Characters event = factory.createCharacters("foobar");
verify(event, XMLStreamConstants.CHARACTERS, "foobar");
}
use of javax.xml.stream.events.Characters in project wso2-synapse by wso2.
the class XMLEventWriterDelegate method add.
public void add(XMLEvent event) throws XMLStreamException {
switch(event.getEventType()) {
case XMLStreamConstants.ATTRIBUTE:
Attribute attribute = (Attribute) event;
QName attrName = attribute.getName();
delegate.writeAttribute(attrName.getPrefix(), attrName.getNamespaceURI(), attrName.getLocalPart(), attribute.getValue());
break;
case XMLStreamConstants.END_DOCUMENT:
delegate.writeEndDocument();
break;
case XMLStreamConstants.END_ELEMENT:
delegate.writeEndElement();
break;
case XMLStreamConstants.NAMESPACE:
Namespace namespace = (Namespace) event;
delegate.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI());
break;
case XMLStreamConstants.START_DOCUMENT:
StartDocument startDocument = (StartDocument) event;
if (startDocument.encodingSet()) {
// encoding defined?
delegate.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion());
} else {
delegate.writeStartDocument(startDocument.getVersion());
}
break;
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
QName elemName = startElement.getName();
delegate.writeStartElement(elemName.getPrefix(), elemName.getLocalPart(), elemName.getNamespaceURI());
Iterator<?> namespaces = startElement.getNamespaces();
while (namespaces.hasNext()) {
add((Namespace) namespaces.next());
}
Iterator<?> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
add((Attribute) attributes.next());
}
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.CDATA:
Characters characters = event.asCharacters();
if (characters.isCData()) {
delegate.writeCData(characters.getData());
} else {
delegate.writeCharacters(characters.getData());
}
break;
case XMLStreamConstants.COMMENT:
delegate.writeComment(((Comment) event).getText());
break;
case XMLStreamConstants.DTD:
delegate.writeDTD(((DTD) event).getDocumentTypeDeclaration());
break;
case XMLStreamConstants.ENTITY_REFERENCE:
delegate.writeEntityRef(((EntityReference) event).getName());
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
ProcessingInstruction processingInstruction = (ProcessingInstruction) event;
delegate.writeProcessingInstruction(processingInstruction.getTarget(), processingInstruction.getData());
break;
case XMLStreamConstants.SPACE:
break;
default:
throw new XMLStreamException("Cannot write event " + event);
}
}
use of javax.xml.stream.events.Characters in project nebula.widgets.nattable by eclipse.
the class RegexMarkupValue method applyMarkup.
@Override
public String applyMarkup(String input) {
String result = "";
if (getOriginalRegexValue() != null && !getOriginalRegexValue().isEmpty()) {
XMLEventReader parser = null;
try {
parser = this.factory.createXMLEventReader(new StringReader(RichTextPainter.FAKE_ROOT_TAG_START + input + RichTextPainter.FAKE_ROOT_TAG_END));
while (parser.hasNext()) {
XMLEvent event = parser.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
break;
case XMLStreamConstants.END_DOCUMENT:
parser.close();
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
String text = characters.getData();
if (this.caseInsensitive) {
int flags = this.unicodeCase ? Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE : Pattern.CASE_INSENSITIVE;
result += Pattern.compile(getOriginalRegexValue(), flags).matcher(text).replaceAll(this.markupValue);
} else {
result += Pattern.compile(getOriginalRegexValue()).matcher(text).replaceAll(this.markupValue);
}
break;
default:
result += event.toString();
}
}
} catch (XMLStreamException e) {
e.printStackTrace();
} finally {
if (parser != null) {
try {
parser.close();
} catch (XMLStreamException e) {
e.printStackTrace();
}
}
}
result = result.replace(RichTextPainter.FAKE_ROOT_TAG_START, "").replace(RichTextPainter.FAKE_ROOT_TAG_END, "");
} else {
result = input;
}
return result;
}
use of javax.xml.stream.events.Characters in project hibernate-orm by hibernate.
the class BaseXMLEventReader method 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();
}
Aggregations