javax.xml.stream.events.Comment Java Examples

The following examples show how to use javax.xml.stream.events.Comment. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XMLEventStreamReader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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();
	}
}
 
Example #2
Source File: XmlFilterReader.java    From knox with Apache License 2.0 5 votes vote down vote up
private void processComment( Comment event ) {
  if( currentlyBuffering() ) {
    stack.peek().node.appendChild( document.createComment( event.getText() ) );
  } else {
    writer.write( "<!--" );
    writer.write( event.getText() );
    writer.write( "-->" );
  }
}
 
Example #3
Source File: XmlFilterReader.java    From knox with Apache License 2.0 5 votes vote down vote up
private void processEvent( XMLEvent event ) throws ParserConfigurationException, XPathExpressionException, IOException, XMLStreamException {
  int type = event.getEventType();
  switch( type ) {
    case XMLStreamConstants.START_DOCUMENT:
      processStartDocument( (StartDocument)event );
      break;
    case XMLStreamConstants.END_DOCUMENT:
      processEndDocument();
      break;
    case XMLStreamConstants.START_ELEMENT:
      if( parser.peek().getEventType() == XMLStreamConstants.END_ELEMENT ) {
        isEmptyElement = true;
      }
      processStartElement( event.asStartElement());
      break;
    case XMLStreamConstants.END_ELEMENT:
      processEndElement( event.asEndElement() );
      isEmptyElement = false;
      break;
    case XMLStreamConstants.CHARACTERS:
    case XMLStreamConstants.CDATA:
    case XMLStreamConstants.SPACE:
      processCharacters( event.asCharacters() );
      break;
    case XMLStreamConstants.COMMENT:
      processComment( (Comment)event );
      break;
    case XMLStreamConstants.DTD:
    case XMLStreamConstants.NAMESPACE:
    case XMLStreamConstants.ATTRIBUTE:
    case XMLStreamConstants.ENTITY_REFERENCE:
    case XMLStreamConstants.ENTITY_DECLARATION:
    case XMLStreamConstants.NOTATION_DECLARATION:
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
    default:
      // Fail if we run into any of these for now.
      throw new IllegalStateException( Integer.toString( type ) );
  }
}
 
Example #4
Source File: XMLEventStreamReader.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
	}
}
 
Example #5
Source File: XMLEventStreamReader.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public String getText() {
	if (this.event.isCharacters()) {
		return this.event.asCharacters().getData();
	}
	else if (this.event.getEventType() == XMLEvent.COMMENT) {
		return ((Comment) this.event).getText();
	}
	else {
		throw new IllegalStateException();
	}
}
 
Example #6
Source File: XMLEventStreamReader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public String getText() {
	if (this.event.isCharacters()) {
		return this.event.asCharacters().getData();
	}
	else if (this.event.getEventType() == XMLEvent.COMMENT) {
		return ((Comment) this.event).getText();
	}
	else {
		throw new IllegalStateException();
	}
}
 
Example #7
Source File: StaxEventXMLReader.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void handleComment(Comment comment) throws SAXException {
	if (getLexicalHandler() != null) {
		char[] ch = comment.getText().toCharArray();
		getLexicalHandler().comment(ch, 0, ch.length);
	}
}
 
Example #8
Source File: XmlMerger.java    From aion-germany with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Read all {@link javax.xml.stream.events.XMLEvent}'s from specified file and write them onto the {@link javax.xml.stream.XMLEventWriter}
 *
 * @param file
 *            File to import
 * @param skipRoot
 *            Skip-root flag
 * @param writer
 *            Destenation writer
 * @throws XMLStreamException
 *             On event reading/writing error.
 * @throws FileNotFoundException
 *             if the reading file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
 */
private void importFile(File file, boolean skipRoot, XMLEventWriter writer, Properties metadata) throws XMLStreamException, IOException {
	logger.debug("Appending file " + file);
	metadata.setProperty(file.getPath(), makeHash(file));

	XMLEventReader reader = null;

	try {
		reader = inputFactory.createXMLEventReader(new FileReader(file));

		QName firstTagQName = null;

		while (reader.hasNext()) {
			XMLEvent event = reader.nextEvent();

			// skip start and end of document.
			if (event.isStartDocument() || event.isEndDocument()) {
				continue;
			}
			// skip all comments.
			if (event instanceof Comment) {
				continue;
			}
			// skip white-spaces and all ignoreable white-spaces.
			if (event.isCharacters()) {
				if (event.asCharacters().isWhiteSpace() || event.asCharacters().isIgnorableWhiteSpace()) {
					continue;
				}
			}

			// modify root-tag of imported file.
			if (firstTagQName == null && event.isStartElement()) {
				firstTagQName = event.asStartElement().getName();

				if (skipRoot) {
					continue;
				}
				else {
					StartElement old = event.asStartElement();

					event = eventFactory.createStartElement(old.getName(), old.getAttributes(), null);
				}
			}

			// if root was skipped - skip root end too.
			if (event.isEndElement() && skipRoot && event.asEndElement().getName().equals(firstTagQName)) {
				continue;
			}

			// finally - write tag
			writer.add(event);
		}
	}
	finally {
		if (reader != null) {
			try {
				reader.close();
			}
			catch (Exception ignored) {
			}
		}
	}
}
 
Example #9
Source File: StaxEventXMLReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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();
	}

}
 
Example #10
Source File: StaxEventXMLReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void handleComment(Comment comment) throws SAXException {
	if (getLexicalHandler() != null) {
		char[] ch = comment.getText().toCharArray();
		getLexicalHandler().comment(ch, 0, ch.length);
	}
}
 
Example #11
Source File: StaxEventXMLReader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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();
	}

}
 
Example #12
Source File: Issue41Test.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@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());
    }

}
 
Example #13
Source File: XMLEventFactoryWrapper.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Comment createComment(String text) {
    return defaultImpl.createComment(text);
}
 
Example #14
Source File: XMLEventFactoryImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Comment createComment(String text) {
    return null;
}
 
Example #15
Source File: StaxEventXMLReader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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();
	}

}
 
Example #16
Source File: StaxEventXMLReader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private void handleComment(Comment comment) throws SAXException {
	if (getLexicalHandler() != null) {
		char[] ch = comment.getText().toCharArray();
		getLexicalHandler().comment(ch, 0, ch.length);
	}
}
 
Example #17
Source File: StaxEventXMLReader.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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();
	}

}
 
Example #18
Source File: StaxEventXMLReader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void handleComment(Comment comment) throws SAXException {
	if (getLexicalHandler() != null) {
		char[] ch = comment.getText().toCharArray();
		getLexicalHandler().comment(ch, 0, ch.length);
	}
}
 
Example #19
Source File: XmlUtils.java    From entity-fishing with Apache License 2.0 2 votes vote down vote up
/**
 * Compares two {@link XMLEvent} instances. This method delegates actual
 * matching to the appropriate overloaded method.
 * 
 * @param a
 *            The first event.
 * @param b
 *            The second event.
 * @return <code>true</code> if the events match, <code>false</code>
 *         otherwise.
 */
public static boolean eventsMatch(XMLEvent a, XMLEvent b) {

	if (a == b) {

		return true;

	} else if (a == null || b == null) {

		return false;

	} else if (a.getEventType() == b.getEventType()) {

		switch (a.getEventType()) {

		case XMLEvent.START_ELEMENT:
			return eventsMatch(a.asStartElement(), b.asStartElement());

		case XMLEvent.END_ELEMENT:
			return eventsMatch(a.asEndElement(), b.asEndElement());

		case XMLEvent.CDATA:
		case XMLEvent.SPACE:
		case XMLEvent.CHARACTERS:
			return eventsMatch(a.asCharacters(), b.asCharacters());

		case XMLEvent.COMMENT:
			return eventsMatch((Comment) a, (Comment) b);

		case XMLEvent.ENTITY_REFERENCE:
			return eventsMatch((EntityReference) a, (EntityReference) b);

		case XMLEvent.ATTRIBUTE:
			return eventsMatch((Attribute) a, (Attribute) b);

		case XMLEvent.NAMESPACE:
			return eventsMatch((Namespace) a, (Namespace) b);

		case XMLEvent.START_DOCUMENT:
			return eventsMatch((StartDocument) a, (StartDocument) b);

		case XMLEvent.END_DOCUMENT:
			return eventsMatch((EndDocument) a, (EndDocument) b);

		case XMLEvent.PROCESSING_INSTRUCTION:
			return eventsMatch((ProcessingInstruction) a, (ProcessingInstruction) b);

		case XMLEvent.DTD:
			return eventsMatch((DTD) a, (DTD) b);

		case XMLEvent.ENTITY_DECLARATION:
			return eventsMatch((EntityDeclaration) a, (EntityDeclaration) b);

		case XMLEvent.NOTATION_DECLARATION:
			return eventsMatch((NotationDeclaration) a, (NotationDeclaration) b);

		}

	}

	return false;

}