1 29 30 package nextapp.echo2.app.componentxml; 31 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.util.HashMap ; 35 import java.util.Map ; 36 37 import javax.xml.parsers.DocumentBuilder ; 38 import javax.xml.parsers.DocumentBuilderFactory ; 39 import javax.xml.parsers.ParserConfigurationException ; 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 ; 47 import org.w3c.dom.Element ; 48 import org.xml.sax.SAXException ; 49 50 53 public class StyleSheetLoader { 54 55 70 public static StyleSheet load(String resourceName, ClassLoader classLoader) 71 throws ComponentXmlException { 72 InputStream 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 ex) { } } 81 } 82 } 83 84 97 public static StyleSheet load(InputStream in, ClassLoader classLoader) 98 throws ComponentXmlException { 99 Document document; 100 try { 101 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 102 factory.setNamespaceAware(true); 103 DocumentBuilder builder = factory.newDocumentBuilder(); 104 document = builder.parse(in); 105 } catch (IOException ex) { 106 throw new ComponentXmlException("Failed to parse InputStream.", ex); 107 } catch (ParserConfigurationException ex) { 108 throw new ComponentXmlException("Failed to parse InputStream.", ex); 109 } catch (SAXException ex) { 110 throw new ComponentXmlException("Failed to parse InputStream.", ex); 111 } 112 113 PropertyLoader propertyLoader = PropertyLoader.forClassLoader(classLoader); 114 115 Map namedStyleMap = new HashMap (); 116 117 MutableStyleSheet styleSheet = new MutableStyleSheet(); 118 Element styleSheetElement = document.getDocumentElement(); 119 Element [] styleElements = DomUtil.getChildElementsByTagName(styleSheetElement, "style"); 120 121 for (int i = 0; i < styleElements.length; ++i) { 123 String 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 type = styleElements[i].getAttribute("type"); 128 129 Class componentClass; 130 try { 131 componentClass = Class.forName(type, true, classLoader); 132 } catch (ClassNotFoundException ex) { 133 continue; 136 } 137 138 DerivedMutableStyle style = new DerivedMutableStyle(); 139 140 Element propertiesElement = DomUtil.getChildElementByTagName(styleElements[i], "properties"); 141 Style propertyStyle = propertyLoader.createStyle(propertiesElement, type); 142 style.addStyleContent(propertyStyle); 143 144 Map classToStyleMap = (Map ) namedStyleMap.get(name); 145 if (classToStyleMap == null) { 146 classToStyleMap = new HashMap (); 147 namedStyleMap.put(name, classToStyleMap); 148 } 149 classToStyleMap.put(componentClass, style); 150 151 styleSheet.addStyle(componentClass, name, style); 152 } 153 154 for (int i = 0; i < styleElements.length; ++i) { 156 if (styleElements[i].hasAttribute("base-name")) { 157 String name = styleElements[i].getAttribute("name"); 158 String type = styleElements[i].getAttribute("type"); 159 Class componentClass; 160 try { 161 componentClass = Class.forName(type, true, classLoader); 162 } catch (ClassNotFoundException ex) { 163 continue; 166 } 167 168 Map classToStyleMap = (Map ) namedStyleMap.get(name); 169 DerivedMutableStyle style = (DerivedMutableStyle) classToStyleMap.get(componentClass); 170 171 String baseName = styleElements[i].getAttribute("base-name"); 172 173 classToStyleMap = (Map ) 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 .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 |