1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package coras.internalmodel;
22
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.net.URL;
27 import java.util.Collection;
28 import java.util.Iterator;
29
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.transform.Result;
32 import javax.xml.transform.Source;
33 import javax.xml.transform.Transformer;
34 import javax.xml.transform.TransformerException;
35 import javax.xml.transform.dom.DOMSource;
36 import javax.xml.transform.stream.StreamResult;
37
38 import no.sintef.assetrepository.interfaces.AssetRepresentationPK;
39 import no.sintef.assetrepository.interfaces.AssetRepresentationValue;
40 import no.sintef.assetrepository.interfaces.AssetServices;
41 import no.sintef.assetrepository.interfaces.AssetServicesUtil;
42 import no.sintef.assetrepository.interfaces.AssetVersionValue;
43 import no.sintef.assetrepository.interfaces.VersionedAssetPK;
44 import no.sintef.assetrepository.interfaces.VersionedAssetValue;
45 import no.sintef.clix.ClixEngine;
46 import no.sintef.xml.XmlException;
47 import no.sintef.xml.XmlHelper;
48
49 import org.apache.log4j.Logger;
50 import org.w3c.dom.Document;
51 import org.w3c.dom.Element;
52 import org.w3c.dom.Node;
53 import org.xml.sax.InputSource;
54 import org.xml.sax.SAXException;
55
56 import coras.repository.session.CorasServicesEJBImpl;
57 import coras.representations.RepresentationTypeEnum;
58 import coras.types.ElementTypeEnum;
59
60
61 public class ConsistencyChecker {
62
63 private static final Logger LOGGER = Logger.getLogger(ConsistencyChecker.class.getName());
64
65 private static final String NAMESPACE_URI = "http://coras.sourceforge.net/schemas/coras-internalmodel-1_0.xsd";
66 private static final String PREFIX = "ci";
67 private static final String ROOT_ELEM = "consistencyCheck";
68
69 private static ConsistencyChecker theChecker = null;
70 private ClassLoader cl = null;
71 private URL clix2HtmlTransformerUrl = null;
72
73
74 public static synchronized ConsistencyChecker getConsistencyChecker() {
75 if (theChecker == null) {
76 try {
77 theChecker = new ConsistencyChecker();
78 } catch (Exception e) {
79 LOGGER.error("Could not create ConsistencyChecker: " + e);
80 e.printStackTrace();
81 theChecker = null;
82 }
83 }
84 return theChecker;
85 }
86
87
88 private ConsistencyChecker() throws ParserConfigurationException, IOException, SAXException {
89 cl = getClass().getClassLoader();
90 clix2HtmlTransformerUrl = cl.getResource("coras/internalmodel/consistency/Clix2Html.xsl");
91 }
92
93
94 public String checkConsistency(VersionedAssetPK resultPk) throws Exception {
95 if (resultPk == null)
96 return null;
97
98 AssetServices services = AssetServicesUtil.getHome().create();
99 VersionedAssetValue result = services.getVersionedAsset(resultPk);
100 AssetVersionValue resultVersion = services.getAssetVersion(result.getBaseVersion());
101
102 String typeName = (String) resultVersion.getProperties().get("type");
103 ElementTypeEnum type = ElementTypeEnum.forName(typeName);
104 String subtype = (String) resultVersion.getProperties().get("subtype");
105
106 String name = "coras/internalmodel/consistency/" + type + (subtype != null ? '/' + subtype.replaceAll(" ", "_") : "") + ".clix";
107 URL rulesUrl = cl.getResource(name);
108 if (rulesUrl == null) {
109 return "<P>No consistency check defined for element type " + type + (subtype != null ? "/" + subtype : "") + ".</P>";
110 }
111
112 Node resultContent = null;
113 if (type == ElementTypeEnum.TABLE) {
114 Object content = getRepresentationContent(resultVersion, RepresentationTypeEnum.TABLE, services);
115 if (content instanceof Node) {
116 resultContent = (Node) content;
117 }
118 }
119 if (resultContent != null) {
120 Element internalRepr = CorasServicesEJBImpl.getInternalRepresentation(resultVersion.getParent(), services);
121 Document elementDoc = getElementDoc(resultContent, internalRepr);
122 String clixOutput = ClixEngine.genReport(elementDoc, rulesUrl);
123 String htmlReport = genHtmlReport(clixOutput);
124 return htmlReport;
125 } else {
126 return null;
127 }
128 }
129
130 /***
131 * @param clixOutput
132 * @return
133 * @throws IOException
134 * @throws XmlException
135 * @throws TransformerException
136 */
137 private String genHtmlReport(String clixOutput) throws XmlException, IOException, TransformerException {
138 Transformer clixt2HtmlTransformer = XmlHelper.getTransformer(clix2HtmlTransformerUrl, LOGGER);
139 Source consistencySource = new DOMSource(XmlHelper.parse(new InputSource(new StringReader(clixOutput))));
140 ByteArrayOutputStream baos = new ByteArrayOutputStream();
141 Result htmlResult = new StreamResult(baos);
142 clixt2HtmlTransformer.transform(consistencySource, htmlResult);
143 String result = new String(baos.toByteArray(), "UTF-8");
144 return result;
145 }
146
147
148 private Document getElementDoc(Node resultContent, Node internalRepr) {
149 if (resultContent == null || internalRepr == null)
150 return null;
151
152 try {
153 Document doc = XmlHelper.createDocument(NAMESPACE_URI, PREFIX, ROOT_ELEM, null);
154 Element rootElem = doc.getDocumentElement();
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 internalRepr = doc.importNode(internalRepr, true);
171 if (internalRepr != null)
172 rootElem.appendChild(internalRepr);
173
174 resultContent = XmlHelper.getRootElement(resultContent);
175 if (resultContent != null) {
176 resultContent = doc.importNode(resultContent, true);
177 resultContent = XmlHelper.getRootElement(resultContent);
178 if (resultContent != null)
179 rootElem.appendChild(resultContent);
180 }
181
182 return doc;
183 } catch (Exception e) {
184 e.printStackTrace();
185 return null;
186 }
187 }
188
189
190 private Object getRepresentationContent(AssetVersionValue resultVersion, RepresentationTypeEnum type, AssetServices services) {
191
192 if (resultVersion == null || type == null)
193 return null;
194 Collection representations = resultVersion.getRepresentations();
195 if (representations == null)
196 return null;
197 for (Iterator i = representations.iterator(); i.hasNext(); ) {
198 AssetRepresentationPK representationPk = (AssetRepresentationPK) i.next();
199 try {
200 AssetRepresentationValue representation = services.getAssetRepresentation(representationPk);
201
202
203 if (type == RepresentationTypeEnum.forName(representation.getType())) {
204 return representation.getContent();
205 }
206 } catch (Exception e) {
207 e.printStackTrace();
208 }
209 }
210 return null;
211 }
212
213
214 }