javax.xml.transform.sax.SAXResult Java Examples

The following examples show how to use javax.xml.transform.sax.SAXResult. 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: TestSmlInterpretationExtractor.java    From JVoiceXML with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Test case for a simple recognition process with a single tag.
 * @exception Exception
 *            test failed
 */
@Test
public void testTag() throws Exception {
    final TransformerFactory factory = TransformerFactory.newInstance();
    final Transformer transformer = factory.newTransformer();
    final InputStream in =
            TestSmlInterpretationExtractor.class.getResourceAsStream(
                    "sml-tag.xml");
    final Source source = new StreamSource(in);
    final SmlInterpretationExtractor extractor =
            new SmlInterpretationExtractor();
    final Result result = new SAXResult(extractor);
    transformer.transform(source, result);
    Assert.assertEquals("Good morning Dirk",
            extractor.getUtterance());
    Assert.assertEquals(0.7378733, extractor.getConfidence(),
            MAX_CONFIDENCE_DIFF);
    final List<SmlInterpretation> interpretations =
            extractor.getInterpretations();
    Assert.assertEquals(0, interpretations.size());
    Assert.assertEquals("Projectmanager", extractor.getUtteranceTag());
}
 
Example #2
Source File: MarshallingSource.java    From java-technology-stack with MIT License 6 votes vote down vote up
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;
		}
	}
}
 
Example #3
Source File: Jaxb2MarshallerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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();
}
 
Example #4
Source File: CastorMarshallerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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).startDocument();
	ordered.verify(contentHandler).startPrefixMapping("tns", "http://samples.springframework.org/flight");
	ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"),
			eq("flights"), eq("tns:flights"), isA(Attributes.class));
	ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"),
			eq("flight"), eq("tns:flight"), isA(Attributes.class));
	ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"),
			eq("number"), eq("tns:number"), isA(Attributes.class));
	ordered.verify(contentHandler).characters(eq(new char[]{'4', '2'}), eq(0), eq(2));
	ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "number", "tns:number");
	ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flight", "tns:flight");
	ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flights", "tns:flights");
	ordered.verify(contentHandler).endPrefixMapping("tns");
	ordered.verify(contentHandler).endDocument();
}
 
Example #5
Source File: Bug6846132Test.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSAXResult() {
    DefaultHandler handler = new DefaultHandler();

    final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>";
    try {
        SAXResult saxResult = new SAXResult(handler);
        // saxResult.setSystemId("jaxp-ri/unit-test/javax/xml/stream/XMLOutputFactoryTest/cr6846132.xml");
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = ofac.createXMLStreamWriter(saxResult);
        writer.writeStartDocument("1.0");
        writer.writeStartElement("root");
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
        writer.close();
    } catch (Exception e) {
        if (e instanceof UnsupportedOperationException) {
            // expected
        } else {
            e.printStackTrace();
            Assert.fail(e.toString());
        }
    }
}
 
Example #6
Source File: Bug6846132Test.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSAXResult1() {
    DefaultHandler handler = new DefaultHandler();

    try {
        SAXResult saxResult = new SAXResult(handler);
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        XMLEventWriter writer = ofac.createXMLEventWriter(saxResult);
    } catch (Exception e) {
        if (e instanceof UnsupportedOperationException) {
            // expected
        } else {
            e.printStackTrace();
            Assert.fail(e.toString());
        }
    }
}
 
Example #7
Source File: MCRFoFormatterFOP.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void transform(MCRContent input, OutputStream out) throws TransformerException, IOException {
    try {
        final FOUserAgent userAgent = fopFactory.newFOUserAgent();
        userAgent.setProducer(new MessageFormat("MyCoRe {0} ({1})", Locale.ROOT)
            .format(new Object[] { MCRCoreVersion.getCompleteVersion(), userAgent.getProducer() }));

        final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out);
        final Source src = input.getSource();
        final Result res = new SAXResult(fop.getDefaultHandler());
        Transformer transformer = getTransformerFactory().newTransformer();
        transformer.transform(src, res);
    } catch (FOPException e) {
        throw new TransformerException(e);
    } finally {
        out.close();
    }
}
 
Example #8
Source File: FORendererApacheFOP.java    From docx4j-export-FO with Apache License 2.0 6 votes vote down vote up
protected void render(FopFactory fopFactory, FOUserAgent foUserAgent, String outputFormat, Source foDocumentSrc, PlaceholderReplacementHandler.PlaceholderLookup placeholderLookup, OutputStream outputStream) throws Docx4JException {
	Fop fop = null;
	Result result = null;
	try {
		if (foUserAgent==null) {
			fop = fopFactory.newFop(outputFormat, outputStream);
		} else {
			fop = fopFactory.newFop(outputFormat, foUserAgent, outputStream);				
		}
		result = (placeholderLookup == null ?
				//1 Pass
				new SAXResult(fop.getDefaultHandler()) :
				//2 Pass
				new SAXResult(new PlaceholderReplacementHandler(fop.getDefaultHandler(), placeholderLookup)));
	} catch (FOPException e) {
		throw new Docx4JException("Exception setting up result for fo transformation: " + e.getMessage(), e);
	}
	
	XmlSerializerUtil.serialize(foDocumentSrc, result, false, false);
}
 
Example #9
Source File: Jaxb2MarshallerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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();
}
 
Example #10
Source File: ApacheFopWorker.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/** Transform an xsl-fo StreamSource to the specified output format.
 * @param src The xsl-fo StreamSource instance
 * @param stylesheet Optional stylesheet StreamSource instance
 * @param fop
 */
public static void transform(StreamSource src, StreamSource stylesheet, Fop fop) throws FOPException {
    Result res = new SAXResult(fop.getDefaultHandler());
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;
        if (stylesheet == null) {
            transformer = factory.newTransformer();
        } else {
            transformer = factory.newTransformer(stylesheet);
        }
        transformer.setURIResolver(new LocalResolver(transformer.getURIResolver()));
        transformer.transform(src, res);
    } catch (Exception e) {
        throw new FOPException(e);
    }
}
 
Example #11
Source File: AbstractMarshaller.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Marshals the object graph with the given root into the provided {@code javax.xml.transform.Result}.
 * <p>This implementation inspects the given result, and calls {@code marshalDomResult},
 * {@code marshalSaxResult}, or {@code marshalStreamResult}.
 * @param graph the root of the object graph to marshal
 * @param result the result to marshal to
 * @throws IOException if an I/O exception occurs
 * @throws XmlMappingException if the given object cannot be marshalled to the result
 * @throws IllegalArgumentException if {@code result} if neither a {@code DOMResult},
 * a {@code SAXResult}, nor a {@code StreamResult}
 * @see #marshalDomResult(Object, javax.xml.transform.dom.DOMResult)
 * @see #marshalSaxResult(Object, javax.xml.transform.sax.SAXResult)
 * @see #marshalStreamResult(Object, javax.xml.transform.stream.StreamResult)
 */
@Override
public final void marshal(Object graph, Result result) throws IOException, XmlMappingException {
	if (result instanceof DOMResult) {
		marshalDomResult(graph, (DOMResult) result);
	}
	else if (StaxUtils.isStaxResult(result)) {
		marshalStaxResult(graph, result);
	}
	else if (result instanceof SAXResult) {
		marshalSaxResult(graph, (SAXResult) result);
	}
	else if (result instanceof StreamResult) {
		marshalStreamResult(graph, (StreamResult) result);
	}
	else {
		throw new IllegalArgumentException("Unknown Result type: " + result.getClass());
	}
}
 
Example #12
Source File: TrAXFilter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void setContentHandler (ContentHandler handler)
{
    _transformerHandler.setResult(new SAXResult(handler));
    if (getParent() == null) {
            try {
                createParent();
            }
            catch (SAXException  e) {
               return;
            }
    }
    getParent().setContentHandler(_transformerHandler);
}
 
Example #13
Source File: TrAXFilter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void setContentHandler (ContentHandler handler)
{
    _transformerHandler.setResult(new SAXResult(handler));
    if (getParent() == null) {
            try {
                createParent();
            }
            catch (SAXException  e) {
               return;
            }
    }
    getParent().setContentHandler(_transformerHandler);
}
 
Example #14
Source File: XMLSerializer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example #15
Source File: PipeTest.java    From rice with Educational Community License v2.0 5 votes vote down vote up
@Test
public void testPipe()
throws TransformerException, TransformerConfigurationException, 
        SAXException, IOException	   
{
   // Instantiate  a TransformerFactory.
 	TransformerFactory tFactory = TransformerFactory.newInstance();
   // Determine whether the TransformerFactory supports The use uf SAXSource 
   // and SAXResult
   if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE))
   { 
     // Cast the TransformerFactory to SAXTransformerFactory.
     SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);	  
     // Create a TransformerHandler for each stylesheet.
     TransformerHandler tHandler1 = saxTFactory.newTransformerHandler(new StreamSource(PipeTest.class.getResourceAsStream("foo1.xsl")));
     TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource(PipeTest.class.getResourceAsStream("foo2.xsl")));
     TransformerHandler tHandler3 = saxTFactory.newTransformerHandler(new StreamSource(PipeTest.class.getResourceAsStream("foo3.xsl")));
   
     // Create an XMLReader.
    XMLReader reader = XMLReaderFactory.createXMLReader();
     reader.setContentHandler(tHandler1);
     reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler1);

     tHandler1.setResult(new SAXResult(tHandler2));
     tHandler2.setResult(new SAXResult(tHandler3));

     // transformer3 outputs SAX events to the serializer.
     java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
     xmlProps.setProperty("indent", "yes");
     xmlProps.setProperty("standalone", "no");
     Serializer serializer = SerializerFactory.getSerializer(xmlProps);
     serializer.setOutputStream(System.out);
     tHandler3.setResult(new SAXResult(serializer.asContentHandler()));

    // Parse the XML input document. The input ContentHandler and output ContentHandler
     // work in separate threads to optimize performance.   
     reader.parse(new InputSource(PipeTest.class.getResourceAsStream("foo.xml")));
   }
 }
 
Example #16
Source File: DBFNamespaceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method transforms a Node without any xsl file and uses SAXResult to
 * invoke the callbacks through a ContentHandler. If namespace processing is
 * not chosen, namespaceURI in callbacks should be an empty string otherwise
 * it should be namespaceURI.
 *
 * @throws Exception If any errors occur.
 */
private void dummyTransform(Document document, String fileName)
        throws Exception {
    DOMSource domSource = new DOMSource(document);
    try(MyCHandler chandler = MyCHandler.newInstance(new File(fileName))) {
        TransformerFactory.newInstance().newTransformer().
            transform(domSource, new SAXResult(chandler));
    }
}
 
Example #17
Source File: AstroProcessor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void process(String outputfile, TransformerHandler... filters) throws Exception {
    XMLReader catparser = getXMLReader();

    File catalogfile = new File(catalogFileName);
    InputSource catsrc = isfact.newInputSource(catalogfile.getPath());

    TransformerHandler outfilter = ffact.newHTMLOutput();
    // create an array from the Vector of filters...

    // hook the filters up to each other, there may be zero filters
    int nfilters = filters.length;
    if (nfilters != 0) {
        TransformerHandler prev = null;
        for (int i = 0; i < filters.length; i++) {
            TransformerHandler curr = filters[i];
            if (prev != null) {
                prev.setResult(new SAXResult(curr));
            }
            prev = curr;
        }
        // hook up the last filter to the output filter
        prev.setResult(new SAXResult(outfilter));
        // hook up the catalog parser to the first filter...
        catparser.setContentHandler(filters[0]);
    } else {
        // There are no query filters,
        // hook up the catalog parser directly to output filter...
        catparser.setContentHandler(outfilter);
    }
    // hook up the output filter to the output file or std out
    if (outputfile != null) {
        outfilter.setResult(new StreamResult(outputfile));
    } else {
        outfilter.setResult(new StreamResult(System.out));
    }

    catparser.parse(catsrc);
}
 
Example #18
Source File: JDBCSQLXML.java    From evosql with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves a new Result for setting the XML value designated by this
 * SQLXML instance.
 *
 * @param resultClass The class of the result, or null.
 * @throws java.sql.SQLException if there is an error processing the XML
 *         value or the state is not writable
 * @return for setting the XML value designated by this SQLXML instance.
 */
protected <T extends Result>T createResult(
        Class<T> resultClass) throws SQLException {

    checkWritable();
    setWritable(false);
    setReadable(true);

    if (JAXBResult.class.isAssignableFrom(resultClass)) {

        // Must go first presently, since JAXBResult extends SAXResult
        // (purely as an implementation detail) and it's not possible
        // to instantiate a valid JAXBResult with a Zero-Args
        // constructor(or any subclass thereof, due to the finality of
        // its private UnmarshallerHandler)
        // FALL THROUGH... will throw an exception
    } else if ((resultClass == null)
               || StreamResult.class.isAssignableFrom(resultClass)) {
        return createStreamResult(resultClass);
    } else if (DOMResult.class.isAssignableFrom(resultClass)) {
        return createDOMResult(resultClass);
    } else if (SAXResult.class.isAssignableFrom(resultClass)) {
        return createSAXResult(resultClass);
    } else if (StAXResult.class.isAssignableFrom(resultClass)) {
        return createStAXResult(resultClass);
    }

    throw JDBCUtil.invalidArgument("resultClass: " + resultClass);
}
 
Example #19
Source File: DispatchSourceClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static String decodeSource(Source source, String uri, String name) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
    Transformer transformer = transformerFactory.newTransformer();
    ContentHandler handler = new ContentHandler(uri, name);
    transformer.transform(source, new SAXResult(handler));
    return handler.getValue();
}
 
Example #20
Source File: Processor.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
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());
  }
}
 
Example #21
Source File: XStreamMarshallerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #22
Source File: Processor.java    From jtransc with Apache License 2.0 5 votes vote down vote up
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());
    }
}
 
Example #23
Source File: TestResponseExtractor.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Test method for {@link org.jvoicexml.implementation.marc.ResponseExtractor#getEventId()}.
 * @exception Exception test failed
 */
@Test
public void testGetEventId() throws Exception {
    final String response = "<event id=\"JVoiceXMLTrack:end\"/>";
    final TransformerFactory transformerFactory =
            TransformerFactory.newInstance();
    final Transformer transformer = transformerFactory.newTransformer();
    final StringReader reader = new StringReader(response);
    final Source source = new StreamSource(reader);
    final ResponseExtractor extractor = new ResponseExtractor();
    final Result result = new SAXResult(extractor);
    transformer.transform(source, result);
    Assert.assertEquals("JVoiceXMLTrack:end", extractor.getEventId());
}
 
Example #24
Source File: JibxMarshaller.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
	}
}
 
Example #25
Source File: XMLSerializer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example #26
Source File: ResultFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory method for producing {@link XmlSerializer) from {@link javax.xml.transform.Result}.
 *
 * This method supports {@link javax.xml.transform.sax.SAXResult},
 * {@link javax.xml.transform.stream.StreamResult}, and {@link javax.xml.transform.dom.DOMResult}.
 *
 * @param result the Result that will receive output from the XmlSerializer
 * @return an implementation of XmlSerializer that will produce output on the supplied Result
 */
public static XmlSerializer createSerializer(Result result) {
    if (result instanceof SAXResult)
        return new SaxSerializer((SAXResult) result);
    if (result instanceof DOMResult)
        return new DomSerializer((DOMResult) result);
    if (result instanceof StreamResult)
        return new StreamSerializer((StreamResult) result);
    if (result instanceof TXWResult)
        return new TXWSerializer(((TXWResult)result).getWriter());

    throw new UnsupportedOperationException("Unsupported Result type: " + result.getClass().getName());
}
 
Example #27
Source File: ResultFactory.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory method for producing {@link XmlSerializer) from {@link javax.xml.transform.Result}.
 *
 * This method supports {@link javax.xml.transform.sax.SAXResult},
 * {@link javax.xml.transform.stream.StreamResult}, and {@link javax.xml.transform.dom.DOMResult}.
 *
 * @param result the Result that will receive output from the XmlSerializer
 * @return an implementation of XmlSerializer that will produce output on the supplied Result
 */
public static XmlSerializer createSerializer(Result result) {
    if (result instanceof SAXResult)
        return new SaxSerializer((SAXResult) result);
    if (result instanceof DOMResult)
        return new DomSerializer((DOMResult) result);
    if (result instanceof StreamResult)
        return new StreamSerializer((StreamResult) result);
    if (result instanceof TXWResult)
        return new TXWSerializer(((TXWResult)result).getWriter());

    throw new UnsupportedOperationException("Unsupported Result type: " + result.getClass().getName());
}
 
Example #28
Source File: ResultFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory method for producing {@link XmlSerializer) from {@link javax.xml.transform.Result}.
 *
 * This method supports {@link javax.xml.transform.sax.SAXResult},
 * {@link javax.xml.transform.stream.StreamResult}, and {@link javax.xml.transform.dom.DOMResult}.
 *
 * @param result the Result that will receive output from the XmlSerializer
 * @return an implementation of XmlSerializer that will produce output on the supplied Result
 */
public static XmlSerializer createSerializer(Result result) {
    if (result instanceof SAXResult)
        return new SaxSerializer((SAXResult) result);
    if (result instanceof DOMResult)
        return new DomSerializer((DOMResult) result);
    if (result instanceof StreamResult)
        return new StreamSerializer((StreamResult) result);
    if (result instanceof TXWResult)
        return new TXWSerializer(((TXWResult)result).getWriter());

    throw new UnsupportedOperationException("Unsupported Result type: " + result.getClass().getName());
}
 
Example #29
Source File: SmartTransformerFactoryImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.TransformerFactory implementation.
 * Look up the value of a feature (to see if it is supported).
 * This method must be updated as the various methods and features of this
 * class are implemented.
 *
 * @param name The feature name
 * @return 'true' if feature is supported, 'false' if not
 */
public boolean getFeature(String name) {
    // All supported features should be listed here
    String[] features = {
        DOMSource.FEATURE,
        DOMResult.FEATURE,
        SAXSource.FEATURE,
        SAXResult.FEATURE,
        StreamSource.FEATURE,
        StreamResult.FEATURE
    };

    // feature name cannot be null
    if (name == null) {
        ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_GET_FEATURE_NULL_NAME);
        throw new NullPointerException(err.toString());
    }

    // Inefficient, but it really does not matter in a function like this
    for (int i = 0; i < features.length; i++) {
        if (name.equals(features[i]))
            return true;
    }

    // secure processing?
    if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
        return featureSecureProcessing;
    }

    // unknown feature
    return false;
}
 
Example #30
Source File: TrAXFilter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void setContentHandler (ContentHandler handler)
{
    _transformerHandler.setResult(new SAXResult(handler));
    if (getParent() == null) {
            try {
                createParent();
            }
            catch (SAXException  e) {
               return;
            }
    }
    getParent().setContentHandler(_transformerHandler);
}