/** * Create a SAXResult that targets a SAX2 {@link org.xml.sax.ContentHandler}. * * @param handler Must be a non-null ContentHandler reference. */ public SAXResult(ContentHandler handler) { setHandler(handler); }
/** * Construct a new instance of the {@code StaxResult} with the specified {@code XMLStreamWriter}. * @param streamWriter the {@code XMLStreamWriter} to write to */ public StaxResult(XMLStreamWriter streamWriter) { StaxStreamHandler handler = new StaxStreamHandler(streamWriter); super.setHandler(handler); super.setLexicalHandler(handler); this.streamWriter = streamWriter; }
/** * Template method for handling {@code SAXResult}s. * <p>This implementation delegates to {@code marshalSaxHandlers}. * @param graph the root of the object graph to marshal * @param saxResult the {@code SAXResult} * @throws XmlMappingException if the given object cannot be marshalled to the result * @see #marshalSaxHandlers(Object, org.xml.sax.ContentHandler, org.xml.sax.ext.LexicalHandler) */ protected void marshalSaxResult(Object graph, SAXResult saxResult) throws XmlMappingException { ContentHandler contentHandler = saxResult.getHandler(); Assert.notNull(contentHandler, "ContentHandler not set on SAXResult"); LexicalHandler lexicalHandler = saxResult.getLexicalHandler(); marshalSaxHandlers(graph, contentHandler, lexicalHandler); }
public final ContentHandler createContentHandler() { try { TransformerHandler handler = saxtf.newTransformerHandler(templates); handler.setResult(new SAXResult(outputHandler)); return handler; } catch (TransformerConfigurationException ex) { throw new RuntimeException(ex.toString()); } } }
@Override protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) throws XmlMappingException, IOException { return transformAndUnmarshal(new SAXSource(xmlReader, inputSource), inputSource.getEncoding()); }
/** * Returns a transformer handler that serializes incoming SAX events to * XHTML or HTML (depending the given method) using the given output encoding. * * @param encoding output encoding, or <code>null</code> for the platform default */ private static TransformerHandler getTransformerHandler(OutputStream out, String method, String encoding) throws TransformerConfigurationException { TransformerHandler transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler(); Transformer transformer = transformerHandler.getTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, method); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); if (encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); } transformerHandler.setResult(new StreamResult(new PrintStream(out))); return transformerHandler; }
public final void marshal( Object obj, org.xml.sax.ContentHandler handler ) throws JAXBException { checkNotNull( obj, "obj", handler, "handler" ); marshal( obj, new SAXResult(handler) ); }
@Override protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, @Nullable LexicalHandler lexicalHandler) throws XmlMappingException { try { // JiBX does not support SAX natively, so we write to a buffer first, and transform that to the handlers SAXResult saxResult = new SAXResult(contentHandler); saxResult.setLexicalHandler(lexicalHandler); transformAndMarshal(graph, saxResult); } catch (IOException ex) { throw new MarshallingFailureException("JiBX marshalling exception", ex); } }
private Object unmarshal( SAXSource source ) throws JAXBException { XMLReader r = source.getXMLReader(); if( r == null ) r = getXMLReader(); return unmarshal( r, source.getInputSource() ); }
/** * Creates a new identity transformer. */ public static TransformerHandler createTransformerHandler(boolean disableSecureProcessing) { try { SAXTransformerFactory tf = (SAXTransformerFactory)XmlFactory.createTransformerFactory(disableSecureProcessing); return tf.newTransformerHandler(); } catch (TransformerConfigurationException e) { throw new Error(e); // impossible } }
public Source getAssociatedStylesheet(Source source, String media, String title, String charset) throws TransformerConfigurationException { if (_currFactory == null) { createXSLTCTransformerFactory(); } return _currFactory.getAssociatedStylesheet(source, media, title, charset); }
InputSource inputSource = new InputSource(streamSource.getInputStream()); inputSource.setEncoding(getDefaultEncoding()); return unmarshalSaxSource(new SAXSource(inputSource)); return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getReader()))); return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getSystemId())));
@Test public void marshalSaxResult() throws Exception { ContentHandler contentHandler = mock(ContentHandler.class); SAXResult result = new SAXResult(contentHandler); marshaller.marshal(flight, result); InOrder ordered = inOrder(contentHandler); ordered.verify(contentHandler).startDocument(); ordered.verify(contentHandler).startElement(eq(""), eq("flight"), eq("flight"), isA(Attributes.class)); ordered.verify(contentHandler).startElement(eq(""), eq("flightNumber"), eq("flightNumber"), isA(Attributes.class)); ordered.verify(contentHandler).characters(isA(char[].class), eq(0), eq(2)); ordered.verify(contentHandler).endElement("", "flightNumber", "flightNumber"); ordered.verify(contentHandler).endElement("", "flight", "flight"); ordered.verify(contentHandler).endDocument(); }
private void parse() throws SAXException { SAXResult result = new SAXResult(getContentHandler()); result.setLexicalHandler(getLexicalHandler()); try { this.marshaller.marshal(this.content, result); } catch (IOException ex) { SAXParseException saxException = new SAXParseException(ex.getMessage(), null, null, -1, -1, ex); ErrorHandler errorHandler = getErrorHandler(); if (errorHandler != null) { errorHandler.fatalError(saxException); } else { throw saxException; } } } }
/** * Construct a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}. * @param eventWriter the {@code XMLEventWriter} to write to */ public StaxResult(XMLEventWriter eventWriter) { StaxEventHandler handler = new StaxEventHandler(eventWriter); super.setHandler(handler); super.setLexicalHandler(handler); this.eventWriter = eventWriter; }
@Test @SuppressWarnings("deprecation") public void unmarshalSAXSource() throws Exception { XMLReader reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING))); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Test public void isStaxResultInvalid() throws Exception { assertFalse("A StAX Result", StaxUtils.isStaxResult(new DOMResult())); assertFalse("A StAX Result", StaxUtils.isStaxResult(new SAXResult())); assertFalse("A StAX Result", StaxUtils.isStaxResult(new StreamResult())); }
/** * Construct a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}. * @param eventWriter the {@code XMLEventWriter} to write to */ public StaxResult(XMLEventWriter eventWriter) { StaxEventHandler handler = new StaxEventHandler(eventWriter); super.setHandler(handler); super.setLexicalHandler(handler); this.eventWriter = eventWriter; }
@Test public void isStaxSourceInvalid() throws Exception { assertFalse("A StAX Source", StaxUtils.isStaxSource(new DOMSource())); assertFalse("A StAX Source", StaxUtils.isStaxSource(new SAXSource())); assertFalse("A StAX Source", StaxUtils.isStaxSource(new StreamSource())); }
/** * Construct a new instance of the {@code StaxResult} with the specified {@code XMLStreamWriter}. * @param streamWriter the {@code XMLStreamWriter} to write to */ public StaxResult(XMLStreamWriter streamWriter) { StaxStreamHandler handler = new StaxStreamHandler(streamWriter); super.setHandler(handler); super.setLexicalHandler(handler); this.streamWriter = streamWriter; }