001: /**
002: * $Id: AnnotationConfigLoader.java,v 1.1 2006/06/07 06:20:13 pieterdegr Exp $
003: * Copyright (C) 2005 carambacomponents.org
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */package org.caramba.config;
019:
020: import org.apache.commons.beanutils.PropertyUtils;
021: import org.apache.commons.lang.StringUtils;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.caramba.DestroyMode;
025: import org.caramba.action.Action;
026: import org.caramba.action.ForwardToPage;
027: import org.caramba.i18n.DefaultMessageResources;
028: import org.caramba.i18n.MessageResources;
029: import org.caramba.i18n.PropertyResourceBundleMessageResource;
030: import org.caramba.plugin.PlugIn;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.NamedNodeMap;
034: import org.w3c.dom.Node;
035: import org.w3c.dom.NodeList;
036: import org.xml.sax.EntityResolver;
037: import org.xml.sax.InputSource;
038: import org.xml.sax.SAXException;
039:
040: import javax.xml.parsers.DocumentBuilder;
041: import javax.xml.parsers.DocumentBuilderFactory;
042: import java.io.ByteArrayInputStream;
043: import java.io.IOException;
044: import java.io.InputStream;
045: import java.net.URL;
046: import java.util.regex.Pattern;
047:
048: /**
049: * User: Pieter
050: */
051: public class AnnotationConfigLoader {
052: private static final transient Log log = LogFactory
053: .getLog(AnnotationConfigLoader.class);
054:
055: public static CarambaConfig load(InputStream pInputStream)
056: throws ConfigLoaderException {
057: CarambaConfig config = new CarambaConfig();
058:
059: try {
060: DocumentBuilderFactory dbf = DocumentBuilderFactory
061: .newInstance();
062:
063: DocumentBuilder builder = null;
064: dbf.setNamespaceAware(false);
065: builder = dbf.newDocumentBuilder();
066: builder.setEntityResolver(new EntityResolver() {
067: public InputSource resolveEntity(String pPublicId,
068: String pSystemId) throws SAXException,
069: IOException {
070: Pattern pattern = Pattern
071: .compile("-//DTD Caramba Configuration//EN");
072: //Todo find something to make it work for all versions
073: if (pattern.matcher(pPublicId).matches()) {
074: InputStream is = null;
075: try {
076: URL url = new URL(pSystemId);
077: is = url.openStream();
078: } catch (Throwable e) {
079: //could not contact pPublicId, using internal one
080: is = getClass().getClassLoader()
081: .getResourceAsStream(
082: "caramba-config.dtd");
083: log.debug("could not validate with dtd at "
084: + pPublicId);
085: if (is != null) {
086: log.debug("using dtd on classpath. ");
087: } else {
088: log
089: .debug("dtd not found on classpath, so xml is not been validated. ");
090: is = new ByteArrayInputStream(
091: new byte[0]);
092: }
093: }
094: return new InputSource(is);
095: }
096: return null;
097: }
098: });
099:
100: Document document = builder.parse(pInputStream);
101:
102: Element root = document.getDocumentElement();
103: NodeList pageNodeList = root.getElementsByTagName("page");
104: for (int i = 0; i < pageNodeList.getLength(); i++) {
105: Node pageNode = pageNodeList.item(i);
106: NamedNodeMap attributes = pageNode.getAttributes();
107: String name = attributes.getNamedItem("name")
108: .getNodeValue();
109: String resource = attributes.getNamedItem("resource")
110: .getNodeValue();
111: String className = attributes.getNamedItem("class")
112: .getNodeValue();
113: PageConfig pageConfig = new PageConfig(name, resource,
114: className);
115: Node destroyModeItem = attributes
116: .getNamedItem("destroy-mode");
117: if (destroyModeItem != null) {
118: String destroyModeName = destroyModeItem
119: .getNodeValue();
120: DestroyMode destroyMode = DestroyMode
121: .getByName(destroyModeName);
122: if (destroyMode == null)
123: throw new ConfigLoaderException(
124: "Unsupported DestroyMode in page "
125: + name + " : "
126: + destroyModeName);
127: pageConfig.setDestroyMode(destroyMode);
128: }
129: config.addPageConfig(pageConfig);
130: }
131:
132: NodeList panelNodeList = root.getElementsByTagName("panel");
133: for (int i = 0; i < panelNodeList.getLength(); i++) {
134: Node panelNode = panelNodeList.item(i);
135: NamedNodeMap attributes = panelNode.getAttributes();
136: String name = attributes.getNamedItem("name")
137: .getNodeValue();
138: String resource = attributes.getNamedItem("resource")
139: .getNodeValue();
140: String className = attributes.getNamedItem("class")
141: .getNodeValue();
142: PanelConfig panelConfig = new PanelConfig(name,
143: resource, className);
144: config.addPanelConfig(panelConfig);
145: }
146:
147: NodeList messageResourcesNodeList = root
148: .getElementsByTagName("message-resources");
149: //should only have 0 or 1 element
150: int length = messageResourcesNodeList.getLength();
151: if (length > 0) {
152: Node messageResourcesNode = messageResourcesNodeList
153: .item(0);
154: Node classAttribute = messageResourcesNode
155: .getAttributes().getNamedItem("class");
156: String messageResourcesClassName = classAttribute != null ? classAttribute
157: .getNodeValue()
158: : null;
159: MessageResources messageResources;
160: if (StringUtils.isNotEmpty(messageResourcesClassName)) {
161: messageResources = (MessageResources) Class
162: .forName(messageResourcesClassName)
163: .newInstance();
164: } else {
165: messageResources = new DefaultMessageResources();
166: }
167: config.setMessageResources(messageResources);
168: NodeList childNodes = messageResourcesNode
169: .getChildNodes();
170: for (int i = 0; i < childNodes.getLength(); i++) {
171: Node node = childNodes.item(i);
172: if (node.getNodeName().equalsIgnoreCase(
173: "message-resource")) {
174: Node namedItem = node.getAttributes()
175: .getNamedItem("name");
176: if (namedItem == null) {
177: throw new ConfigLoaderException(
178: "name of a <message-resource> element cannot be null");
179: }
180: String baseName = namedItem.getNodeValue();
181: //for now, add always PropertyResourceBunldleMessageResources
182: messageResources
183: .addMessageResource(new PropertyResourceBundleMessageResource(
184: baseName));
185: }
186: }
187: }
188:
189: //Plugins
190: NodeList plugInNodeList = root
191: .getElementsByTagName("plug-in");
192: for (int i = 0; i < plugInNodeList.getLength(); i++) {
193: Node node = plugInNodeList.item(i);
194: String className = node.getAttributes().getNamedItem(
195: "className").getNodeValue();
196: PlugIn plugIn = (PlugIn) Class.forName(className)
197: .newInstance();
198: NodeList childNodes = node.getChildNodes();
199: for (int j = 0; j < childNodes.getLength(); j++) {
200: Node childNode = childNodes.item(j);
201: if ("set-property".equalsIgnoreCase(childNode
202: .getNodeName())) {
203: String name = childNode.getAttributes()
204: .getNamedItem("property")
205: .getNodeValue();
206: String value = childNode.getAttributes()
207: .getNamedItem("value").getNodeValue();
208: PropertyUtils.setProperty(plugIn, name, value);
209: }
210: }
211: config.addPlugIn(plugIn);
212: }
213:
214: //Global Forwards
215: NodeList globalForwardsNodeList = root
216: .getElementsByTagName("global-forwards");
217: if (globalForwardsNodeList.getLength() > 1)
218: throw new ConfigLoaderException(
219: "There can only be one global-forwards element");
220:
221: if (globalForwardsNodeList.getLength() == 1) {
222: Node node = globalForwardsNodeList.item(0);
223: NodeList childNodes = node.getChildNodes();
224: for (int j = 0; j < childNodes.getLength(); j++) {
225: Node childNode = childNodes.item(j);
226: if ("forward".equalsIgnoreCase(childNode
227: .getNodeName())) {
228: String forwardName = childNode.getAttributes()
229: .getNamedItem("name").getNodeValue();
230: String pageName = childNode.getAttributes()
231: .getNamedItem("pageName")
232: .getNodeValue();
233: ForwardToPage forward = new ForwardToPage(
234: pageName);
235: config.addGlobalForward(forwardName, forward);
236: }
237: }
238: }
239:
240: //Actions
241: NodeList actionNodeList = root
242: .getElementsByTagName("action");
243: for (int i = 0; i < actionNodeList.getLength(); i++) {
244: Node node = actionNodeList.item(i);
245: String actionName = node.getAttributes().getNamedItem(
246: "name").getNodeValue();
247: String className = node.getAttributes().getNamedItem(
248: "class").getNodeValue();
249: Action action = (Action) Class.forName(className)
250: .newInstance();
251: DefaultForwardConfig forwardConfig = new DefaultForwardConfig(
252: config);
253: NodeList childNodes = node.getChildNodes();
254: for (int j = 0; j < childNodes.getLength(); j++) {
255: Node childNode = childNodes.item(j);
256: if ("forward".equalsIgnoreCase(childNode
257: .getNodeName())) {
258: String forwardName = childNode.getAttributes()
259: .getNamedItem("name").getNodeValue();
260: String pageName = childNode.getAttributes()
261: .getNamedItem("pageName")
262: .getNodeValue();
263: ForwardToPage forward = new ForwardToPage(
264: pageName);
265: forwardConfig.addForward(forwardName, forward);
266: }
267: }
268: config.addAction(actionName, forwardConfig, action);
269: }
270:
271: //Libraries (javascript resources
272: NodeList librariesNodeList = root
273: .getElementsByTagName("libraries");
274: if (librariesNodeList.getLength() > 1)
275: throw new ConfigLoaderException(
276: "There can only be one libraries element");
277:
278: if (librariesNodeList.getLength() == 1) {
279: Node node = librariesNodeList.item(0);
280: NodeList childNodes = node.getChildNodes();
281: for (int j = 0; j < childNodes.getLength(); j++) {
282: Node childNode = childNodes.item(j);
283: if ("javascript".equalsIgnoreCase(childNode
284: .getNodeName())) {
285: String resource = childNode.getAttributes()
286: .getNamedItem("resource")
287: .getNodeValue();
288: config.addLibraryResource(resource);
289: }
290: }
291: }
292:
293: } catch (Exception e) {
294: throw new ConfigLoaderException(e);
295: }
296:
297: return config;
298:
299: }
300: }
|