KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > Config


1 package dinamica;
2
3 import electric.xml.*;
4 import java.sql.Types JavaDoc;
5
6 /**
7  * Encapsulates access to the XML configuration
8  * of a framework transaction
9  * <br>
10  * Creation date: 4/10/2003<br>
11  * Last Update: 4/10/2003<br>
12  * (c) 2003 Martin Cordova<br>
13  * This code is released under the LGPL license<br>
14  * @author Martin Cordova
15  */

16 public class Config
17 {
18
19     /** xml parser objects */
20     Document _doc = null;
21     Element _root = null;
22     Element _trans = null;
23     Element _output = null;
24
25     /** summary */
26     public String JavaDoc summary = null;
27
28     /** performance logs */
29     public String JavaDoc mvcLog = null;
30     public String JavaDoc jdbcLog = null;
31
32     /** transaction classname */
33     public String JavaDoc transClassName = null;
34
35     /** transaction datasource */
36     public String JavaDoc transDataSource = null;
37     
38     /** transaction validator */
39     public String JavaDoc transValidator = null;
40     
41     /** automatic transactions */
42     public String JavaDoc transTransactions = null;
43
44     /** auto-create recordset definitions */
45     Recordset _rs = new Recordset();
46
47     /** output classname */
48     public String JavaDoc outClassName = null;
49     
50     /** output template */
51     public String JavaDoc outTemplate = null;
52     
53     /** output content-type */
54     public String JavaDoc contentType = null;
55     
56     /** output expiration */
57     public String JavaDoc expiration = null;
58     
59     /** flag that indicates if http headers shoud be set */
60     public String JavaDoc headers = null;
61     
62     /** specific request encoding for the current Action */
63     public String JavaDoc requestEncoding = null;
64     
65     /** encoding used to read template files */
66     public String JavaDoc templateEncoding = null;
67     
68     /** output data binding commands */
69     Recordset _print = new Recordset();
70             
71     /** transaction path */
72     public String JavaDoc path = null;
73     
74             
75     /**
76      * Default constructor
77      * @param xmlData Transaction configuration data in XML
78      * @param path Transaction directory relative to the context
79      * @throws Throwable
80      */

81     public Config(String JavaDoc xmlData, String JavaDoc path) throws Throwable JavaDoc
82     {
83         
84         /* remember path */
85         this.path = path;
86         
87         /* create basic xml objects */
88         _doc = new Document(xmlData);
89         _root = _doc.getRoot();
90         _trans = _root.getElement("transaction");
91         _output = _root.getElement("output");
92
93         Element encoding = _root.getElement("request-encoding");
94         if (encoding!=null)
95             requestEncoding = encoding.getTextString();
96
97         /* summary */
98         summary = _root.getElement("summary").getString();
99
100         /* MVC logs */
101         mvcLog = _root.getElement("log").getString();
102
103         /* structure of the recordset */
104         _rs.append("id", Types.VARCHAR);
105         _rs.append("source", Types.VARCHAR);
106         _rs.append("scope", Types.VARCHAR);
107         _rs.append("onempty", Types.VARCHAR);
108         _rs.append("maxrows", Types.VARCHAR);
109         _rs.append("datasource", Types.VARCHAR);
110         _rs.append("params", Types.VARCHAR);
111     
112         /* structure of the recordset */
113         _print.append("mode", Types.VARCHAR);
114         _print.append("recordset", Types.VARCHAR);
115         _print.append("tag", Types.VARCHAR);
116         _print.append("control", Types.VARCHAR);
117         _print.append("pagesize", Types.VARCHAR);
118         _print.append("alternate-colors", Types.VARCHAR);
119         _print.append("null-value", Types.VARCHAR);
120     
121         /* read transaction configuration */
122         if (_trans!=null)
123         {
124             Element e = _trans.getElement("datasource");
125             if (e!=null)
126                 transDataSource = e.getString();
127                 
128             transClassName = _trans.getElement("classname").getString();
129             transValidator = _trans.getElement("validator").getString();
130             transTransactions = _trans.getElement("transaction").getString();
131             jdbcLog = _trans.getElement("jdbc-log").getString();
132
133             /* any auto-create recordsets? */
134             Elements rsElems = _trans.getElements("recordset");
135             while (rsElems.hasMoreElements())
136             {
137                 _rs.addNew();
138                 Element rs = rsElems.next();
139                 String JavaDoc id = rs.getAttribute("id");
140                 String JavaDoc mode = rs.getAttribute("source");
141                 String JavaDoc scope = rs.getAttribute("scope");
142                 String JavaDoc onempty = rs.getAttribute("on-empty-return");
143                 String JavaDoc maxrows = rs.getAttribute("max-rows");
144                 String JavaDoc rsDataSrc = rs.getAttribute("datasource");
145                 if (rsDataSrc!=null && rsDataSrc.trim().equals(""))
146                     rsDataSrc = null;
147                 String JavaDoc rsParams = rs.getAttribute("params");
148                 if (rsParams!=null && rsParams.trim().equals(""))
149                     rsParams = null;
150                 
151                 _rs.setValue("id", id);
152                 _rs.setValue("source", mode);
153                 _rs.setValue("scope", scope);
154                 _rs.setValue("onempty", onempty);
155                 _rs.setValue("maxrows", maxrows);
156                 _rs.setValue("datasource", rsDataSrc);
157                 _rs.setValue("params", rsParams);
158             }
159             if ( _rs.getRecordCount()>0)
160                 _rs.top();
161         }
162
163         /* read transaction configuration */
164         if (_output!=null)
165         {
166             Element x = null;
167             outClassName = _output.getElement("classname").getString();
168             
169             //PATCH 2005-02-17 encoding support
170
x = _output.getElement("template");
171             if (x!=null) {
172                 templateEncoding = x.getAttribute("file-encoding");
173                 if (templateEncoding!=null && templateEncoding.trim().equals(""))
174                     templateEncoding=null;
175                 outTemplate = x.getString();
176             }
177                 
178             x = _output.getElement("content-type");
179             if (x!=null) {
180                 contentType = x.getString();
181                 //PATCH 2005-02-17 encoding support
182
if (contentType.indexOf("charset")<0 && templateEncoding!=null)
183                     contentType = contentType + "; charset=" + templateEncoding;
184             }
185                 
186                 
187             x = _output.getElement("expiration");
188             if (x!=null)
189                 expiration = x.getString();
190             x = _output.getElement("set-http-headers");
191             if (x!=null)
192                 headers = x.getString();
193             else
194                 headers = "false";
195             
196             /* any auto-create recordsets? */
197             Elements rsElems = _output.getElements("print");
198             while (rsElems.hasMoreElements())
199             {
200                 _print.addNew();
201                 Element rs = rsElems.next();
202                 String JavaDoc mode = rs.getAttribute("mode");
203                 String JavaDoc rset = rs.getAttribute("recordset");
204                 String JavaDoc tag = rs.getAttribute("tag");
205                 String JavaDoc control = rs.getAttribute("control");
206                 String JavaDoc pagesize = rs.getAttribute("pagesize");
207                 String JavaDoc altColors = rs.getAttribute("alternate-colors");
208                 String JavaDoc nullValue = rs.getAttribute("null-value");
209                 
210                 _print.setValue("mode", mode);
211                 _print.setValue("recordset", rset);
212                 _print.setValue("tag", tag);
213                 _print.setValue("control", control);
214                 _print.setValue("pagesize", pagesize);
215                 _print.setValue("alternate-colors", altColors);
216                 _print.setValue("null-value", nullValue);
217             }
218             if ( _print.getRecordCount()>0)
219                 _print.top();
220             
221         }
222     
223     }
224     
225     /**
226      * Return configuration for auto-create recordsets defined in transaction config.xml file
227      * @return Recordset with fields: id, source and scope.
228      */

229     public Recordset getRecordsets()
230     {
231         return _rs;
232     }
233     
234     /**
235      * Return configuration for print (dtaa-binding) commands defined in transaction config.xml file
236      * @return Recordset with fields: mode, recordset, tag, control
237      */

238     public Recordset getPrintCommands()
239     {
240         return _print;
241     }
242     
243     /**
244      * Provide access to Electric XML document object
245      * @return
246      */

247     public Document getDocument()
248     {
249         return _doc;
250     }
251     
252     /**
253      * Provides easy access to custom elements in config.xml file
254      * @param tagName An element or tag name under the root element.<br>
255      * Supports XPath expressions.
256      * @return Element value
257      * @throws Throwable if the element cannot be found
258      */

259     public String JavaDoc getConfigValue(String JavaDoc tagName) throws Throwable JavaDoc
260     {
261         Element e = _root.getElement(new XPath(tagName));
262         if (e!=null)
263             return e.getString();
264         else
265             throw new Throwable JavaDoc("Configuration element not found: " + tagName);
266         
267     }
268     
269     /**
270      * Provides easy access to custom elements in config.xml file
271      * @param tagName An element or tag name under the root element.<br>
272      * Supports XPath expressions.
273      * @param value Default value - This is returned if the element cannot be found
274      * @return Element's value or default value if Element was now found
275      * @throws Throwable On XML parser exceptions
276      */

277     public String JavaDoc getConfigValue(String JavaDoc tagName, String JavaDoc value) throws Throwable JavaDoc
278     {
279         Element e = _root.getElement(new XPath(tagName));
280         if (e!=null)
281             return e.getString();
282         else
283             return value;
284         
285     }
286     
287     /**
288      * Return URI to foward on a given exit code
289      * @param exitCode
290      * @return
291      * @throws Throwable
292      */

293     public String JavaDoc getUriForExitCode(int exitCode) throws Throwable JavaDoc
294     {
295         String JavaDoc uri = null;
296         
297         Element onexit = _root.getElement( new XPath("//on-exit[@return-code='" + exitCode + "']") );
298         if (onexit!=null)
299             uri = onexit.getAttributeValue("forward-to");
300         return uri;
301     }
302
303 }
304
Popular Tags