Coverage Report - com.aurea.maven.plugins.sonic.sdo.AbstractDataObjectInstance
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDataObjectInstance
0%
0/38
0%
0/6
1.875
 
 1  
 package com.aurea.maven.plugins.sonic.sdo;
 2  
 
 3  
 import java.io.ByteArrayOutputStream;
 4  
 import java.io.File;
 5  
 import java.io.IOException;
 6  
 import java.io.InputStream;
 7  
 import java.util.HashMap;
 8  
 
 9  
 import org.apache.tuscany.sdo.util.SDOUtil;
 10  
 
 11  
 import com.aurea.maven.plugins.sonic.utils.FileUtilities;
 12  
 
 13  
 import commonj.sdo.DataObject;
 14  
 import commonj.sdo.helper.HelperContext;
 15  
 import commonj.sdo.helper.XMLDocument;
 16  
 import commonj.sdo.helper.XMLHelper;
 17  
 
 18  
 /**
 19  
  *
 20  
  */
 21  
 @SuppressWarnings("deprecation")
 22  0
 public abstract class AbstractDataObjectInstance implements IDataObjectInstance {
 23  
 
 24  
 
 25  0
   private DataObject                            object = null;
 26  0
   private static HashMap<String, HelperContext> HELPER_CONTEXT     = null;
 27  
 
 28  
   /**
 29  
    * @return String
 30  
    */
 31  
   protected abstract String getResourceLocation();
 32  
 
 33  
   /**
 34  
    * @return String
 35  
    */
 36  
   protected abstract String getNamespace();
 37  
 
 38  
   /**
 39  
    * @return String
 40  
    */
 41  
   protected abstract String getXSDType();
 42  
 
 43  
   /**
 44  
    * @return HelperContext
 45  
    * @throws Exception
 46  
    *           Exception
 47  
    */
 48  
   protected final synchronized HelperContext getHelperContext() throws Exception {
 49  
 
 50  0
     if (HELPER_CONTEXT.get(getResourceLocation()) == null) {
 51  0
       HelperContext ctxt = SDOUtil.createHelperContext();
 52  0
       InputStream is = AbstractDataObjectInstance.class.getClassLoader().getResourceAsStream(getResourceLocation());
 53  0
       ctxt.getXSDHelper().define(is, getResourceLocation());
 54  0
       HELPER_CONTEXT.put(getResourceLocation(), ctxt);
 55  
     }
 56  0
     return HELPER_CONTEXT.get(getResourceLocation());
 57  
   }
 58  
 
 59  
   /**
 60  
    * @param _is
 61  
    *          _is
 62  
    * @throws Exception
 63  
    *           Exception
 64  
    */
 65  
   public final void load(final InputStream _is) throws Exception {
 66  
 
 67  0
     if (object != null) {
 68  0
       object.delete();
 69  0
       object = null;
 70  
     }
 71  
 
 72  
     try {
 73  0
       XMLDocument doc = getHelperContext().getXMLHelper().load(_is);
 74  0
       object = doc.getRootObject();
 75  0
     } catch (IOException ioe) {
 76  0
       ioe.printStackTrace();
 77  0
       throw new Exception("Could not load DataObject ");
 78  0
     }
 79  0
   }
 80  
 
 81  
   /**
 82  
    * @param _dest
 83  
    *          _dest
 84  
    * @throws Exception
 85  
    *           Exception
 86  
    */
 87  
   public final void save(final File _dest) throws Exception {
 88  
 
 89  0
     FileUtilities.writeFile(toString().getBytes(), _dest.getAbsolutePath());
 90  0
   }
 91  
 
 92  
   /**
 93  
    * @return DataObject
 94  
    */
 95  
   public final DataObject getDataObject() {
 96  
 
 97  0
     if (object == null) {
 98  
       try {
 99  0
         HelperContext localHc = getHelperContext();
 100  0
         object = localHc.getDataFactory().create(getNamespace(), getXSDType());
 101  0
       } catch (Exception e) {
 102  0
         e.printStackTrace();
 103  0
       }
 104  
     }
 105  0
     return object;
 106  
   }
 107  
 
 108  
   /**
 109  
    * @return String
 110  
    */
 111  
   public final String toString() {
 112  
 
 113  0
     String result = null;
 114  
 
 115  
     try {
 116  0
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
 117  0
       XMLHelper helper = getHelperContext().getXMLHelper();
 118  0
       helper.save(getDataObject(), getNamespace(), getXSDType(), bos);
 119  0
       result = new String(bos.toByteArray());
 120  0
     } catch (Exception e) {
 121  
       // TODO: handle this
 122  0
     }
 123  
 
 124  0
     return result;
 125  
   }
 126  
 
 127  
   static {
 128  0
     HELPER_CONTEXT = new HashMap<String, HelperContext>();
 129  0
   }
 130  
 }