@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"); } }
@Test public void testReplacing() throws Exception { JAXPTestUtilities.tryRunWithTmpPermission(() -> { XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setProperty("javax.xml.stream.isReplacingEntityReferences", true); StringReader sr = new StringReader(XML); XMLEventReader reader = factory.createXMLEventReader(sr); boolean sawUndef = false; boolean sawDef = false; while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); // System.out.println("Event: " + event); if (event.isEntityReference()) { EntityReference ref = (EntityReference) event; if ("def".equals(ref.getName())) { sawDef = true; } else if ("undef".equals(ref.getName())) { sawUndef = true; } else { throw new IllegalArgumentException("Unexpected entity name"); } } } Assert.assertEquals(false, sawDef); Assert.assertEquals(true, sawUndef); reader.close(); }, new FilePermission("/tmp/this/does/not/exist/but/that/is/ok", "read")); }
@Test public void testNotReplacing() throws Exception { JAXPTestUtilities.tryRunWithTmpPermission(() -> { XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setProperty("javax.xml.stream.isReplacingEntityReferences", false); StringReader sr = new StringReader(XML); XMLEventReader reader = factory.createXMLEventReader(sr); boolean sawUndef = false; boolean sawDef = false; while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); // System.out.println("Event: " + event); if (event.isEntityReference()) { EntityReference ref = (EntityReference) event; if ("def".equals(ref.getName())) { sawDef = true; } else if ("undef".equals(ref.getName())) { sawUndef = true; } else { throw new IllegalArgumentException("Unexpected entity name"); } } } Assert.assertEquals(true, sawDef); Assert.assertEquals(true, sawUndef); reader.close(); }, new FilePermission("/tmp/this/does/not/exist/but/that/is/ok", "read")); }
@Test public void testReplacing() throws Exception { XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setProperty("javax.xml.stream.isReplacingEntityReferences", true); StringReader sr = new StringReader(XML); XMLEventReader reader = factory.createXMLEventReader(sr); boolean sawUndef = false; boolean sawDef = false; while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); // System.out.println("Event: " + event); if (event.isEntityReference()) { EntityReference ref = (EntityReference) event; if ("def".equals(ref.getName())) { sawDef = true; } else if ("undef".equals(ref.getName())) { sawUndef = true; } else { throw new IllegalArgumentException("Unexpected entity name"); } } } Assert.assertEquals(false, sawDef); Assert.assertEquals(true, sawUndef); reader.close(); }
@Test public void testNotReplacing() throws Exception { XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setProperty("javax.xml.stream.isReplacingEntityReferences", false); StringReader sr = new StringReader(XML); XMLEventReader reader = factory.createXMLEventReader(sr); boolean sawUndef = false; boolean sawDef = false; while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); // System.out.println("Event: " + event); if (event.isEntityReference()) { EntityReference ref = (EntityReference) event; if ("def".equals(ref.getName())) { sawDef = true; } else if ("undef".equals(ref.getName())) { sawUndef = true; } else { throw new IllegalArgumentException("Unexpected entity name"); } } } Assert.assertEquals(true, sawDef); Assert.assertEquals(true, sawUndef); reader.close(); }
private void handleEntityReference(EntityReference reference) throws SAXException { if (getLexicalHandler() != null) { getLexicalHandler().startEntity(reference.getName()); } if (getLexicalHandler() != null) { getLexicalHandler().endEntity(reference.getName()); } }
@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(); }
@Override public EntityReference createEntityReference(String name, EntityDeclaration entityDeclaration) { EntityReferenceEvent event = new EntityReferenceEvent(name, entityDeclaration); if(location != null)event.setLocation(location); return event; }
@Override public EntityReference createEntityReference(String name, EntityDeclaration declaration) { return defaultImpl.createEntityReference(name, declaration); }
@Override public EntityReference createEntityReference(String name, EntityDeclaration declaration) { 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(); } }
public EntityReference createEntityReference(String name, EntityDeclaration declaration) { return new EntityReferenceImpl(location, declaration, name); }
@Override public EntityReference createEntityReference( String name, EntityDeclaration declaration ) { return delegate.createEntityReference( name, declaration ); }
public EntityReference createEntityReference(String name, EntityDeclaration declaration) { return new EntityReferenceImpl(name, declaration, fLocation); }
public void entityReference(EntityReference event) throws XMLStreamException { fEventWriter.add(event); }
/** * Create an entity reference event. */ public abstract EntityReference createEntityReference(String name, EntityDeclaration declaration);
public void entityReference(EntityReference event) throws XMLStreamException { fStreamWriter.writeEntityRef(event.getName()); }
public void entityReference(EntityReference event) throws XMLStreamException;