@Override public String getText() { XMLSecEvent xmlSecEvent = getCurrentEvent(); switch (xmlSecEvent.getEventType()) { case ENTITY_REFERENCE: return ((EntityReference) xmlSecEvent).getDeclaration().getReplacementText(); case DTD: return ((DTD) xmlSecEvent).getDocumentTypeDeclaration(); case COMMENT: return ((Comment) xmlSecEvent).getText(); case CDATA: case SPACE: case CHARACTERS: return xmlSecEvent.asCharacters().getData(); default: throw new IllegalStateException("Current state not TEXT"); } }
@Override public char[] getTextCharacters() { XMLSecEvent xmlSecEvent = getCurrentEvent(); switch (xmlSecEvent.getEventType()) { case ENTITY_REFERENCE: return ((EntityReference) xmlSecEvent).getDeclaration().getReplacementText().toCharArray(); case DTD: return ((DTD) xmlSecEvent).getDocumentTypeDeclaration().toCharArray(); case COMMENT: return ((Comment) xmlSecEvent).getText().toCharArray(); case CDATA: case SPACE: case CHARACTERS: return xmlSecEvent.asCharacters().getText(); default: throw new IllegalStateException("Current state not TEXT"); } }
@Override public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) throws XMLStreamException { XMLSecEvent xmlSecEvent = getCurrentEvent(); switch (xmlSecEvent.getEventType()) { case ENTITY_REFERENCE: ((EntityReference) xmlSecEvent).getDeclaration().getReplacementText().getChars(sourceStart, sourceStart + length, target, targetStart); return length; case DTD: ((DTD) xmlSecEvent).getDocumentTypeDeclaration().getChars(sourceStart, sourceStart + length, target, targetStart); return length; case COMMENT: ((Comment) xmlSecEvent).getText().getChars(sourceStart, sourceStart + length, target, targetStart); return length; case CDATA: case SPACE: case CHARACTERS: xmlSecEvent.asCharacters().getData().getChars(sourceStart, sourceStart + length, target, targetStart); return length; default: throw new IllegalStateException("Current state not TEXT"); } }
@Override public int getTextLength() { XMLSecEvent xmlSecEvent = getCurrentEvent(); switch (xmlSecEvent.getEventType()) { case ENTITY_REFERENCE: return ((EntityReference) xmlSecEvent).getDeclaration().getReplacementText().length(); case DTD: return ((DTD) xmlSecEvent).getDocumentTypeDeclaration().length(); case COMMENT: return ((Comment) xmlSecEvent).getText().length(); case CDATA: case SPACE: case CHARACTERS: return xmlSecEvent.asCharacters().getData().length(); default: throw new IllegalStateException("Current state not TEXT"); } }
@Override public String getText() { if (this.event.isCharacters()) { return event.asCharacters().getData(); } else if (this.event.getEventType() == XMLEvent.COMMENT) { return ((Comment) this.event).getText(); } else { throw new IllegalStateException(); } }
private void processEvent(XmlParserContext ctx, XMLEvent xmlEvent) throws BagriException { if (ctx.getDocRoot() == null) { ctx.firstEvents.add(xmlEvent); if (xmlEvent.getEventType() == XMLStreamConstants.START_ELEMENT) { String root = "/" + xmlEvent.asStartElement().getName(); ctx.addDocument(root); for (XMLEvent event: ctx.firstEvents) { processEvent(ctx, event); } } } else { switch (xmlEvent.getEventType()) { case XMLStreamConstants.START_ELEMENT: startElement(ctx, xmlEvent.asStartElement()); break; case XMLStreamConstants.CHARACTERS: if (!xmlEvent.asCharacters().isWhiteSpace()) { ctx.addCharacters(xmlEvent.asCharacters().getData()); } break; case XMLStreamConstants.END_ELEMENT: ctx.endElement(); break; case XMLStreamConstants.ATTRIBUTE: ctx.addAttribute(((Attribute) xmlEvent).getName(), ((Attribute) xmlEvent).getValue()); break; case XMLStreamConstants.COMMENT: ctx.addComment(((Comment) xmlEvent).getText()); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: ctx.addProcessingInstruction(((ProcessingInstruction) xmlEvent).getTarget(), ((ProcessingInstruction) xmlEvent).getData()); break; default: break; } } }
public String getText() { if (this.event.isCharacters()) { return event.asCharacters().getData(); } else if (this.event.getEventType() == XMLEvent.COMMENT) { return ((Comment) this.event).getText(); } else { throw new IllegalStateException(); } }
@Test public void testEvents() { XMLEventFactory f = XMLEventFactory.newInstance(); final String contents = "test <some> text & more! [[]] --"; final String prefix = "prefix"; final String uri = "http://foo"; final String localName = "elem"; try { StartDocument sd = f.createStartDocument(); writeAsEncodedUnicode(sd); Comment c = f.createComment("some comments"); writeAsEncodedUnicode(c); StartElement se = f.createStartElement(prefix, uri, localName); ProcessingInstruction pi = f.createProcessingInstruction("target", "data"); writeAsEncodedUnicode(pi); Namespace ns = f.createNamespace(prefix, uri); writeAsEncodedUnicode(ns); Characters characters = f.createCharacters(contents); writeAsEncodedUnicode(characters); // CData Characters cdata = f.createCData(contents); writeAsEncodedUnicode(cdata); // Attribute QName attrName = new QName("http://test.com", "attr", "ns"); Attribute attr = f.createAttribute(attrName, "value"); writeAsEncodedUnicode(attr); // prefix, uri, localName EndElement ee = f.createEndElement(prefix, uri, localName); writeAsEncodedUnicode(ee); EndDocument ed = f.createEndDocument(); writeAsEncodedUnicode(ed); } catch (Exception e) { Assert.fail(e.getMessage()); } }
@Override public Comment createComment(String text) { return defaultImpl.createComment(text); }
@Override public Comment createComment(String text) { return null; }
@Override protected void parseInternal() throws SAXException, XMLStreamException { boolean documentStarted = false; boolean documentEnded = false; int elementDepth = 0; while (this.reader.hasNext() && elementDepth >= 0) { XMLEvent event = this.reader.nextEvent(); if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) { handleStartDocument(event); documentStarted = true; } switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: handleStartDocument(event); documentStarted = true; break; case XMLStreamConstants.START_ELEMENT: elementDepth++; handleStartElement(event.asStartElement()); break; case XMLStreamConstants.END_ELEMENT: elementDepth--; if (elementDepth >= 0) { handleEndElement(event.asEndElement()); } break; case XMLStreamConstants.PROCESSING_INSTRUCTION: handleProcessingInstruction((ProcessingInstruction) event); break; case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.SPACE: case XMLStreamConstants.CDATA: handleCharacters(event.asCharacters()); break; case XMLStreamConstants.END_DOCUMENT: handleEndDocument(); documentEnded = true; break; case XMLStreamConstants.NOTATION_DECLARATION: handleNotationDeclaration((NotationDeclaration) event); break; case XMLStreamConstants.ENTITY_DECLARATION: handleEntityDeclaration((EntityDeclaration) event); break; case XMLStreamConstants.COMMENT: handleComment((Comment) event); break; case XMLStreamConstants.DTD: handleDtd((DTD) event); break; case XMLStreamConstants.ENTITY_REFERENCE: handleEntityReference((EntityReference) event); break; } } if (documentStarted && !documentEnded) { handleEndDocument(); } }
private void handleComment(Comment comment) throws SAXException { if (getLexicalHandler() != null) { char[] ch = comment.getText().toCharArray(); getLexicalHandler().comment(ch, 0, ch.length); } }
public void add(XMLEvent event) throws XMLStreamException { QName name; String uri; switch (event.getEventType()) { case XMLStreamConstants.START_ELEMENT: StartElement startElement = event.asStartElement(); name = startElement.getName(); uri = name.getNamespaceURI(); if (uri != null && !"".equals(uri)) writer.writeStartElement(name.getPrefix(), name.getLocalPart(), uri); else writer.writeStartElement(name.getLocalPart()); break; case XMLStreamConstants.END_ELEMENT: writer.writeEndElement(); break; case XMLStreamConstants.ATTRIBUTE: Attribute attribute = (Attribute) event; name = attribute.getName(); uri = name.getNamespaceURI(); if (uri != null && !"".equals(uri)) writer.writeAttribute(name.getPrefix(), uri, name.getLocalPart(), attribute.getValue()); else writer.writeAttribute(name.getLocalPart(), attribute.getValue()); break; case XMLStreamConstants.NAMESPACE: Namespace namespace = (Namespace) event; uri = namespace.getNamespaceURI(); writer.writeNamespace(namespace.getPrefix(), uri); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: ProcessingInstruction pi = (ProcessingInstruction) event; String data = pi.getData(); if (data == null) writer.writeProcessingInstruction(pi.getTarget()); else writer.writeProcessingInstruction(pi.getTarget(), data); break; case XMLStreamConstants.COMMENT: Comment comment = (Comment) event; writer.writeComment(comment.getText()); break; case XMLStreamConstants.START_DOCUMENT: StartDocument startDocument = (StartDocument) event; writer.writeStartDocument(startDocument.getVersion()); break; case XMLStreamConstants.END_DOCUMENT: writer.writeEndDocument(); break; case XMLStreamConstants.DTD: DTD dtd = (DTD) event; writer.writeDTD(dtd.getDocumentTypeDeclaration()); break; case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.SPACE: Characters characters = event.asCharacters(); writer.writeCharacters(characters.getData()); break; case XMLStreamConstants.CDATA: Characters cdata = event.asCharacters(); writer.writeCData(cdata.getData()); break; } }
public Comment createComment(String text) { return new CommentImpl(location, text); }
@Override public Comment createComment( String text ) { return delegate.createComment( text ); }
public Comment createComment(String text) { return new CommentImpl(text, fLocation); }
public void comment(Comment event) throws XMLStreamException { fEventWriter.add(event); }
public void comment(Comment event) throws XMLStreamException { fStreamWriter.writeComment(event.getText()); }
private void processComment(final Comment comment, final NodeLocation location) { // Create and append Comment node final org.w3c.dom.Comment commentNode = doc.createComment(comment.getText()); state.getCurrentParentNode().appendChild(commentNode); state.createdNode(); }
/** * Create a comment event. */ public abstract Comment createComment(String text);
public void comment(Comment event) throws XMLStreamException;