1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.impl.impex.xml; |
18 | |
|
19 | |
import java.io.IOException; |
20 | |
import java.util.Collection; |
21 | |
import java.util.LinkedHashSet; |
22 | |
import java.util.LinkedList; |
23 | |
import java.util.List; |
24 | |
import java.util.Set; |
25 | |
|
26 | |
import javax.xml.XMLConstants; |
27 | |
import javax.xml.parsers.DocumentBuilder; |
28 | |
import javax.xml.parsers.DocumentBuilderFactory; |
29 | |
import javax.xml.parsers.ParserConfigurationException; |
30 | |
|
31 | |
import org.apache.commons.lang.exception.ExceptionUtils; |
32 | |
import org.apache.log4j.Logger; |
33 | |
import org.kuali.rice.core.api.impex.xml.XmlDoc; |
34 | |
import org.kuali.rice.core.api.impex.xml.XmlDocCollection; |
35 | |
import org.kuali.rice.core.api.impex.xml.XmlIngesterService; |
36 | |
import org.kuali.rice.core.framework.impex.xml.XmlImpexRegistry; |
37 | |
import org.kuali.rice.core.framework.impex.xml.XmlLoader; |
38 | |
import org.xml.sax.EntityResolver; |
39 | |
import org.xml.sax.ErrorHandler; |
40 | |
import org.xml.sax.SAXException; |
41 | |
import org.xml.sax.SAXParseException; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | 0 | public class XmlIngesterServiceImpl implements XmlIngesterService { |
75 | |
|
76 | 0 | private static final Logger LOG = Logger.getLogger(XmlIngesterServiceImpl.class); |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | 0 | private EntityResolver resolver = new ClassLoaderEntityResolver(); |
82 | |
|
83 | |
private XmlDigesterService digesterService; |
84 | |
|
85 | |
private XmlImpexRegistry xmlImpexRegistry; |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | 0 | private boolean validate = true; |
91 | |
|
92 | |
|
93 | |
|
94 | |
public void setXmlDigesterService(XmlDigesterService digesterService) { |
95 | 0 | this.digesterService = digesterService; |
96 | 0 | } |
97 | |
|
98 | |
public void setXmlImpexRegistry(XmlImpexRegistry xmlImpexRegistry) { |
99 | 0 | this.xmlImpexRegistry = xmlImpexRegistry; |
100 | 0 | } |
101 | |
|
102 | |
public void setEntityResolver(EntityResolver resolver) { |
103 | 0 | this.resolver = resolver; |
104 | 0 | } |
105 | |
|
106 | |
public void setValidate(boolean b) { |
107 | 0 | validate = b; |
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
private static void addProcessingException(XmlDoc xmlDoc, String message, Throwable t) { |
113 | 0 | String msg = xmlDoc.getProcessingMessage(); |
114 | 0 | if (msg == null) { |
115 | 0 | msg = ""; |
116 | |
} |
117 | 0 | msg += message + "\n" + ExceptionUtils.getFullStackTrace(t); |
118 | 0 | xmlDoc.setProcessingMessage(msg); |
119 | 0 | } |
120 | |
|
121 | |
private static void validate(final XmlDoc xmlDoc, EntityResolver resolver) throws ParserConfigurationException, IOException, SAXException { |
122 | 0 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
123 | 0 | dbf.setValidating(true); |
124 | 0 | dbf.setNamespaceAware( true ); |
125 | 0 | dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI); |
126 | 0 | DocumentBuilder db = dbf.newDocumentBuilder(); |
127 | 0 | db.setEntityResolver(resolver); |
128 | 0 | db.setErrorHandler(new ErrorHandler() { |
129 | |
public void warning(SAXParseException se) { |
130 | 0 | LOG.warn("Warning parsing xml doc " + xmlDoc, se); |
131 | 0 | addProcessingException(xmlDoc, "Warning parsing xml doc " + xmlDoc, se); |
132 | 0 | } |
133 | |
public void error(SAXParseException se) throws SAXException { |
134 | 0 | LOG.error("Error parsing xml doc " + xmlDoc, se); |
135 | 0 | addProcessingException(xmlDoc, "Error parsing xml doc " + xmlDoc, se); |
136 | 0 | throw se; |
137 | |
} |
138 | |
public void fatalError(SAXParseException se) throws SAXException { |
139 | 0 | LOG.error("Fatal error parsing xml doc " + xmlDoc, se); |
140 | 0 | addProcessingException(xmlDoc, "Fatal error parsing xml doc " + xmlDoc, se); |
141 | 0 | throw se; |
142 | |
} |
143 | |
}); |
144 | 0 | db.parse(xmlDoc.getStream()); |
145 | 0 | } |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
private static void validate(List<XmlDocCollection> collections, EntityResolver resolver, Set<XmlDocCollection> successful, Set<XmlDocCollection> failed) { |
156 | |
|
157 | 0 | for (XmlDocCollection collection : collections) |
158 | |
{ |
159 | |
|
160 | |
|
161 | 0 | for (XmlDoc xmlDoc : collection.getXmlDocs()) |
162 | |
{ |
163 | |
try |
164 | |
{ |
165 | 0 | validate(xmlDoc, resolver); |
166 | 0 | } catch (Exception e) |
167 | |
{ |
168 | 0 | LOG.error("Error validating doc: " + xmlDoc, e); |
169 | 0 | addProcessingException(xmlDoc, "Error validating doc: " + xmlDoc, e); |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | 0 | failed.add(collection); |
176 | 0 | } |
177 | |
} |
178 | |
|
179 | |
|
180 | 0 | successful.add(collection); |
181 | |
} |
182 | 0 | } |
183 | |
|
184 | |
private void ingest(XmlLoader xmlLoader, Collection<XmlDocCollection> xmlDocCollections, String principalId, Set<Object> successful, Set<XmlDocCollection> failed) { |
185 | 0 | for (XmlDocCollection xmlDocCollection : xmlDocCollections) |
186 | |
{ |
187 | |
|
188 | 0 | if (failed.contains(xmlDocCollection)) |
189 | |
{ |
190 | 0 | LOG.debug("Skipping " + xmlDocCollection.getFile() + "..."); |
191 | 0 | continue; |
192 | |
} |
193 | |
|
194 | |
try |
195 | |
{ |
196 | 0 | digesterService.digest(xmlLoader, xmlDocCollection, principalId); |
197 | 0 | } catch (Exception e) |
198 | |
{ |
199 | 0 | LOG.error("Caught Exception loading xml data from " + xmlDocCollection.getFile() + ". Will move associated file to problem dir.", e); |
200 | 0 | failed.add(xmlDocCollection); |
201 | 0 | } |
202 | |
} |
203 | 0 | } |
204 | |
|
205 | |
public Collection<XmlDocCollection> ingest(List<XmlDocCollection> collections) throws Exception { |
206 | 0 | return ingest(collections, null); |
207 | |
} |
208 | |
|
209 | |
private void ingestThroughOrderedLoaders(Collection<XmlDocCollection> xmlDocCollections, String principalId, Set<Object> successful, Set<XmlDocCollection> failed) { |
210 | 0 | LOG.debug("Ingesting through ordered XmlLoaders"); |
211 | 0 | List<XmlLoader> xmlLoaders = xmlImpexRegistry.getLoaders(); |
212 | 0 | for (XmlLoader xmlLoader : xmlLoaders) { |
213 | 0 | LOG.debug("Ingesting through ordered XmlLoader: " + xmlLoader); |
214 | 0 | ingest(xmlLoader, xmlDocCollections, principalId, successful, failed); |
215 | |
} |
216 | 0 | } |
217 | |
|
218 | |
public Collection<XmlDocCollection> ingest(List<XmlDocCollection> collections, String principalId) { |
219 | 0 | Set<XmlDocCollection> failed = new LinkedHashSet<XmlDocCollection>(); |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | 0 | if (validate) { |
225 | 0 | Set<XmlDocCollection> successful = new LinkedHashSet<XmlDocCollection>(); |
226 | 0 | validate(collections, resolver, successful, failed); |
227 | 0 | collections = new LinkedList<XmlDocCollection>(successful); |
228 | |
} |
229 | |
|
230 | 0 | Set<Object> successful = new LinkedHashSet<Object>(); |
231 | |
|
232 | 0 | ingestThroughOrderedLoaders(collections, principalId, successful, failed); |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | 0 | return failed; |
239 | |
} |
240 | |
} |