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