* Created on: Apr 10, 2009\r
* Created by: Jonathan\r
*\r
- * Revamped to do away with ThreadLocal 5/27/2011, JG1555\r
+ * Revamped to do away with ThreadLocal 5/27/2011, JonathanGathman\r
*\r
* (c) 2009 SBC Knowledge Ventures, L.P. All rights reserved.\r
******************************************************************* \r
*\r
*/\r
public class JAXBumar {\r
- // Need to store off possible JAXBContexts based on Class, which will be stored in Creator\r
- private static Map<Class<?>[],Pool<SUnmarshaller>> pools = new HashMap<Class<?>[], Pool<SUnmarshaller>>();\r
-\r
- private Class<?> cls;\r
- private Schema schema;\r
- private Pool<SUnmarshaller> mpool;;\r
-\r
- // Handle Marshaller class setting of properties only when needed\r
- private class SUnmarshaller {\r
- private Unmarshaller u;\r
- private Schema s;\r
- \r
- public SUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {\r
- u = unmarshaller;\r
- s = null;\r
- }\r
- \r
- public Unmarshaller get(Schema schema) throws JAXBException {\r
- if(s != schema) {\r
- u.setSchema(s = schema);\r
- }\r
- return u;\r
- }\r
- }\r
- \r
- private class Creator implements Pool.Creator<SUnmarshaller> {\r
- private JAXBContext jc;\r
- private String name;\r
- \r
- public Creator(Class<?>[] classes) throws JAXBException {\r
- jc = JAXBContext.newInstance(classes);\r
- name = "JAXBumar: " + classes[0].getName();\r
- }\r
- \r
- // @Override\r
- public SUnmarshaller create() throws APIException {\r
- try {\r
- return new SUnmarshaller(jc.createUnmarshaller());\r
- } catch (JAXBException e) {\r
- throw new APIException(e);\r
- }\r
- }\r
- \r
- public String toString() {\r
- return name;\r
- }\r
-\r
- // @Override\r
- public void destroy(SUnmarshaller sui) {\r
- // Nothing to do\r
- }\r
- \r
- // @Override\r
- public boolean isValid(SUnmarshaller t) {\r
- return true; \r
- }\r
-\r
- // @Override\r
- public void reuse(SUnmarshaller t) {\r
- // Nothing to do here\r
- }\r
-\r
- }\r
-\r
- private Pool<SUnmarshaller> getPool(Class<?> ... classes) throws JAXBException {\r
- Pool<SUnmarshaller> mp;\r
- synchronized(pools) {\r
- mp = pools.get(classes);\r
- if(mp==null) {\r
- pools.put(classes,mp = new Pool<SUnmarshaller>(new Creator(classes)));\r
- }\r
- } \r
- return mp;\r
- }\r
-\r
- public JAXBumar(Class<?> ... classes) throws JAXBException {\r
- cls = classes[0];\r
- mpool = getPool(classes);\r
- schema = null;\r
- }\r
- \r
- /**\r
- * Constructs a new JAXBumar with schema validation enabled.\r
- * \r
- * @param schema\r
- * @param theClass\r
- * @throws JAXBException\r
- */\r
- public JAXBumar(Schema schema, Class<?> ... classes) throws JAXBException {\r
- cls = classes[0];\r
- mpool = getPool(classes);\r
- this.schema = schema;\r
- }\r
- \r
- @SuppressWarnings("unchecked")\r
- public<O> O unmarshal(LogTarget env, Node node) throws JAXBException, APIException {\r
- Pooled<SUnmarshaller> s = mpool.get(env);\r
- try {\r
- return s.content.get(schema).unmarshal(node,(Class<O>)cls).getValue();\r
- } finally {\r
- s.done();\r
- }\r
-\r
- }\r
- \r
- @SuppressWarnings("unchecked")\r
- public<O> O unmarshal(LogTarget env, String xml) throws JAXBException, APIException {\r
- if(xml==null) throw new JAXBException("Null Input for String unmarshal");\r
- Pooled<SUnmarshaller> s = mpool.get(env);\r
- try {\r
- return (O)s.content.get(schema).unmarshal(\r
- new StreamSource(new StringReader(xml))\r
- ,(Class<O>)cls).getValue();\r
- } finally {\r
- s.done();\r
- }\r
- }\r
- \r
- @SuppressWarnings("unchecked")\r
- public<O> O unmarshal(LogTarget env, File xmlFile) throws JAXBException, APIException {\r
- Pooled<SUnmarshaller> s = mpool.get(env);\r
- try {\r
- return (O)s.content.get(schema).unmarshal(xmlFile);\r
- } finally {\r
- s.done();\r
- }\r
-\r
- }\r
- \r
- @SuppressWarnings("unchecked")\r
- public<O> O unmarshal(LogTarget env,InputStream is) throws JAXBException, APIException {\r
- Pooled<SUnmarshaller> s = mpool.get(env);\r
- try {\r
- return (O)s.content.get(schema).unmarshal(is);\r
- } finally {\r
- s.done();\r
- }\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- public<O> O unmarshal(LogTarget env, Reader rdr) throws JAXBException, APIException {\r
- Pooled<SUnmarshaller> s = mpool.get(env);\r
- try {\r
- return (O)s.content.get(schema).unmarshal(rdr);\r
- } finally {\r
- s.done();\r
- }\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- public<O> O unmarshal(LogTarget env, XMLStreamReader xsr) throws JAXBException, APIException {\r
- Pooled<SUnmarshaller> s = mpool.get(env);\r
- try {\r
- return (O)s.content.get(schema).unmarshal(xsr,(Class<O>)cls).getValue();\r
- } finally {\r
- s.done();\r
- }\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- public<O> O unmarshal(LogTarget env, XMLEventReader xer) throws JAXBException, APIException {\r
- Pooled<SUnmarshaller> s = mpool.get(env);\r
- try {\r
- return (O)s.content.get(schema).unmarshal(xer,(Class<O>)cls).getValue();\r
- } finally {\r
- s.done();\r
- }\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- public<O> O newInstance() throws InstantiationException, IllegalAccessException{\r
- return ((Class<O>)cls).newInstance();\r
- }\r
+ // Need to store off possible JAXBContexts based on Class, which will be stored in Creator\r
+ private static Map<Class<?>[],Pool<SUnmarshaller>> pools = new HashMap<>();\r
+\r
+ private Class<?> cls;\r
+ private Schema schema;\r
+ private Pool<SUnmarshaller> mpool;;\r
+\r
+ // Handle Marshaller class setting of properties only when needed\r
+ private class SUnmarshaller {\r
+ private Unmarshaller u;\r
+ private Schema s;\r
+ \r
+ public SUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {\r
+ u = unmarshaller;\r
+ s = null;\r
+ }\r
+ \r
+ public Unmarshaller get(Schema schema) throws JAXBException {\r
+ if(s != schema) {\r
+ u.setSchema(s = schema);\r
+ }\r
+ return u;\r
+ }\r
+ }\r
+ \r
+ private class Creator implements Pool.Creator<SUnmarshaller> {\r
+ private JAXBContext jc;\r
+ private String name;\r
+ \r
+ public Creator(Class<?>[] classes) throws JAXBException {\r
+ jc = JAXBContext.newInstance(classes);\r
+ name = "JAXBumar: " + classes[0].getName();\r
+ }\r
+ \r
+ // @Override\r
+ public SUnmarshaller create() throws APIException {\r
+ try {\r
+ return new SUnmarshaller(jc.createUnmarshaller());\r
+ } catch (JAXBException e) {\r
+ throw new APIException(e);\r
+ }\r
+ }\r
+ \r
+ public String toString() {\r
+ return name;\r
+ }\r
+\r
+ // @Override\r
+ public void destroy(SUnmarshaller sui) {\r
+ // Nothing to do\r
+ }\r
+ \r
+ // @Override\r
+ public boolean isValid(SUnmarshaller t) {\r
+ return true; \r
+ }\r
+\r
+ // @Override\r
+ public void reuse(SUnmarshaller t) {\r
+ // Nothing to do here\r
+ }\r
+\r
+ }\r
+\r
+ private Pool<SUnmarshaller> getPool(Class<?> ... classes) throws JAXBException {\r
+ Pool<SUnmarshaller> mp;\r
+ synchronized(pools) {\r
+ mp = pools.get(classes);\r
+ if(mp==null) {\r
+ pools.put(classes,mp = new Pool<SUnmarshaller>(new Creator(classes)));\r
+ }\r
+ } \r
+ return mp;\r
+ }\r
+\r
+ public JAXBumar(Class<?> ... classes) throws JAXBException {\r
+ cls = classes[0];\r
+ mpool = getPool(classes);\r
+ schema = null;\r
+ }\r
+ \r
+ /**\r
+ * Constructs a new JAXBumar with schema validation enabled.\r
+ * \r
+ * @param schema\r
+ * @param theClass\r
+ * @throws JAXBException\r
+ */\r
+ public JAXBumar(Schema schema, Class<?> ... classes) throws JAXBException {\r
+ cls = classes[0];\r
+ mpool = getPool(classes);\r
+ this.schema = schema;\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ public<O> O unmarshal(LogTarget env, Node node) throws JAXBException, APIException {\r
+ Pooled<SUnmarshaller> s = mpool.get(env);\r
+ try {\r
+ return s.content.get(schema).unmarshal(node,(Class<O>)cls).getValue();\r
+ } finally {\r
+ s.done();\r
+ }\r
+\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ public<O> O unmarshal(LogTarget env, String xml) throws JAXBException, APIException {\r
+ if(xml==null) throw new JAXBException("Null Input for String unmarshal");\r
+ Pooled<SUnmarshaller> s = mpool.get(env);\r
+ try {\r
+ return (O)s.content.get(schema).unmarshal(\r
+ new StreamSource(new StringReader(xml))\r
+ ,(Class<O>)cls).getValue();\r
+ } finally {\r
+ s.done();\r
+ }\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ public<O> O unmarshal(LogTarget env, File xmlFile) throws JAXBException, APIException {\r
+ Pooled<SUnmarshaller> s = mpool.get(env);\r
+ try {\r
+ return (O)s.content.get(schema).unmarshal(xmlFile);\r
+ } finally {\r
+ s.done();\r
+ }\r
+\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ public<O> O unmarshal(LogTarget env,InputStream is) throws JAXBException, APIException {\r
+ Pooled<SUnmarshaller> s = mpool.get(env);\r
+ try {\r
+ return (O)s.content.get(schema).unmarshal(is);\r
+ } finally {\r
+ s.done();\r
+ }\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ public<O> O unmarshal(LogTarget env, Reader rdr) throws JAXBException, APIException {\r
+ Pooled<SUnmarshaller> s = mpool.get(env);\r
+ try {\r
+ return (O)s.content.get(schema).unmarshal(rdr);\r
+ } finally {\r
+ s.done();\r
+ }\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ public<O> O unmarshal(LogTarget env, XMLStreamReader xsr) throws JAXBException, APIException {\r
+ Pooled<SUnmarshaller> s = mpool.get(env);\r
+ try {\r
+ return (O)s.content.get(schema).unmarshal(xsr,(Class<O>)cls).getValue();\r
+ } finally {\r
+ s.done();\r
+ }\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ public<O> O unmarshal(LogTarget env, XMLEventReader xer) throws JAXBException, APIException {\r
+ Pooled<SUnmarshaller> s = mpool.get(env);\r
+ try {\r
+ return (O)s.content.get(schema).unmarshal(xer,(Class<O>)cls).getValue();\r
+ } finally {\r
+ s.done();\r
+ }\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ public<O> O newInstance() throws InstantiationException, IllegalAccessException{\r
+ return ((Class<O>)cls).newInstance();\r
+ }\r
}\r