Search in sources :

Example 41 with Result

use of javax.xml.transform.Result in project Payara by payara.

the class XMLActionReporter method writeXML.

private void writeXML(Document doc, OutputStream os) throws TransformerConfigurationException, TransformerException {
    Source source = new DOMSource(doc);
    Result result = new StreamResult(os);
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, result);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 42 with Result

use of javax.xml.transform.Result in project Payara by payara.

the class XmlObject method toString.

public String toString(int indent) {
    Document document = getDocument();
    document.appendChild(createNode(document));
    try {
        Source source = new DOMSource(document);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        if (indent > -1) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
        }
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 43 with Result

use of javax.xml.transform.Result in project tomee by apache.

the class Sxc method marshall.

public static void marshall(final JAXBObject objectType, final Object object, final OutputStream outputStream) throws JAXBException {
    final Result result = new StreamResult(outputStream);
    marshal(objectType, object, result);
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 44 with Result

use of javax.xml.transform.Result in project Gargoyle by callakrsos.

the class DesignerFx method getXmlString.

/**
	 * Returns an unformatted xml string (without the declaration)
	 *
	 * @throws TransformerException if the XML cannot be converted to a string
	 */
private static String getXmlString(Node node) throws TransformerException {
    StringWriter writer = new StringWriter();
    Source source = new DOMSource(node.getAsDocument());
    Result result = new StreamResult(writer);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer xformer = transformerFactory.newTransformer();
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.transform(source, result);
    return writer.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 45 with Result

use of javax.xml.transform.Result in project jersey by jersey.

the class WadlGeneratorJAXBGrammarGenerator method buildModelAndSchemas.

/**
     * Build the JAXB model and generate the schemas based on tha data
     *
     * @param extraFiles additional files.
     * @return class to {@link QName} resolver.
     */
private Resolver buildModelAndSchemas(final Map<String, ApplicationDescription.ExternalGrammar> extraFiles) {
    // Lets get all candidate classes so we can create the JAX-B context
    // include any @XmlSeeAlso references.
    final Set<Class> classSet = new HashSet<>(seeAlsoClasses);
    for (final TypeCallbackPair pair : nameCallbacks) {
        final GenericType genericType = pair.genericType;
        final Class<?> clazz = genericType.getRawType();
        if (clazz.getAnnotation(XmlRootElement.class) != null) {
            classSet.add(clazz);
        } else if (SPECIAL_GENERIC_TYPES.contains(clazz)) {
            final Type type = genericType.getType();
            if (type instanceof ParameterizedType) {
                final Type parameterType = ((ParameterizedType) type).getActualTypeArguments()[0];
                if (parameterType instanceof Class) {
                    classSet.add((Class) parameterType);
                }
            }
        }
    }
    // Create a JAX-B context, and use this to generate us a bunch of
    // schema objects
    JAXBIntrospector introspector = null;
    try {
        final JAXBContext context = JAXBContext.newInstance(classSet.toArray(new Class[classSet.size()]));
        final List<StreamResult> results = new ArrayList<>();
        context.generateSchema(new SchemaOutputResolver() {

            int counter = 0;

            @Override
            public Result createOutput(final String namespaceUri, final String suggestedFileName) {
                final StreamResult result = new StreamResult(new CharArrayWriter());
                result.setSystemId("xsd" + (counter++) + ".xsd");
                results.add(result);
                return result;
            }
        });
        for (final StreamResult result : results) {
            final CharArrayWriter writer = (CharArrayWriter) result.getWriter();
            final byte[] contents = writer.toString().getBytes("UTF8");
            extraFiles.put(result.getSystemId(), new ApplicationDescription.ExternalGrammar(// I don't think there is a specific media type for XML Schema
            MediaType.APPLICATION_XML_TYPE, contents));
        }
        // Create an introspector
        //
        introspector = context.createJAXBIntrospector();
    } catch (final JAXBException e) {
        LOGGER.log(Level.SEVERE, "Failed to generate the schema for the JAX-B elements", e);
    } catch (final IOException e) {
        LOGGER.log(Level.SEVERE, "Failed to generate the schema for the JAX-B elements due to an IO error", e);
    }
    if (introspector != null) {
        final JAXBIntrospector copy = introspector;
        return new Resolver() {

            public QName resolve(final Class type) {
                Object parameterClassInstance = null;
                try {
                    final Constructor<?> defaultConstructor = AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor<?>>() {

                        @SuppressWarnings("unchecked")
                        @Override
                        public Constructor<?> run() throws NoSuchMethodException {
                            final Constructor<?> constructor = type.getDeclaredConstructor();
                            constructor.setAccessible(true);
                            return constructor;
                        }
                    });
                    parameterClassInstance = defaultConstructor.newInstance();
                } catch (final InstantiationException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                    LOGGER.log(Level.FINE, null, ex);
                } catch (final PrivilegedActionException ex) {
                    LOGGER.log(Level.FINE, null, ex.getCause());
                }
                if (parameterClassInstance == null) {
                    return null;
                }
                try {
                    return copy.getElementName(parameterClassInstance);
                } catch (final NullPointerException e) {
                    // annotation is passed as a parameter of #getElementName method.
                    return null;
                }
            }
        };
    } else {
        // No resolver created
        return null;
    }
}
Also used : ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) CharArrayWriter(java.io.CharArrayWriter) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) ParameterizedType(java.lang.reflect.ParameterizedType) SchemaOutputResolver(javax.xml.bind.SchemaOutputResolver) HashSet(java.util.HashSet) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) GenericType(javax.ws.rs.core.GenericType) StreamResult(javax.xml.transform.stream.StreamResult) SchemaOutputResolver(javax.xml.bind.SchemaOutputResolver) PrivilegedActionException(java.security.PrivilegedActionException) Constructor(java.lang.reflect.Constructor) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) ApplicationDescription(org.glassfish.jersey.server.wadl.internal.ApplicationDescription) InvocationTargetException(java.lang.reflect.InvocationTargetException) JAXBIntrospector(javax.xml.bind.JAXBIntrospector) MediaType(javax.ws.rs.core.MediaType) GenericType(javax.ws.rs.core.GenericType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Aggregations

Result (javax.xml.transform.Result)80 StreamResult (javax.xml.transform.stream.StreamResult)72 Source (javax.xml.transform.Source)52 Transformer (javax.xml.transform.Transformer)52 DOMSource (javax.xml.transform.dom.DOMSource)42 TransformerFactory (javax.xml.transform.TransformerFactory)33 StringWriter (java.io.StringWriter)24 TransformerException (javax.xml.transform.TransformerException)21 IOException (java.io.IOException)20 StreamSource (javax.xml.transform.stream.StreamSource)20 SAXResult (javax.xml.transform.sax.SAXResult)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)15 Document (org.w3c.dom.Document)14 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)11 DOMResult (javax.xml.transform.dom.DOMResult)11 InputSource (org.xml.sax.InputSource)11 SAXException (org.xml.sax.SAXException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9