KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > list > xml > XmlListRepositoryDigester


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.list.xml;
25
26
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.riotfamily.common.beans.PropertyUtils;
31 import org.riotfamily.common.xml.XmlUtils;
32 import org.riotfamily.common.xml.DocumentDigester;
33 import org.riotfamily.riot.dao.RiotDao;
34 import org.riotfamily.riot.list.ColumnConfig;
35 import org.riotfamily.riot.list.ListConfig;
36 import org.riotfamily.riot.list.ListRepository;
37 import org.springframework.beans.factory.BeanFactory;
38 import org.springframework.core.io.Resource;
39 import org.springframework.util.xml.DomUtils;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42
43 /**
44  *
45  */

46 public class XmlListRepositoryDigester implements DocumentDigester {
47     
48     public static final String JavaDoc NAMESPACE = "http://www.riotfamily.org/schema/riot/list-config";
49     
50     private static final String JavaDoc LIST = "list";
51     
52     private static final String JavaDoc ID = "id";
53     
54     private static final String JavaDoc[] LIST_ATTRS = new String JavaDoc[] {
55         "id", "id-property", "filterFormId=filter-form",
56         "defaultCommandId=default-command", "order-by",
57         "row-style-property", "search"
58     };
59     
60     private static final String JavaDoc DAO = "dao";
61     
62     private static final String JavaDoc DAO_REF = "ref";
63     
64     private static final String JavaDoc DAO_CLASS = "class";
65     
66     private static final String JavaDoc PROPERTY = "property";
67     
68     private static final String JavaDoc COLUMNS = "columns";
69     
70     private static final String JavaDoc COLUMN = "column";
71     
72     private static final String JavaDoc[] COLUMN_ATTRS = new String JavaDoc[] {
73         "sortable", "lookup-level", "@renderer", "property", "case-sensitive"
74     };
75         
76     private static final String JavaDoc COMMAND = "command";
77     
78     private ListRepository listRepository;
79     
80     private BeanFactory beanFactory;
81         
82     public XmlListRepositoryDigester(ListRepository listRepository,
83             BeanFactory beanFactory) {
84         
85         this.listRepository = listRepository;
86         this.beanFactory = beanFactory;
87     }
88
89     public void digest(Document JavaDoc doc, Resource resource) {
90         Element JavaDoc root = doc.getDocumentElement();
91         List JavaDoc nodes = DomUtils.getChildElementsByTagName(root, LIST);
92         Iterator JavaDoc it = nodes.iterator();
93         while (it.hasNext()) {
94             Element JavaDoc ele = (Element JavaDoc) it.next();
95             String JavaDoc namespace = ele.getNamespaceURI();
96             if (namespace == null || namespace.equals(NAMESPACE)) {
97                 listRepository.addListConfig(digestListConfig(ele));
98             }
99         }
100     }
101     
102     /**
103      * Creates a ListConfig by digesting a <list&gt tag.
104      */

105     protected ListConfig digestListConfig(Element JavaDoc listElement) {
106         ListConfig listConfig = new ListConfig();
107         
108         XmlUtils.populate(listConfig, listElement, LIST_ATTRS);
109         
110         digestDao(listConfig, listElement);
111         digestColumns(listConfig, listElement);
112         digestCommands(listConfig, listElement);
113         
114         return listConfig;
115     }
116
117     /**
118      * Creates (or retrieves) a RiotDao by digesting a <dao> tag.
119      */

120     protected void digestDao(ListConfig listConfig,
121             Element JavaDoc listElement) {
122         
123         Element JavaDoc ele = DomUtils.getChildElementByTagName(listElement, DAO);
124         
125         RiotDao dao = null;
126         boolean singleton = false;
127         
128         String JavaDoc ref = XmlUtils.getAttribute(ele, DAO_REF);
129         if (ref != null) {
130             dao = (RiotDao) beanFactory.getBean(ref, RiotDao.class);
131             singleton = beanFactory.isSingleton(ref);
132         }
133         else {
134             String JavaDoc className = XmlUtils.getAttribute(ele, DAO_CLASS);
135             if (className != null) {
136                 dao = instanciateDao(className);
137             }
138         }
139         List JavaDoc nodes = DomUtils.getChildElementsByTagName(ele, PROPERTY);
140         if (singleton && !nodes.isEmpty()) {
141             throw new RuntimeException JavaDoc(PROPERTY
142                     + " must not be applied to singleton beans.");
143         }
144         XmlUtils.populate(dao, nodes, beanFactory);
145         
146         listConfig.setDao(dao);
147     }
148     
149     /**
150      * Creates a new instance of the specified Dao class.
151      */

152     protected RiotDao instanciateDao(String JavaDoc className) {
153         return (RiotDao) PropertyUtils.newInstance(className);
154     }
155
156     /**
157      * Adds columns to the given ListConfig by digesting the <columns>
158      * child of the given <list> element.
159      */

160     protected void digestColumns(ListConfig listConfig,
161             Element JavaDoc listElement) {
162         
163         Element JavaDoc columns = DomUtils.getChildElementByTagName(listElement, COLUMNS);
164         
165         List JavaDoc nodes = DomUtils.getChildElementsByTagName(columns, COLUMN);
166         Iterator JavaDoc it = nodes.iterator();
167         while (it.hasNext()) {
168             listConfig.addColumnConfig(digestColumn((Element JavaDoc) it.next()));
169         }
170         
171         nodes = DomUtils.getChildElementsByTagName(columns, COMMAND);
172         it = nodes.iterator();
173         while (it.hasNext()) {
174             Element JavaDoc e = (Element JavaDoc) it.next();
175             String JavaDoc commandId = XmlUtils.getAttribute(e, ID);
176             listConfig.addColumnCommand(listRepository.getCommand(commandId));
177         }
178     }
179     
180     /**
181      * Creates a ColumnConfig by digesting the given element.
182      */

183     protected ColumnConfig digestColumn(Element JavaDoc ele) {
184         ColumnConfig columnConfig = new ColumnConfig();
185         XmlUtils.populate(columnConfig, ele, COLUMN_ATTRS, beanFactory);
186         if (columnConfig.getRenderer() == null) {
187             columnConfig.setRenderer(listRepository.getDefaultCellRenderer());
188         }
189         return columnConfig;
190     }
191     
192     protected void digestCommands(ListConfig listConfig, Element JavaDoc listElement) {
193         List JavaDoc nodes = DomUtils.getChildElementsByTagName(listElement, COMMAND);
194         Iterator JavaDoc it = nodes.iterator();
195         while (it.hasNext()) {
196             Element JavaDoc ele = (Element JavaDoc) it.next();
197             String JavaDoc commandId = XmlUtils.getAttribute(ele, ID);
198             listConfig.addCommand(listRepository.getCommand(commandId));
199         }
200     }
201         
202 }
203
Popular Tags