SAXSource saxSource = (SAXSource) source; xmlReader = saxSource.getXMLReader(); inputSource = saxSource.getInputSource();
/** * Attempt to obtain a SAX InputSource object from a Source * object. * * @param source Must be a non-null Source reference. * * @return An InputSource, or null if Source can not be converted. */ public static InputSource sourceToInputSource(Source source) { if (source instanceof SAXSource) { return ((SAXSource) source).getInputSource(); } else if (source instanceof StreamSource) { StreamSource ss = (StreamSource) source; InputSource isource = new InputSource(ss.getSystemId()); isource.setByteStream(ss.getInputStream()); isource.setCharacterStream(ss.getReader()); isource.setPublicId(ss.getPublicId()); return isource; } else { return null; } } }
private Object unmarshal( SAXSource source ) throws JAXBException { XMLReader r = source.getXMLReader(); if( r == null ) r = getXMLReader(); return unmarshal( r, source.getInputSource() ); }
if (saxSource.getInputSource() == null) { saxSource.setInputSource(new InputSource()); return unmarshalSaxReader(saxSource.getXMLReader(), saxSource.getInputSource());
@Test public void readSAXSource() throws Exception { MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8")); inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage); InputSource inputSource = result.getInputSource(); String s = FileCopyUtils.copyToString(new InputStreamReader(inputSource.getByteStream())); assertThat("Invalid result", s, isSimilarTo(BODY)); }
@Test public void readSAXSourceExternal() throws Exception { MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8")); inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); converter.setSupportDtd(true); SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage); InputSource inputSource = result.getInputSource(); XMLReader reader = result.getXMLReader(); reader.setContentHandler(new DefaultHandler() { @Override public void characters(char[] ch, int start, int length) { String s = new String(ch, start, length); assertNotEquals("Invalid result", "Foo Bar", s); } }); reader.parse(inputSource); }
public Object unmarshal0( Source source, JaxBeanInfo expectedType ) throws JAXBException { if (source instanceof SAXSource) { SAXSource ss = (SAXSource) source; XMLReader locReader = ss.getXMLReader(); if (locReader == null) { locReader = getXMLReader(); } return unmarshal0(locReader, ss.getInputSource(), expectedType); } if (source instanceof StreamSource) { return unmarshal0(getXMLReader(), streamSourceToInputSource((StreamSource) source), expectedType); } if (source instanceof DOMSource) { return unmarshal0(((DOMSource) source).getNode(), expectedType); } // we don't handle other types of Source throw new IllegalArgumentException(); }
@Override public <T> JAXBElement<T> unmarshal( Source source, Class<T> expectedType ) throws JAXBException { if (source instanceof SAXSource) { SAXSource ss = (SAXSource) source; XMLReader locReader = ss.getXMLReader(); if (locReader == null) { locReader = getXMLReader(); } return unmarshal(locReader, ss.getInputSource(), expectedType); } if (source instanceof StreamSource) { return unmarshal(getXMLReader(), streamSourceToInputSource((StreamSource) source), expectedType); } if (source instanceof DOMSource) { return unmarshal(((DOMSource) source).getNode(), expectedType); } // we don't handle other types of Source throw new IllegalArgumentException(); }
/** * This class wraps an ErrorListener into a MessageHandler in order to * capture messages reported via xsl:message. */ static class MessageHandler extends org.apache.xalan.xsltc.runtime.MessageHandler { private ErrorListener _errorListener; public MessageHandler(ErrorListener errorListener) { _errorListener = errorListener; } public void displayMessage(String msg) { if(_errorListener == null) { System.err.println(msg); } else { try { _errorListener.warning(new TransformerException(msg)); } catch (TransformerException e) { // ignored } } } }
@Test public void readSAXSourceWithXmlBomb() 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")); SAXSource result = (SAXSource) this.converter.read(SAXSource.class, inputMessage); this.thrown.expect(SAXException.class); this.thrown.expectMessage("DOCTYPE"); InputSource inputSource = result.getInputSource(); XMLReader reader = result.getXMLReader(); reader.parse(inputSource); }
final SAXSource sax = (SAXSource) source; XMLReader reader = sax.getXMLReader(); final InputSource input = sax.getInputSource(); boolean userReader = true;
((StreamSource)source).getReader()==null)|| (source instanceof SAXSource && ((SAXSource)source).getInputSource()==null && ((SAXSource)source).getXMLReader()==null )|| (source instanceof DOMSource && ((DOMSource)source).getNode()==null)){
((StreamSource)source).getReader()==null)|| (source instanceof SAXSource && ((SAXSource)source).getInputSource()==null && ((SAXSource)source).getXMLReader()==null )|| (source instanceof DOMSource && ((DOMSource)source).getNode()==null)){
input = sax.getInputSource();
((StreamSource)source).getReader()==null)|| (source instanceof SAXSource && ((SAXSource)source).getInputSource()==null && ((SAXSource)source).getXMLReader()==null )|| (source instanceof DOMSource && ((DOMSource)source).getNode()==null)){
((StreamSource)source).getReader()==null)|| (source instanceof SAXSource && ((SAXSource)source).getInputSource()==null && ((SAXSource)source).getXMLReader()==null )|| (source instanceof DOMSource && ((DOMSource)source).getNode()==null)){
((StreamSource)source).getReader()==null)|| (source instanceof SAXSource && ((SAXSource)source).getInputSource()==null && ((SAXSource)source).getXMLReader()==null )|| (source instanceof DOMSource && ((DOMSource)source).getNode()==null)){
public Object unmarshal0( Source source, JaxBeanInfo expectedType ) throws JAXBException { if (source instanceof SAXSource) { SAXSource ss = (SAXSource) source; XMLReader locReader = ss.getXMLReader(); if (locReader == null) { locReader = getXMLReader(); } return unmarshal0(locReader, ss.getInputSource(), expectedType); } if (source instanceof StreamSource) { return unmarshal0(getXMLReader(), streamSourceToInputSource((StreamSource) source), expectedType); } if (source instanceof DOMSource) { return unmarshal0(((DOMSource) source).getNode(), expectedType); } // we don't handle other types of Source throw new IllegalArgumentException(); }
when(sax1.getInputSource()).thenReturn(inputSource1); closeableMocks.add(sax1); when(sax2.getInputSource()).thenReturn(inputSource2); closeableMocks.add(sax2);
@Override public <T> JAXBElement<T> unmarshal( Source source, Class<T> expectedType ) throws JAXBException { if (source instanceof SAXSource) { SAXSource ss = (SAXSource) source; XMLReader locReader = ss.getXMLReader(); if (locReader == null) { locReader = getXMLReader(); } return unmarshal(locReader, ss.getInputSource(), expectedType); } if (source instanceof StreamSource) { return unmarshal(getXMLReader(), streamSourceToInputSource((StreamSource) source), expectedType); } if (source instanceof DOMSource) { return unmarshal(((DOMSource) source).getNode(), expectedType); } // we don't handle other types of Source throw new IllegalArgumentException(); }