001: /*
002: * Copyright 2005 jWic group (http://www.jwic.de)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: * de.jwic.base.ApplicationSetup
017: * Created on 23.03.2005
018: * $Id: XmlApplicationSetup.java,v 1.6 2006/08/14 09:34:59 lordsam Exp $
019: */
020: package de.jwic.base;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.lang.reflect.Constructor;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Map;
029:
030: import org.dom4j.Document;
031: import org.dom4j.Element;
032: import org.dom4j.io.SAXReader;
033: import org.xml.sax.InputSource;
034:
035: import de.jwic.util.DTDEntityResolver;
036:
037: /**
038: * Loads the application setup from an xml file.
039: *
040: * @author Florian Lippisch
041: * @version $Revision: 1.6 $
042: */
043: public class XmlApplicationSetup implements IApplicationSetup {
044:
045: public final static String PUBLICID = "-//jWic//DTD xwic 3.0//EN";
046: public final static String SYSTEMID = "http://www.jwic.de/dtd/xwic-3.0.dtd";
047: public final static String DTD_RESOURCEPATH = "/de/jwic/base/xwic.dtd";
048:
049: private final static long serialVersionUID = 6009335074727417445L;
050:
051: private final static String NODE_NAME = "name";
052: private final static String NODE_CLASS = "class";
053: private final static String NODE_ROOTCONTROL = "rootcontrol";
054: private final static String NODE_SERIALIZABLE = "serializable";
055: private final static String NODE_SINGLESESSION = "singlesession";
056: private final static String NODE_USEAJAX = "useAjaxRendering";
057: private final static String NODE_REQUIREAUTH = "requireauth";
058: private final static String NODE_PROPERTY = "property";
059: private final static String ATTR_NAME = "name";
060: private final static String ATTR_CLASSNAME = "classname";
061:
062: /** The name of the application */
063: private String name = null;
064: /** The classname of the root control */
065: private String rootControlClass = null;
066: /** The name of the root control. Default is "root" */
067: private String rootControlName = "root";
068: /** The name of the application class */
069: private String appClassName = null;
070:
071: private boolean serializable = true;
072: private boolean singleSession = false;
073: private boolean requireAuth = false;
074: private boolean useAjaxRendering = true;
075: private Map properties = null;
076:
077: /**
078: * Create an ApplicationSetup from the specified file.
079: * @param filename
080: */
081: public XmlApplicationSetup(String filename) {
082:
083: try {
084: SAXReader reader = new SAXReader();
085: reader.setEntityResolver(new DTDEntityResolver(PUBLICID,
086: SYSTEMID, DTD_RESOURCEPATH));
087: reader.setIncludeExternalDTDDeclarations(false);
088:
089: Document document = reader.read(new File(filename));
090:
091: readDocument(document);
092:
093: } catch (Exception e1) {
094: throw new RuntimeException(
095: "Error reading applicationSetup: " + e1, e1);
096: }
097:
098: }
099:
100: /**
101: * Create an ApplicationSetup from the specified stream.
102: * @param filename
103: */
104: public XmlApplicationSetup(InputStream stream) {
105:
106: try {
107: SAXReader reader = new SAXReader();
108: reader.setEntityResolver(new DTDEntityResolver(PUBLICID,
109: SYSTEMID, DTD_RESOURCEPATH));
110: reader.setIncludeExternalDTDDeclarations(false);
111:
112: Document document = reader.read(stream);
113:
114: readDocument(document);
115:
116: } catch (Exception e1) {
117: throw new RuntimeException(
118: "Error reading applicationSetup: " + e1, e1);
119: }
120:
121: }
122:
123: /**
124: * @param source
125: * @throws IOException
126: */
127: public XmlApplicationSetup(InputSource source) {
128:
129: try {
130: SAXReader reader = new SAXReader();
131: reader.setEntityResolver(new DTDEntityResolver(PUBLICID,
132: SYSTEMID, DTD_RESOURCEPATH));
133: reader.setIncludeExternalDTDDeclarations(false);
134:
135: Document document = reader.read(source);
136:
137: readDocument(document);
138:
139: } catch (Exception e1) {
140: throw new RuntimeException(
141: "Error reading applicationSetup: " + e1, e1);
142: }
143: }
144:
145: /**
146: * Read the document content.
147: * @param document
148: */
149: private void readDocument(Document document) {
150:
151: Element root = document.getRootElement();
152: for (Iterator it = root.elementIterator(); it.hasNext();) {
153: Element node = (Element) it.next();
154: String nodeName = node.getName();
155: if (nodeName.equals(NODE_NAME)) {
156: name = node.getText();
157: } else if (nodeName.equals(NODE_CLASS)) {
158: appClassName = node.getText();
159: } else if (nodeName.equals(NODE_ROOTCONTROL)) {
160: rootControlName = node.attribute(ATTR_NAME).getValue();
161: rootControlClass = node.attribute(ATTR_CLASSNAME)
162: .getValue();
163: } else if (nodeName.equals(NODE_SERIALIZABLE)) {
164: String s = node.getText();
165: serializable = "true".equals(s) | "1".equals(s)
166: | "on".equals(s);
167: } else if (nodeName.equals(NODE_SINGLESESSION)) {
168: String s = node.getText();
169: singleSession = "true".equals(s) | "1".equals(s)
170: | "on".equals(s);
171: } else if (nodeName.equals(NODE_REQUIREAUTH)) {
172: String s = node.getText();
173: requireAuth = "true".equals(s) | "1".equals(s)
174: | "on".equals(s);
175: } else if (nodeName.equals(NODE_USEAJAX)) {
176: String s = node.getText();
177: useAjaxRendering = "true".equals(s) | "1".equals(s)
178: | "on".equals(s);
179: } else if (nodeName.equals(NODE_PROPERTY)) {
180: if (properties == null) {
181: properties = new HashMap();
182: }
183: properties.put(node.attribute(ATTR_NAME).getValue(),
184: node.getText());
185: }
186: }
187:
188: }
189:
190: /* (non-Javadoc)
191: * @see de.jwic.base.IApplicationSetup#getName()
192: */
193: public String getName() {
194: return name;
195: }
196:
197: /* (non-Javadoc)
198: * @see de.jwic.base.IApplicationSetup#getRootControlName()
199: */
200: public String getRootControlName() {
201: return rootControlName;
202: }
203:
204: /**
205: * Returns the classname of the root control.
206: * @return
207: */
208: public String getRootControlClass() {
209: return rootControlClass;
210: }
211:
212: /* (non-Javadoc)
213: * @see de.jwic.base.IApplicationSetup#createApplication()
214: */
215: public IApplication createApplication() {
216:
217: try {
218: if (appClassName != null) {
219: return (IApplication) Class.forName(appClassName)
220: .newInstance();
221: }
222: // create a dummy application that creates the root class. This is
223: // for compatibility with jWic v2.x.
224: IApplication app = new Application() {
225: private static final long serialVersionUID = 1L;
226:
227: public Control createRootControl(
228: IControlContainer container) {
229: try {
230: Class clazz = Class.forName(rootControlClass);
231: Control control;
232: Constructor cstr = clazz
233: .getConstructor(new Class[] {
234: IControlContainer.class,
235: String.class });
236: control = (Control) cstr
237: .newInstance(new Object[] { container,
238: rootControlName });
239: return control;
240: } catch (Exception e) {
241: throw new ControlNotAvailableException(
242: "Can not create instance of '"
243: + rootControlClass
244: + "'. Cause: " + e, e);
245: }
246: }
247: };
248: return app;
249: } catch (Exception e) {
250: throw new JWicException("Can not create application '"
251: + appClassName + "':" + e, e);
252: }
253: }
254:
255: /* (non-Javadoc)
256: * @see de.jwic.base.IApplicationSetup#isRequireAuthentication()
257: */
258: public boolean isRequireAuthentication() {
259: return requireAuth;
260: }
261:
262: /* (non-Javadoc)
263: * @see de.jwic.base.IApplicationSetup#isSerializable()
264: */
265: public boolean isSerializable() {
266: return serializable;
267: }
268:
269: /* (non-Javadoc)
270: * @see de.jwic.base.IApplicationSetup#isSingleSession()
271: */
272: public boolean isSingleSession() {
273: return singleSession;
274: }
275:
276: /* (non-Javadoc)
277: * @see de.jwic.base.IApplicationSetup#getProperty(java.lang.String)
278: */
279: public String getProperty(String key) {
280: if (properties == null) {
281: return null;
282: }
283: return (String) properties.get(key);
284: }
285:
286: /* (non-Javadoc)
287: * @see de.jwic.base.IApplicationSetup#isUseAjaxRendering()
288: */
289: public boolean isUseAjaxRendering() {
290: return useAjaxRendering;
291: }
292:
293: /**
294: * @return Returns the appClassName.
295: */
296: public String getAppClassName() {
297: return appClassName;
298: }
299:
300: }
|