1 /*** $Id: DAOGenerator.java,v 1.1.1.1 2004/05/18 10:50:16 mochoa Exp $*/
2 package org.j2ee.dao.gen;
3
4 import java.util.*;
5 //io
6 import java.io.*;
7 //net
8 import java.net.URL;
9 //jaxp
10 import javax.xml.transform.Transformer;
11 import javax.xml.transform.TransformerFactory;
12 import javax.xml.transform.stream.StreamSource;
13 import javax.xml.transform.stream.StreamResult;
14
15 /***
16 * Last modified $Date: 2004/05/18 10:50:16 $
17 * @version $Revision: 1.1.1.1 $
18 * @author jvlio (jvlio@users.sourceforge.net) - 04/11/2002 - 01:51:09
19 */
20 public class DAOGenerator {
21
22
23 public static final String ACTIONS_ENTRY="actions";
24 public static final String XSL_ACTION_SUFFIX = "xsl";
25 public static final String XSL_FNAME_SUFFIX = "filename";
26
27 /***
28 * this Map store actions (the keys) and the xsl (the values), there are two kinds of values:
29 * if is String, the value is the resource string readed from ResourceBoundle xsl suffix.
30 * if not a String, then the value is a javax.xml.transform.Transformer object.
31 * @associates String
32 */
33 private Map actionsMap = null;
34
35 /***
36 * This Map store the file name pattern associated with the accion (the key of this map).
37 * The patterns is a String.
38 * @associates String
39 */
40 private Map fnamesMap = null;
41
42 /***
43 * This ResourceBundle have the action configuration for generator
44 */
45 private ResourceBundle resourceBoundle = null;
46
47 /***
48 * A cached TransformerFactory, is created noly once during get.
49 */
50 private static TransformerFactory transformerFactory = null;
51
52 /***
53 * A flag for indicate if loadResources was called.
54 */
55 boolean loaded = false;
56
57
58 /***
59 * Default generator.
60 */
61 public DAOGenerator() {
62 actionsMap = new HashMap();
63 fnamesMap = new HashMap();
64 loaded = false;
65 }
66
67 /***
68 * Create a Generator with rb configuration.
69 * @param rb
70 */
71 public DAOGenerator(ResourceBundle rb) {
72 this();
73 setResourceBoundle(rb);
74 }
75
76
77
78 /***
79 * Get last ResourceBundle configuration file.
80 * If you set more that once ResourceBundle, her configarations was added to generator configuaration.
81 * @return a ResourceBundle.
82 */
83 public ResourceBundle getResourceBoundle() {
84 if (resourceBoundle==null) resourceBoundle=loadDefaultResourceBundle();
85 return resourceBoundle;
86 }
87 /***
88 * Set ResourceBundle and add her configuration to this generator (don clear generator state, this
89 * method generate an aditive configaration).
90 * @param resourceBoundle a new ResourceBundle configuration file.
91 */
92 public void setResourceBoundle(ResourceBundle resourceBoundle) {
93 this.resourceBoundle = resourceBoundle;
94 loaded = false;
95 loadResources();
96 }
97
98
99
100 /***
101 * Get a TransformerFactory, if this is null, then create a new one.
102 * @return A TransformerFactory.
103 */
104 public TransformerFactory getTransformerFactory(ClassLoader loader) {
105 if (DAOGenerator.transformerFactory==null) {
106 Class ctf = null;
107 try {
108 ctf = Class.forName("org.apache.xalan.processor.TransformerFactoryImpl",true,loader);
109 setTransformerFactory((TransformerFactory)ctf.newInstance());
110 } catch (Exception e) { e.printStackTrace(); }
111 /*try {
112 Class otf = Class.forName("javax.xml.transform.TransformerFactory",true,loader);
113 System.out.println(otf+" classloader="+otf.getClassLoader());
114 otf = loader.loadClass("javax.xml.transform.TransformerFactory");
115 System.out.println(otf+" classloader="+otf.getClassLoader());
116 Class.forName("org.apache.xalan.processor.TransformerFactoryImpl",true,loader);
117 } catch (Exception e) { e.printStackTrace(); }
118 System.out.println(DAOGenerator.class.getClassLoader());
119 System.out.println(TransformerFactory.class.getClassLoader());
120 setTransformerFactory(TransformerFactory.newInstance());*/
121 }
122 return DAOGenerator.transformerFactory;
123 }
124
125 /***
126 * Set a new TransformerFactory
127 * @param transformerFactory A TransformerFactory.
128 */
129 public void setTransformerFactory(TransformerFactory transformerFactory) {
130 DAOGenerator.transformerFactory = transformerFactory;
131 }
132
133
134
135 public Map getActionsMap() {
136 if (!loaded) loadResources();
137 return actionsMap;
138 }
139 public void setActionsMap(Map actionsMap) { this.actionsMap = actionsMap; }
140
141
142
143 public Map getFileNamesMap() {
144 if (!loaded) loadResources();
145 return fnamesMap;
146 }
147 public void setFileNamesMap(Map fnamesMap) { this.fnamesMap = fnamesMap; }
148
149 public boolean isLoaded() { return loaded; }
150
151
152
153 public void clearResources() {
154 actionsMap.clear();
155 fnamesMap.clear();
156 }
157 public void loadResources() {
158 ResourceBundle rb = this.getResourceBoundle();
159 if(rb==null) return; //if (rb==nul) => no new resources load.
160 //find actions names
161 String sactions = Util.getInstance().getResourceString(rb,ACTIONS_ENTRY);
162 if (sactions!=null) {
163 String[] actions = Util.getInstance().tokenize(sactions);
164 String sxsl = null;
165 String filename = null;
166 for (int i=0; i<actions.length; i++) {
167 sxsl = Util.getInstance().getResourceString(rb,actions[i]+"."+XSL_ACTION_SUFFIX);
168 actionsMap.put(actions[i],sxsl);
169 filename = Util.getInstance().getResourceString(rb,actions[i]+"."+XSL_FNAME_SUFFIX);
170 fnamesMap.put(actions[i],filename);
171 // todo check nulls!!
172 }
173 }
174 loaded = true;
175 }
176
177
178 public String getFilenamePattern(String action) { return (String)getFileNamesMap().get(action); }
179
180
181 public String createDAOXML(GenData gd) {
182 StringBuffer sb = new StringBuffer();
183
184 sb.append("<dao ver=\"1.0\">\n");
185 sb.append(" <common_name>"+gd.getCommon_name()+"</common_name>\n");
186 sb.append(" <package>"+gd.getDao_package()+"</package>\n");
187 sb.append(" <dto package=\""+gd.getDto_package()+"\" name=\""+gd.getDto_name()+"\"/>\n");
188 sb.append(" <table name=\""+gd.getTableData().getName()+"\">\n");
189 for(Iterator it=gd.getTableData().iterator(); it.hasNext();) {
190 FieldData fd = (FieldData)it.next();
191 sb.append(" <field name=\""+fd.getName()+
192 "\" pk=\""+(fd.isPk()? "yes" : "no")+
193 "\" property_name=\""+fd.getJavaProp()+
194 "\" sql_type=\""+GenTypes.sqlTypeToString(fd.getSqlType())+
195 "\" jdbc_type=\""+GenTypes.getRsetProp()[fd.getJavaType()]+
196 "\" java_type=\""+GenTypes.getJavaTypes()[fd.getJavaType()]+"\" " );
197 if (GenTypes.getJavaWrappers()[fd.getJavaType()]!=null)
198 sb.append("java_class_wrapper=\""+GenTypes.getJavaWrappers()[fd.getJavaType()]+"\"");
199 // todo: nulls missing.
200 sb.append("/>\n");
201 }
202 sb.append(" </table>\n");
203 sb.append("</dao>\n");
204
205 return sb.toString();
206 }
207
208
209 public String createDAOAction(String action, String daoxml, ClassLoader loader) throws Exception {
210 //geting xsl
211 Object xslo = getActionsMap().get(action);
212 //todo throw a propietary exception (dont use Exception)
213 if (xslo == null) throw new Exception("XSL file associated with action \""+action+"\" not found.");
214
215 StringWriter res = new StringWriter();
216 Transformer aTransformer = null;
217
218 if (xslo instanceof String)
219 try {
220 URL url = loader.getResource((String)xslo);
221 InputStream is = url.openStream(); //todo if(url==null) then throw EXEPTION!!!
222 InputStreamReader isr = new InputStreamReader(is);
223 aTransformer = getTransformerFactory(loader).newTransformer(new StreamSource(isr));
224 getActionsMap().put(action,aTransformer);
225 } catch (Exception ex) {
226 ex.printStackTrace();
227 }
228 else
229 aTransformer = (Transformer)xslo;
230
231 try {
232 aTransformer.transform(new StreamSource(new StringReader(daoxml)),new StreamResult(res));
233 } catch (Exception ex) {
234 ex.printStackTrace();
235 }
236 return res.toString();
237 }
238
239
240 public String createDAOAction(String action, GenData gd, ClassLoader loader) throws Exception {
241 return createDAOAction(action,createDAOXML(gd),loader);
242 }
243
244
245 private ResourceBundle loadDefaultResourceBundle() {
246 // Load ResourceBundle
247 ResourceBundle lrb = null;
248 //@todo use jndi too, find the way to add every property file, not only a xor
249 String fname = System.getProperty("org.j2ee.dao.daogen");
250 if (fname!=null) {
251 try {
252 lrb = new java.util.PropertyResourceBundle(new FileInputStream(new File(fname)));
253 } catch (IOException ioex) {}
254 }
255 else {
256 try {
257 // search in root classpath
258 lrb = ResourceBundle.getBundle("daogen",Locale.getDefault());
259 } catch (MissingResourceException mrex1) {
260 try {
261 // search in gen package
262 lrb = ResourceBundle.getBundle("org.j2ee.dao.gen.daogen",Locale.getDefault());
263 } catch (MissingResourceException mrex2) {}
264 }
265 }
266 return lrb;
267 }
268
269
270
271
272
273 }
This page was automatically generated by Maven