|
|
@ -41,10 +41,10 @@ public class Resources {
|
|
|
|
|
|
|
|
|
|
|
|
//cached schemas
|
|
|
|
//cached schemas
|
|
|
|
private static Map<Class<?>,Schema> schemas = new HashMap<Class<?>, Schema>();
|
|
|
|
private static Map<Class<?>,Schema> schemas = new HashMap<Class<?>, Schema>();
|
|
|
|
|
|
|
|
|
|
|
|
private static Map<Class<?>, JAXBContext> contexts = new HashMap<Class<?>, JAXBContext>();
|
|
|
|
private static Map<Class<?>, JAXBContext> contexts = new HashMap<Class<?>, JAXBContext>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
static {
|
|
|
|
|
|
|
|
|
|
|
|
schemaFactory.setResourceResolver(new SchemaResolver());
|
|
|
|
schemaFactory.setResourceResolver(new SchemaResolver());
|
|
|
@ -92,22 +92,23 @@ public class Resources {
|
|
|
|
public static void validate(Resource resource) throws IllegalArgumentException, Exception {
|
|
|
|
public static void validate(Resource resource) throws IllegalArgumentException, Exception {
|
|
|
|
|
|
|
|
|
|
|
|
Schema schema = schema(resource.getClass());
|
|
|
|
Schema schema = schema(resource.getClass());
|
|
|
|
|
|
|
|
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
ByteArrayInputStream in = new ByteArrayInputStream(marshal(resource,out).toByteArray());
|
|
|
|
ByteArrayInputStream in = new ByteArrayInputStream(marshal(resource,out).toByteArray());
|
|
|
|
|
|
|
|
|
|
|
|
schema.newValidator().validate(new StreamSource(in));
|
|
|
|
schema.newValidator().validate(new StreamSource(in));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//helper
|
|
|
|
//helper
|
|
|
|
private static synchronized Schema schema(Class<?> resourceClass) throws Exception {
|
|
|
|
private static synchronized Schema schema(Class<?> resourceClass) throws Exception {
|
|
|
|
|
|
|
|
|
|
|
|
String schemaResource = schemaResources.get(resourceClass);
|
|
|
|
String schemaResource = schemaResources.get(resourceClass);
|
|
|
|
|
|
|
|
|
|
|
|
if (schemaResource==null)
|
|
|
|
if (schemaResource==null)
|
|
|
|
throw new IllegalArgumentException("no known schema for:\n "+resourceClass);
|
|
|
|
throw new IllegalArgumentException("no known schema for:\n "+resourceClass);
|
|
|
|
|
|
|
|
|
|
|
|
Schema schema = schemas.get(resourceClass);
|
|
|
|
Schema schema = schemas.get(resourceClass);
|
|
|
|
if (schema==null) {
|
|
|
|
if (schema==null) {
|
|
|
|
InputStream stream = Resources.class.getClassLoader().getResourceAsStream(schemaResource);
|
|
|
|
InputStream stream = Resources.class.getClassLoader().getResourceAsStream(schemaResource);
|
|
|
@ -123,22 +124,22 @@ public class Resources {
|
|
|
|
* @param stream the stream in input
|
|
|
|
* @param stream the stream in input
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static <T extends OutputStream> T marshal(Object resource,T stream) {
|
|
|
|
public static <T extends OutputStream> T marshal(Object resource,T stream) {
|
|
|
|
|
|
|
|
|
|
|
|
marshal(resource, new StreamResult(stream));
|
|
|
|
marshal(resource, new StreamResult(stream));
|
|
|
|
return stream;
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Write the serialisation of a given resource to a given character stream.
|
|
|
|
* Write the serialisation of a given resource to a given character stream.
|
|
|
|
* @param resource the resource
|
|
|
|
* @param resource the resource
|
|
|
|
* @param stream the stream in input
|
|
|
|
* @param stream the stream in input
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static <T extends Writer> T marshal(Object resource,T stream) {
|
|
|
|
public static <T extends Writer> T marshal(Object resource,T stream) {
|
|
|
|
|
|
|
|
|
|
|
|
marshal(resource,new StreamResult(stream));
|
|
|
|
marshal(resource,new StreamResult(stream));
|
|
|
|
return stream;
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Write the serialisation of a given resource to a {@link Result}.
|
|
|
|
* Write the serialisation of a given resource to a {@link Result}.
|
|
|
|
* @param resource the resource
|
|
|
|
* @param resource the resource
|
|
|
@ -146,16 +147,16 @@ public class Resources {
|
|
|
|
* @return the result in input
|
|
|
|
* @return the result in input
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static <T extends Result> T marshal(Object resource,T result) {
|
|
|
|
public static <T extends Result> T marshal(Object resource,T result) {
|
|
|
|
|
|
|
|
|
|
|
|
if (resource instanceof Resource)
|
|
|
|
if (resource instanceof Resource)
|
|
|
|
((Resource) resource).lock.lock();
|
|
|
|
((Resource) resource).lock.lock();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
JAXBContext context = context(resource.getClass());
|
|
|
|
JAXBContext context = context(resource.getClass());
|
|
|
|
Marshaller m = context.createMarshaller();
|
|
|
|
Marshaller m = context.createMarshaller();
|
|
|
|
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
|
|
|
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
|
|
|
|
|
|
|
|
|
|
|
m.marshal(resource,result);
|
|
|
|
m.marshal(resource,result);
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception e) {
|
|
|
|
catch(Exception e) {
|
|
|
@ -164,7 +165,7 @@ public class Resources {
|
|
|
|
if (resource instanceof Resource)
|
|
|
|
if (resource instanceof Resource)
|
|
|
|
((Resource) resource).lock.unlock();
|
|
|
|
((Resource) resource).lock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -195,7 +196,7 @@ public class Resources {
|
|
|
|
public static <T> T unmarshal(Class<T> resourceClass, InputStream stream) {
|
|
|
|
public static <T> T unmarshal(Class<T> resourceClass, InputStream stream) {
|
|
|
|
return unmarshal(resourceClass,new StreamSource(stream));
|
|
|
|
return unmarshal(resourceClass,new StreamSource(stream));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Creates a resource of given class from its serialisation in a given {@link Source}.
|
|
|
|
* Creates a resource of given class from its serialisation in a given {@link Source}.
|
|
|
|
* @param resourceClass the class of the resource
|
|
|
|
* @param resourceClass the class of the resource
|
|
|
@ -212,16 +213,16 @@ public class Resources {
|
|
|
|
throw new RuntimeException("deserialisation error",e);
|
|
|
|
throw new RuntimeException("deserialisation error",e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//helper
|
|
|
|
//helper
|
|
|
|
private static synchronized JAXBContext context(Class<?> resourceClass) throws Exception {
|
|
|
|
private static synchronized JAXBContext context(Class<?> resourceClass) throws Exception {
|
|
|
|
|
|
|
|
|
|
|
|
JAXBContext ctx = contexts.get(resourceClass);
|
|
|
|
JAXBContext ctx = contexts.get(resourceClass);
|
|
|
|
if (ctx==null) {
|
|
|
|
if (ctx==null) {
|
|
|
|
ctx = JAXBContext.newInstance(resourceClass);
|
|
|
|
ctx = JAXBContext.newInstance(resourceClass);
|
|
|
|
contexts.put(resourceClass,ctx);
|
|
|
|
contexts.put(resourceClass,ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
return ctx;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|