Java Code Examples for javax.xml.transform.stax.StAXSource#getXMLStreamReader()

The following examples show how to use javax.xml.transform.stax.StAXSource#getXMLStreamReader() . 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: SourceHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void readStAXSourceExternal() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	converter.setSupportDtd(true);
	StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
	XMLStreamReader streamReader = result.getXMLStreamReader();
	assertTrue(streamReader.hasNext());
	streamReader.next();
	streamReader.next();
	String s = streamReader.getLocalName();
	assertEquals("root", s);
	try {
		s = streamReader.getElementText();
		assertNotEquals("Foo Bar", s);
	}
	catch (XMLStreamException ex) {
		// Some parsers raise a parse exception
	}
	streamReader.close();
}
 
Example 2
Source File: SourceHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void readStAXSourceExternal() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	converter.setSupportDtd(true);
	StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
	XMLStreamReader streamReader = result.getXMLStreamReader();
	assertTrue(streamReader.hasNext());
	streamReader.next();
	streamReader.next();
	String s = streamReader.getLocalName();
	assertEquals("root", s);
	try {
		s = streamReader.getElementText();
		assertNotEquals("Foo Bar", s);
	}
	catch (XMLStreamException ex) {
		// Some parsers raise a parse exception
	}
	streamReader.close();
}
 
Example 3
Source File: SourceHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void readStAXSourceExternal() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	converter.setSupportDtd(true);
	StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
	XMLStreamReader streamReader = result.getXMLStreamReader();
	assertTrue(streamReader.hasNext());
	streamReader.next();
	streamReader.next();
	String s = streamReader.getLocalName();
	assertEquals("root", s);
	try {
		s = streamReader.getElementText();
		assertNotEquals("Foo Bar", s);
	}
	catch (XMLStreamException ex) {
		// Some parsers raise a parse exception
	}
	streamReader.close();
}
 
Example 4
Source File: SourceHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void readStAXSource() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
	XMLStreamReader streamReader = result.getXMLStreamReader();
	assertTrue(streamReader.hasNext());
	streamReader.nextTag();
	String s = streamReader.getLocalName();
	assertEquals("root", s);
	s = streamReader.getElementText();
	assertEquals("Hello World", s);
	streamReader.close();
}
 
Example 5
Source File: SourceHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void readStAXSourceWithXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<root>&lol9;</root>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
	StAXSource result = (StAXSource) this.converter.read(StAXSource.class, inputMessage);

	XMLStreamReader streamReader = result.getXMLStreamReader();
	assertTrue(streamReader.hasNext());
	streamReader.next();
	streamReader.next();
	String s = streamReader.getLocalName();
	assertEquals("root", s);
	assertThatExceptionOfType(XMLStreamException.class).isThrownBy(() ->
			streamReader.getElementText())
		.withMessageContaining("\"lol9\"");
}
 
Example 6
Source File: SourceHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void readStAXSource() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
	XMLStreamReader streamReader = result.getXMLStreamReader();
	assertTrue(streamReader.hasNext());
	streamReader.nextTag();
	String s = streamReader.getLocalName();
	assertEquals("root", s);
	s = streamReader.getElementText();
	assertEquals("Hello World", s);
	streamReader.close();
}
 
Example 7
Source File: SourceHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void readStAXSourceWithXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<root>&lol9;</root>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
	StAXSource result = (StAXSource) this.converter.read(StAXSource.class, inputMessage);

	XMLStreamReader streamReader = result.getXMLStreamReader();
	assertTrue(streamReader.hasNext());
	streamReader.next();
	streamReader.next();
	String s = streamReader.getLocalName();
	assertEquals("root", s);

	this.thrown.expectMessage("\"lol9\"");
	s = streamReader.getElementText();
}
 
Example 8
Source File: SourceHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void readStAXSource() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
	XMLStreamReader streamReader = result.getXMLStreamReader();
	assertTrue(streamReader.hasNext());
	streamReader.nextTag();
	String s = streamReader.getLocalName();
	assertEquals("root", s);
	s = streamReader.getElementText();
	assertEquals("Hello World", s);
	streamReader.close();
}
 
Example 9
Source File: SourceHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void readStAXSourceWithXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<root>&lol9;</root>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
	StAXSource result = (StAXSource) this.converter.read(StAXSource.class, inputMessage);

	XMLStreamReader streamReader = result.getXMLStreamReader();
	assertTrue(streamReader.hasNext());
	streamReader.next();
	streamReader.next();
	String s = streamReader.getLocalName();
	assertEquals("root", s);

	this.thrown.expectMessage("\"lol9\"");
	s = streamReader.getElementText();
}