/** * Generate schema. * * @throws Exception the exception */ @Test public void generateSchema() throws Exception { File schemaFile = fileAnticipator.expecting("tca-datacollection-config.xsd"); context.generateSchema(new TestOutputResolver(schemaFile)); if (fileAnticipator.isInitialized()) { fileAnticipator.deleteExpected(); } }
public static String generateSchemma(Class<?>... classes) { try { JAXBContext jc = JAXBContext.newInstance(classes); StringSchemaOutputResolver ssor = new StringSchemaOutputResolver(); jc.generateSchema(ssor); return ssor.getSchemma(); } catch (Exception e) { throw new RuntimeException("Failed to generate schemma", e); } }
public static void main(String[] args) throws Exception { final File baseDir = new File("./data/static_data"); class MySchemaOutputResolver extends SchemaOutputResolver { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { return new StreamResult(new File(baseDir, "static_data1.xsd")); } } JAXBContext context = JAXBContext.newInstance(StaticData.class); context.generateSchema(new MySchemaOutputResolver()); }
private static void createXsd() throws JAXBException, IOException { JAXBContext jaxbContext = JAXBContext.newInstance(Characters.class); SchemaOutputResolver sor = new MySchemaOutputResolver("Characters.xsd"); jaxbContext.generateSchema(sor); jaxbContext = JAXBContext.newInstance(Theme.class); sor = new MySchemaOutputResolver("Theme.xsd"); jaxbContext.generateSchema(sor); // jaxbContext = JAXBContext.newInstance(LearningAct.class); // sor = new MySchemaOutputResolver("LearningAct.xsd"); // jaxbContext.generateSchema(sor); jaxbContext = JAXBContext.newInstance(Lesson.class); sor = new MySchemaOutputResolver("Lesson.xsd"); jaxbContext.generateSchema(sor); jaxbContext = JAXBContext.newInstance(Challenge.class); sor = new MySchemaOutputResolver("Challenge.xsd"); jaxbContext.generateSchema(sor); jaxbContext = JAXBContext.newInstance(Locale.class); sor = new MySchemaOutputResolver("Locale.xsd"); jaxbContext.generateSchema(sor); jaxbContext = JAXBContext.newInstance(Subject.class); sor = new MySchemaOutputResolver("Subject.xsd"); jaxbContext.generateSchema(sor); }
protected void generateXSDForClass(Class<?> element) { try { String name = element.getSimpleName().toLowerCase(); String fileName = name + ".xsd"; // $NON-NLS-1$ JAXBContext jaxbContext = JAXBContext.newInstance(element); CustomSchemaOutputResolver sor = new CustomSchemaOutputResolver(fileName); jaxbContext.generateSchema(sor); File file = sor.getFile(); String content = FileUtil.fileToString(file); generateExamle(file, content); spec.getCoreRaml().addGlobalSchema(name, content, false, false); } catch (JAXBException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * generateXSDForClass. * * @param element a {@link java.lang.Class} object. */ protected String generateXSDForClass(Class<?> element) { try { String name = firstLetterToLowerCase(element.getSimpleName()); JAXBContext jaxbContext = JAXBContext.newInstance(element); CustomSchemaOutputResolver sor = new CustomSchemaOutputResolver(name); jaxbContext.generateSchema(sor); File file = sor.getFile(); if (file != null) { String content = FileUtil.fileToString(file); generateExamle(file, content); spec.getCoreRaml().addGlobalSchema(name + "-xml", content, false, true); return content; } } catch (JAXBException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
@Test public void marshalDocument() { Document document = new Document(); Export export = new Export(); ExportKopf exportKopf = new ExportKopf(); exportKopf.setQuelle("lokal"); export.setExportKopf(exportKopf); ExportInhalt exportInhalt = new ExportInhalt(); export.setExportInhalt(exportInhalt); export.setSchemaVersion("bec811a9807a8c8da403d70b9b5e22ad"); document.setExport(export); try { JAXBContext jaxbContext = JAXBContext.newInstance(Document.class); jaxbContext.generateSchema( new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { StreamResult streamResult = new StreamResult( new PrintWriter(System.err) { @Override public void close() {} }); streamResult.setSystemId(suggestedFileName); return streamResult; } }); System.out.println("Context class: " + jaxbContext.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(document, System.out); } catch (JAXBException | IOException e) { e.printStackTrace(); fail(); } }
/** * Generates an XSD for the managed class. * * @return The XSD string */ protected String generateSchema() throws IOException { final XmlOutputResolver outputResolver = new XmlOutputResolver(); jaxb.generateSchema(outputResolver); return outputResolver.getOutput(encoding); }
/** * 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( Map<String, ApplicationDescription.ExternalGrammar> extraFiles) { // Lets get all candidate classes so we can create the JAX-B context // include any @XmlSeeAlso references. Set<Class> classSet = new HashSet<Class>(seeAlsoClasses); for (TypeCallbackPair pair : nameCallbacks) { final GenericType genericType = pair.genericType; Class<?> clazz = genericType.getRawType(); // Is this class itself interesting? if (clazz.getAnnotation(XmlRootElement.class) != null) { classSet.add(clazz); } else if (SPECIAL_GENERIC_TYPES.contains(clazz)) { Type type = genericType.getType(); if (type instanceof ParameterizedType) { 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 { JAXBContext context = JAXBContext.newInstance(classSet.toArray(new Class[classSet.size()])); final List<StreamResult> results = new ArrayList<StreamResult>(); context.generateSchema( new SchemaOutputResolver() { int counter = 0; @Override public Result createOutput(String namespaceUri, String suggestedFileName) { StreamResult result = new StreamResult(new CharArrayWriter()); result.setSystemId("xsd" + (counter++) + ".xsd"); results.add(result); return result; } }); // Store the new files for later use // for (StreamResult result : results) { CharArrayWriter writer = (CharArrayWriter) result.getWriter(); byte[] contents = writer.toString().getBytes("UTF8"); extraFiles.put( result.getSystemId(), new ApplicationDescription.ExternalGrammar( MediaType .APPLICATION_XML_TYPE, // I don't think there is a specific media type for XML // Schema contents)); } // Create an introspector // introspector = context.createJAXBIntrospector(); } catch (JAXBException e) { LOGGER.log(Level.SEVERE, "Failed to generate the schema for the JAX-B elements", e); } catch (IOException e) { LOGGER.log( Level.SEVERE, "Failed to generate the schema for the JAX-B elements due to an IO error", e); } // Create introspector if (introspector != null) { final JAXBIntrospector copy = introspector; return new Resolver() { public QName resolve(Class type) { Object parameterClassInstance = null; try { Constructor<?> defaultConstructor = type.getDeclaredConstructor(); defaultConstructor.setAccessible(true); parameterClassInstance = defaultConstructor.newInstance(); } catch (InstantiationException ex) { LOGGER.log(Level.FINE, null, ex); } catch (IllegalAccessException ex) { LOGGER.log(Level.FINE, null, ex); } catch (IllegalArgumentException ex) { LOGGER.log(Level.FINE, null, ex); } catch (InvocationTargetException ex) { LOGGER.log(Level.FINE, null, ex); } catch (SecurityException ex) { LOGGER.log(Level.FINE, null, ex); } catch (NoSuchMethodException ex) { LOGGER.log(Level.FINE, null, ex); } if (parameterClassInstance == null) { return null; } try { return copy.getElementName(parameterClassInstance); } catch (NullPointerException e) { // EclipseLink throws an NPE if an object annotated with @XmlType and without the // @XmlRootElement // annotation is passed as a parameter of #getElementName method. return null; } } }; } else { return null; // No resolver created } }