List of usage examples for javax.xml.bind SchemaOutputResolver SchemaOutputResolver
SchemaOutputResolver
From source file:se.mithlond.services.shared.test.entity.PlainJaxbContextRule.java
/** * Acquires a JAXB Schema from the provided JAXBContext. * * @param ctx The context for which am XSD should be constructed. * @return A tuple holding the constructed XSD from the provided JAXBContext, and * the LSResourceResolver synthesized during the way. * @throws NullPointerException if ctx was {@code null}. * @throws IllegalArgumentException if a JAXB-related exception occurred while extracting the schema. *//*from ww w.ja v a2 s. c o m*/ public static Tuple<Schema, LSResourceResolver> generateTransientXSD(final JAXBContext ctx) throws NullPointerException, IllegalArgumentException { // Check sanity org.apache.commons.lang3.Validate.notNull(ctx, "Cannot handle null ctx argument."); final SortedMap<String, ByteArrayOutputStream> namespace2SchemaMap = new TreeMap<>(); try { ctx.generateSchema(new SchemaOutputResolver() { /** * {@inheritDoc} */ @Override public Result createOutput(final String namespaceUri, final String suggestedFileName) throws IOException { // The types should really be annotated with @XmlType(namespace = "... something ...") // to avoid using the default ("") namespace. if (namespaceUri.isEmpty()) { log.warn("Received empty namespaceUri while resolving a generated schema. " + "Did you forget to add a @XmlType(namespace = \"... something ...\") annotation " + "to your class?"); } // Create the result ByteArrayOutputStream final ByteArrayOutputStream out = new ByteArrayOutputStream(); final StreamResult toReturn = new StreamResult(out); toReturn.setSystemId(""); // Map the namespaceUri to the schemaResult. namespace2SchemaMap.put(namespaceUri, out); // All done. return toReturn; } }); } catch (IOException e) { throw new IllegalArgumentException("Could not acquire Schema snippets.", e); } // Convert to an array of StreamSource. final MappedSchemaResourceResolver resourceResolver = new MappedSchemaResourceResolver(); final StreamSource[] schemaSources = new StreamSource[namespace2SchemaMap.size()]; int counter = 0; for (Map.Entry<String, ByteArrayOutputStream> current : namespace2SchemaMap.entrySet()) { final byte[] schemaSnippetAsBytes = current.getValue().toByteArray(); resourceResolver.addNamespace2SchemaEntry(current.getKey(), new String(schemaSnippetAsBytes)); if (log.isDebugEnabled()) { log.info("Generated schema [" + (counter + 1) + "/" + schemaSources.length + "]:\n " + new String(schemaSnippetAsBytes)); } // Copy the schema source to the schemaSources array. schemaSources[counter] = new StreamSource(new ByteArrayInputStream(schemaSnippetAsBytes), ""); // Increase the counter counter++; } try { // All done. final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schemaFactory.setResourceResolver(resourceResolver); final Schema transientSchema = schemaFactory.newSchema(schemaSources); // All done. return new Tuple<>(transientSchema, resourceResolver); } catch (final SAXException e) { throw new IllegalArgumentException("Could not create Schema from snippets.", e); } }