1 package org.opensync.engine.server;
2
3 import org.opensync.engine.server.OpenSync;
4 import org.opensync.engine.util.FileHelper;
5
6 import java.io.*;
7 import java.net.*;
8 import javax.xml.transform.*;
9 import javax.xml.transform.stream.*;
10 import org.xml.sax.SAXException;
11 import org.xml.sax.SAXParseException;
12
13 /***
14 * This class is used to to transform a business view to an other.
15 * It begins by creating the xsl sheet from the xml mapping file and
16 * finish by apply it on the xml view.
17 */
18
19 public class Mapper implements ErrorListener {
20
21 /***
22 * The xslt transfomer use to produice the xsl sheet
23 *
24 */
25 protected Transformer genMappingTransformer;
26 /***
27 * The transformer factory
28 *
29 *
30 */
31 protected TransformerFactory transformerFactory = TransformerFactory.newInstance();
32
33 /***
34 * Consruct a <code>Mapper</code> object
35 *
36 */
37 public Mapper() {
38 try {
39 String fileSeparator = System.getProperty("file.separator");
40 Templates templates = transformerFactory.newTemplates(
41 new StreamSource(new StringReader(
42 FileHelper.fileToString(
43 OpenSync.getInstance().getFilePath("etc"+fileSeparator+"gen_mapping.xsl", true)
44 )
45 )
46 )
47 );
48 genMappingTransformer = templates.newTransformer();
49 genMappingTransformer.setErrorListener(this);
50 }
51 catch (Exception ex) {
52 throw new RuntimeException(ex.getMessage());
53 }
54 }
55
56
57 public void error(TransformerException exception) throws TransformerException {
58 throw exception;
59 }
60
61
62 public void fatalError(TransformerException exception) throws TransformerException {
63 throw exception;
64 }
65
66 public void warning(TransformerException exception) throws TransformerException {
67 throw exception;
68 }
69
70 /***
71 * Generate the xsl sheet from the xml mapping
72 *
73 * @param xmlMapping the xml mapping to transform the view
74 * @exception TransformerException
75 * @exception FileNotFoundException
76 */
77 private String generateXslMapping(String xmlMapping)throws FileNotFoundException, TransformerException{
78 StringWriter resultWriter = new StringWriter();
79 genMappingTransformer.transform(
80 new StreamSource(new StringReader(xmlMapping)),
81 new StreamResult(resultWriter)
82 );
83
84 return resultWriter.getBuffer().toString();
85 }
86
87 /***
88 * Generate the xsl sheet from the xml mapping file
89 *
90 * @param xmlMappingFile the xml mapping file to transform the view
91 * @exception TransformerException
92 * @exception FileNotFoundException
93 */
94 public String generateXslMapping(File xmlMappingFile)throws OpenSyncException, SAXException, FileNotFoundException, TransformerException, MalformedURLException{
95 StringWriter resultWriter = new StringWriter();
96
97
98
99 try {
100 genMappingTransformer.transform(
101 new StreamSource(xmlMappingFile.toURL().toExternalForm()),
102 new StreamResult(resultWriter)
103 );
104 } catch (TransformerException tex) {
105 SAXParseException spex = (SAXParseException)tex.getException();
106 String message = "\r\nError at line "+spex.getLineNumber()+", column "+spex.getColumnNumber()+" in the file "+spex.getSystemId()+": "+spex.getLocalizedMessage();
107 throw new OpenSyncException(message);
108 }
109
110 return resultWriter.getBuffer().toString();
111 }
112
113 /***
114 * Transform the view from the xml mapping
115 *
116 * @param xmlMapping the xml mapping
117 * @param xmlView the view in xml format
118 * @exception TransformerException
119 * @exception IOException
120 */
121 synchronized public String map(String xmlMapping,String xmlView)
122 throws TransformerException, IOException {
123
124
125
126
127
128 String xslMapping = generateXslMapping(xmlMapping);
129
130
131
132 Templates templates = transformerFactory.newTemplates(
133 new StreamSource(new StringReader(xslMapping))
134 );
135
136 Transformer transformer = templates.newTransformer();
137 transformer.setErrorListener(this);
138 StringWriter resultWriter = new StringWriter();
139 String fileSeparator = System.getProperty("file.separator");
140 String home_etc = OpenSync.getInstance().getFilePath("etc"+fileSeparator, false);
141 String uriXmlView = new File(home_etc).toURL().toExternalForm();
142 transformer.transform(
143 new StreamSource(new StringReader(xmlView), uriXmlView),
144 new StreamResult(resultWriter)
145 );
146 return resultWriter.getBuffer().toString();
147 }
148
149 /***
150 * Transform the view from the xml mapping
151 *
152 * @param xmlMapping the xml mapping
153 * @param xmlView the view in xml format
154 * @exception TransformerException
155 * @exception IOException
156 */
157 synchronized public String map(File xmlMappingFile,String xmlView)
158 throws TransformerException, OpenSyncException, IOException, SAXException {
159
160
161
162
163
164 String xslMapping = generateXslMapping(xmlMappingFile);
165 if (org.opensync.tools.Utils.debug) OpenSync.getInstance().getLog().debug(Log.ROOT,"xslMapping :" +xslMapping);
166
167
168
169 Templates templates = transformerFactory.newTemplates(
170 new StreamSource(new StringReader(xslMapping))
171 );
172
173 Transformer transformer = templates.newTransformer();
174 transformer.setErrorListener(this);
175 StringWriter resultWriter = new StringWriter();
176 StreamSource xmlViewSource = new StreamSource(new StringReader(xmlView));
177
178
179 String fileSeparator = System.getProperty("file.separator");
180 String home_etc = OpenSync.getInstance().getFilePath("etc"+fileSeparator, false);
181 String uriXmlView = new File(home_etc).toURL().toExternalForm();
182 xmlViewSource.setSystemId(uriXmlView);
183
184
185
186
187
188
189
190 try {
191 transformer.transform( xmlViewSource,
192 new StreamResult(resultWriter));
193 }
194 catch (TransformerConfigurationException tce) {
195 try {
196 this.fatalError(tce);
197 }
198 catch (TransformerException ex) {
199 throw ex;
200 }
201 }
202 catch (TransformerException te) {
203 try {
204 this.error(te);
205 }
206 catch (TransformerException ex) {
207 throw ex;
208 }
209 }
210
211
212
213
214
215
216 return resultWriter.getBuffer().toString();
217 }
218
219 }