001: package dinamica;
002:
003: import electric.xml.*;
004: import java.sql.Types;
005:
006: /**
007: * Encapsulates access to the XML configuration
008: * of a framework transaction
009: * <br>
010: * Creation date: oct/4/2003<br>
011: * Last Update: jan/3/2005<br>
012: * (c) 2003 Martin Cordova<br>
013: * This code is released under the LGPL license<br>
014: * @author Martin Cordova
015: */
016: public class Config {
017:
018: /** xml parser objects */
019: Document _doc = null;
020: Element _root = null;
021: Element _trans = null;
022: Element _output = null;
023:
024: /** summary */
025: public String summary = null;
026:
027: /** performance logs */
028: public String mvcLog = null;
029: public String jdbcLog = null;
030:
031: /** transaction classname */
032: public String transClassName = null;
033:
034: /** transaction datasource */
035: public String transDataSource = null;
036:
037: /** transaction validator */
038: public String transValidator = null;
039:
040: /** automatic transactions */
041: public String transTransactions = null;
042:
043: /** auto-create recordset definitions */
044: Recordset _rs = new Recordset();
045:
046: /** output classname */
047: public String outClassName = null;
048:
049: /** output template */
050: public String outTemplate = null;
051:
052: /** output content-type */
053: public String contentType = null;
054:
055: /** output expiration */
056: public String expiration = null;
057:
058: /** flag that indicates if http headers shoud be set */
059: public String headers = null;
060:
061: /** specific request encoding for the current Action */
062: public String requestEncoding = null;
063:
064: /** encoding used to read template files */
065: public String templateEncoding = null;
066:
067: /** output data binding commands */
068: Recordset _print = new Recordset();
069:
070: /** transaction path */
071: public String path = null;
072:
073: /** customized error handler for this action -if any- */
074: public String onErrorAction = null;
075:
076: /** save request parameters as session attributes using their names as IDs */
077: public String validatorInSession = null;
078:
079: /**
080: * Default constructor
081: * @param xmlData Transaction configuration data in XML
082: * @param path Transaction directory relative to the context
083: * @throws Throwable
084: */
085: public Config(String xmlData, String path) throws Throwable {
086:
087: /* remember path */
088: this .path = path;
089:
090: /* create basic xml objects */
091: _doc = new Document(xmlData);
092: _root = _doc.getRoot();
093: _trans = _root.getElement("transaction");
094: _output = _root.getElement("output");
095:
096: Element encoding = _root.getElement("request-encoding");
097: if (encoding != null)
098: requestEncoding = encoding.getTextString();
099:
100: //patch 2006-01-03 - custom error handler for this action
101: Element onError = _root.getElement("on-error");
102: if (onError != null)
103: onErrorAction = onError.getTextString();
104: //end of patch
105:
106: /* summary */
107: summary = _root.getElement("summary").getString();
108:
109: /* MVC logs */
110: mvcLog = _root.getElement("log").getString();
111:
112: /* structure of the recordset */
113: _rs.append("id", Types.VARCHAR);
114: _rs.append("source", Types.VARCHAR);
115: _rs.append("scope", Types.VARCHAR);
116: _rs.append("onempty", Types.VARCHAR);
117: _rs.append("maxrows", Types.VARCHAR);
118: _rs.append("datasource", Types.VARCHAR);
119: _rs.append("params", Types.VARCHAR);
120:
121: /* structure of the recordset */
122: _print.append("mode", Types.VARCHAR);
123: _print.append("recordset", Types.VARCHAR);
124: _print.append("tag", Types.VARCHAR);
125: _print.append("control", Types.VARCHAR);
126: _print.append("pagesize", Types.VARCHAR);
127: _print.append("alternate-colors", Types.VARCHAR);
128: _print.append("null-value", Types.VARCHAR);
129:
130: /* read transaction configuration */
131: if (_trans != null) {
132: Element e = _trans.getElement("datasource");
133: if (e != null)
134: transDataSource = e.getString();
135:
136: transClassName = _trans.getElement("classname").getString();
137: transValidator = _trans.getElement("validator").getString();
138: transTransactions = _trans.getElement("transaction")
139: .getString();
140: jdbcLog = _trans.getElement("jdbc-log").getString();
141:
142: Element val = _trans.getElement("validator");
143: validatorInSession = val.getAttribute("session");
144:
145: /* any auto-create recordsets? */
146: Elements rsElems = _trans.getElements("recordset");
147: while (rsElems.hasMoreElements()) {
148: _rs.addNew();
149: Element rs = rsElems.next();
150: String id = rs.getAttribute("id");
151: String mode = rs.getAttribute("source");
152: String scope = rs.getAttribute("scope");
153: String onempty = rs.getAttribute("on-empty-return");
154: String maxrows = rs.getAttribute("max-rows");
155: String rsDataSrc = rs.getAttribute("datasource");
156: if (rsDataSrc != null && rsDataSrc.trim().equals(""))
157: rsDataSrc = null;
158: String rsParams = rs.getAttribute("params");
159: if (rsParams != null && rsParams.trim().equals(""))
160: rsParams = null;
161:
162: _rs.setValue("id", id);
163: _rs.setValue("source", mode);
164: _rs.setValue("scope", scope);
165: _rs.setValue("onempty", onempty);
166: _rs.setValue("maxrows", maxrows);
167: _rs.setValue("datasource", rsDataSrc);
168: _rs.setValue("params", rsParams);
169: }
170: if (_rs.getRecordCount() > 0)
171: _rs.top();
172: }
173:
174: /* read transaction configuration */
175: if (_output != null) {
176: Element x = null;
177: outClassName = _output.getElement("classname").getString();
178:
179: //PATCH 2005-02-17 encoding support
180: x = _output.getElement("template");
181: if (x != null) {
182: templateEncoding = x.getAttribute("file-encoding");
183: if (templateEncoding != null
184: && templateEncoding.trim().equals(""))
185: templateEncoding = null;
186: outTemplate = x.getString();
187: }
188:
189: x = _output.getElement("content-type");
190: if (x != null) {
191: contentType = x.getString();
192: //PATCH 2005-02-17 encoding support
193: if (contentType.indexOf("charset") < 0
194: && templateEncoding != null)
195: contentType = contentType + "; charset="
196: + templateEncoding;
197: }
198:
199: x = _output.getElement("expiration");
200: if (x != null)
201: expiration = x.getString();
202: x = _output.getElement("set-http-headers");
203: if (x != null)
204: headers = x.getString();
205: else
206: headers = "false";
207:
208: /* any auto-create recordsets? */
209: Elements rsElems = _output.getElements("print");
210: while (rsElems.hasMoreElements()) {
211: _print.addNew();
212: Element rs = rsElems.next();
213: String mode = rs.getAttribute("mode");
214: String rset = rs.getAttribute("recordset");
215: String tag = rs.getAttribute("tag");
216: String control = rs.getAttribute("control");
217: String pagesize = rs.getAttribute("pagesize");
218: String altColors = rs.getAttribute("alternate-colors");
219: String nullValue = rs.getAttribute("null-value");
220:
221: _print.setValue("mode", mode);
222: _print.setValue("recordset", rset);
223: _print.setValue("tag", tag);
224: _print.setValue("control", control);
225: _print.setValue("pagesize", pagesize);
226: _print.setValue("alternate-colors", altColors);
227: _print.setValue("null-value", nullValue);
228: }
229: if (_print.getRecordCount() > 0)
230: _print.top();
231:
232: }
233:
234: }
235:
236: /**
237: * Return configuration for auto-create recordsets defined in transaction config.xml file
238: * @return Recordset with fields: id, source and scope.
239: */
240: public Recordset getRecordsets() {
241: return _rs;
242: }
243:
244: /**
245: * Return configuration for print (dtaa-binding) commands defined in transaction config.xml file
246: * @return Recordset with fields: mode, recordset, tag, control
247: */
248: public Recordset getPrintCommands() {
249: return _print;
250: }
251:
252: /**
253: * Provide access to Electric XML document object
254: * @return
255: */
256: public Document getDocument() {
257: return _doc;
258: }
259:
260: /**
261: * Provides easy access to custom elements in config.xml file
262: * @param tagName An element or tag name under the root element.<br>
263: * Supports XPath expressions.
264: * @return Element value
265: * @throws Throwable if the element cannot be found
266: */
267: public String getConfigValue(String tagName) throws Throwable {
268: Element e = _root.getElement(new XPath(tagName));
269: if (e != null)
270: return e.getString();
271: else
272: throw new Throwable("Configuration element not found: "
273: + tagName);
274:
275: }
276:
277: /**
278: * Provides easy access to custom elements in config.xml file
279: * @param tagName An element or tag name under the root element.<br>
280: * Supports XPath expressions.
281: * @param value Default value - This is returned if the element cannot be found
282: * @return Element's value or default value if Element was now found
283: * @throws Throwable On XML parser exceptions
284: */
285: public String getConfigValue(String tagName, String value)
286: throws Throwable {
287: Element e = _root.getElement(new XPath(tagName));
288: if (e != null)
289: return e.getString();
290: else
291: return value;
292:
293: }
294:
295: /**
296: * Return URI to foward on a given exit code
297: * @param exitCode
298: * @return
299: * @throws Throwable
300: */
301: public String getUriForExitCode(int exitCode) throws Throwable {
302: String uri = null;
303:
304: Element onexit = _root.getElement(new XPath(
305: "//on-exit[@return-code='" + exitCode + "']"));
306: if (onexit != null)
307: uri = onexit.getAttributeValue("forward-to");
308: return uri;
309: }
310:
311: }
|