Coverage Report - org.kuali.rice.kew.batch.KEWXmlDataLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
KEWXmlDataLoader
0%
0/49
0%
0/14
2.714
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kew.batch;
 17  
 
 18  
 import org.apache.commons.io.IOUtils;
 19  
 import org.kuali.rice.core.api.CoreApiServiceLocator;
 20  
 import org.kuali.rice.core.api.config.ConfigurationException;
 21  
 import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection;
 22  
 import org.kuali.rice.core.api.impex.xml.XmlDoc;
 23  
 import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
 24  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 25  
 import org.springframework.core.io.DefaultResourceLoader;
 26  
 import org.springframework.core.io.Resource;
 27  
 
 28  
 import java.io.File;
 29  
 import java.io.FileInputStream;
 30  
 import java.io.FileOutputStream;
 31  
 import java.io.IOException;
 32  
 import java.io.InputStream;
 33  
 import java.util.ArrayList;
 34  
 import java.util.List;
 35  
 
 36  
 
 37  
 /**
 38  
  * This is a description of what this class does - arh14 don't forget to fill this in. 
 39  
  * 
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  *
 42  
  */
 43  0
 public class KEWXmlDataLoader {
 44  
     /*protected void loadXmlFile(String fileName) {
 45  
         if (fileName.indexOf('/') < 0) {
 46  
             this.loadXmlFile(getClass(), fileName);
 47  
         } else {
 48  
             loadXmlStream(getClass().getClassLoader().getResourceAsStream(fileName));
 49  
         }
 50  
     }*/
 51  
 
 52  
     /**
 53  
      * Loads the XML specified by the resource string, which should be in Spring resource notation
 54  
      * @param resource resource string in Spring resource notation
 55  
      * @throws Exception 
 56  
      */
 57  
     public static void loadXmlResource(String resource) throws Exception {
 58  0
         Resource res = new DefaultResourceLoader().getResource(resource);
 59  0
         InputStream xmlFile = res.getInputStream();
 60  0
         if (xmlFile == null) {
 61  0
             throw new ConfigurationException("Didn't find resource " + resource);
 62  
         }
 63  
         try {
 64  0
             loadXmlStream(xmlFile);
 65  
         } finally {
 66  0
             xmlFile.close();
 67  0
         }
 68  
 
 69  0
     }
 70  
 
 71  
     /**
 72  
      * Loads the XML resource from the classloader, from the package of the specified class
 73  
      * if the class appears relative, or from the root of the classloader if it contains a slash
 74  
      * @param clazz the class whose package should be used to qualify the path
 75  
      * @param path the package-relative path of the XML resource
 76  
      * @throws Exception
 77  
      */
 78  
     public static void loadXmlClassLoaderResource(Class clazz, String path) throws Exception {
 79  0
         if (path.indexOf('/') < 0) {
 80  0
             loadXmlPackageResource(clazz, path);
 81  
         } else {
 82  0
             loadXmlClassLoaderResource(clazz.getClassLoader(), path);
 83  
         }
 84  0
     }
 85  
 
 86  
     /**
 87  
      * Loads the XML resource from the classloader, from the package of the specified class.
 88  
      * @param clazz the class whose package should be used to qualify the path
 89  
      * @param path the package-relative path of the XML resource
 90  
      * @throws Exception
 91  
      */
 92  
     public static void loadXmlPackageResource(Class clazz, String path) throws Exception {
 93  0
         InputStream xmlFile = clazz.getResourceAsStream(path);
 94  0
         if (xmlFile == null) {
 95  0
             throw new WorkflowRuntimeException("Didn't find resource " + path);
 96  
         }
 97  
         try {
 98  0
             loadXmlStream(xmlFile);
 99  
         } finally {
 100  0
             xmlFile.close();
 101  0
         }
 102  0
     }
 103  
 
 104  
     /**
 105  
      * Loads the XML resource from the specified classloader
 106  
      * @param classloader the classloader from which to load the resource
 107  
      * @param path the classloader path of the XML resource
 108  
      * @throws Exception
 109  
      */
 110  
     public static void loadXmlClassLoaderResource(ClassLoader classloader, String path) throws Exception {
 111  0
         InputStream xmlFile = classloader.getResourceAsStream(path);
 112  0
         if (xmlFile == null) {
 113  0
             throw new WorkflowRuntimeException("Didn't find resource " + path);
 114  
         }
 115  
         try {
 116  0
             loadXmlStream(xmlFile);
 117  
         } finally {
 118  0
             xmlFile.close();
 119  0
         }
 120  0
     }
 121  
 
 122  
     /**
 123  
      * Load the XML file from the file system.
 124  
      * 
 125  
      * @param fileName the path to the XML file
 126  
      * @throws Exception
 127  
      */
 128  
     public static void loadXmlFile(String fileName) throws Exception {
 129  0
         FileInputStream fis = new FileInputStream(fileName);
 130  
         try {
 131  0
             loadXmlStream(fis);
 132  
         } finally {
 133  0
             fis.close();
 134  0
         }
 135  0
     }
 136  
 
 137  
     /**
 138  
      * Loads XML from a stream
 139  
      * @param xmlStream the XML byte stream
 140  
      * @throws Exception
 141  
      */
 142  
     public static void loadXmlStream(InputStream xmlStream) throws Exception {
 143  0
        List<XmlDocCollection> xmlFiles = new ArrayList<XmlDocCollection>();
 144  0
         XmlDocCollection docCollection = getFileXmlDocCollection(xmlStream, "UnitTestTemp");
 145  
         //XmlDocCollection docCollection = new StreamXmlDocCollection(xmlStream);
 146  0
         xmlFiles.add(docCollection);
 147  0
         CoreApiServiceLocator.getXmlIngesterService().ingest(xmlFiles);
 148  0
         for (XmlDoc doc: docCollection.getXmlDocs()) {
 149  0
             if (!doc.isProcessed()) {
 150  0
                 throw new RuntimeException("Failed to ingest xml doc: " + doc.getName());
 151  
             }
 152  
         }
 153  0
     }
 154  
 
 155  
     /**
 156  
      * Helper method that turns a stream into a FileXmlDocCollection by first making a copy on the file system.
 157  
      * @param xmlFile
 158  
      * @param tempFileName
 159  
      * @return
 160  
      * @throws IOException
 161  
      */
 162  
     public static FileXmlDocCollection getFileXmlDocCollection(InputStream stream, String tempFileName) throws IOException {
 163  0
         if (stream == null) {
 164  0
             throw new RuntimeException("Stream is null!");
 165  
         }
 166  
 
 167  0
         File temp = File.createTempFile(tempFileName, ".xml");
 168  0
         temp.deleteOnExit();
 169  
 
 170  0
         FileOutputStream fos = new FileOutputStream(temp);
 171  
         try {
 172  0
             IOUtils.copy(stream, fos);
 173  
         } finally {
 174  0
             fos.close();
 175  0
         }
 176  
 
 177  0
         return new FileXmlDocCollection(temp);
 178  
     }
 179  
 }