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.net.URL;
24  import java.rmi.RemoteException;
25  import java.util.Collection;
26  import java.util.Iterator;
27  
28  import javax.xml.bind.JAXBContext;
29  import javax.xml.bind.JAXBException;
30  import javax.xml.bind.Unmarshaller;
31  import javax.xml.transform.Transformer;
32  import javax.xml.transform.dom.DOMResult;
33  import javax.xml.transform.dom.DOMSource;
34  
35  import no.sintef.assetrepository.interfaces.AssetRepresentationPK;
36  import no.sintef.assetrepository.interfaces.AssetRepresentationValue;
37  import no.sintef.assetrepository.interfaces.AssetServices;
38  import no.sintef.assetrepository.interfaces.AssetServicesUtil;
39  import no.sintef.assetrepository.interfaces.AssetVersionValue;
40  import no.sintef.assetrepository.interfaces.VersionedAssetPK;
41  import no.sintef.assetrepository.interfaces.VersionedAssetValue;
42  import no.sintef.xml.XmlHelper;
43  
44  import org.apache.log4j.Logger;
45  import org.w3c.dom.Document;
46  import org.w3c.dom.Element;
47  import org.w3c.dom.Node;
48  
49  import coras.repository.session.CorasServicesEJBImpl;
50  import coras.representations.RepresentationTypeEnum;
51  import coras.table.jaxb.TableType;
52  
53  public class TableFiller {
54  
55  	private static final Logger LOGGER = Logger.getLogger(TableFiller.class);
56  
57  	private static final String NAMESPACE_URI = "http://coras.sourceforge.net/schemas/coras-internal-xml-1_0.xsd";
58      private static final String PREFIX = "ci";
59      private static final String ROOT_ELEM = "tableFiller";
60  	
61      private static TableFiller theFiller = null;
62      private ClassLoader cl = null;
63      private Unmarshaller unmarshaller = null;
64      
65      private TableFiller() throws JAXBException {
66          cl = getClass().getClassLoader();
67          JAXBContext jc = JAXBContext.newInstance("coras.table.jaxb");
68          unmarshaller = jc.createUnmarshaller();
69      }
70      
71      public static synchronized TableFiller getTableFiller() {
72      	if (theFiller == null) {
73      		try {
74  				theFiller = new TableFiller();
75  			} catch (JAXBException e) {
76                  LOGGER.error("Could not create TableFiller: " + e);
77  				e.printStackTrace();
78  				theFiller = null;
79  			}
80      	}
81      	return theFiller;
82      }
83      
84  	public TableType fillTable(VersionedAssetPK resultPk) throws Exception {
85          AssetServices services = AssetServicesUtil.getHome().create();
86          VersionedAssetValue result = services.getVersionedAsset(resultPk);
87          AssetVersionValue resultVersion = services.getAssetVersion(result.getBaseVersion());
88          
89          String subtype = (String) resultVersion.getProperties().get("subtype");
90          String name = "coras/internalmodel/tablefiller/" + subtype.replaceAll(" ", "_") + ".xsl";
91          URL tableFillerUrl = cl.getResource(name);
92          if (tableFillerUrl == null) {
93          	return null;
94          }
95          Transformer tableFillerTransformer = XmlHelper.getTransformer(tableFillerUrl, LOGGER);
96          
97          Node content = getTableContent(resultVersion, services);
98          if (content == null) {
99          	return null;
100         }
101         Element internalRepr = CorasServicesEJBImpl.getInternalRepresentation(resultVersion.getParent(), services); //imf.getInternalRepresentation(resultVersions);
102         Document elementDoc = getElementDoc(content, internalRepr);
103         DOMSource elementDocSource = new DOMSource(elementDoc);
104         DOMResult newContentResult = new DOMResult();
105         tableFillerTransformer.transform(elementDocSource, newContentResult);
106         Node newResult = newContentResult.getNode();
107         return (TableType) unmarshaller.unmarshal(newResult);
108 	}
109 
110 	private Node getTableContent(AssetVersionValue resultVersion, AssetServices services) throws RemoteException {
111 		Collection representations = resultVersion.getRepresentations();
112 		for (Iterator i = representations.iterator(); i.hasNext();) {
113 			AssetRepresentationPK representationPk = (AssetRepresentationPK) i.next();
114 			AssetRepresentationValue representation = services.getAssetRepresentation(representationPk);
115 			if (RepresentationTypeEnum.TABLE == RepresentationTypeEnum.forName(representation.getType())) {
116 				return (Node) representation.getContent();
117 			}
118 		}
119 		return null;
120 	}
121 
122 	private Document getElementDoc(Node resultContent, Node internalRepr) {
123 	    if (resultContent == null || internalRepr == null)
124 	        return null;
125 	    
126 	    try {
127 	        Document doc = XmlHelper.createDocument(NAMESPACE_URI, PREFIX, ROOT_ELEM, null);
128 	        Element rootElem = doc.getDocumentElement();
129 	        
130 	        /*
131 	        String type = resultVersion.getType();
132 	        IRepresentation repr = resultVersion.getRepresentationByType(type);
133 	        if (repr instanceof XmlRepresentation) {
134 	            Node n = (Node)repr.getContent();
135 	            if (n != null) {
136 	                n = doc.importNode(n, true);
137 	                n = XmlHelper.getRootElement(n);
138 	                if (n != null)
139 	                    rootElem.appendChild(n);
140 	            }
141 	        }
142 	        */
143 	        resultContent = XmlHelper.getRootElement(resultContent);
144 	        if (resultContent != null) {
145 	            resultContent = doc.importNode(resultContent, true);
146 	            resultContent = XmlHelper.getRootElement(resultContent);
147 	            if (resultContent != null)
148 	                rootElem.appendChild(resultContent);
149 	        }
150 	        
151 	        internalRepr = doc.importNode(internalRepr, true);
152 	        if (internalRepr != null)
153 	            rootElem.appendChild(internalRepr);
154 	        return doc;
155 	    } catch (Exception e) {
156 	        e.printStackTrace();
157 	        return null;
158 	    }
159 	}
160 	
161 }