List of usage examples for javax.xml.bind JAXBContext generateSchema
public void generateSchema(SchemaOutputResolver outputResolver) throws IOException
From source file:org.niord.web.api.ApiRestService.java
/** * Returns the XSD for the Message class. * Two XSDs are produced, "schema1.xsd" and "schema2.xsd". The latter is the main schema for * Message and will import the former.//from w ww. j a v a2 s . c om * @return the XSD for the Message class */ @ApiOperation(value = "Returns XSD model of the Message class", tags = { "messages" }) @GET @Path("/xsd/{schemaFile}") @Produces("application/xml;charset=UTF-8") @GZIP @NoCache public String getMessageXsd( @ApiParam(value = "The schema file, either schema1.xsd or schema2.xsd", example = "schema2.xsd") @PathParam("schemaFile") String schemaFile) throws Exception { if (!schemaFile.equals("schema1.xsd") && !schemaFile.equals("schema2.xsd")) { throw new Exception("Only 'schema1.xsd' and 'schema2.xsd' allowed"); } JAXBContext jaxbContext = JAXBContext.newInstance(MessageVo.class); Map<String, StringWriter> result = new HashMap<>(); SchemaOutputResolver sor = new SchemaOutputResolver() { @Override public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException { StringWriter out = new StringWriter(); result.put(suggestedFileName, out); StreamResult result = new StreamResult(out); result.setSystemId(app.getBaseUri() + "/rest/public/v1/xsd/" + suggestedFileName); return result; } }; // Generate the schemas jaxbContext.generateSchema(sor); return result.get(schemaFile).toString(); }
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. *//* w w w . j a va2 s . c om*/ 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); } }