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 2006-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  
 
 17  
 package org.kuali.rice.kew.batch;
 18  
 
 19  
 import org.apache.commons.io.IOUtils;
 20  
 import org.kuali.rice.core.api.CoreApiServiceLocator;
 21  
 import org.kuali.rice.core.api.config.ConfigurationException;
 22  
 import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection;
 23  
 import org.kuali.rice.core.api.impex.xml.XmlDoc;
 24  
 import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
 25  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 26  
 import org.springframework.core.io.DefaultResourceLoader;
 27  
 import org.springframework.core.io.Resource;
 28  
 
 29  
 import java.io.File;
 30  
 import java.io.FileInputStream;
 31  
 import java.io.FileOutputStream;
 32  
 import java.io.IOException;
 33  
 import java.io.InputStream;
 34  
 import java.util.ArrayList;
 35  
 import java.util.List;
 36  
 
 37  
 
 38  
 /**
 39  
  * This is a description of what this class does - arh14 don't forget to fill this in. 
 40  
  * 
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  *
 43  
  */
 44  0
 public class KEWXmlDataLoader {
 45  
     /*protected void loadXmlFile(String fileName) {
 46  
         if (fileName.indexOf('/') < 0) {
 47  
             this.loadXmlFile(getClass(), fileName);
 48  
         } else {
 49  
             loadXmlStream(getClass().getClassLoader().getResourceAsStream(fileName));
 50  
         }
 51  
     }*/
 52  
 
 53  
     /**
 54  
      * Loads the XML specified by the resource string, which should be in Spring resource notation
 55  
      * @param resource resource string in Spring resource notation
 56  
      * @throws Exception 
 57  
      */
 58  
     public static void loadXmlResource(String resource) throws Exception {
 59  0
         Resource res = new DefaultResourceLoader().getResource(resource);
 60  0
         InputStream xmlFile = res.getInputStream();
 61  0
         if (xmlFile == null) {
 62  0
             throw new ConfigurationException("Didn't find resource " + resource);
 63  
         }
 64  
         try {
 65  0
             loadXmlStream(xmlFile);
 66  
         } finally {
 67  0
             xmlFile.close();
 68  0
         }
 69  
 
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Loads the XML resource from the classloader, from the package of the specified class
 74  
      * if the class appears relative, or from the root of the classloader if it contains a slash
 75  
      * @param clazz the class whose package should be used to qualify the path
 76  
      * @param path the package-relative path of the XML resource
 77  
      * @throws Exception
 78  
      */
 79  
     public static void loadXmlClassLoaderResource(Class clazz, String path) throws Exception {
 80  0
         if (path.indexOf('/') < 0) {
 81  0
             loadXmlPackageResource(clazz, path);
 82  
         } else {
 83  0
             loadXmlClassLoaderResource(clazz.getClassLoader(), path);
 84  
         }
 85  0
     }
 86  
 
 87  
     /**
 88  
      * Loads the XML resource from the classloader, from the package of the specified class.
 89  
      * @param clazz the class whose package should be used to qualify the path
 90  
      * @param path the package-relative path of the XML resource
 91  
      * @throws Exception
 92  
      */
 93  
     public static void loadXmlPackageResource(Class clazz, String path) throws Exception {
 94  0
         InputStream xmlFile = clazz.getResourceAsStream(path);
 95  0
         if (xmlFile == null) {
 96  0
             throw new WorkflowRuntimeException("Didn't find resource " + path);
 97  
         }
 98  
         try {
 99  0
             loadXmlStream(xmlFile);
 100  
         } finally {
 101  0
             xmlFile.close();
 102  0
         }
 103  0
     }
 104  
 
 105  
     /**
 106  
      * Loads the XML resource from the specified classloader
 107  
      * @param classloader the classloader from which to load the resource
 108  
      * @param path the classloader path of the XML resource
 109  
      * @throws Exception
 110  
      */
 111  
     public static void loadXmlClassLoaderResource(ClassLoader classloader, String path) throws Exception {
 112  0
         InputStream xmlFile = classloader.getResourceAsStream(path);
 113  0
         if (xmlFile == null) {
 114  0
             throw new WorkflowRuntimeException("Didn't find resource " + path);
 115  
         }
 116  
         try {
 117  0
             loadXmlStream(xmlFile);
 118  
         } finally {
 119  0
             xmlFile.close();
 120  0
         }
 121  0
     }
 122  
 
 123  
     /**
 124  
      * Load the XML file from the file system.
 125  
      * 
 126  
      * @param fileName the path to the XML file
 127  
      * @throws Exception
 128  
      */
 129  
     public static void loadXmlFile(String fileName) throws Exception {
 130  0
         FileInputStream fis = new FileInputStream(fileName);
 131  
         try {
 132  0
             loadXmlStream(fis);
 133  
         } finally {
 134  0
             fis.close();
 135  0
         }
 136  0
     }
 137  
 
 138  
     /**
 139  
      * Loads XML from a stream
 140  
      * @param xmlStream the XML byte stream
 141  
      * @throws Exception
 142  
      */
 143  
     public static void loadXmlStream(InputStream xmlStream) throws Exception {
 144  0
        List<XmlDocCollection> xmlFiles = new ArrayList<XmlDocCollection>();
 145  0
         XmlDocCollection docCollection = getFileXmlDocCollection(xmlStream, "UnitTestTemp");
 146  
         //XmlDocCollection docCollection = new StreamXmlDocCollection(xmlStream);
 147  0
         xmlFiles.add(docCollection);
 148  0
         CoreApiServiceLocator.getXmlIngesterService().ingest(xmlFiles);
 149  0
         for (XmlDoc doc: docCollection.getXmlDocs()) {
 150  0
             if (!doc.isProcessed()) {
 151  0
                 throw new RuntimeException("Failed to ingest xml doc: " + doc.getName());
 152  
             }
 153  
         }
 154  0
     }
 155  
 
 156  
     /**
 157  
      * Helper method that turns a stream into a FileXmlDocCollection by first making a copy on the file system.
 158  
      * @param xmlFile
 159  
      * @param tempFileName
 160  
      * @return
 161  
      * @throws IOException
 162  
      */
 163  
     public static FileXmlDocCollection getFileXmlDocCollection(InputStream stream, String tempFileName) throws IOException {
 164  0
         if (stream == null) {
 165  0
             throw new RuntimeException("Stream is null!");
 166  
         }
 167  
 
 168  0
         File temp = File.createTempFile(tempFileName, ".xml");
 169  0
         temp.deleteOnExit();
 170  
 
 171  0
         FileOutputStream fos = new FileOutputStream(temp);
 172  
         try {
 173  0
             IOUtils.copy(stream, fos);
 174  
         } finally {
 175  0
             fos.close();
 176  0
         }
 177  
 
 178  0
         return new FileXmlDocCollection(temp);
 179  
     }
 180  
 }