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 no.sintef.umt;
22  
23  import java.net.URL;
24  
25  import javax.xml.transform.Transformer;
26  import javax.xml.transform.dom.DOMResult;
27  import javax.xml.transform.dom.DOMSource;
28  
29  import no.sintef.xml.XmlException;
30  import no.sintef.xml.XmlHelper;
31  
32  import org.apache.log4j.Logger;
33  import org.w3c.dom.Node;
34  
35  /***
36   * Helper class to transform XMI to XMI-Light.
37   * 
38   * @author Fredrik Vraalsen
39   */
40  public final class XmiLightTransformer {
41  
42  	private static final Logger LOGGER = Logger.getLogger(XmiLightTransformer.class);
43  
44  	private static XmiLightTransformer theTransformer = null;
45  	
46  	// TODO : use ThreadLocal to avoid synchronized access to transformers...
47  	private Transformer xmiLightTransformer = null;
48  
49  	/***
50  	 * Singleton constructor.
51  	 */
52  	private XmiLightTransformer() {
53  		ClassLoader cl = getClass().getClassLoader();
54  	    URL url = cl.getResource("xmi2XMI-Light.xsl");
55  	    LOGGER.debug("XMI Light transformer url = " + url);
56  		try {
57  			xmiLightTransformer = XmlHelper.getTransformer(url, LOGGER);
58  		} catch (XmlException e) {
59  			LOGGER.fatal("Unable to create XMI Light transformer", e);
60  		}
61  	}
62  	
63  	/***
64  	 * Get the XMI -> XMI-Light Transformer instance.
65  	 * 
66  	 * @return the XMI -> XMI-Light Transformer, or null if unable to create
67  	 */
68  	public static synchronized XmiLightTransformer getTransformer() {
69  		if (theTransformer == null) {
70  			try {
71  				theTransformer = new XmiLightTransformer();
72  			} catch (Exception e) {
73  				LOGGER.error("Could not create XmiLightTransformer", e);
74  				theTransformer = null;
75  			}
76  		}
77  		return theTransformer;
78  	}
79  
80  	/***
81  	 * Transform an XMI Document into XMI-Light.
82  	 * 
83  	 * @param xmi
84  	 *            the XMI Document to transform
85  	 * @return the XMI-Light
86  	 */
87  	public Node getXmiLight(Node xmi) {
88  		try {
89  			DOMSource source = new DOMSource(xmi);
90  			DOMResult result = new DOMResult();
91  			synchronized (xmiLightTransformer) {
92  				xmiLightTransformer.transform(source, result);
93  			}
94  			return result.getNode();
95  		} catch (Exception e) {
96  			LOGGER.warn("Error transforming DGM to XMI light - offending XML:\n"
97  					+ new String(XmlHelper.xmlToUtf8(xmi, true)), e);
98  			return null;
99  		}
100 	}
101 
102 }