Increase coverage for Env module
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / jaxb / JAXBData.java
index e1c54c6..84502ad 100644 (file)
-/**
- * ============LICENSE_START====================================================
- * org.onap.aaf
- * ===========================================================================
- * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
- * ===========================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END====================================================
- *
- */
-
-package org.onap.aaf.misc.env.jaxb;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Reader;
-import java.io.Writer;
-
-import javax.xml.bind.JAXBException;
-
-import org.onap.aaf.misc.env.APIException;
-import org.onap.aaf.misc.env.Data;
-import org.onap.aaf.misc.env.Env;
-import org.onap.aaf.misc.env.EnvJAXB;
-import org.onap.aaf.misc.env.old.IOStringifier;
-import org.onap.aaf.misc.env.old.Objectifier;
-import org.onap.aaf.misc.env.old.Stringifier;
-/**
- * <H1>Data</H1>
- * <i>Data</i> facilitates lazy marshaling of data with a pre-determined
- * marshaling mechanism.<p>
- * 
- * It stores either Object (defined by Generic {@literal <T>}) or String.<p>  
- * 
- * On asking for Object of type {@literal <T>}, it will respond with the object
- * if it exists, or unmarshal the string and pass the result back.<p>
- * 
- * On asking for String, it will respond with the String
- * if it exists, or marshal the String and pass the result back.<p>
- * 
- * @author Jonathan
- *
- * @param <T>
- */
-public final class JAXBData<T> implements Data<T>{
-       private Stringifier<T> stringifier;
-       private Objectifier<T> objectifier;
-       private String dataAsString;
-       private T dataAsObject;
-       private Class<T> tclass;
-       private JAXBDF<T> df;
-       private Env creatingEnv;
-       private boolean options[] = new boolean[] {false, false};
-       
-       /**
-        * Construct a Data Object with an appropriate Stringifier, Objectifier and Class to support
-        * 
-        * @param env
-        * @param strfr
-        * @param objfr
-        * @param text
-        * @param typeClass
-        */
-       JAXBData(Env env, JAXBDF<T> df, Stringifier<T> strfr, Objectifier<T> objfr, String text, Class<T> typeClass) {
-               dataAsString = text;
-               dataAsObject = null;
-               stringifier = strfr;
-               objectifier = objfr;
-               tclass = typeClass;
-               creatingEnv = env;
-               this.df = df;
-       }
-       
-       
-       /**
-        * Construct a Data Object with an appropriate Stringifier, Objectifier and Object (which will
-        * yield it's class)
-        * 
-        * @param env
-        * @param strfr
-        * @param objfr
-        * @param object
-        */
-       @SuppressWarnings("unchecked")
-       JAXBData(Env env, JAXBDF<T> df, Stringifier<T> strfr, Objectifier<T> objfr, T object) {
-               dataAsString = null;
-               dataAsObject = object;
-               stringifier = strfr;
-               objectifier = objfr;
-               tclass = (Class<T>) object.getClass();
-               creatingEnv = env;
-               this.df = df;
-       }
-
-       /**
-        * Respond with the String if it exists, or marshal the String and pass the result back.<p>
-        * 
-        * Explicitly use a specific Env for logging purposes
-        * 
-        * @param env
-        * @return String
-        * @throws APIException
-        */
-       public String asString(EnvJAXB env) throws APIException {
-               if(dataAsString!=null) {
-                       return dataAsString;
-               } else {
-                       return dataAsString = stringifier.stringify(env, dataAsObject);
-               }
-       }
-
-       /**
-        * Respond with the String if it exists, or marshal the String and pass the result back.
-        * 
-        * However, use the Env the Data Object was created with.
-        * 
-        * @return String
-        * @throws APIException
-        */
-       // @Override
-       public String asString() throws APIException {
-               if(dataAsString!=null) {
-                       return dataAsString;
-               } else {
-                       return dataAsString = stringifier.stringify(creatingEnv, dataAsObject,options);
-               }
-       }
-       
-       public Data<T> to(OutputStream os) throws APIException, IOException {
-               if(dataAsString!=null) {
-                       os.write(dataAsString.getBytes());
-               } else if (stringifier instanceof IOStringifier){
-                       ((IOStringifier<T>)stringifier).stringify(creatingEnv, dataAsObject, os, options);
-               } else {
-                       dataAsString = stringifier.stringify(creatingEnv, dataAsObject, options);
-                       os.write(dataAsString.getBytes());
-               }
-               return this;
-       }
-
-
-       // @Override
-       public JAXBData<T> to(Writer writer) throws APIException, IOException {
-               if(dataAsString!=null) {
-                       writer.write(dataAsString);
-               } else if (stringifier instanceof IOStringifier){
-                       ((IOStringifier<T>)stringifier).stringify(creatingEnv, dataAsObject, writer, options);
-               } else {
-                       dataAsString = stringifier.stringify(creatingEnv, dataAsObject, options);
-                       writer.write(dataAsString);
-               }
-               return this;
-       }
-
-
-       public InputStream getInputStream() throws APIException {
-               if(dataAsString==null) {
-                       dataAsString = stringifier.stringify(creatingEnv,dataAsObject,options);
-               }
-               return new ByteArrayInputStream(dataAsString.getBytes());
-       }
-       
-       /**
-        * Respond with the Object of type {@literal <T>} if it exists, or unmarshal from String 
-        * and pass the result back.<p>
-        * 
-        * Explicitly use a specific Env for logging purposes
-        * 
-        * @param env
-        * @return T
-        * @throws APIException
-        */
-
-       public T asObject(EnvJAXB env) throws APIException {
-               if(dataAsObject !=null) {
-                       return dataAsObject;
-               } else {
-                       // Some Java compilers need two statements here
-                       dataAsObject = objectifier.objectify(env, dataAsString);
-                       return dataAsObject;
-               }
-       }
-
-       /**
-        * Respond with the Object of type {@literal <T>} if it exists, or unmarshal from String 
-        * and pass the result back.<p>
-        *
-        * However, use the Env the Data Object was created with.
-        * 
-        * @return T
-        * @throws APIException
-        */
-       // @Override
-       public T asObject() throws APIException {
-               if(dataAsObject !=null) {
-                       return dataAsObject;
-               } else {
-                       // Some Java compilers need two statements here
-                       dataAsObject = objectifier.objectify(creatingEnv, dataAsString);
-                       return dataAsObject;
-               }
-       }
-       
-
-       /**
-        * Return the Class Type supported by this DataObject
-        * 
-        * @return {@literal Class<T>}
-        */
-       // @Override
-       public Class<T> getTypeClass() {
-               return tclass;
-       }
-       
-       
-       /**
-        * For Debugging Convenience, we marshal to String if possible.
-        * 
-        * Behavior is essentially the same as asString(), except asString() throws
-        * an APIException.  <p>
-        * Since toString() must not throw exceptions, the function just catches and prints an
-        * error, which is probably not the behavior desired.<p>
-        *  
-        * Therefore, use "asString()" where possible in actual Transactional code. 
-        * 
-        * @see java.lang.Object#toString()
-        */
-       // @Override
-       public String toString() {
-               if(dataAsString!=null) {
-                       return dataAsString;
-               } else {
-                       try {
-                               return dataAsString = stringifier.stringify(creatingEnv, dataAsObject);
-                       } catch (APIException e) {
-                               return "ERROR - Can't Stringify from Object " + e.getLocalizedMessage();
-                       }
-               }
-       }
-
-       public Data<T> load(T t) throws APIException {
-               dataAsObject = t;
-               dataAsString = null;
-               return this;
-       }
-
-
-       public Data<T> load(String str) throws APIException {
-               dataAsObject = null;
-               dataAsString = str;
-               return this;
-       }
-
-
-       public Data<T> load(InputStream is) throws APIException {
-               try {
-                       dataAsObject = df.jumar.unmarshal(creatingEnv.debug(),is);
-                       dataAsString = null;
-               } catch (JAXBException e) {
-                       throw new APIException(e);
-               }
-               return this;
-       }
-
-
-       public Data<T> load(Reader rdr) throws APIException {
-               try {
-                       dataAsObject = df.jumar.unmarshal(creatingEnv.debug(),rdr);
-                       dataAsString = null;
-               } catch (JAXBException e) {
-                       throw new APIException(e);
-               }
-               return this;
-       }
-
-
-       // @Override
-       public void direct(InputStream input, OutputStream output) throws APIException, IOException {
-               byte b[] = new byte[128];
-               int count;
-               do {
-                       count = input.read(b);
-                       if(count>0)output.write(b, 0, count);
-               } while(count>=0);
-       }
-
-
-       // @Override
-       public Data<T> out(TYPE type) {
-               // it's going to be XML regardless...
-               return this;
-       }
-
-
-       // @Override
-       public Data<T> in(TYPE type) {
-               // Not Supported... will still be XML
-               return this;
-       }
-
-
-       // @Override
-       public Data<T> option(int option) {
-               options[0] = (option&Data.PRETTY)==Data.PRETTY;
-               options[1] = (option&Data.FRAGMENT)==Data.FRAGMENT;
-               return this;
-       }
-       
+/**\r
+ * ============LICENSE_START====================================================\r
+ * org.onap.aaf\r
+ * ===========================================================================\r
+ * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.\r
+ * ===========================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END====================================================\r
+ *\r
+ */\r
+\r
+package org.onap.aaf.misc.env.jaxb;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+import java.io.Reader;\r
+import java.io.Writer;\r
+\r
+import javax.xml.bind.JAXBException;\r
+\r
+import org.onap.aaf.misc.env.APIException;\r
+import org.onap.aaf.misc.env.Data;\r
+import org.onap.aaf.misc.env.Env;\r
+import org.onap.aaf.misc.env.EnvJAXB;\r
+import org.onap.aaf.misc.env.old.IOStringifier;\r
+import org.onap.aaf.misc.env.old.Objectifier;\r
+import org.onap.aaf.misc.env.old.Stringifier;\r
+/**\r
+ * <H1>Data</H1>\r
+ * <i>Data</i> facilitates lazy marshaling of data with a pre-determined\r
+ * marshaling mechanism.<p>\r
+ * \r
+ * It stores either Object (defined by Generic {@literal <T>}) or String.<p>  \r
+ * \r
+ * On asking for Object of type {@literal <T>}, it will respond with the object\r
+ * if it exists, or unmarshal the string and pass the result back.<p>\r
+ * \r
+ * On asking for String, it will respond with the String\r
+ * if it exists, or marshal the String and pass the result back.<p>\r
+ * \r
+ * @author Jonathan\r
+ *\r
+ * @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
+\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
+\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
+\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
+       /**\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
+\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
+\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
+\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
+\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