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