KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > plaf > css > CSSLookAndFeel


1 /*
2  * $Id: CSSLookAndFeel.java,v 1.7 2005/05/17 15:07:33 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.plaf.css;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.wings.session.SessionManager;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 public class CSSLookAndFeel
25         extends org.wings.plaf.LookAndFeel {
26     private static final String JavaDoc WEB_INF = "WEB-INF/";
27     private final transient static Log log = LogFactory.getLog(CSSLookAndFeel.class);
28     private static final String JavaDoc PROPERTIES_FILENAME_START = CSSLookAndFeel.class.getPackage().getName();
29     private static final String JavaDoc PROPERTIES_FILENAME_END = ".properties";
30     private static final String JavaDoc PROPERTIES_CLASSPATH = PROPERTIES_FILENAME_START.replace('.','/').concat("/");
31
32     public CSSLookAndFeel() throws IOException JavaDoc {
33         super(loadProperties());
34     }
35
36     private static Properties JavaDoc loadProperties() throws IOException JavaDoc {
37         // default properties
38
StringBuffer JavaDoc propertiesFilename = new StringBuffer JavaDoc(PROPERTIES_FILENAME_START);
39         propertiesFilename.append(PROPERTIES_FILENAME_END);
40         // browser dependent properties
41
String JavaDoc browserType = SessionManager.getSession().getUserAgent().getBrowserType().getShortName();
42         StringBuffer JavaDoc browserPropertiesFilename = new StringBuffer JavaDoc(PROPERTIES_FILENAME_START);
43         browserPropertiesFilename.append(".");
44         browserPropertiesFilename.append(browserType);
45         browserPropertiesFilename.append(PROPERTIES_FILENAME_END);
46
47         Properties JavaDoc properties = new Properties JavaDoc();
48         properties = loadProperties(propertiesFilename);
49         try {
50             properties.putAll(loadProperties(browserPropertiesFilename));
51         } catch (IOException JavaDoc e) {
52             log.info("An Exception occured while loading browser specific properties. This is OK.");
53         }
54         return properties;
55     }
56
57     /**
58      * Loads properties from a file. First looks in the classPath for the
59      * default file. Throws an exception if that is not found. Then looks
60      * for an overriding file in the webapp container. If this is not found,
61      * log this info.
62      * @param propertiesFilename the name of the properties file.
63      * @throws IOException if default (in classPath) is not found.
64      * @return The Properties loaded
65      * @throws IOException
66      */

67     private static Properties JavaDoc loadProperties(StringBuffer JavaDoc propertiesFilename) throws IOException JavaDoc {
68         Properties JavaDoc properties;
69         InputStream JavaDoc in;
70         IOException JavaDoc finalException = null;
71         // first load defaults from classpath, and if it fails, throw Exception
72
final String JavaDoc classPath = PROPERTIES_CLASSPATH + propertiesFilename.toString();
73         properties = loadPropertiesFromClasspath(classPath);
74         // now load from webapp folder, log if fails.
75
String JavaDoc webappUrl = WEB_INF + propertiesFilename.toString();
76         properties.putAll(loadPropertiesFromContainer(webappUrl));
77         return properties;
78     }
79
80     /**
81      * Loads a file from the webapp's dir into a properties file.
82      * @param webappUrl the file's url
83      * @return The Properties loaded
84      */

85     private static Properties JavaDoc loadPropertiesFromContainer(String JavaDoc webappUrl) {
86         Properties JavaDoc properties = new Properties JavaDoc();
87         InputStream JavaDoc in;
88         try {
89             in = SessionManager.getSession().getServletContext().getResourceAsStream(webappUrl);
90             properties.load(in);
91             in.close();
92         } catch (Exception JavaDoc e) {
93             final String JavaDoc error = "Unable to open " + webappUrl + " due to "+e+".\nIt seems you didn't provide a custom config file.";
94             log.warn(error);
95         }
96         return properties;
97     }
98
99     /**
100      * Loads a file from the webapp's classpath into a properties file.
101      * @param classPath the file's classpath
102      * @return The Properties loaded
103      * @throws IOException
104      */

105     private static Properties JavaDoc loadPropertiesFromClasspath(final String JavaDoc classPath) throws IOException JavaDoc {
106         Properties JavaDoc properties = new Properties JavaDoc();
107         InputStream JavaDoc in;
108         try {
109             in = CSSLookAndFeel.class.getClassLoader().getResourceAsStream(classPath);
110             properties.load(in);
111             in.close();
112         } catch (Exception JavaDoc e) {
113             final String JavaDoc error = "Unable to open " + classPath + " from classPath due to "+e+".\nPlease check deployment!";
114             log.warn(error);
115             throw new IOException JavaDoc(error);
116         }
117         return properties;
118     }
119 }
120
121
122
Popular Tags