本文整理汇总了Java中javax.xml.stream.XMLStreamConstants.CDATA属性的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamConstants.CDATA属性的具体用法?Java XMLStreamConstants.CDATA怎么用?Java XMLStreamConstants.CDATA使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类javax.xml.stream.XMLStreamConstants
的用法示例。
在下文中一共展示了XMLStreamConstants.CDATA属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: nextTagSkipDTD
/**
* A copy of the nextTag algorithm from the XMLStreamReader javadocs,
* but which also skips over DTD events as well as whitespace,
* comments and PIs.
*
* @param xsr the reader to advance
* @return {@link XMLStreamConstants#START_ELEMENT} or
* {@link XMLStreamConstants#END_ELEMENT} for the next tag.
* @throws XMLStreamException
*/
private static int nextTagSkipDTD(XMLStreamReader xsr)
throws XMLStreamException {
int eventType = xsr.next();
while((eventType == XMLStreamConstants.CHARACTERS && xsr.isWhiteSpace())
|| (eventType == XMLStreamConstants.CDATA && xsr.isWhiteSpace())
|| eventType == XMLStreamConstants.SPACE
|| eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
|| eventType == XMLStreamConstants.COMMENT
|| eventType == XMLStreamConstants.DTD) {
eventType = xsr.next();
}
if(eventType != XMLStreamConstants.START_ELEMENT
&& eventType != XMLStreamConstants.END_ELEMENT) {
throw new XMLStreamException("expected start or end tag", xsr
.getLocation());
}
return eventType;
}
示例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: getEventTypeName
private static String getEventTypeName(int eventType) {
switch (eventType) {
case XMLStreamConstants.START_ELEMENT:
return "START_ELEMENT";
case XMLStreamConstants.END_ELEMENT:
return "END_ELEMENT";
case XMLStreamConstants.SPACE:
return "SPACE";
case XMLStreamConstants.CHARACTERS:
return "CHARACTERS";
case XMLStreamConstants.PROCESSING_INSTRUCTION:
return "PROCESSING_INSTRUCTION";
case XMLStreamConstants.CDATA:
return "CDATA";
case XMLStreamConstants.COMMENT:
return "COMMENT";
case XMLStreamConstants.ENTITY_REFERENCE:
return "ENTITY_REFERENCE";
case XMLStreamConstants.START_DOCUMENT:
return "START_DOCUMENT";
case XMLStreamConstants.END_DOCUMENT:
return "END_DOCUMENT";
}
return null;
}
示例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: getEventName
/**
* Get the human readable event name for the numeric event id
*/
public static String getEventName(int eventId) {
switch (eventId) {
case XMLStreamConstants.START_ELEMENT:
return "StartElementEvent";
case XMLStreamConstants.END_ELEMENT:
return "EndElementEvent";
case XMLStreamConstants.PROCESSING_INSTRUCTION:
return "ProcessingInstructionEvent";
case XMLStreamConstants.CHARACTERS:
return "CharacterEvent";
case XMLStreamConstants.COMMENT:
return "CommentEvent";
case XMLStreamConstants.START_DOCUMENT:
return "StartDocumentEvent";
case XMLStreamConstants.END_DOCUMENT:
return "EndDocumentEvent";
case XMLStreamConstants.ENTITY_REFERENCE:
return "EntityReferenceEvent";
case XMLStreamConstants.ATTRIBUTE:
return "AttributeBase";
case XMLStreamConstants.DTD:
return "DTDEvent";
case XMLStreamConstants.CDATA:
return "CDATA";
}
return "UNKNOWN_EVENT_TYPE";
}
示例6: 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";
}
示例7: isWhiteSpace
/**
* Returns true if the cursor points to a character data event that consists of all whitespace
* Application calling this method needs to cache the value and avoid calling this method again
* for the same event.
* @return
*/
public boolean isWhiteSpace() {
if(isCharacters() || (fEventType == XMLStreamConstants.CDATA)){
char [] ch = this.getTextCharacters();
final int start = this.getTextStart();
final int end = start + this.getTextLength();
for (int i = start; i < end; i++){
if(!XMLChar.isSpace(ch[i])){
return false;
}
}
return true;
}
return false;
}
示例8: getElementText
/** Reads the content of a text-only element. Precondition:
* the current event is START_ELEMENT. Postcondition:
* The current event is the corresponding END_ELEMENT.
* @throws XMLStreamException if the current event is not a START_ELEMENT or if
* a non text element is encountered
*/
public String getElementText() throws XMLStreamException {
if(getEventType() != XMLStreamConstants.START_ELEMENT) {
throw new XMLStreamException(
"parser must be on START_ELEMENT to read next text", getLocation());
}
int eventType = next();
StringBuffer content = new StringBuffer();
while(eventType != XMLStreamConstants.END_ELEMENT ) {
if(eventType == XMLStreamConstants.CHARACTERS
|| eventType == XMLStreamConstants.CDATA
|| eventType == XMLStreamConstants.SPACE
|| eventType == XMLStreamConstants.ENTITY_REFERENCE) {
content.append(getText());
} else if(eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
|| eventType == XMLStreamConstants.COMMENT) {
// skipping
} else if(eventType == XMLStreamConstants.END_DOCUMENT) {
throw new XMLStreamException("unexpected end of document when reading element text content");
} else if(eventType == XMLStreamConstants.START_ELEMENT) {
throw new XMLStreamException(
"elementGetText() function expects text only elment but START_ELEMENT was encountered.", getLocation());
} else {
throw new XMLStreamException(
"Unexpected event type "+ eventType, getLocation());
}
eventType = next();
}
return content.toString();
}
示例9: isWhiteSpace
/**
* Returns true if the cursor points to a character data event that consists
* of all whitespace Application calling this method needs to cache the
* value and avoid calling this method again for the same event.
*
* @return
*/
public boolean isWhiteSpace() {
if (isCharacters() || (fEventType == XMLStreamConstants.CDATA)) {
char[] ch = this.getTextCharacters();
final int start = this.getTextStart();
final int end = start + this.getTextLength();
for (int i = start; i < end; i++) {
if (!XMLChar.isSpace(ch[i])) {
return false;
}
}
return true;
}
return false;
}
示例10: 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();
}
}
}
示例11: readTextWithNodes
/**
* Processes the TextWithNodes element from this XMLStreamReader,
* returning the text content of the document. The supplied map is
* updated with the offset of each Node element encountered. The
* reader must be positioned on the starting TextWithNodes tag and
* will be returned positioned on the corresponding closing tag.
*
* @param xsr
* @param nodeIdToOffsetMap
* @return the text content of the document
*/
public static String readTextWithNodes(XMLStreamReader xsr,
Map<Integer, Long> nodeIdToOffsetMap) throws XMLStreamException {
StringBuffer textBuf = new StringBuffer(20480);
int eventType;
while((eventType = xsr.next()) != XMLStreamConstants.END_ELEMENT) {
switch(eventType) {
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.CDATA:
textBuf.append(xsr.getTextCharacters(), xsr.getTextStart(), xsr
.getTextLength());
break;
case XMLStreamConstants.START_ELEMENT:
// only Node elements allowed
xsr.require(XMLStreamConstants.START_ELEMENT, null, "Node");
String idString = xsr.getAttributeValue(null, "id");
if(idString == null) {
throw new XMLStreamException("Node element has no id", xsr
.getLocation());
}
try {
Integer id = Integer.valueOf(idString);
Long offset = Long.valueOf(textBuf.length());
nodeIdToOffsetMap.put(id, offset);
}
catch(NumberFormatException nfe) {
throw new XMLStreamException("Node element must have "
+ "integer id", xsr.getLocation());
}
// Node element must be empty
if(xsr.next() != XMLStreamConstants.END_ELEMENT) {
throw new XMLStreamException("Node element within TextWithNodes "
+ "must be empty.", xsr.getLocation());
}
break;
default:
// do nothing - ignore comments, PIs...
}
}
return textBuf.toString();
}
示例12: 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();
}
示例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: 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 = staxStreamReader.getEventType();
if(event == XMLStreamConstants.START_DOCUMENT) {
// nextTag doesn't correctly handle DTDs
while( !staxStreamReader.isStartElement() )
event = staxStreamReader.next();
}
if( event!=XMLStreamConstants.START_ELEMENT)
throw new IllegalStateException("The current event is not START_ELEMENT\n but " + event);
handleStartDocument(staxStreamReader.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 :
handleCharacters();
break;
// otherwise simply ignore
}
event=staxStreamReader.next();
}
staxStreamReader.next(); // move beyond the end tag.
handleEndDocument();
} catch (SAXException e) {
throw new XMLStreamException(e);
}
}
示例15: bridge
public void bridge() throws XMLStreamException {
try {
// remembers the nest level of elements to know when we are done.
int depth=0;
event = staxEventReader.peek();
if( !event.isStartDocument() && !event.isStartElement() )
throw new IllegalStateException();
// if the parser is on START_DOCUMENT, skip ahead to the first element
do {
event = staxEventReader.nextEvent();
} while( !event.isStartElement() );
handleStartDocument(event.asStartElement().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.getEventType()) {
case XMLStreamConstants.START_ELEMENT :
handleStartElement(event.asStartElement());
depth++;
break;
case XMLStreamConstants.END_ELEMENT :
depth--;
handleEndElement(event.asEndElement());
if(depth==0) break OUTER;
break;
case XMLStreamConstants.CHARACTERS :
case XMLStreamConstants.CDATA :
case XMLStreamConstants.SPACE :
handleCharacters(event.asCharacters());
break;
}
event=staxEventReader.nextEvent();
}
handleEndDocument();
event = null; // avoid keeping a stale reference
} catch (SAXException e) {
throw new XMLStreamException(e);
}
}