View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
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             String type = resultVersion.getType();
158             IRepresentation repr = resultVersion.getRepresentationByType(type);
159             if (repr instanceof XmlRepresentation) {
160                 Node n = (Node)repr.getContent();
161                 if (n != null) {
162                     n = doc.importNode(n, true);
163                     n = XmlHelper.getRootElement(n);
164                     if (n != null)
165                         rootElem.appendChild(n);
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 		// LOGGER.debug("getRepresentationContent for type " + type);
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                 // AssetRepresentationLocal representation = AssetRepresentationUtil.getLocalHome().findByPrimaryKey(representationPk);
202                 // LOGGER.debug("representation[" + representation.getName() + "]: " + representation.getType() + " content type " + representation.getContent().getClass());
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 }