public static @NotNull Optional<Schema> load(@NotNull JAXBContext context) { try { final List<ByteArrayOutputStream> outputs = new ArrayList<>(); context.generateSchema(new SchemaOutputResolver() { @Override public @NotNull Result createOutput(@NotNull String namespace, @NotNull String suggestedFileName) { final ByteArrayOutputStream output = new ByteArrayOutputStream(); outputs.add(output); final StreamResult result = new StreamResult(output); result.setSystemId(""); return result; } }); return Optional.ofNullable( SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(outputs.stream() .map(ByteArrayOutputStream::toByteArray) .map(ByteArrayInputStream::new) .map(input -> new StreamSource(input, "")) .toArray(StreamSource[]::new)) ); } catch (IOException | SAXException e) { logger.error("Failed to load schema", e); return Optional.empty(); } }
private void generate() { try { JAXBContext.newInstance(Configuration.class).generateSchema(new SchemaOutputResolver() { @Override public @NotNull Result createOutput(@NotNull String namespace, @NotNull String suggestedFileName) throws MalformedURLException { final File file = new File(filename); final StreamResult result = new StreamResult(file); result.setSystemId(file.toURI().toURL().toString()); return result; } }); logger.info("Schema successfully generated to " + filename); } catch (JAXBException | IOException e) { logger.error("Failed to generate schema", e); } }
private static DOMResult extractSchemaResult(JAXBContext ctx) throws IOException { final Set<DOMResult> resultWrapper = new HashSet<>(); ctx.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { DOMResult result = new DOMResult(); result.setSystemId(suggestedFileName); resultWrapper.add(result); return result; } }); return resultWrapper.iterator().next(); }
public static void main(String[] args) throws IOException, JAXBException { // specify where the generated XML schema will be created String schemaDir = "/Users/silmaril/WORK/UW/Workspaces/MyEclipse8.5/MSDaPlUploadQueue/"; final File dir = new File(schemaDir); // create a JAXBContext for the MsJob class JAXBContext ctx = JAXBContext.newInstance(MsJob.class); // generate an XML schema from the annotated object model; create it // in the dir specified earlier under the default name, schema1.xsd ctx.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String schemaName) throws IOException { return new StreamResult(new File(dir, "msjob_schema.xsd")); } }); }
public static void main(String [] args) { try { Class<?>[] classes = new Class[3]; classes[0] = PathologyCaseUpdateAttributeResultType.class; classes[1] = PathologyAcquisitionSiteType.class; classes[2] = PathologyReadingSiteType.class; JAXBContext jaxbContext = JAXBContext.newInstance(classes); SchemaOutputResolver sor = new MySchemaOutputResolver(); jaxbContext.generateSchema(sor); } catch(Exception ex) { ex.printStackTrace(); } }
public void generateSchema(final OutputStream stream, final Class<T> clazz, final String namespace) { try { JAXBContext context = JAXBContext.newInstance(clazz); context.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { StreamResult result = new StreamResult(stream); result.setSystemId(namespace); return result; } }); } catch (Exception e) { throw new RuntimeException("Failed to write object to stream.", e); } }
@Bean public XsdSchemaCollection schemaCollection() throws JAXBException, IOException { JAXBContext context = JAXBContext.newInstance(CalculatorRequest.class, CalculatorResponse.class); List<ByteArrayOutputStream> outs = new ArrayList<>(); context.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); outs.add(out); StreamResult result = new StreamResult(out); result.setSystemId(suggestedFileName); return result; } }); Resource[] resources = outs.stream(). map(ByteArrayOutputStream::toByteArray). map(InMemoryResource::new). collect(Collectors.toList()). toArray(new Resource[]{}); return new CommonsXsdSchemaCollection(resources); }
public static List<DOMResult> generateJaxbSchemas( JAXBContext context, final Map<String, DOMResult> builtIns) throws IOException { final List<DOMResult> results = new ArrayList<DOMResult>(); context.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String ns, String file) throws IOException { DOMResult result = new DOMResult(); if (builtIns.containsKey(ns)) { DOMResult dr = builtIns.get(ns); result.setSystemId(dr.getSystemId()); results.add(dr); return result; } result.setSystemId(file); results.add(result); return result; } }); return results; }
public static void main(String[] args) throws Exception { System.out.print("Generting XML schema in " + Constants.CONFIG_SCHEMA_FILE + "... "); JAXBContext ctx = JAXBContext.newInstance(WFSConfig.class); ctx.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { File file; if (namespaceUri.equals("http://www.3dcitydb.org/importer-exporter/config")) file = new File(Constants.CONFIG_SCHEMA_FILE); else file = new File(Constants.CONFIG_SCHEMA_PATH + "/ows/" + suggestedFileName); file.getAbsoluteFile().getParentFile().mkdirs(); StreamResult res = new StreamResult(); res.setSystemId(file.toURI().toString()); return res; } }); System.out.println("finished."); }
public static void main(String[] args) throws Exception { final File file = new File("src/main/resources/org/citydb/database/schema/3dcitydb-schema.xsd"); System.out.print("Generting XML schema in " + file.getAbsolutePath() + "..."); JAXBContext ctx = JAXBContext.newInstance(SchemaMapping.class); ctx.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { StreamResult res = new StreamResult(file); res.setSystemId(file.toURI().toString()); return res; } }); System.out.println("finished."); }
private static void storeSchema( final JAXBContext context, final BiFunction<String, String, File> locator) throws IOException { context.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(final String namespaceUri, final String suggestedFileName) throws IOException { final File file = locator.apply(namespaceUri, suggestedFileName); final Result output = new StreamResult(file); //output.setSystemId(suggestedFileName); return output; } }); }
public void generateSchema() { Class[] classes = new Class[1]; classes[0] = CbmNode.class; JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(classes); SchemaOutputResolver sor = new MySchemaOutputResolver(); jaxbContext.generateSchema(sor); } catch (Throwable e) { System.err .println("An error has occurred while generating the schema: " + e.getMessage()); e.printStackTrace(); } System.out .println("Schema generation complete. \nThe schema has been written to " + XML_SCHEMA); }
public void generateSchema(){ JAXBContext jc; try { jc = JAXBContext.newInstance(Dialog.class, DialogModel.class); jc.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { StreamResult result = new StreamResult(new FileWriter(suggestedFileName)); result.setSystemId(suggestedFileName); return result; } }); } catch (Exception e) { e.printStackTrace(); } }
/** * Generates a schema object for the specified class * @param clazz the class to generate the schema for * @return the generated schema object * @throws JAXBException * @throws IOException * @throws SAXException */ @SuppressWarnings("rawtypes") public static Schema generateSchema(Class clazz) throws JAXBException, IOException, SAXException { // get context to write schema for JAXBContext context = JAXBContext.newInstance(clazz); // write schema in DOMResult using SchemaOutputResolver final List<DOMResult> results = new ArrayList<DOMResult>(); context.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String ns, String file) throws IOException { DOMResult result = new DOMResult(); result.setSystemId(file); results.add(result); return result; } }); // transform domresult in source DOMResult res = results.get(0); DOMSource src = new DOMSource(res.getNode()); // write schema object from source return sf.newSchema(src); }
@Override public void generateSchema(Path outputPath) { try { JAXBContext jaxbContext = getJAXBContext(); SchemaOutputResolver sor = new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { StreamResult result = new StreamResult(); result.setSystemId(outputPath.toUri().toString()); return result; } }; jaxbContext.generateSchema(sor); } catch (JAXBException | IOException e) { throw new WindupException("Error generating Windup schema due to: " + e.getMessage(), e); } }
private SchemaOutputResolver createSchemaOutputResolver(Config config, String xmlpath) { File baseDir = new File(xmlpath, config.getBaseDir().getPath()); SchemaOutputResolverImpl outResolver = new SchemaOutputResolverImpl (baseDir); for( Schema schema : (List<Schema>)config.getSchema() ) { String namespace = schema.getNamespace(); File location = schema.getLocation(); outResolver.addSchemaInfo(namespace,location); } return outResolver; }
protected void generateSchema(File dir, String name) throws IOException { jaxbContext.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { File schFile = new File(dir, name + ".xsd"); StreamResult result = new StreamResult(schFile); result.setSystemId(schFile.toURI().toURL().toString()); return result; } }); }
@Before public void init() { schema = new StringWriter(); sink = new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { StreamResult sr = new StreamResult(schema); sr.setSystemId("/dev/null"); return sr; } }; }
@Before public void setUp() throws Exception { writer = new StringWriter(); sink = new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { StreamResult sr = new StreamResult(writer); sr.setSystemId("/dev/null"); return sr; } }; Assert.assertNull(null);// Shut up, Eclipse! Assert.assertEquals("", result()); }
@Before public void init() { schema = new StringWriter(); sink = new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { StreamResult sr = new StreamResult(schema); sr.setSystemId("/dev/null"); return sr; } }; assertEquals("", schema()); }
public void testCreateSchema() throws Exception { JAXBContext jctx = JAXBContext.newInstance(WorkflowDefinition.class); final Map<String, StreamResult> schemas = new HashMap<String, StreamResult>(); jctx.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { StreamResult sr = new StreamResult(new StringWriter()); sr.setSystemId(namespaceUri); schemas.put(namespaceUri, sr); return sr; } }); System.out.println(schemas.get(WorkflowProfileConsts.NS_WORKFLOW_V1).getWriter().toString()); }
public static void main(String[] args) throws JAXBException, IOException { JAXBContext jaxbContext = JAXBContext.newInstance(ServiceImpl.class, ServiceList.class, ParameterImpl.class, ParameterReference.class, NumericParameterImpl.class, StateParameterImpl.class, SwitchParameterImpl.class, StringParameterImpl.class); SchemaOutputResolver sor = new MySchemaOutputResolver(); jaxbContext.generateSchema(sor); }
/** * Used for testing. */ public SchemaOutputResolver createTestResolver() { return new SchemaOutputResolver() { public Result createOutput(String namespaceUri, String suggestedFileName) { SAXResult r = new SAXResult(new DefaultHandler()); r.setSystemId(suggestedFileName); return r; } }; }
@Test(expected = MarshalException.class) public void generatesAndUsesSchema() throws JAXBException, IOException, SAXException { final JAXBContext context = JAXBContext.newInstance(A.class); final DOMResult result = new DOMResult(); result.setSystemId("schema.xsd"); context.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) { return result; } }); @SuppressWarnings("deprecation") final SchemaFactory schemaFactory = SchemaFactory .newInstance(WellKnownNamespace.XML_SCHEMA); final Schema schema = schemaFactory.newSchema(new DOMSource(result .getNode())); final Marshaller marshaller = context.createMarshaller(); marshaller.setSchema(schema); // Works marshaller.marshal(new A("works"), System.out); // Fails marshaller.marshal(new A(null), System.out); }
public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Deployment.class); jc.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException { return new StreamResult(suggestedFileName); } }); }
public static void main(String[] args) throws JAXBException, IOException { JAXBContext jaxbContext = JAXBContext.newInstance(Presets.class); SchemaOutputResolver sor = new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { File file = new File("src/jsettlers/mapcreator/presetloader/preset.xsd"); StreamResult result = new StreamResult(file); result.setSystemId(file.toURI().toURL().toString()); return result; } }; jaxbContext.generateSchema(sor); System.out.println("Finished!"); }
private static void writeXmlSchema(Class xmlRoot) throws JAXBException, IOException { JAXBContext context = JAXBContext.newInstance(xmlRoot); context.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { return new StreamResult(new File("/Users/mattia",suggestedFileName)); } }); }
protected List<DOMResult> generateJaxbSchemas(JAXBContext context) throws IOException { final List<DOMResult> results = new ArrayList<DOMResult>(); context.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String ns, String file) throws IOException { DOMResult result = new DOMResult(); result.setSystemId(file); results.add(result); return result; } }); return results; }
public static void main(String[] args) { JAXBContext context; try { context = JAXBContext.newInstance(SounderConfig.class); SchemaOutputResolver sor = new PSSchemaOutputResolver("config_schema.xsd"); context.generateSchema(sor); } catch (Exception e) { e.printStackTrace(); } }
private static void generateXmlSchema(JAXBContext ctx) throws IOException { SchemaOutputResolver resolver = new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { File file = new File(suggestedFileName); // NOSONAR StreamResult result = new StreamResult(file); result.setSystemId(file); return result; } }; ctx.generateSchema(resolver); }
public void generateSchema(final String path) throws IOException, JAXBException { JAXBContext context = JAXBContext.newInstance(this.getClass()); context.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { return new StreamResult(new File(path, suggestedFileName)); } }); }
private SchemaOutputResolver createSchemaOutputResolver(Config config, String xmlpath) { File baseDir = new File(xmlpath, config.getBaseDir().getPath()); SchemaOutputResolverImpl schemaOutputResolver = new SchemaOutputResolverImpl (baseDir); for( Schema schema : (List<Schema>)config.getSchema() ) { String namespace = schema.getNamespace(); File location = schema.getLocation(); schemaOutputResolver.addSchemaInfo(namespace,location); } return schemaOutputResolver; }
private String tryBuildSchemaFromJaxbAnnotatedClass() throws JAXBException, IOException { final StringWriter stringWriter = new StringWriter(); JAXBContext.newInstance(clazz).generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { final StreamResult result = new StreamResult(stringWriter); result.setSystemId("schema" + clazz.getSimpleName()); return result; } }); return stringWriter.toString(); }