001: package net.xoetrope.xui;
002:
003: import java.io.File;
004: import java.io.InputStream;
005: import java.net.URL;
006: import java.util.Properties;
007: import java.util.Vector;
008:
009: import net.xoetrope.debug.DebugLogger;
010: import net.xoetrope.xml.XmlParserFactory;
011: import net.xoetrope.xui.build.BuildProperties;
012: import net.xoetrope.xui.data.XDataBindingFactory;
013: import net.xoetrope.xui.data.XModel;
014: import net.xoetrope.xui.helper.MessageHelper;
015: import net.xoetrope.xui.style.XStyleManager;
016:
017: /**
018: * A holder for references to the objects and resources used by an Xui project
019: * <p> Copyright (c) Xoetrope Ltd., 2002-2003</p>
020: * <p> $Revision: 1.17 $</p>
021: * <p> License: see License.txt</p>
022: */
023: public class XProject {
024: protected XStyleManager styleManager;
025: protected XResourceManager resourceManager;
026: protected XPageManager pageManager;
027: protected XModel modelRoot;
028: protected XmlParserFactory xmlParserFactory;
029: protected MessageHelper messageHelper;
030: protected Vector bindingFactories;
031:
032: protected String startupFile;
033: protected Properties startSettings;
034: protected URL documentBase;
035:
036: protected Class defaultModelClass = net.xoetrope.xui.data.XBaseModel.class;
037:
038: /**
039: * Create a new project
040: */
041: protected XProject() {
042: styleManager = null;
043: resourceManager = null;
044: pageManager = null;
045: modelRoot = null;
046: xmlParserFactory = null;
047: bindingFactories = new Vector();
048: }
049:
050: /**
051: * Setup the project and configure its resources
052: * @param startFile the startup file
053: */
054: public void initialise(String startFile) {
055: pageManager = new XPageManager();
056: resourceManager = new XResourceManager();
057: setStartupFile(startFile);
058: DebugLogger.setDebugLevel(getStartupParam("LogLevel"));
059: styleManager = new XStyleManager(10);
060: }
061:
062: //----------------------------------------------------------------------------
063:
064: /**
065: * Get a reference to the XStyleManager. A new instance is created if required
066: * @return the style manager
067: * @since 1.03
068: */
069: public XStyleManager getStyleManager() {
070: if (styleManager == null) {
071: throw new java.lang.UnsupportedOperationException(
072: "No XStyleManager instantiated, please check the startup sequence");
073: }
074:
075: return styleManager;
076: }
077:
078: /**
079: * Reset the styleManager
080: */
081: public void resetStyleManager() {
082: styleManager = null;
083: }
084:
085: //----------------------------------------------------------------------------
086:
087: //----------------------------------------------------------------------------
088: /**
089: * Gets an instance of the resource manager. A new instance is created if required
090: * @return the XResourceManager instance
091: */
092: public XResourceManager getResourceManager() {
093: if (resourceManager == null) {
094: throw new java.lang.UnsupportedOperationException(
095: "No XResourceManager instantiated, please check the startup sequence");
096: }
097:
098: return resourceManager;
099: }
100:
101: /**
102: * Sets the project's resource manager.
103: * @param rm the new resource manager
104: */
105: public void setResourceManager(XResourceManager rm) {
106: resourceManager = rm;
107: }
108:
109: /**
110: * Reset the resourceManager
111: */
112: public void resetResourceManager() {
113: resourceManager = null;
114: }
115:
116: //----------------------------------------------------------------------------
117:
118: //----------------------------------------------------------------------------
119: /**
120: * Gets an instance of the page manager. A new instance is created if required
121: * @return the XPageManager instance
122: * @since 1.03
123: */
124: public XPageManager getPageManager() {
125: if (pageManager == null) {
126: int i = 0; // Deliberately throw an exception
127: i = 10 / i;
128: }
129:
130: return pageManager;
131: }
132:
133: //----------------------------------------------------------------------------
134:
135: //----------------------------------------------------------------------------
136: /**
137: * Get the root instance of the model. This class is unique to the project so it
138: * should be unique. If no instance has been created then a new one is
139: * constructed.
140: * @return the root XModel instance.
141: */
142: public XModel getModel() {
143: if (modelRoot == null) {
144: try {
145: modelRoot = (XModel) defaultModelClass.newInstance();
146: } catch (IllegalAccessException ex) {
147: ex.printStackTrace();
148: } catch (InstantiationException ex) {
149: ex.printStackTrace();
150: }
151: modelRoot.setId("base");
152: }
153: return modelRoot;
154: }
155:
156: /**
157: * Sets the default model class. By default the XBaseModel is
158: * used. An instance is not constructed till the first call to getInstance().
159: * If an instance has been constructed then this method will have no effect.
160: * @param className the name of the model class e.g. net.xoetrope.xui.data.XBaseModel
161: */
162: public void setDefaultModel(String className) {
163: try {
164: defaultModelClass = Class.forName(className);
165: } catch (ClassNotFoundException ex) {
166: }
167: }
168:
169: /**
170: * Reset the model to its initial state. Discard all the child nodess
171: */
172: public void resetModel() {
173: modelRoot = null;
174: }
175:
176: //----------------------------------------------------------------------------
177:
178: //----------------------------------------------------------------------------
179: /**
180: * Get the XML parser factory
181: * @return the parser factory
182: */
183: public XmlParserFactory getXmlParserFactory() {
184: if (xmlParserFactory == null)
185: xmlParserFactory = new XmlParserFactory();
186:
187: return xmlParserFactory;
188: }
189:
190: //----------------------------------------------------------------------------
191:
192: //----------------------------------------------------------------------------
193: /**
194: * Get the message helper (for formatting messages)
195: * @return the message helper
196: */
197: public MessageHelper getMessageHelper() {
198: if (messageHelper == null)
199: messageHelper = new MessageHelper();
200:
201: return messageHelper;
202: }
203:
204: //----------------------------------------------------------------------------
205:
206: //----------------------------------------------------------------------------
207: /**
208: * Get the binding factories. The binding factories are requested to create
209: * component data bindings when a page is loaded from XML.
210: * @return the registered binding factories
211: */
212: public Vector getBindingsFactories() {
213: if (bindingFactories == null)
214: bindingFactories = new Vector();
215:
216: return bindingFactories;
217: }
218:
219: /**
220: * Register a binding factory with the project
221: * @param fact the new factory
222: */
223: public void registerBindingFactory(XDataBindingFactory fact) {
224: bindingFactories.addElement(fact);
225: }
226:
227: //----------------------------------------------------------------------------
228:
229: //----------------------------------------------------------------------------
230: /**
231: * Sets the startup file and loads the associated resource.
232: * @param file the name of the startup resource file.
233: */
234: public void setStartupFile(String fileName) {
235: startupFile = fileName;
236: int pos = fileName.lastIndexOf(File.separatorChar);
237: try {
238: if (pos > 0) {
239: if (BuildProperties.DEBUG) {
240: DebugLogger.trace("Startup file: " + fileName);
241: DebugLogger.trace("-- Document base: " + "file://"
242: + fileName.substring(0, pos + 1));
243: DebugLogger.trace("-- Startup file: "
244: + fileName.substring(pos + 1));
245: }
246: documentBase = new URL("file", "", XResourceManager
247: .slashify(fileName.substring(0, pos + 1), true));
248: resourceManager.setDocumentBase(documentBase);
249: startupFile = fileName.substring(pos + 1);
250: }
251: } catch (Exception ex) {
252: ex.printStackTrace();
253: }
254:
255: try {
256: InputStream is = resourceManager
257: .getBufferedInputStream(startupFile);
258: startSettings = new Properties();
259: startSettings.load(is);
260: resourceManager
261: .setDefaultEncoding(getStartupParam("DefaultEncoding"));
262: } catch (Exception e) {
263: if (BuildProperties.DEBUG)
264: DebugLogger.logError("Unable to load startup file: "
265: + fileName);
266: else
267: System.err.println(e.getMessage());
268: }
269:
270: try {
271: String ncl = getStartupParam("NumClassLoaders");
272: if (ncl != null) {
273: int numClassLoaders = new Integer(ncl).intValue();
274: for (int i = 0; i < numClassLoaders; i++) {
275: ClassLoader cl = (ClassLoader) Class.forName(
276: getStartupParam("ClassLoader" + (i + 1)))
277: .newInstance();
278: resourceManager.addCustomClassLoader(cl);
279: }
280: }
281: } catch (Exception e) {
282: if (BuildProperties.DEBUG)
283: DebugLogger.logError("Unable to load class loader: "
284: + e.getMessage());
285: else
286: System.err.println(e.getMessage());
287: }
288:
289: try {
290: String helper = getStartupParam("LayoutHelper");
291: if (helper != null) {
292: XLayoutHelper layoutHelper = (XLayoutHelper) Class
293: .forName(helper).newInstance();
294: XComponentFactory.setLayoutHelper(layoutHelper);
295: }
296: } catch (Exception e) {
297: if (BuildProperties.DEBUG)
298: DebugLogger
299: .logError("Unable to set the layout helper: "
300: + e.getMessage());
301: }
302: }
303:
304: /**
305: * Gets a startup parameter
306: * @param name the paramenter name
307: * @return the value
308: */
309: public String getStartupParam(String name) {
310: if (startSettings == null) {
311: if (BuildProperties.DEBUG)
312: DebugLogger
313: .logWarning("No startup file available: param '"
314: + name + "' not found");
315: return null;
316: }
317:
318: return startSettings.getProperty(name);
319: }
320:
321: /**
322: * Sets a startup parameter
323: * @param name the paramenter name
324: * @param value the new value
325: */
326: public void setStartupParam(String name, String value) {
327: startSettings.setProperty(name, value);
328: }
329:
330: /**
331: * Gets a startup parameter
332: * @param name the paramenter name
333: * @return the value
334: */
335: public int getStartupParamAsInt(String name) {
336: String p = startSettings.getProperty(name);
337: if (p != null)
338: return Integer.parseInt(p);
339: return 0;
340: }
341: //----------------------------------------------------------------------------
342: }
|