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.representations;
22  
23  import java.awt.image.BufferedImage;
24  import java.io.ByteArrayInputStream;
25  import java.io.ByteArrayOutputStream;
26  import java.io.File;
27  import java.io.FileInputStream;
28  import java.io.FileNotFoundException;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.net.URL;
32  import java.util.Calendar;
33  
34  import javax.imageio.ImageIO;
35  import javax.xml.bind.JAXBException;
36  
37  import no.sintef.assetrepository.Representation;
38  import no.sintef.file.FileSuffixFilter;
39  import no.sintef.file.IOUtils;
40  import no.sintef.file.MimeTypeEnum;
41  import no.sintef.xml.XmlException;
42  import no.sintef.xml.XmlHelper;
43  
44  import org.w3c.dom.Element;
45  import org.w3c.dom.Node;
46  import org.xml.sax.InputSource;
47  
48  import coras.common.CorasRepresentation;
49  import coras.fta.OpenFTAParser;
50  import coras.fta.jaxb.FaultTree;
51  import coras.types.IUMLModel;
52  import coras.types.SubtypeEnum;
53  
54  /***
55   * @author fvr
56   *
57   * TODO To change the template for this generated type comment go to
58   * Window - Preferences - Java - Code Style - Code Templates
59   */
60  public class RepresentationFactory {
61  
62      /***
63       * @param element
64       * @return
65       */
66      public static CorasRepresentation createRepresentation(Representation representation) {
67          if (representation == null) {
68              return null;
69          }
70          RepresentationTypeEnum type = RepresentationTypeEnum.forName(representation.getType());
71          if (type == RepresentationTypeEnum.DGM_MODEL) {
72          	return new DgmModel(representation);
73          } else if (type == RepresentationTypeEnum.XMI_MODEL) {
74              return new XmiModel(representation);
75          } else if (type == RepresentationTypeEnum.ZUMLZARGO_MODEL) {
76          	return new ZumlZargoModel(representation);
77          } else if (type == RepresentationTypeEnum.XMI_LIGHT) {
78          	return new XmiLightRepresentation(representation);
79          } else if (type == RepresentationTypeEnum.FAULT_TREE_MODEL) {
80              return new FaultTreeModel(representation);
81          } else if (type == RepresentationTypeEnum.TABLE) {
82              return new TableRepresentation(representation);
83          } else if (type == RepresentationTypeEnum.DOCUMENT) {
84              return new DocumentRepresentation(representation);
85          } else if (type == RepresentationTypeEnum.IMAGE) {
86              if (representation.getContent() instanceof Node) {
87                  return new SVGImage(representation);
88              } else {
89                  return new BitmapImage(representation);
90              }
91          } else {
92              return null;
93          }
94      }
95  
96      /***
97  	 * @param type
98  	 * @param f
99  	 * @return
100 	 * @throws XmlException
101 	 * @throws IOException
102 	 */
103     public static CorasRepresentation createRepresentation(RepresentationTypeEnum type, File f) throws IOException, XmlException {
104     	return createRepresentation(type, f, null);
105     }
106     
107     /***
108      * 
109      * @param type
110      * @param f
111      * @param filename
112      * @return
113      * @throws IOException
114      * @throws XmlException
115      */
116 	public static CorasRepresentation createRepresentation(RepresentationTypeEnum type, File f, String filename) throws IOException, XmlException {
117 	    if (type == RepresentationTypeEnum.DGM_MODEL
118 	    		|| type == RepresentationTypeEnum.XMI_MODEL
119 	    		|| type == RepresentationTypeEnum.ZUMLZARGO_MODEL
120 	    		|| type == RepresentationTypeEnum.XMI_LIGHT) {
121 	    	return createUMLModel(f, filename);
122 	    } else if (type == RepresentationTypeEnum.FAULT_TREE_MODEL) {
123 	        return createFaultTreeModel(f, filename);
124 	    } else if (type == RepresentationTypeEnum.DOCUMENT) {
125 	        return createDocument(f, filename);
126 	    } else if (type == RepresentationTypeEnum.IMAGE) {
127 	        return createDiagram(f, filename);
128 	    } else {
129 	        return null; // FIXME
130 	    }
131 	}
132 
133 	public static DocumentRepresentation createDocument(File documentFile) throws IOException {
134 		return createDocument(documentFile, null);
135 	}
136 	
137 	/***
138      * @param documentFile
139      * @return
140      * @throws IOException
141      */
142     public static DocumentRepresentation createDocument(File documentFile, String filename) throws IOException {
143     	String name = filename != null ? filename : documentFile.getName();
144         byte[] bytes = IOUtils.readFully(documentFile); // Will throw exception if null
145         return createDocument(name, bytes);
146     }
147     
148     public static DocumentRepresentation createDocument(String name, byte[] bytes) throws IOException {
149         Representation representation = 
150             createRepresentation(RepresentationTypeEnum.DOCUMENT,
151                     name,
152                     bytes);
153         return new DocumentRepresentation(representation);
154     }
155 
156     public static UMLModelRepresentation createUMLModel(File umlFile) throws IOException, XmlException {
157     	return createUMLModel(umlFile, null);
158     }
159     
160     public static UMLModelRepresentation createUMLModel(File umlFile, String filename) throws IOException, XmlException {
161         String name = filename != null ? filename : umlFile.getName();
162         MimeTypeEnum mimeType = MimeTypeEnum.getMimeType(name);
163         InputStream is = new FileInputStream(umlFile);
164         if (mimeType == MimeTypeEnum.APPLICATION_BINARY) {
165         	byte[] content = IOUtils.readFully(umlFile);
166         	return createZumlZargoModel(name, content);
167         } else {
168         	org.w3c.dom.Document doc = XmlHelper.parse(new InputSource(is));
169         	return createXmiOrDgmModel(name, doc);
170         }
171     }
172     
173     public static UMLModelRepresentation createZumlZargoModel(String name, byte[] content) {
174     	// TODO validate XMI
175     	Representation representation = 
176     		_createRepresentation(RepresentationTypeEnum.ZUMLZARGO_MODEL,
177     				name,
178     				MimeTypeEnum.APPLICATION_BINARY,
179     				content);
180     	return new ZumlZargoModel(representation);
181     }
182     
183     public static UMLModelRepresentation createXmiOrDgmModel(String name, org.w3c.dom.Document doc) {
184     	// TODO validate XMI
185     	Element rootElem = XmlHelper.getRootElement(doc);
186     	if ("XMI".equals(rootElem.getLocalName())) {
187     		Representation representation = 
188     			_createRepresentation(RepresentationTypeEnum.XMI_MODEL,
189     					name,
190     					MimeTypeEnum.TEXT_XML,
191     					doc);
192     		return new XmiModel(representation);
193     	} else if ("java".equals(rootElem.getLocalName())) {
194     		Representation representation = 
195     			_createRepresentation(RepresentationTypeEnum.DGM_MODEL,
196     					name,
197     					MimeTypeEnum.TEXT_XML,
198     					doc);
199     		return new DgmModel(representation);
200     	} else {
201     		// FIXME error handling
202     		return null;
203     	}
204     }
205     
206     public static XmiLightRepresentation createXmiLight(IUMLModel umlModel) {
207     	UMLModelRepresentation model = umlModel.getUMLModel();
208     	if (model == null) {
209     		return null;
210     	}
211     	Node xmiLight = model.getXmiLight();
212     	String name = FileSuffixFilter.getPrefix(model.getFilename()) + ".xmiLight";
213         Representation representation =
214         	_createRepresentation(RepresentationTypeEnum.XMI_LIGHT, 
215         			name, 
216         			MimeTypeEnum.TEXT_XML, 
217         			xmiLight);
218         return new XmiLightRepresentation(representation);
219     }
220         
221     /*
222     public static XmiLightRepresentation createXmiLight(UMLModelExperience umlModel) {
223         XmiModel xmi = umlModel.getXmi();
224         SubtypeEnum subtype = umlModel.getSubtype();
225         return createXmiLight(xmi, subtype);
226     }
227         
228     public static XmiLightRepresentation createXmiLight(XmiModel xmi, SubtypeEnum subtype) {
229         Node xmiNode = xmi.getXmlContent();
230         Document xmiDoc = xmiNode instanceof Document ? (Document) xmiNode : xmiNode.getOwnerDocument();
231         Node xmiLight = XmiLightTransformer.getTransformer().getXmiLight(xmiDoc, subtype);
232         String name = xmi.getFilename();
233         String suffix = FileSuffixFilter.getSuffix(name);
234         if (suffix != null) {
235             name = name.replaceAll("//." + suffix, ".xmilight"); // FIXME only replace at end of string
236         }
237         Representation representation =
238             _createRepresentation(RepresentationTypeEnum.XMI_LIGHT, name, MimeTypeEnum.TEXT_XML, xmiLight);
239         return new XmiLightRepresentation(representation);
240     }
241     */
242     
243     public static FaultTreeModel createFaultTreeModel(File faultTreeModelFile) throws IOException, XmlException {
244     	return createFaultTreeModel(faultTreeModelFile, null);
245     }
246     
247     public static FaultTreeModel createFaultTreeModel(File faultTreeModelFile, String filename) throws IOException, XmlException {
248     	String name = filename != null ? filename : faultTreeModelFile.getName();
249     	String suffix = FileSuffixFilter.getSuffix(name);
250     	InputStream is;
251     	if ("fta".equalsIgnoreCase(suffix)) {
252     		try {
253     			OpenFTAParser parser = new OpenFTAParser();
254     			FaultTree faultTree = parser.parse(faultTreeModelFile);
255     			ByteArrayOutputStream baos = new ByteArrayOutputStream();
256     			parser.serialize(faultTree, baos);
257     			is = new ByteArrayInputStream(baos.toByteArray());
258     			name = FileSuffixFilter.getPrefix(name) + ".xml";
259 			} catch (JAXBException e) {
260 				throw new IOException(e.getMessage());
261 			}
262     	} else {
263     		is = new FileInputStream(faultTreeModelFile);
264     	}
265         org.w3c.dom.Document doc = XmlHelper.parse(new InputSource(is));
266         // TODO validate XML
267         Representation representation = 
268             createRepresentation(RepresentationTypeEnum.FAULT_TREE_MODEL,
269                     name,
270                     doc);
271         return new FaultTreeModel(representation);
272     }
273 
274     public static DiagramRepresentation createDiagram(File diagramFile) throws IOException {
275     	return createDiagram(diagramFile, null);
276     }
277     
278     /***
279      * @param diagramFile
280      * @return
281      * @throws IOException
282      */
283     public static DiagramRepresentation createDiagram(File diagramFile, String filename) throws IOException {
284         try {
285             return createSVGImage(diagramFile, filename);
286         } catch (Exception e) { 
287             return createBitmapImage(diagramFile, filename);
288         }
289     }
290     
291     private static SVGImage createSVGImage(File svgFile, String filename) throws IOException, XmlException {
292     	String name = filename != null ? filename : svgFile.getName();
293         org.w3c.dom.Document doc = XmlHelper.parse(new InputSource(new FileInputStream(svgFile)));
294         return createSVGImage(name, doc);
295     }
296     
297     public static SVGImage createSVGImage(String name, org.w3c.dom.Document doc) {
298         // FIXME validate SVG
299         Representation representation = 
300             // createRepresentation(RepresentationTypeEnum.SVG_IMAGE,
301             createRepresentation(RepresentationTypeEnum.IMAGE,
302                     name,
303                     doc);
304         return new SVGImage(representation);
305     }
306     
307     private static BitmapImage createBitmapImage(File bitmapFile, String filename) throws IOException {
308     	String name = filename != null ? filename : bitmapFile.getName();
309         byte[] bytes = IOUtils.readFully(bitmapFile);
310         return createBitmapImage(name, bytes);
311     }
312     
313     public static BitmapImage createBitmapImage(String name, byte[] bytes) throws IOException {
314         BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
315         Representation representation = 
316             //createRepresentation(RepresentationTypeEnum.BITMAP_IMAGE,
317             createRepresentation(RepresentationTypeEnum.IMAGE,
318                     name,
319                     bytes);
320         return new BitmapImage(representation);
321     }
322 
323     public static TableRepresentation createTable(SubtypeEnum subtype) throws XmlException, FileNotFoundException {
324         if (! SubtypeEnum.TABLE_VALUES.contains(subtype)) {
325             throw new IllegalArgumentException("type must be one of the table types");
326         }
327 
328         String name = subtype.toString().replace(' ', '_') + ".xml";
329         String templateName = "coras/table/templates/" + name;
330 		URL templateUrl = RepresentationFactory.class.getClassLoader().getResource(templateName);
331 		Node template = XmlHelper.getDocument(templateUrl);
332 		if (template == null) {
333 		    throw new FileNotFoundException("Unable to find table template: " + templateName);
334 		}
335 		return createTable(template, name);
336     }
337     
338     public static TableRepresentation createTable(Node content, String name) {
339         Representation representation = 
340             _createRepresentation(RepresentationTypeEnum.TABLE,
341                     name,
342                     MimeTypeEnum.TEXT_XML,
343                     content);
344         return new TableRepresentation(representation);
345     }
346     
347     private static Representation createRepresentation(RepresentationTypeEnum type, String name, Object content) {
348         return _createRepresentation(type, name, MimeTypeEnum.getMimeType(name), content);
349     }
350     
351     private static Representation _createRepresentation(RepresentationTypeEnum type, String name, MimeTypeEnum mimeType, Object content) {
352         return Representation.create(type.toString(), name, mimeType, content, Calendar.getInstance());
353     }
354 
355 }