use of org.platformlayer.xml.MemorySchemaOutputResolver in project platformlayer by platformlayer.
the class ServiceResource method getSchema.
@GET
@Path("schema")
@Produces({ XML })
public String getSchema() throws IOException, JAXBException {
ServiceProvider serviceProvider = getServiceProvider();
String namespace = null;
List<Class<?>> javaClasses = Lists.newArrayList();
for (ModelClass<?> modelClass : serviceProvider.getModels().all()) {
javaClasses.add(modelClass.getJavaClass());
String modelNamespace = modelClass.getPrimaryNamespace();
if (namespace == null) {
namespace = modelNamespace;
} else if (!namespace.equals(modelNamespace)) {
throw new IllegalStateException();
}
}
JAXBContext jaxbContext = JAXBContext.newInstance(javaClasses.toArray(new Class<?>[javaClasses.size()]));
MemorySchemaOutputResolver schemaOutputResolver = new MemorySchemaOutputResolver();
jaxbContext.generateSchema(schemaOutputResolver);
Map<String, StringWriter> writers = schemaOutputResolver.getWriters();
StringWriter writer = writers.get(namespace);
if (writer == null) {
throw new IllegalArgumentException();
}
return writer.getBuffer().toString();
}
Aggregations