* 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 JAXBmar {\r
- // Need to store off possible JAXBContexts based on Class, which will be stored in Creator\r
- private static Map<Class<?>[],Pool<PMarshaller>> pools = new HashMap<Class<?>[], Pool<PMarshaller>>();\r
-\r
- // Handle Marshaller class setting of properties only when needed\r
- private class PMarshaller {\r
- private Marshaller m;\r
- private boolean p;\r
- private boolean f;\r
- \r
- public PMarshaller(Marshaller marshaller) throws JAXBException {\r
- m = marshaller;\r
- m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");\r
- m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, p = false);\r
- m.setProperty(Marshaller.JAXB_FRAGMENT, f = false);\r
- }\r
- \r
- public Marshaller get(boolean pretty, boolean fragment) throws JAXBException {\r
- if(pretty != p) {\r
- m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, p = pretty);\r
- }\r
- if(fragment != f) {\r
- m.setProperty(Marshaller.JAXB_FRAGMENT, f = fragment);\r
- }\r
- return m;\r
- }\r
- }\r
- \r
- private class Creator implements Pool.Creator<PMarshaller> {\r
- private JAXBContext jc;\r
- private String name;\r
- public Creator(Class<?>[] classes) throws JAXBException {\r
- jc = JAXBContext.newInstance(classes);\r
- name = "JAXBmar: " + classes[0].getName();\r
- }\r
- \r
- // @Override\r
- public PMarshaller create() throws APIException {\r
- try {\r
- return new PMarshaller(jc.createMarshaller());\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 reuse(PMarshaller pm) {\r
- // Nothing to do\r
- }\r
- \r
- // @Override\r
- public void destroy(PMarshaller pm) {\r
- // Nothing to do\r
- }\r
-\r
- // @Override\r
- public boolean isValid(PMarshaller t) {\r
- return true; \r
- }\r
- }\r
-\r
- //TODO isn't UTF-8 a standard string somewhere for encoding?\r
- private boolean fragment= false;\r
- private boolean pretty=false;\r
- private QName qname;\r
- \r
- private Pool<PMarshaller> mpool; // specific Pool associated with constructed Classes\r
- private Class<?> cls;\r
- \r
- private Pool<PMarshaller> getPool(Class<?> ... classes) throws JAXBException {\r
- Pool<PMarshaller> mp;\r
- synchronized(pools) {\r
- mp = pools.get(classes);\r
- if(mp==null) {\r
- pools.put(classes,mp = new Pool<PMarshaller>(new Creator(classes)));\r
- }\r
- } \r
- return mp;\r
- }\r
- \r
- public JAXBmar(Class<?>... classes) throws JAXBException {\r
- cls = classes[0];\r
- mpool = getPool(classes);\r
- qname = null;\r
- }\r
-\r
- public JAXBmar(QName theQname, Class<?>... classes) throws JAXBException {\r
- cls = classes[0];\r
- mpool = getPool(classes);\r
- qname = theQname;\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- public<O> O marshal(LogTarget lt,O o, Writer writer, boolean ... options) throws JAXBException, APIException {\r
- boolean pretty, fragment;\r
- pretty = options.length>0?options[0]:this.pretty;\r
- fragment = options.length>1?options[1]:this.fragment;\r
- Pooled<PMarshaller> m = mpool.get(lt);\r
- try {\r
- if(qname==null) {\r
- m.content.get(pretty,fragment).marshal(o, writer);\r
- } else {\r
- m.content.get(pretty,fragment).marshal(\r
- new JAXBElement<O>(qname, (Class<O>)cls, o ),\r
- writer);\r
- }\r
- return o;\r
- } finally {\r
- m.done();\r
- }\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- public<O> O marshal(LogTarget lt, O o, OutputStream os, boolean ... options) throws JAXBException, APIException {\r
- boolean pretty, fragment;\r
- pretty = options.length>0?options[0]:this.pretty;\r
- fragment = options.length>1?options[1]:this.fragment;\r
- Pooled<PMarshaller> m = mpool.get(lt);\r
- try {\r
- if(qname==null) {\r
- m.content.get(pretty,fragment).marshal(o, os);\r
- } else {\r
- m.content.get(pretty,fragment).marshal(\r
- new JAXBElement<O>(qname, (Class<O>)cls, o ),os);\r
- }\r
- return o;\r
- } finally {\r
- m.done();\r
- }\r
- }\r
- \r
- public<O> O marshal(LogTarget lt, O o, Writer writer, Class<O> clss) throws JAXBException, APIException {\r
- Pooled<PMarshaller> m = mpool.get(lt);\r
- try {\r
- if(qname==null) {\r
- m.content.get(pretty,fragment).marshal(o, writer);\r
- } else {\r
- m.content.get(pretty,fragment).marshal(\r
- new JAXBElement<O>(qname, clss, o),writer);\r
- }\r
- return o;\r
- } finally {\r
- m.done();\r
- }\r
- \r
- }\r
-\r
- public<O> O marshal(LogTarget lt, O o, OutputStream os, Class<O> clss) throws JAXBException, APIException {\r
- Pooled<PMarshaller> m = mpool.get(lt);\r
- try {\r
- if(qname==null) { \r
- m.content.get(pretty,fragment).marshal(o, os);\r
- } else {\r
- m.content.get(pretty,fragment).marshal(\r
- new JAXBElement<O>(qname, clss, o ),os);\r
- }\r
- return o;\r
- } finally {\r
- m.done();\r
- }\r
- }\r
-\r
- /**\r
- * @return\r
- */\r
- public Class<?> getMarshalClass() {\r
- return cls;\r
- }\r
-\r
- public<O> String stringify(LogTarget lt, O o) throws JAXBException, APIException {\r
- StringWriter sw = new StringWriter();\r
- marshal(lt,o,sw);\r
- return sw.toString();\r
- }\r
-\r
- public JAXBmar pretty(boolean pretty) {\r
- this.pretty = pretty;\r
- return this;\r
- }\r
- \r
- public JAXBmar asFragment(boolean fragment) {\r
- this.fragment = fragment;\r
- return this;\r
- }\r
+ // Need to store off possible JAXBContexts based on Class, which will be stored in Creator\r
+ private static Map<Class<?>[],Pool<PMarshaller>> pools = new HashMap<>();\r
+\r
+ // Handle Marshaller class setting of properties only when needed\r
+ private class PMarshaller {\r
+ private Marshaller m;\r
+ private boolean p;\r
+ private boolean f;\r
+ \r
+ public PMarshaller(Marshaller marshaller) throws JAXBException {\r
+ m = marshaller;\r
+ m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");\r
+ m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, p = false);\r
+ m.setProperty(Marshaller.JAXB_FRAGMENT, f = false);\r
+ }\r
+ \r
+ public Marshaller get(boolean pretty, boolean fragment) throws JAXBException {\r
+ if(pretty != p) {\r
+ m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, p = pretty);\r
+ }\r
+ if(fragment != f) {\r
+ m.setProperty(Marshaller.JAXB_FRAGMENT, f = fragment);\r
+ }\r
+ return m;\r
+ }\r
+ }\r
+ \r
+ private class Creator implements Pool.Creator<PMarshaller> {\r
+ private JAXBContext jc;\r
+ private String name;\r
+ public Creator(Class<?>[] classes) throws JAXBException {\r
+ jc = JAXBContext.newInstance(classes);\r
+ name = "JAXBmar: " + classes[0].getName();\r
+ }\r
+ \r
+ // @Override\r
+ public PMarshaller create() throws APIException {\r
+ try {\r
+ return new PMarshaller(jc.createMarshaller());\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 reuse(PMarshaller pm) {\r
+ // Nothing to do\r
+ }\r
+ \r
+ // @Override\r
+ public void destroy(PMarshaller pm) {\r
+ // Nothing to do\r
+ }\r
+\r
+ // @Override\r
+ public boolean isValid(PMarshaller t) {\r
+ return true; \r
+ }\r
+ }\r
+\r
+ //TODO isn't UTF-8 a standard string somewhere for encoding?\r
+ private boolean fragment= false;\r
+ private boolean pretty=false;\r
+ private QName qname;\r
+ \r
+ private Pool<PMarshaller> mpool; // specific Pool associated with constructed Classes\r
+ private Class<?> cls;\r
+ \r
+ private Pool<PMarshaller> getPool(Class<?> ... classes) throws JAXBException {\r
+ Pool<PMarshaller> mp;\r
+ synchronized(pools) {\r
+ mp = pools.get(classes);\r
+ if(mp==null) {\r
+ pools.put(classes,mp = new Pool<PMarshaller>(new Creator(classes)));\r
+ }\r
+ } \r
+ return mp;\r
+ }\r
+ \r
+ public JAXBmar(Class<?>... classes) throws JAXBException {\r
+ cls = classes[0];\r
+ mpool = getPool(classes);\r
+ qname = null;\r
+ }\r
+\r
+ public JAXBmar(QName theQname, Class<?>... classes) throws JAXBException {\r
+ cls = classes[0];\r
+ mpool = getPool(classes);\r
+ qname = theQname;\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ public<O> O marshal(LogTarget lt,O o, Writer writer, boolean ... options) throws JAXBException, APIException {\r
+ boolean pretty, fragment;\r
+ pretty = options.length>0?options[0]:this.pretty;\r
+ fragment = options.length>1?options[1]:this.fragment;\r
+ Pooled<PMarshaller> m = mpool.get(lt);\r
+ try {\r
+ if(qname==null) {\r
+ m.content.get(pretty,fragment).marshal(o, writer);\r
+ } else {\r
+ m.content.get(pretty,fragment).marshal(\r
+ new JAXBElement<O>(qname, (Class<O>)cls, o ),\r
+ writer);\r
+ }\r
+ return o;\r
+ } finally {\r
+ m.done();\r
+ }\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ public<O> O marshal(LogTarget lt, O o, OutputStream os, boolean ... options) throws JAXBException, APIException {\r
+ boolean pretty, fragment;\r
+ pretty = options.length>0?options[0]:this.pretty;\r
+ fragment = options.length>1?options[1]:this.fragment;\r
+ Pooled<PMarshaller> m = mpool.get(lt);\r
+ try {\r
+ if(qname==null) {\r
+ m.content.get(pretty,fragment).marshal(o, os);\r
+ } else {\r
+ m.content.get(pretty,fragment).marshal(\r
+ new JAXBElement<O>(qname, (Class<O>)cls, o ),os);\r
+ }\r
+ return o;\r
+ } finally {\r
+ m.done();\r
+ }\r
+ }\r
+ \r
+ public<O> O marshal(LogTarget lt, O o, Writer writer, Class<O> clss) throws JAXBException, APIException {\r
+ Pooled<PMarshaller> m = mpool.get(lt);\r
+ try {\r
+ if(qname==null) {\r
+ m.content.get(pretty,fragment).marshal(o, writer);\r
+ } else {\r
+ m.content.get(pretty,fragment).marshal(\r
+ new JAXBElement<O>(qname, clss, o),writer);\r
+ }\r
+ return o;\r
+ } finally {\r
+ m.done();\r
+ }\r
+ \r
+ }\r
+\r
+ public<O> O marshal(LogTarget lt, O o, OutputStream os, Class<O> clss) throws JAXBException, APIException {\r
+ Pooled<PMarshaller> m = mpool.get(lt);\r
+ try {\r
+ if(qname==null) { \r
+ m.content.get(pretty,fragment).marshal(o, os);\r
+ } else {\r
+ m.content.get(pretty,fragment).marshal(\r
+ new JAXBElement<O>(qname, clss, o ),os);\r
+ }\r
+ return o;\r
+ } finally {\r
+ m.done();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * @return\r
+ */\r
+ public Class<?> getMarshalClass() {\r
+ return cls;\r
+ }\r
+\r
+ public<O> String stringify(LogTarget lt, O o) throws JAXBException, APIException {\r
+ StringWriter sw = new StringWriter();\r
+ marshal(lt,o,sw);\r
+ return sw.toString();\r
+ }\r
+\r
+ public JAXBmar pretty(boolean pretty) {\r
+ this.pretty = pretty;\r
+ return this;\r
+ }\r
+ \r
+ public JAXBmar asFragment(boolean fragment) {\r
+ this.fragment = fragment;\r
+ return this;\r
+ }\r
}\r