Search in sources :

Example 1 with SchemaOutputResolver

use of javax.xml.bind.SchemaOutputResolver in project jbpm by kiegroup.

the class GenerateDeployemntDescriptorSchema method main.

public static void main(String[] args) throws Exception {
    JAXBContext jaxbContext = DeploymentDescriptorIO.getContext();
    SchemaOutputResolver sor = new FileSchemaOutputResolver();
    jaxbContext.generateSchema(sor);
}
Also used : SchemaOutputResolver(javax.xml.bind.SchemaOutputResolver) JAXBContext(javax.xml.bind.JAXBContext)

Example 2 with SchemaOutputResolver

use of javax.xml.bind.SchemaOutputResolver in project zm-mailbox by Zimbra.

the class Jaxb2Xsds method createXsds.

/**
 * Create XSDs for all reachable objects from either the requests and responses or the HeaderContext
 */
public static void createXsds() {
    List<Class<?>> classList = Lists.newArrayList();
    classList.addAll(JaxbUtil.getJaxbRequestAndResponseClasses());
    classList.add(HeaderContext.class);
    JAXBContext jaxbContext;
    try {
        jaxbContext = JAXBContext.newInstance(classList.toArray(new Class[classList.size()]));
    } catch (JAXBException e) {
        throw new RuntimeException(String.format("Problem creating JAXBContext", ARG_DIR), e);
    }
    SchemaOutputResolver sor = new ZimbraSchemaOutputResolver(dir);
    try {
        jaxbContext.generateSchema(sor);
    } catch (IOException e) {
        throw new RuntimeException(String.format("Problem generating schemas", ARG_DIR), e);
    }
}
Also used : SchemaOutputResolver(javax.xml.bind.SchemaOutputResolver) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException)

Example 3 with SchemaOutputResolver

use of javax.xml.bind.SchemaOutputResolver in project jaffa-framework by jaffa-projects.

the class PrintXmlUtility method writeXsd.

/**
 * Writes an XSD of all classes in the context to the specified filename.
 *
 * @param filename the file name.
 * @throws Exception
 */
public void writeXsd(String filename) throws IOException {
    SchemaOutputResolver resolver = new FileSchemaOutputResolver(filename);
    context.generateSchema(resolver);
}
Also used : SchemaOutputResolver(javax.xml.bind.SchemaOutputResolver)

Example 4 with SchemaOutputResolver

use of javax.xml.bind.SchemaOutputResolver 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)

Example 5 with SchemaOutputResolver

use of javax.xml.bind.SchemaOutputResolver in project opencast by opencast.

the class JaxbXmlSchemaGenerator method getXmlSchema.

/**
 * Builds an xml schema from a JAXBContext.
 *
 * @param jaxbContext
 *          the jaxb context
 * @return the xml as a string
 * @throws IOException
 *           if the JAXBContext can not be transformed into an xml schema
 */
public static String getXmlSchema(JAXBContext jaxbContext) throws IOException {
    final StringWriter writer = new StringWriter();
    jaxbContext.generateSchema(new SchemaOutputResolver() {

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult streamResult = new StreamResult(writer);
            streamResult.setSystemId("");
            return streamResult;
        }
    });
    return writer.toString();
}
Also used : SchemaOutputResolver(javax.xml.bind.SchemaOutputResolver) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Aggregations

SchemaOutputResolver (javax.xml.bind.SchemaOutputResolver)8 IOException (java.io.IOException)6 Result (javax.xml.transform.Result)5 StreamResult (javax.xml.transform.stream.StreamResult)5 ArrayList (java.util.ArrayList)3 JAXBContext (javax.xml.bind.JAXBContext)3 StringWriter (java.io.StringWriter)2 JAXBException (javax.xml.bind.JAXBException)2 DOMResult (javax.xml.transform.dom.DOMResult)2 CharArrayWriter (java.io.CharArrayWriter)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 PrivilegedActionException (java.security.PrivilegedActionException)1 HashSet (java.util.HashSet)1 GenericType (javax.ws.rs.core.GenericType)1 MediaType (javax.ws.rs.core.MediaType)1 JAXBIntrospector (javax.xml.bind.JAXBIntrospector)1 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)1