KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > componentxml > StyleSheetLoader


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.componentxml;
31
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import javax.xml.parsers.DocumentBuilder JavaDoc;
38 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
39 import javax.xml.parsers.ParserConfigurationException JavaDoc;
40
41 import nextapp.echo2.app.DerivedMutableStyle;
42 import nextapp.echo2.app.MutableStyleSheet;
43 import nextapp.echo2.app.Style;
44 import nextapp.echo2.app.StyleSheet;
45 import nextapp.echo2.app.util.DomUtil;
46 import org.w3c.dom.Document JavaDoc;
47 import org.w3c.dom.Element JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
49
50 /**
51  * Loads XML style sheets.
52  */

53 public class StyleSheetLoader {
54     
55     /**
56      * Parses an XML style sheet and returns a <code>StyleSheet</code>
57      * instance.
58      * <p>
59      * Styles for components that cannot be loaded by the specified
60      * <code>ClassLoader</code> will be ignored.
61      *
62      * @param resourceName the name of the resource on the
63      * <code>CLASSPATH</code> containing the XML data
64      * @param classLoader the <code>ClassLoader</code> with which to
65      * instantiate property objects
66      * @return the created <code>StyleSheet</code> or null if the resource
67      * does not exist
68      * @throws ComponentXmlException if parsing/instantiation errors occur
69      */

70     public static StyleSheet load(String JavaDoc resourceName, ClassLoader JavaDoc classLoader)
71     throws ComponentXmlException {
72         InputStream JavaDoc in = null;
73         try {
74             in = classLoader.getResourceAsStream(resourceName);
75             if (in == null) {
76                 return null;
77             }
78             return load(in, classLoader);
79         } finally {
80             if (in != null) { try { in.close(); } catch (IOException JavaDoc ex) { } }
81         }
82     }
83
84     /**
85      * Parses an XML style sheet and returns a <code>StyleSheet</code>
86      * instance.
87      * <p>
88      * Styles for components that cannot be loaded by the specified
89      * <code>ClassLoader</code> will be ignored.
90      *
91      * @param in the <code>InputStream</code> containing the XML data
92      * @param classLoader the <code>ClassLoader</code> with which to
93      * instantiate property objects
94      * @return the created <code>StyleSheet</code>
95      * @throws ComponentXmlException if parsing/instantiation errors occur
96      */

97     public static StyleSheet load(InputStream JavaDoc in, ClassLoader JavaDoc classLoader)
98     throws ComponentXmlException {
99         Document JavaDoc document;
100         try {
101             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
102             factory.setNamespaceAware(true);
103             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
104             document = builder.parse(in);
105         } catch (IOException JavaDoc ex) {
106             throw new ComponentXmlException("Failed to parse InputStream.", ex);
107         } catch (ParserConfigurationException JavaDoc ex) {
108             throw new ComponentXmlException("Failed to parse InputStream.", ex);
109         } catch (SAXException JavaDoc ex) {
110             throw new ComponentXmlException("Failed to parse InputStream.", ex);
111         }
112         
113         PropertyLoader propertyLoader = PropertyLoader.forClassLoader(classLoader);
114
115         Map JavaDoc namedStyleMap = new HashMap JavaDoc();
116         
117         MutableStyleSheet styleSheet = new MutableStyleSheet();
118         Element JavaDoc styleSheetElement = document.getDocumentElement();
119         Element JavaDoc[] styleElements = DomUtil.getChildElementsByTagName(styleSheetElement, "style");
120         
121         // First pass, load style information.
122
for (int i = 0; i < styleElements.length; ++i) {
123             String JavaDoc name = styleElements[i].getAttribute("name");
124             if (!styleElements[i].hasAttribute("type")) {
125                 throw new ComponentXmlException("Component type not specified in style: " + name, null);
126             }
127             String JavaDoc type = styleElements[i].getAttribute("type");
128             
129             Class JavaDoc componentClass;
130             try {
131                 componentClass = Class.forName(type, true, classLoader);
132             } catch (ClassNotFoundException JavaDoc ex) {
133                 // StyleSheet contains reference to Component which does not exist in this ClassLoader,
134
// and thus should be ignored.
135
continue;
136             }
137             
138             DerivedMutableStyle style = new DerivedMutableStyle();
139             
140             Element JavaDoc propertiesElement = DomUtil.getChildElementByTagName(styleElements[i], "properties");
141             Style propertyStyle = propertyLoader.createStyle(propertiesElement, type);
142             style.addStyleContent(propertyStyle);
143
144             Map JavaDoc classToStyleMap = (Map JavaDoc) namedStyleMap.get(name);
145             if (classToStyleMap == null) {
146                 classToStyleMap = new HashMap JavaDoc();
147                 namedStyleMap.put(name, classToStyleMap);
148             }
149             classToStyleMap.put(componentClass, style);
150             
151             styleSheet.addStyle(componentClass, name, style);
152         }
153         
154         // Second pass, bind derived styles to base styles where applicable.
155
for (int i = 0; i < styleElements.length; ++i) {
156             if (styleElements[i].hasAttribute("base-name")) {
157                 String JavaDoc name = styleElements[i].getAttribute("name");
158                 String JavaDoc type = styleElements[i].getAttribute("type");
159                 Class JavaDoc componentClass;
160                 try {
161                     componentClass = Class.forName(type, true, classLoader);
162                 } catch (ClassNotFoundException JavaDoc ex) {
163                     // StyleSheet contains reference to Component which does not exist in this ClassLoader,
164
// and thus should be ignored.
165
continue;
166                 }
167
168                 Map JavaDoc classToStyleMap = (Map JavaDoc) namedStyleMap.get(name);
169                 DerivedMutableStyle style = (DerivedMutableStyle) classToStyleMap.get(componentClass);
170                 
171                 String JavaDoc baseName = styleElements[i].getAttribute("base-name");
172                 
173                 classToStyleMap = (Map JavaDoc) namedStyleMap.get(baseName);
174                 if (classToStyleMap == null) {
175                     throw new ComponentXmlException("Invalid base style name for style name " + name + ".", null);
176                 }
177                 Style baseStyle = (Style) classToStyleMap.get(componentClass);
178                 while (baseStyle == null && componentClass != Object JavaDoc.class) {
179                     componentClass = componentClass.getSuperclass();
180                     baseStyle = (Style) classToStyleMap.get(componentClass);
181                 }
182                 if (baseStyle == null) {
183                     throw new ComponentXmlException("Invalid base style name for style name " + name + ".", null);
184                 }
185                 
186                 style.setParentStyle(baseStyle);
187             }
188         }
189     
190         return styleSheet;
191     }
192 }
193
Popular Tags