SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance(); // These templates objects could be reused and obtained from elsewhere. Templates templates1 = stf.newTemplates(new StreamSource( getClass().getResourceAsStream("MyStylesheet1.xslt"))); Templates templates2 = stf.newTemplates(new StreamSource( getClass().getResourceAsStream("MyStylesheet1.xslt"))); TransformerHandler th1 = stf.newTransformerHandler(templates1); TransformerHandler th2 = stf.newTransformerHandler(templates2); th1.setResult(new SAXResult(th2)); th2.setResult(new StreamResult(System.out)); Transformer t = stf.newTransformer(); t.transform(new StreamSource(System.in), new SAXResult(th1)); // th1 feeds th2, which in turn feeds System.out.
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 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); } }
public final void marshal( Object obj, org.xml.sax.ContentHandler handler ) throws JAXBException { checkNotNull( obj, "obj", handler, "handler" ); marshal( obj, new SAXResult(handler) ); }
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; } } } }
public <E> void writeDom(E element, DomHandler<E, ?> domHandler, Object parentBean, String fieldName) throws SAXException { Source source = domHandler.marshal(element,this); if(contentHandlerAdapter==null) contentHandlerAdapter = new ContentHandlerAdaptor(this); try { getIdentityTransformer().transform(source,new SAXResult(contentHandlerAdapter)); } catch (TransformerException e) { reportError(fieldName,e); } }
public void setContentHandler (ContentHandler handler) { _transformerHandler.setResult(new SAXResult(handler)); if (getParent() == null) { try { createParent(); } catch (SAXException e) { return; } } getParent().setContentHandler(_transformerHandler); }
@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(); }
transformerHandler.setResult(new StreamResult(_stringWriter)); _active = true; return (T) new SAXResult(transformerHandler); } catch (TransformerException te) { throw new PSQLException(GT.tr("Unable to create SAXResult for SQLXML."),
@Test public void marshalSAXResult() throws Exception { ContentHandler contentHandler = mock(ContentHandler.class); SAXResult result = new SAXResult(contentHandler); marshaller.marshal(flights, result); InOrder ordered = inOrder(contentHandler); ordered.verify(contentHandler).setDocumentLocator(isA(Locator.class)); ordered.verify(contentHandler).startDocument(); ordered.verify(contentHandler).startPrefixMapping("", "http://samples.springframework.org/flight"); ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flights"), eq("flights"), isA(Attributes.class)); ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flight"), eq("flight"), isA(Attributes.class)); ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("number"), eq("number"), isA(Attributes.class)); ordered.verify(contentHandler).characters(isA(char[].class), eq(0), eq(2)); ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "number", "number"); ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flight", "flight"); ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flights", "flights"); ordered.verify(contentHandler).endPrefixMapping(""); ordered.verify(contentHandler).endDocument(); }
transformerHandler.setResult(new StreamResult(_stringWriter)); _active = true; return new SAXResult(transformerHandler); } catch (TransformerException te) { throw new PSQLException(GT.tr("Unable to create SAXResult for SQLXML."), PSQLState.UNEXPECTED_ERROR, te);
@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())); }
public void read(Document document) throws MimeTypeException { try { TransformerFactory factory = TransformerFactory.newInstance(); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); Transformer transformer = factory.newTransformer(); transformer.transform(new DOMSource(document), new SAXResult(this)); } catch (TransformerException e) { throw new MimeTypeException("Failed to parse type registry", e); } }
public <E> void writeDom(E element, DomHandler<E, ?> domHandler, Object parentBean, String fieldName) throws SAXException { Source source = domHandler.marshal(element,this); if(contentHandlerAdapter==null) contentHandlerAdapter = new ContentHandlerAdaptor(this); try { getIdentityTransformer().transform(source,new SAXResult(contentHandlerAdapter)); } catch (TransformerException e) { reportError(fieldName,e); } }
SAXResult result = new SAXResult(handler);
@Test(expected = MessagingException.class) public void testWithUnsupportedSaxResult() throws Exception { SAXResult result = new SAXResult(); resToDocTransformer.transformResult(result); }
@Test public void testWithUnsupportedSaxResult() { assertThatThrownBy(() -> this.transformer.transformResult(new SAXResult())) .isExactlyInstanceOf(MessagingException.class); }
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()); } } }