View Javadoc

1   /*
2    * EDI-Knight Integration and Transformation Platform
3    * Copyright (C) 2006-2007 Holger Joest <hjoest@users.sourceforge.net>
4    *
5    * This program is free software; you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation; either version 2 of the License, or
8    * (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package net.sf.ediknight.common;
21  
22  import java.io.BufferedInputStream;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileOutputStream;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.io.OutputStreamWriter;
29  import java.io.Writer;
30  
31  import javax.xml.parsers.DocumentBuilder;
32  import javax.xml.parsers.DocumentBuilderFactory;
33  
34  import net.sf.ediknight.Converter;
35  import net.sf.ediknight.ConverterConfiguration;
36  import net.sf.ediknight.ConverterFactory;
37  
38  import org.w3c.dom.Document;
39  import org.w3c.dom.Element;
40  import org.w3c.dom.Node;
41  import org.w3c.dom.NodeList;
42  
43  import com.thoughtworks.xstream.XStream;
44  import com.thoughtworks.xstream.io.xml.DomDriver;
45  
46  
47  /***
48   * @author Holger Joest
49   */
50  public class ConverterFactoryImpl
51  extends ConverterFactory {
52  
53      /***
54       * Creates a new converter instance.
55       *
56       * @param name the converter configuration name
57       * @return the converter
58       */
59      @Override
60      public final Converter createConverter(String name) {
61          ClassLoader classLoader =
62              Thread.currentThread().getContextClassLoader();
63          String home = System.getProperty("user.home");
64          File configurations =
65              new File(home + "/.ediknight/configurations/");
66          if (!configurations.exists()) {
67              configurations.mkdirs();
68          }
69          File converterConf =
70              new File(configurations, name + ".xml");
71          try {
72              if (!converterConf.exists()) {
73                  OutputStream ostream =
74                      new FileOutputStream(converterConf);
75                  try {
76                      Writer writer =
77                          new OutputStreamWriter(
78                                  ostream, "UTF-8");
79                      try {
80                          writer.write(
81                                  "<?xml version=\"1.0\""
82                                  + " encoding=\"UTF-8\"?>\n");
83                          writer.write(
84                                  "<converter>\n");
85                          writer.write(
86                                  "    <class>net.sf.ediknight.common."
87                                  + "DefaultConverter</class>\n");
88                          writer.write(
89                                  "</converter>\n");
90                      } finally {
91                          writer.close();
92                      }
93                  } finally {
94                      ostream.close();
95                  }
96              }
97              DocumentBuilderFactory builderFactory =
98                  DocumentBuilderFactory.newInstance();
99              DocumentBuilder builder = builderFactory.newDocumentBuilder();
100             InputStream istream = new BufferedInputStream(
101                     new FileInputStream(converterConf));
102             Document document = null;
103             try {
104                 document = builder.parse(istream);
105             } finally {
106                 istream.close();
107             }
108             Element root = document.getDocumentElement();
109             NodeList nodes = root.getElementsByTagName("class");
110             if (nodes.getLength() == 0) {
111                 return null;
112             }
113             Element classElement = (Element) nodes.item(0);
114             Node textNode = classElement.getFirstChild();
115             if (textNode == null) {
116                 return null;
117             }
118             String className = textNode.getNodeValue();
119             Class<? extends Object> clazz =
120                 classLoader.loadClass(className);
121             Converter converter = (Converter) clazz.newInstance();
122             istream = new BufferedInputStream(
123                     new FileInputStream(converterConf));
124             try {
125                 ConverterConfiguration configuration =
126                     getConfiguration(istream);
127                 converter.setConfiguration(configuration);
128             } finally {
129                 istream.close();
130             }
131             return converter;
132         } catch (Exception ex) {
133             throw new RuntimeException("Failed to read"
134                     + " converter configuration", ex);
135         }
136     }
137 
138 
139     /***
140      * @param istream an input stream
141      * @return the converter configuration
142      */
143     private ConverterConfiguration getConfiguration(
144             InputStream istream) {
145         XStream xstream = new XStream(new DomDriver());
146         xstream.setMode(XStream.ID_REFERENCES);
147         xstream.alias("converter", ConverterConfiguration.class);
148         xstream.alias("class", Class.class);
149         xstream.omitField(ConverterConfiguration.class, "class");
150         return (ConverterConfiguration) xstream.fromXML(istream);
151     }
152 
153 }
154