001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of jWebApp.
006: *
007: * jWebApp is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * jWebApp is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with jWebApp; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jwebapp.controller;
020:
021: import jcommontk.utils.StringUtils;
022: import jcommontk.utils.XMLParser;
023: import jcommontk.utils.XMLParserException;
024: import java.io.IOException;
025: import java.net.URL;
026: import java.util.Hashtable;
027: import java.util.Vector;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.NodeList;
030:
031: @SuppressWarnings("unchecked")
032: // working to complete a Java 1.5 version
033: class XMLDataManager extends DataManager {
034: private URL fileUrl;
035: private boolean validate;
036: private Hashtable validationHash;
037: private Vector validationVector;
038: private Hashtable forwardHash;
039: private Hashtable messageHash;
040: private Hashtable requestHash;
041: private Vector requestRegExpVector;
042: private XMLDataParser parser = new XMLDataParser();
043:
044: XMLDataManager(URL fileUrl, boolean validate) throws Exception {
045: this .fileUrl = fileUrl;
046: this .validate = validate;
047:
048: loadData();
049: }
050:
051: boolean loadData() throws Exception {
052: this .validationHash = new Hashtable();
053: this .validationVector = new Vector();
054: forwardHash = new Hashtable();
055: this .messageHash = new Hashtable();
056: this .requestHash = new Hashtable();
057: requestRegExpVector = new Vector();
058:
059: parser.loadXMLData(fileUrl, validate);
060:
061: super .requestHash = this .requestHash;
062: super .requestRegularExpressionVector = requestRegExpVector;
063: super .urlDataHash = forwardHash;
064: super .validationHash = this .validationHash;
065: super .validationVector = this .validationVector;
066: super .messageHash = this .messageHash;
067:
068: return true;
069: }
070:
071: class XMLDataParser extends XMLParser {
072: public void loadXMLData(URL fileUrl, boolean validate)
073: throws IOException, XMLParserException {
074: loadData(fileUrl, validate);
075: }
076:
077: public void processXML(Element e) throws Exception {
078: loadRequests(e);
079: loadUrlForwardsRedirects(e, "jwaUrl", false);
080: loadUrlForwardsRedirects(e, "forward", false);
081: loadUrlForwardsRedirects(e, "redirect", true);
082: loadValidation(e);
083: loadMessages(e);
084: }
085: }
086:
087: void loadRequests(Element e) throws JWebAppException,
088: NoSuchMethodException, ClassNotFoundException,
089: InstantiationException, IllegalAccessException {
090: NodeList nl = e.getElementsByTagName("request");
091:
092: for (int i = 0; i < nl.getLength(); i++) {
093: Element e2 = (Element) nl.item(i);
094:
095: String requestId = StringUtils.emptyToNull(e2
096: .getAttribute("id")), match = StringUtils
097: .emptyToNull(e2.getAttribute("match")), regularExpression = StringUtils
098: .emptyToNull(e2.getAttribute("matchRegExp")), isSecure = StringUtils
099: .emptyToNull(e2.getAttribute("secureConnection")), handlerClass = StringUtils
100: .emptyToNull(e2.getAttribute("handlerClass")), handlerMethod = StringUtils
101: .emptyToNull(e2.getAttribute("handlerMethod"));
102:
103: if (handlerMethod != null) {
104: if (handlerMethod.toLowerCase().startsWith("process")
105: && handlerMethod.length() > "process".length())
106: handlerMethod = handlerMethod.substring("process"
107: .length());
108:
109: handlerMethod = Character.toUpperCase(handlerMethod
110: .charAt(0))
111: + handlerMethod.substring(1);
112: }
113:
114: RequestData r = new RequestData(requestId, match,
115: regularExpression, handlerClass, handlerMethod,
116: new Boolean(isSecure == null ? "false" : isSecure)
117: .booleanValue(), true);
118:
119: loadRequestParameters(r, e2);
120: loadRequestRoles(r, e2);
121: loadRequestForwards(r, e2);
122: loadRequestRedirects(r, e2);
123: loadRequestValidation(r, e2);
124:
125: if (requestId == null)
126: throw new JWebAppException(
127: "Request can not have null request Id");
128:
129: if (regularExpression != null)
130: requestRegExpVector.addElement(r);
131: else
132: this .requestHash.put(match != null ? match : requestId,
133: r);
134: }
135: }
136:
137: void loadRequestParameters(RequestData r, Element e) {
138: NodeList nl = e.getElementsByTagName("parameter");
139:
140: for (int i = 0; i < nl.getLength(); i++)
141: r.getParameterHash().put(
142: ((Element) nl.item(i)).getAttribute("name"),
143: ((Element) nl.item(i)).getAttribute("value"));
144: }
145:
146: void loadRequestRoles(RequestData r, Element e) {
147: NodeList nl = e.getElementsByTagName("security");
148:
149: for (int i = 0; i < nl.getLength(); i++)
150: r.getRoleSet().add(
151: ((Element) nl.item(i)).getAttribute("role"));
152: }
153:
154: void loadRequestForwards(RequestData r, Element e)
155: throws JWebAppException {
156: NodeList nl = e.getElementsByTagName("forward");
157:
158: for (int i = 0; i < nl.getLength(); i++) {
159: String key = StringUtils.emptyToNull(((Element) nl.item(i))
160: .getAttribute("key")), forwardId = StringUtils
161: .emptyToNull(((Element) nl.item(i))
162: .getAttribute("id"));
163:
164: if (key == null || forwardId == null)
165: throw new JWebAppException(
166: "Forward can not have null key (=" + key
167: + ") or Id (=" + forwardId + ")");
168:
169: r.getUrlDataHash().put(key, forwardId);
170: }
171:
172: nl = e.getElementsByTagName("forwardReference");
173:
174: for (int i = 0; i < nl.getLength(); i++) {
175: String key = StringUtils.emptyToNull(((Element) nl.item(i))
176: .getAttribute("key")), forwardId = StringUtils
177: .emptyToNull(((Element) nl.item(i))
178: .getAttribute("idRef"));
179:
180: if (key == null || forwardId == null)
181: throw new JWebAppException(
182: "Forward reference can not have null key (="
183: + key + ") or Id (=" + forwardId + ")");
184:
185: r.getUrlDataHash().put(key, forwardId);
186: }
187: }
188:
189: void loadRequestRedirects(RequestData r, Element e)
190: throws JWebAppException {
191: NodeList nl = e.getElementsByTagName("redirect");
192:
193: for (int i = 0; i < nl.getLength(); i++) {
194: String key = StringUtils.emptyToNull(((Element) nl.item(i))
195: .getAttribute("key")), redirectId = StringUtils
196: .emptyToNull(((Element) nl.item(i))
197: .getAttribute("id"));
198:
199: if (key == null || redirectId == null)
200: throw new JWebAppException(
201: "Redirect can not have null key (=" + key
202: + ") or Id (=" + redirectId + ")");
203:
204: r.getUrlDataHash().put(key, redirectId);
205: }
206:
207: nl = e.getElementsByTagName("redirectReference");
208:
209: for (int i = 0; i < nl.getLength(); i++) {
210: String key = StringUtils.emptyToNull(((Element) nl.item(i))
211: .getAttribute("key")), redirectId = StringUtils
212: .emptyToNull(((Element) nl.item(i))
213: .getAttribute("idRef"));
214:
215: if (key == null || redirectId == null)
216: throw new JWebAppException(
217: "Redirect reference can not have null key (="
218: + key + ") or Id (=" + redirectId + ")");
219:
220: r.getUrlDataHash().put(key, redirectId);
221: }
222: }
223:
224: void loadRequestValidation(RequestData r, Element e)
225: throws JWebAppException {
226: NodeList nl = e.getElementsByTagName("validation");
227:
228: for (int i = 0; i < nl.getLength(); i++) {
229: String validationId = StringUtils.emptyToNull(((Element) nl
230: .item(i)).getAttribute("id")), parameterName = StringUtils
231: .emptyToNull(((Element) nl.item(i))
232: .getAttribute("parameterName"));
233:
234: if (validationId == null || parameterName == null)
235: throw new JWebAppException(
236: "Validation can not have null validation Id (="
237: + validationId
238: + ") or parameter name (="
239: + parameterName + ")");
240:
241: r.getValidationVector().addElement(
242: parameterName + "|" + validationId);
243: }
244:
245: nl = e.getElementsByTagName("validationReference");
246:
247: for (int i = 0; i < nl.getLength(); i++) {
248: String validationId = StringUtils.emptyToNull(((Element) nl
249: .item(i)).getAttribute("idRef")), parameterName = StringUtils
250: .emptyToNull(((Element) nl.item(i))
251: .getAttribute("parameterName"));
252:
253: if (validationId == null || parameterName == null)
254: throw new JWebAppException(
255: "Validation reference can not have null validation Id (="
256: + validationId
257: + ") or parameter name (="
258: + parameterName + ")");
259:
260: r.getValidationVector().addElement(
261: parameterName + "|" + validationId);
262: }
263: }
264:
265: void loadUrlForwardsRedirects(Element e, String xmlSearchElement,
266: boolean isRedirect) throws JWebAppException {
267: NodeList nl = e.getElementsByTagName(xmlSearchElement);
268:
269: for (int i = 0; i < nl.getLength(); i++) {
270: String forwardId = StringUtils.emptyToNull(((Element) nl
271: .item(i)).getAttribute("id")), protocol = StringUtils
272: .emptyToNull(((Element) nl.item(i))
273: .getAttribute("protocol")), url = StringUtils
274: .emptyToNull(((Element) nl.item(i))
275: .getAttribute("url")), isRewrite = StringUtils
276: .emptyToNull(((Element) nl.item(i))
277: .getAttribute("rewrite"));
278:
279: if (forwardId == null || url == null)
280: throw new JWebAppException(
281: "Forwards, Redirects and Urls can not have null forward Id (="
282: + forwardId + ") or URL (=" + url + ")");
283:
284: forwardHash.put(forwardId, new UrlData(forwardId, protocol,
285: url, isRedirect, new Boolean(
286: isRewrite == null ? "true" : isRewrite)
287: .booleanValue()));
288: }
289: }
290:
291: void loadValidation(Element e) throws JWebAppException {
292: NodeList nl = e.getElementsByTagName("validation");
293:
294: for (int i = 0; i < nl.getLength(); i++) {
295: String validationId = StringUtils.emptyToNull(((Element) nl
296: .item(i)).getAttribute("id")), type = StringUtils
297: .emptyToNull(((Element) nl.item(i))
298: .getAttribute("type")), arguments = StringUtils
299: .emptyToNull(((Element) nl.item(i))
300: .getAttribute("arguments")), errorMessage = StringUtils
301: .emptyToNull(((Element) nl.item(i))
302: .getAttribute("errorMessage"));
303:
304: if (validationId == null || type == null)
305: throw new JWebAppException(
306: "Validation can not have null validation Id (="
307: + validationId + ")or type (=" + type
308: + ")");
309:
310: this .validationHash.put(validationId, new ValidationData(
311: validationId, type, arguments, errorMessage));
312: this .validationVector.addElement(new ValidationData(
313: validationId, type, arguments, errorMessage));
314: }
315: }
316:
317: void loadMessages(Element e) {
318: NodeList nl = e.getElementsByTagName("message");
319:
320: for (int i = 0; i < nl.getLength(); i++) {
321: String messageId = StringUtils.emptyToNull(((Element) nl
322: .item(i)).getAttribute("id")), value = StringUtils
323: .emptyToNull(((Element) nl.item(i))
324: .getAttribute("value"));
325:
326: this.messageHash.put(messageId, value);
327: }
328: }
329: }
|