1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.core.api.util.ClasspathOrFileResourceLoader;
25 import org.kuali.rice.kew.api.WorkflowRuntimeException;
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
39
40
41
42
43 public class KEWXmlDataLoader {
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public static void loadXmlResource(String resource) throws Exception {
58 Resource res = new ClasspathOrFileResourceLoader().getResource(resource);
59 InputStream xmlFile = res.getInputStream();
60 if (xmlFile == null) {
61 throw new ConfigurationException("Didn't find resource " + resource);
62 }
63 try {
64 loadXmlStream(xmlFile);
65 } finally {
66 xmlFile.close();
67 }
68
69 }
70
71
72
73
74
75
76
77
78 public static void loadXmlClassLoaderResource(Class clazz, String path) throws Exception {
79 if (path.indexOf('/') < 0) {
80 loadXmlPackageResource(clazz, path);
81 } else {
82 loadXmlClassLoaderResource(clazz.getClassLoader(), path);
83 }
84 }
85
86
87
88
89
90
91
92 public static void loadXmlPackageResource(Class clazz, String path) throws Exception {
93 InputStream xmlFile = clazz.getResourceAsStream(path);
94 if (xmlFile == null) {
95 throw new WorkflowRuntimeException("Didn't find resource " + path);
96 }
97 try {
98 loadXmlStream(xmlFile);
99 } finally {
100 xmlFile.close();
101 }
102 }
103
104
105
106
107
108
109
110 public static void loadXmlClassLoaderResource(ClassLoader classloader, String path) throws Exception {
111 InputStream xmlFile = classloader.getResourceAsStream(path);
112 if (xmlFile == null) {
113 throw new WorkflowRuntimeException("Didn't find resource " + path);
114 }
115 try {
116 loadXmlStream(xmlFile);
117 } finally {
118 xmlFile.close();
119 }
120 }
121
122
123
124
125
126
127
128 public static void loadXmlFile(String fileName) throws Exception {
129 FileInputStream fis = new FileInputStream(fileName);
130 try {
131 loadXmlStream(fis);
132 } finally {
133 fis.close();
134 }
135 }
136
137
138
139
140
141
142 public static void loadXmlStream(InputStream xmlStream) throws Exception {
143 List<XmlDocCollection> xmlFiles = new ArrayList<XmlDocCollection>();
144 XmlDocCollection docCollection = getFileXmlDocCollection(xmlStream, "UnitTestTemp");
145
146 xmlFiles.add(docCollection);
147 CoreApiServiceLocator.getXmlIngesterService().ingest(xmlFiles);
148 for (XmlDoc doc: docCollection.getXmlDocs()) {
149 if (!doc.isProcessed()) {
150 throw new RuntimeException("Failed to ingest xml doc: " + doc.getName());
151 }
152 }
153 }
154
155
156
157
158
159
160
161
162 public static FileXmlDocCollection getFileXmlDocCollection(InputStream stream, String tempFileName) throws IOException {
163 if (stream == null) {
164 throw new RuntimeException("Stream is null!");
165 }
166
167 File temp = File.createTempFile(tempFileName, ".xml");
168 temp.deleteOnExit();
169
170 FileOutputStream fos = new FileOutputStream(temp);
171 try {
172 IOUtils.copy(stream, fos);
173 } finally {
174 fos.close();
175 }
176
177 return new FileXmlDocCollection(temp);
178 }
179 }