* @param <T>\r
*/\r
public final class JAXBData<T> implements Data<T>{\r
- private Stringifier<T> stringifier;\r
- private Objectifier<T> objectifier;\r
- private String dataAsString;\r
- private T dataAsObject;\r
- private Class<T> tclass;\r
- private JAXBDF<T> df;\r
- private Env creatingEnv;\r
- private boolean options[] = new boolean[] {false, false};\r
- \r
- /**\r
- * Construct a Data Object with an appropriate Stringifier, Objectifier and Class to support\r
- * \r
- * @param env\r
- * @param strfr\r
- * @param objfr\r
- * @param text\r
- * @param typeClass\r
- */\r
- JAXBData(Env env, JAXBDF<T> df, Stringifier<T> strfr, Objectifier<T> objfr, String text, Class<T> typeClass) {\r
- dataAsString = text;\r
- dataAsObject = null;\r
- stringifier = strfr;\r
- objectifier = objfr;\r
- tclass = typeClass;\r
- creatingEnv = env;\r
- this.df = df;\r
- }\r
- \r
- \r
- /**\r
- * Construct a Data Object with an appropriate Stringifier, Objectifier and Object (which will\r
- * yield it's class)\r
- * \r
- * @param env\r
- * @param strfr\r
- * @param objfr\r
- * @param object\r
- */\r
- @SuppressWarnings("unchecked")\r
- JAXBData(Env env, JAXBDF<T> df, Stringifier<T> strfr, Objectifier<T> objfr, T object) {\r
- dataAsString = null;\r
- dataAsObject = object;\r
- stringifier = strfr;\r
- objectifier = objfr;\r
- tclass = (Class<T>) object.getClass();\r
- creatingEnv = env;\r
- this.df = df;\r
- }\r
+ private Stringifier<T> stringifier;\r
+ private Objectifier<T> objectifier;\r
+ private String dataAsString;\r
+ private T dataAsObject;\r
+ private Class<T> tclass;\r
+ private JAXBDF<T> df;\r
+ private Env creatingEnv;\r
+ private boolean options[] = new boolean[] {false, false};\r
+ \r
+ /**\r
+ * Construct a Data Object with an appropriate Stringifier, Objectifier and Class to support\r
+ * \r
+ * @param env\r
+ * @param strfr\r
+ * @param objfr\r
+ * @param text\r
+ * @param typeClass\r
+ */\r
+ JAXBData(Env env, JAXBDF<T> df, Stringifier<T> strfr, Objectifier<T> objfr, String text, Class<T> typeClass) {\r
+ dataAsString = text;\r
+ dataAsObject = null;\r
+ stringifier = strfr;\r
+ objectifier = objfr;\r
+ tclass = typeClass;\r
+ creatingEnv = env;\r
+ this.df = df;\r
+ }\r
+ \r
+ \r
+ /**\r
+ * Construct a Data Object with an appropriate Stringifier, Objectifier and Object (which will\r
+ * yield it's class)\r
+ * \r
+ * @param env\r
+ * @param strfr\r
+ * @param objfr\r
+ * @param object\r
+ */\r
+ @SuppressWarnings("unchecked")\r
+ JAXBData(Env env, JAXBDF<T> df, Stringifier<T> strfr, Objectifier<T> objfr, T object) {\r
+ dataAsString = null;\r
+ dataAsObject = object;\r
+ stringifier = strfr;\r
+ objectifier = objfr;\r
+ tclass = (Class<T>) object.getClass();\r
+ creatingEnv = env;\r
+ this.df = df;\r
+ }\r
\r
- /**\r
- * Respond with the String if it exists, or marshal the String and pass the result back.<p>\r
- * \r
- * Explicitly use a specific Env for logging purposes\r
- * \r
- * @param env\r
- * @return String\r
- * @throws APIException\r
- */\r
- public String asString(EnvJAXB env) throws APIException {\r
- if(dataAsString!=null) {\r
- return dataAsString;\r
- } else {\r
- return dataAsString = stringifier.stringify(env, dataAsObject);\r
- }\r
- }\r
+ /**\r
+ * Respond with the String if it exists, or marshal the String and pass the result back.<p>\r
+ * \r
+ * Explicitly use a specific Env for logging purposes\r
+ * \r
+ * @param env\r
+ * @return String\r
+ * @throws APIException\r
+ */\r
+ public String asString(EnvJAXB env) throws APIException {\r
+ if(dataAsString!=null) {\r
+ return dataAsString;\r
+ } else {\r
+ return dataAsString = stringifier.stringify(env, dataAsObject);\r
+ }\r
+ }\r
\r
- /**\r
- * Respond with the String if it exists, or marshal the String and pass the result back.\r
- * \r
- * However, use the Env the Data Object was created with.\r
- * \r
- * @return String\r
- * @throws APIException\r
- */\r
- // @Override\r
- public String asString() throws APIException {\r
- if(dataAsString!=null) {\r
- return dataAsString;\r
- } else {\r
- return dataAsString = stringifier.stringify(creatingEnv, dataAsObject,options);\r
- }\r
- }\r
- \r
- public Data<T> to(OutputStream os) throws APIException, IOException {\r
- if(dataAsString!=null) {\r
- os.write(dataAsString.getBytes());\r
- } else if (stringifier instanceof IOStringifier){\r
- ((IOStringifier<T>)stringifier).stringify(creatingEnv, dataAsObject, os, options);\r
- } else {\r
- dataAsString = stringifier.stringify(creatingEnv, dataAsObject, options);\r
- os.write(dataAsString.getBytes());\r
- }\r
- return this;\r
- }\r
+ /**\r
+ * Respond with the String if it exists, or marshal the String and pass the result back.\r
+ * \r
+ * However, use the Env the Data Object was created with.\r
+ * \r
+ * @return String\r
+ * @throws APIException\r
+ */\r
+ // @Override\r
+ public String asString() throws APIException {\r
+ if(dataAsString!=null) {\r
+ return dataAsString;\r
+ } else {\r
+ return dataAsString = stringifier.stringify(creatingEnv, dataAsObject,options);\r
+ }\r
+ }\r
+ \r
+ public Data<T> to(OutputStream os) throws APIException, IOException {\r
+ if(dataAsString!=null) {\r
+ os.write(dataAsString.getBytes());\r
+ } else if (stringifier instanceof IOStringifier){\r
+ ((IOStringifier<T>)stringifier).stringify(creatingEnv, dataAsObject, os, options);\r
+ } else {\r
+ dataAsString = stringifier.stringify(creatingEnv, dataAsObject, options);\r
+ os.write(dataAsString.getBytes());\r
+ }\r
+ return this;\r
+ }\r
\r
\r
- // @Override\r
- public JAXBData<T> to(Writer writer) throws APIException, IOException {\r
- if(dataAsString!=null) {\r
- writer.write(dataAsString);\r
- } else if (stringifier instanceof IOStringifier){\r
- ((IOStringifier<T>)stringifier).stringify(creatingEnv, dataAsObject, writer, options);\r
- } else {\r
- dataAsString = stringifier.stringify(creatingEnv, dataAsObject, options);\r
- writer.write(dataAsString);\r
- }\r
- return this;\r
- }\r
+ // @Override\r
+ public JAXBData<T> to(Writer writer) throws APIException, IOException {\r
+ if(dataAsString!=null) {\r
+ writer.write(dataAsString);\r
+ } else if (stringifier instanceof IOStringifier){\r
+ ((IOStringifier<T>)stringifier).stringify(creatingEnv, dataAsObject, writer, options);\r
+ } else {\r
+ dataAsString = stringifier.stringify(creatingEnv, dataAsObject, options);\r
+ writer.write(dataAsString);\r
+ }\r
+ return this;\r
+ }\r
\r
\r
- public InputStream getInputStream() throws APIException {\r
- if(dataAsString==null) {\r
- dataAsString = stringifier.stringify(creatingEnv,dataAsObject,options);\r
- }\r
- return new ByteArrayInputStream(dataAsString.getBytes());\r
- }\r
- \r
- /**\r
- * Respond with the Object of type {@literal <T>} if it exists, or unmarshal from String \r
- * and pass the result back.<p>\r
- * \r
- * Explicitly use a specific Env for logging purposes\r
- * \r
- * @param env\r
- * @return T\r
- * @throws APIException\r
- */\r
+ public InputStream getInputStream() throws APIException {\r
+ if(dataAsString==null) {\r
+ dataAsString = stringifier.stringify(creatingEnv,dataAsObject,options);\r
+ }\r
+ return new ByteArrayInputStream(dataAsString.getBytes());\r
+ }\r
+ \r
+ /**\r
+ * Respond with the Object of type {@literal <T>} if it exists, or unmarshal from String \r
+ * and pass the result back.<p>\r
+ * \r
+ * Explicitly use a specific Env for logging purposes\r
+ * \r
+ * @param env\r
+ * @return T\r
+ * @throws APIException\r
+ */\r
\r
- public T asObject(EnvJAXB env) throws APIException {\r
- if(dataAsObject !=null) {\r
- return dataAsObject;\r
- } else {\r
- // Some Java compilers need two statements here\r
- dataAsObject = objectifier.objectify(env, dataAsString);\r
- return dataAsObject;\r
- }\r
- }\r
+ public T asObject(EnvJAXB env) throws APIException {\r
+ if(dataAsObject !=null) {\r
+ return dataAsObject;\r
+ } else {\r
+ // Some Java compilers need two statements here\r
+ dataAsObject = objectifier.objectify(env, dataAsString);\r
+ return dataAsObject;\r
+ }\r
+ }\r
\r
- /**\r
- * Respond with the Object of type {@literal <T>} if it exists, or unmarshal from String \r
- * and pass the result back.<p>\r
- *\r
- * However, use the Env the Data Object was created with.\r
- * \r
- * @return T\r
- * @throws APIException\r
- */\r
- // @Override\r
- public T asObject() throws APIException {\r
- if(dataAsObject !=null) {\r
- return dataAsObject;\r
- } else {\r
- // Some Java compilers need two statements here\r
- dataAsObject = objectifier.objectify(creatingEnv, dataAsString);\r
- return dataAsObject;\r
- }\r
- }\r
- \r
+ /**\r
+ * Respond with the Object of type {@literal <T>} if it exists, or unmarshal from String \r
+ * and pass the result back.<p>\r
+ *\r
+ * However, use the Env the Data Object was created with.\r
+ * \r
+ * @return T\r
+ * @throws APIException\r
+ */\r
+ // @Override\r
+ public T asObject() throws APIException {\r
+ if(dataAsObject !=null) {\r
+ return dataAsObject;\r
+ } else {\r
+ // Some Java compilers need two statements here\r
+ dataAsObject = objectifier.objectify(creatingEnv, dataAsString);\r
+ return dataAsObject;\r
+ }\r
+ }\r
+ \r
\r
- /**\r
- * Return the Class Type supported by this DataObject\r
- * \r
- * @return {@literal Class<T>}\r
- */\r
- // @Override\r
- public Class<T> getTypeClass() {\r
- return tclass;\r
- }\r
- \r
- \r
- /**\r
- * For Debugging Convenience, we marshal to String if possible.\r
- * \r
- * Behavior is essentially the same as asString(), except asString() throws\r
- * an APIException. <p>\r
- * Since toString() must not throw exceptions, the function just catches and prints an\r
- * error, which is probably not the behavior desired.<p>\r
- * \r
- * Therefore, use "asString()" where possible in actual Transactional code. \r
- * \r
- * @see java.lang.Object#toString()\r
- */\r
- // @Override\r
- public String toString() {\r
- if(dataAsString!=null) {\r
- return dataAsString;\r
- } else {\r
- try {\r
- return dataAsString = stringifier.stringify(creatingEnv, dataAsObject);\r
- } catch (APIException e) {\r
- return "ERROR - Can't Stringify from Object " + e.getLocalizedMessage();\r
- }\r
- }\r
- }\r
+ /**\r
+ * Return the Class Type supported by this DataObject\r
+ * \r
+ * @return {@literal Class<T>}\r
+ */\r
+ // @Override\r
+ public Class<T> getTypeClass() {\r
+ return tclass;\r
+ }\r
+ \r
+ \r
+ /**\r
+ * For Debugging Convenience, we marshal to String if possible.\r
+ * \r
+ * Behavior is essentially the same as asString(), except asString() throws\r
+ * an APIException. <p>\r
+ * Since toString() must not throw exceptions, the function just catches and prints an\r
+ * error, which is probably not the behavior desired.<p>\r
+ * \r
+ * Therefore, use "asString()" where possible in actual Transactional code. \r
+ * \r
+ * @see java.lang.Object#toString()\r
+ */\r
+ // @Override\r
+ public String toString() {\r
+ if(dataAsString!=null) {\r
+ return dataAsString;\r
+ } else {\r
+ try {\r
+ return dataAsString = stringifier.stringify(creatingEnv, dataAsObject);\r
+ } catch (APIException e) {\r
+ return "ERROR - Can't Stringify from Object " + e.getLocalizedMessage();\r
+ }\r
+ }\r
+ }\r
\r
- public Data<T> load(T t) throws APIException {\r
- dataAsObject = t;\r
- dataAsString = null;\r
- return this;\r
- }\r
+ public Data<T> load(T t) throws APIException {\r
+ dataAsObject = t;\r
+ dataAsString = null;\r
+ return this;\r
+ }\r
\r
\r
- public Data<T> load(String str) throws APIException {\r
- dataAsObject = null;\r
- dataAsString = str;\r
- return this;\r
- }\r
+ public Data<T> load(String str) throws APIException {\r
+ dataAsObject = null;\r
+ dataAsString = str;\r
+ return this;\r
+ }\r
\r
\r
- public Data<T> load(InputStream is) throws APIException {\r
- try {\r
- dataAsObject = df.jumar.unmarshal(creatingEnv.debug(),is);\r
- dataAsString = null;\r
- } catch (JAXBException e) {\r
- throw new APIException(e);\r
- }\r
- return this;\r
- }\r
+ public Data<T> load(InputStream is) throws APIException {\r
+ try {\r
+ dataAsObject = df.jumar.unmarshal(creatingEnv.debug(),is);\r
+ dataAsString = null;\r
+ } catch (JAXBException e) {\r
+ throw new APIException(e);\r
+ }\r
+ return this;\r
+ }\r
\r
\r
- public Data<T> load(Reader rdr) throws APIException {\r
- try {\r
- dataAsObject = df.jumar.unmarshal(creatingEnv.debug(),rdr);\r
- dataAsString = null;\r
- } catch (JAXBException e) {\r
- throw new APIException(e);\r
- }\r
- return this;\r
- }\r
+ public Data<T> load(Reader rdr) throws APIException {\r
+ try {\r
+ dataAsObject = df.jumar.unmarshal(creatingEnv.debug(),rdr);\r
+ dataAsString = null;\r
+ } catch (JAXBException e) {\r
+ throw new APIException(e);\r
+ }\r
+ return this;\r
+ }\r
\r
\r
- // @Override\r
- public void direct(InputStream input, OutputStream output) throws APIException, IOException {\r
- byte b[] = new byte[128];\r
- int count;\r
- do {\r
- count = input.read(b);\r
- if(count>0)output.write(b, 0, count);\r
- } while(count>=0);\r
- }\r
+ // @Override\r
+ public void direct(InputStream input, OutputStream output) throws APIException, IOException {\r
+ byte b[] = new byte[128];\r
+ int count;\r
+ do {\r
+ count = input.read(b);\r
+ if(count>0)output.write(b, 0, count);\r
+ } while(count>=0);\r
+ }\r
\r
\r
- // @Override\r
- public Data<T> out(TYPE type) {\r
- // it's going to be XML regardless...\r
- return this;\r
- }\r
+ // @Override\r
+ public Data<T> out(TYPE type) {\r
+ // it's going to be XML regardless...\r
+ return this;\r
+ }\r
\r
\r
- // @Override\r
- public Data<T> in(TYPE type) {\r
- // Not Supported... will still be XML\r
- return this;\r
- }\r
+ // @Override\r
+ public Data<T> in(TYPE type) {\r
+ // Not Supported... will still be XML\r
+ return this;\r
+ }\r
\r
\r
- // @Override\r
- public Data<T> option(int option) {\r
- options[0] = (option&Data.PRETTY)==Data.PRETTY;\r
- options[1] = (option&Data.FRAGMENT)==Data.FRAGMENT;\r
- return this;\r
- }\r
- \r
+ // @Override\r
+ public Data<T> option(int option) {\r
+ options[0] = (option&Data.PRETTY)==Data.PRETTY;\r
+ options[1] = (option&Data.FRAGMENT)==Data.FRAGMENT;\r
+ return this;\r
+ }\r
+ \r
}
\ No newline at end of file