use of javax.xml.bind.JAXBIntrospector in project jersey by jersey.
the class WadlGeneratorJAXBGrammarGenerator method buildModelAndSchemas.
/**
* 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(final Map<String, ApplicationDescription.ExternalGrammar> extraFiles) {
// Lets get all candidate classes so we can create the JAX-B context
// include any @XmlSeeAlso references.
final Set<Class> classSet = new HashSet<>(seeAlsoClasses);
for (final TypeCallbackPair pair : nameCallbacks) {
final GenericType genericType = pair.genericType;
final Class<?> clazz = genericType.getRawType();
if (clazz.getAnnotation(XmlRootElement.class) != null) {
classSet.add(clazz);
} else if (SPECIAL_GENERIC_TYPES.contains(clazz)) {
final Type type = genericType.getType();
if (type instanceof ParameterizedType) {
final 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 {
final JAXBContext context = JAXBContext.newInstance(classSet.toArray(new Class[classSet.size()]));
final List<StreamResult> results = new ArrayList<>();
context.generateSchema(new SchemaOutputResolver() {
int counter = 0;
@Override
public Result createOutput(final String namespaceUri, final String suggestedFileName) {
final StreamResult result = new StreamResult(new CharArrayWriter());
result.setSystemId("xsd" + (counter++) + ".xsd");
results.add(result);
return result;
}
});
for (final StreamResult result : results) {
final CharArrayWriter writer = (CharArrayWriter) result.getWriter();
final byte[] contents = writer.toString().getBytes("UTF8");
extraFiles.put(result.getSystemId(), new ApplicationDescription.ExternalGrammar(// I don't think there is a specific media type for XML Schema
MediaType.APPLICATION_XML_TYPE, contents));
}
// Create an introspector
//
introspector = context.createJAXBIntrospector();
} catch (final JAXBException e) {
LOGGER.log(Level.SEVERE, "Failed to generate the schema for the JAX-B elements", e);
} catch (final IOException e) {
LOGGER.log(Level.SEVERE, "Failed to generate the schema for the JAX-B elements due to an IO error", e);
}
if (introspector != null) {
final JAXBIntrospector copy = introspector;
return new Resolver() {
public QName resolve(final Class type) {
Object parameterClassInstance = null;
try {
final Constructor<?> defaultConstructor = AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor<?>>() {
@SuppressWarnings("unchecked")
@Override
public Constructor<?> run() throws NoSuchMethodException {
final Constructor<?> constructor = type.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor;
}
});
parameterClassInstance = defaultConstructor.newInstance();
} catch (final InstantiationException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
LOGGER.log(Level.FINE, null, ex);
} catch (final PrivilegedActionException ex) {
LOGGER.log(Level.FINE, null, ex.getCause());
}
if (parameterClassInstance == null) {
return null;
}
try {
return copy.getElementName(parameterClassInstance);
} catch (final NullPointerException e) {
// annotation is passed as a parameter of #getElementName method.
return null;
}
}
};
} else {
// No resolver created
return null;
}
}
Aggregations