本文整理汇总了Java中javax.xml.stream.XMLStreamConstants.CHARACTERS属性的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamConstants.CHARACTERS属性的具体用法?Java XMLStreamConstants.CHARACTERS怎么用?Java XMLStreamConstants.CHARACTERS使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类javax.xml.stream.XMLStreamConstants
的用法示例。
在下文中一共展示了XMLStreamConstants.CHARACTERS属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readUntilEndElement
public static String readUntilEndElement(String endElementName, XMLStreamReader reader, XmlEventHandler eventHandler) throws XMLStreamException {
String text = null;
int event;
while (!((event = reader.next()) == XMLStreamConstants.END_ELEMENT
&& reader.getLocalName().equals(endElementName))) {
text = null;
switch (event) {
case XMLStreamConstants.START_ELEMENT:
if (eventHandler != null) {
eventHandler.onStartElement();
}
break;
case XMLStreamConstants.CHARACTERS:
text = reader.getText();
break;
default:
break;
}
}
return text;
}
示例2: nextTag
/**
* Skips any insignificant events (COMMENT and PROCESSING_INSTRUCTION) until
* a START_ELEMENT or END_ELEMENT is reached. If other than space characters
* are encountered, an exception is thrown. This method should be used when
* processing element-only content because the parser is not able to
* recognize ignorable whitespace if then DTD is missing or not interpreted.
*
* @return the event type of the element read
* @throws XMLStreamException if the current event is not white space
*/
public int nextTag() throws XMLStreamException {
int eventType = next();
while ((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
|| (eventType == XMLStreamConstants.CDATA && isWhiteSpace())
// skip whitespace
|| eventType == XMLStreamConstants.SPACE
|| eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
|| eventType == XMLStreamConstants.COMMENT) {
eventType = next();
}
if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
throw new XMLStreamException(
"found: " + getEventTypeString(eventType)
+ ", expected " + getEventTypeString(XMLStreamConstants.START_ELEMENT)
+ " or " + getEventTypeString(XMLStreamConstants.END_ELEMENT),
getLocation());
}
return eventType;
}
示例3: readText
/**
* Returns the text contents of the current element being parsed.
*
* @return The text contents of the current element being parsed.
* @throws XMLStreamException
*/
public String readText() throws XMLStreamException {
if (isInsideResponseHeader()) {
return getHeader(currentHeader);
}
if (currentEvent.isAttribute()) {
Attribute attribute = (Attribute)currentEvent;
return attribute.getValue();
}
StringBuilder sb = new StringBuilder();
while (true) {
XMLEvent event = eventReader.peek();
if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
eventReader.nextEvent();
sb.append(event.asCharacters().getData());
} else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) {
return sb.toString();
} else {
throw new RuntimeException("Encountered unexpected event: " + event.toString());
}
}
}
示例4: testStAX
@Test(dataProvider = "xml-data")
public void testStAX(String xml, int chunkSize, int expectedNumOfChunks, boolean withinLimit) throws Exception {
XMLInputFactory xifactory = XMLInputFactory.newInstance();
xifactory.setProperty("http://java.sun.com/xml/stream/properties/report-cdata-event", true);
if (chunkSize > 0) {
xifactory.setProperty(CDATA_CHUNK_SIZE, chunkSize);
}
XMLStreamReader streamReader = xifactory.createXMLStreamReader(new StringReader(xml));
StringBuilder cdata = new StringBuilder();
int numOfChunks = 0;
boolean isWithinLimit = true;
while (streamReader.hasNext()) {
int eventType = streamReader.next();
switch (eventType) {
case XMLStreamConstants.START_ELEMENT:
debugPrint("\nElement: " + streamReader.getLocalName());
break;
case XMLStreamConstants.CDATA:
String text = streamReader.getText();
numOfChunks++;
if (text.length() > chunkSize) {
isWithinLimit = false;
}
debugPrint("\nCDATA: " + text.length());
cdata.append(text);
break;
case XMLStreamConstants.CHARACTERS:
debugPrint("\nCharacters: " + streamReader.getText().length());
break;
}
}
debugPrint("CData in single chunk:" + cdata.toString().length());
Assert.assertEquals(numOfChunks, expectedNumOfChunks);
Assert.assertEquals(isWithinLimit, withinLimit);
}
示例5: readXML
private void readXML(byte[] xmlData, String expectedContent)
throws Exception {
InputStream stream = new ByteArrayInputStream(xmlData);
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader xmlReader
= factory.createXMLStreamReader(stream);
boolean inTestElement = false;
StringBuilder sb = new StringBuilder();
while (xmlReader.hasNext()) {
String ename;
switch (xmlReader.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
ename = xmlReader.getLocalName();
if (ename.equals("writeCharactersWithString")
|| ename.equals("writeCharactersWithArray")) {
inTestElement = true;
}
break;
case XMLStreamConstants.END_ELEMENT:
ename = xmlReader.getLocalName();
if (ename.equals("writeCharactersWithString")
|| ename.equals("writeCharactersWithArray")) {
inTestElement = false;
String content = sb.toString();
System.out.println(ename + " text:'" + content + "' expected:'" + expectedContent+"'");
Assert.assertEquals(content, expectedContent);
sb.setLength(0);
}
break;
case XMLStreamConstants.CHARACTERS:
if (inTestElement) {
sb.append(xmlReader.getText());
}
break;
}
xmlReader.next();
}
}
示例6: testXMLResolver
@Test
public void testXMLResolver() {
try {
XMLInputFactory xifactory = XMLInputFactory.newInstance();
xifactory.setProperty(XMLInputFactory.RESOLVER, new MyStaxResolver());
File file = new File(getClass().getResource("XMLResolverTest.xml").getFile());
String systemId = file.toURI().toString();
InputStream entityxml = new FileInputStream(file);
XMLStreamReader streamReader = xifactory.createXMLStreamReader(systemId, entityxml);
while (streamReader.hasNext()) {
int eventType = streamReader.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
eventType = streamReader.next();
if (eventType == XMLStreamConstants.CHARACTERS) {
String text = streamReader.getText();
Assert.assertTrue(text.contains("replace2"));
}
}
}
} catch (XMLStreamException ex) {
if (ex.getNestedException() != null) {
ex.getNestedException().printStackTrace();
}
// ex.printStackTrace() ;
} catch (Exception io) {
io.printStackTrace();
}
}
示例7: getEventTypeString
public final static String getEventTypeString(int eventType) {
switch (eventType){
case XMLStreamConstants.START_ELEMENT:
return "START_ELEMENT";
case XMLStreamConstants.END_ELEMENT:
return "END_ELEMENT";
case XMLStreamConstants.PROCESSING_INSTRUCTION:
return "PROCESSING_INSTRUCTION";
case XMLStreamConstants.CHARACTERS:
return "CHARACTERS";
case XMLStreamConstants.COMMENT:
return "COMMENT";
case XMLStreamConstants.START_DOCUMENT:
return "START_DOCUMENT";
case XMLStreamConstants.END_DOCUMENT:
return "END_DOCUMENT";
case XMLStreamConstants.ENTITY_REFERENCE:
return "ENTITY_REFERENCE";
case XMLStreamConstants.ATTRIBUTE:
return "ATTRIBUTE";
case XMLStreamConstants.DTD:
return "DTD";
case XMLStreamConstants.CDATA:
return "CDATA";
}
return "UNKNOWN_EVENT_TYPE";
}
示例8: unmarshalNodeContent
private String unmarshalNodeContent(final NamespaceVersion nsVersion, final ModelNode node, final QName nodeElementName, final XMLEventReader reader) throws PolicyException {
StringBuilder valueBuffer = null;
loop:
while (reader.hasNext()) {
try {
final XMLEvent xmlParserEvent = reader.nextEvent();
switch (xmlParserEvent.getEventType()) {
case XMLStreamConstants.COMMENT:
break; // skipping the comments
case XMLStreamConstants.CHARACTERS:
valueBuffer = processCharacters(node.getType(), xmlParserEvent.asCharacters(), valueBuffer);
break;
case XMLStreamConstants.END_ELEMENT:
checkEndTagName(nodeElementName, xmlParserEvent.asEndElement());
break loop; // data exctraction for currently processed policy node is done
case XMLStreamConstants.START_ELEMENT:
final StartElement childElement = xmlParserEvent.asStartElement();
ModelNode childNode = addNewChildNode(nsVersion, node, childElement);
String value = unmarshalNodeContent(nsVersion, childNode, childElement.getName(), reader);
if (childNode.isDomainSpecific()) {
parseAssertionData(nsVersion, value, childNode, childElement);
}
break;
default:
throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0011_UNABLE_TO_UNMARSHALL_POLICY_XML_ELEM_EXPECTED()));
}
} catch (XMLStreamException e) {
throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0068_FAILED_TO_UNMARSHALL_POLICY_EXPRESSION(), e));
}
}
return (valueBuffer == null) ? null : valueBuffer.toString().trim();
}
示例9: supportXMLResolver
@Test(dataProvider = "supportXMLResolver")
public void supportXMLResolver(URI catalogFile, String xml, String expected) throws Exception {
String xmlSource = getClass().getResource(xml).getFile();
CatalogResolver cr = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalogFile);
XMLInputFactory xifactory = XMLInputFactory.newInstance();
xifactory.setProperty(XMLInputFactory.IS_COALESCING, true);
xifactory.setProperty(XMLInputFactory.RESOLVER, cr);
File file = new File(xmlSource);
String systemId = file.toURI().toASCIIString();
InputStream entityxml = new FileInputStream(file);
XMLStreamReader streamReader = xifactory.createXMLStreamReader(systemId, entityxml);
String result = null;
while (streamReader.hasNext()) {
int eventType = streamReader.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
eventType = streamReader.next();
if (eventType == XMLStreamConstants.CHARACTERS) {
result = streamReader.getText();
}
}
}
System.out.println(": expected [" + expected + "] <> actual [" + result.trim() + "]");
Assert.assertEquals(result.trim(), expected);
}
示例10: testEventReader
@Test(dataProvider = "xml-data")
public void testEventReader(String xml, int chunkSize, int expectedNumOfChunks, boolean withinLimit) throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty("http://java.sun.com/xml/stream/properties/report-cdata-event", true);
if (chunkSize > 0) {
xif.setProperty(CDATA_CHUNK_SIZE, chunkSize);
}
XMLEventReader reader = xif.createXMLEventReader(new StringReader(xml));
StringBuilder cdata = new StringBuilder();
int numOfChunks = 0;
boolean isWithinLimit = true;
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
//All text events get reported as Characters events
if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
Characters cdataEvent = (Characters) event;
if (cdataEvent.isCData()) {
String text = cdataEvent.getData();
numOfChunks++;
if (text.length() > chunkSize) {
isWithinLimit = false;
}
debugPrint("\nCDATA: " + text.length());
cdata.append(text);
}
}
}
debugPrint("CData in single chunk:" + cdata.toString().length());
Assert.assertEquals(numOfChunks, expectedNumOfChunks);
Assert.assertEquals(isWithinLimit, withinLimit);
}
示例11: handleFragmentedCharacters
private void handleFragmentedCharacters() throws XMLStreamException, SAXException {
buffer.setLength(0);
// Append characters of first character event
buffer.append(fastInfosetStreamReader.getTextCharacters(),
fastInfosetStreamReader.getTextStart(),
fastInfosetStreamReader.getTextLength());
// Consume all character
while(true) {
switch(fastInfosetStreamReader.peekNext()) {
case XMLStreamConstants.START_ELEMENT :
processBufferedText(true);
return;
case XMLStreamConstants.END_ELEMENT :
processBufferedText(false);
return;
case XMLStreamConstants.CHARACTERS :
case XMLStreamConstants.CDATA :
case XMLStreamConstants.SPACE :
// Append characters of second and subsequent character events
fastInfosetStreamReader.next();
buffer.append(fastInfosetStreamReader.getTextCharacters(),
fastInfosetStreamReader.getTextStart(),
fastInfosetStreamReader.getTextLength());
break;
default:
fastInfosetStreamReader.next();
}
}
}
示例12: handleStartElement
/**
* Parses the current element parser element. This method must only be called when the given stream reader points to a START_ELEMENT
*
* @param r the stream reader
* @return the deserialized element
* @throws XMLStreamException
*/
private Object handleStartElement( XMLStreamReader r ) throws XMLStreamException {
String currentObjectName = r.getLocalName().toLowerCase();
Map<String, String> attributes = new HashMap<>();
for( int i = 0; i < r.getAttributeCount(); i++ )
attributes.put( r.getAttributeLocalName( i ).toLowerCase(), r.getAttributeValue( i ) );
NessusObject nessusObject = nessusObjectByName.containsKey( currentObjectName ) ? nessusObjectByName.get( currentObjectName ) : defaultObject;
Object element = nessusObject.create( currentObjectName, attributes );
while( r.hasNext() ) {
switch( r.next() ) {
case XMLStreamConstants.START_ELEMENT:
Object child = handleStartElement( r );
nessusObject.gotChild( element, child );
break;
case XMLStreamConstants.CHARACTERS:
nessusObject.gotTextValue( element, r.getText() );
break;
case XMLStreamConstants.END_ELEMENT:
return element;
}
}
// should never happen the only return should be from END_ELEMENT
return null;
}
示例13: bridge
public void bridge() throws XMLStreamException {
try {
// remembers the nest level of elements to know when we are done.
int depth=0;
// if the parser is at the start tag, proceed to the first element
int event = fastInfosetStreamReader.getEventType();
if(event == XMLStreamConstants.START_DOCUMENT) {
// nextTag doesn't correctly handle DTDs
while( !fastInfosetStreamReader.isStartElement() )
event = fastInfosetStreamReader.next();
}
if( event!=XMLStreamConstants.START_ELEMENT)
throw new IllegalStateException("The current event is not START_ELEMENT\n but " + event);
// TODO: we don't have to rely on this hack --- we can just emulate
// start/end prefix mappings. But for now, I'll rely on this hack.
handleStartDocument(fastInfosetStreamReader.getNamespaceContext());
OUTER:
while(true) {
// These are all of the events listed in the javadoc for
// XMLEvent.
// The spec only really describes 11 of them.
switch (event) {
case XMLStreamConstants.START_ELEMENT :
handleStartElement();
depth++;
break;
case XMLStreamConstants.END_ELEMENT :
depth--;
handleEndElement();
if(depth==0) break OUTER;
break;
case XMLStreamConstants.CHARACTERS :
case XMLStreamConstants.CDATA :
case XMLStreamConstants.SPACE :
if (predictor.expectText()) {
// Peek at the next event to see if there are
// fragmented characters
event = fastInfosetStreamReader.peekNext();
if (event == XMLStreamConstants.END_ELEMENT)
processNonIgnorableText();
else if (event == XMLStreamConstants.START_ELEMENT)
processIgnorableText();
else
handleFragmentedCharacters();
}
break;
// otherwise simply ignore
}
event=fastInfosetStreamReader.next();
}
fastInfosetStreamReader.next(); // move beyond the end tag.
handleEndDocument();
} catch (SAXException e) {
throw new XMLStreamException(e);
}
}
示例14: 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();
}
示例15: getEventType
@Override
public int getEventType() {
if(xopReferencePresent)
return XMLStreamConstants.CHARACTERS;
return super.getEventType();
}