1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }