KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > editor > xml > XmlEditorRepositoryDigester


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.editor.xml;
25
26 import java.util.Iterator JavaDoc;
27
28 import org.riotfamily.common.xml.DocumentDigester;
29 import org.riotfamily.common.xml.XmlUtils;
30 import org.riotfamily.riot.editor.AbstractEditorDefinition;
31 import org.riotfamily.riot.editor.CustomEditorDefinition;
32 import org.riotfamily.riot.editor.EditorDefinition;
33 import org.riotfamily.riot.editor.FormChooserDefinition;
34 import org.riotfamily.riot.editor.FormDefinition;
35 import org.riotfamily.riot.editor.GroupDefinition;
36 import org.riotfamily.riot.editor.IntermediateDefinition;
37 import org.riotfamily.riot.editor.ListDefinition;
38 import org.riotfamily.riot.editor.ObjectEditorDefinition;
39 import org.riotfamily.riot.editor.TreeDefinition;
40 import org.riotfamily.riot.editor.ViewDefinition;
41 import org.springframework.beans.factory.BeanFactory;
42 import org.springframework.core.io.Resource;
43 import org.springframework.util.xml.DomUtils;
44 import org.w3c.dom.Document JavaDoc;
45 import org.w3c.dom.Element JavaDoc;
46
47 /**
48  *
49  */

50 public class XmlEditorRepositoryDigester implements DocumentDigester {
51
52     public static final String JavaDoc NAMESPACE = "http://www.riotfamily.org/schema/riot/editor-config";
53
54     private static final String JavaDoc GROUP = "group";
55
56     private static final String JavaDoc[] GROUP_ATTR = new String JavaDoc[] {
57         "name", "key", "icon", "hidden"
58     };
59
60     private static final String JavaDoc LIST = "list";
61
62     private static final String JavaDoc TREE = "tree";
63
64     private static final String JavaDoc LIST_EDITOR = LIST + '|' + TREE;
65
66     private static final String JavaDoc[] LIST_ATTR = new String JavaDoc[] {
67         "name", "listId=list-ref", "icon", "hidden", "id"
68     };
69
70     private static final String JavaDoc[] TREE_ATTR = new String JavaDoc[] {
71         "branch-class"
72     };
73
74     private static final String JavaDoc FORM = "form";
75
76     private static final String JavaDoc FORM_CHOOSER = "form-chooser";
77
78     private static final String JavaDoc FORM_CHOOSER_COMMON = "common";
79
80     private static final String JavaDoc VIEW = "view";
81
82     private static final String JavaDoc[] VIEW_ATTR = new String JavaDoc[] {
83         "name", "view-name"
84     };
85
86     private static final String JavaDoc CUSTOM = "custom";
87
88     private static final String JavaDoc CUSTOM_REF = "ref";
89     
90     private static final String JavaDoc[] CUSTOM_ATTR = new String JavaDoc[] {
91         "name", "target", "url", "icon"
92     };
93
94     private static final String JavaDoc OBJECT_EDITOR = FORM + '|'
95             + FORM_CHOOSER + '|' + VIEW + '|' + CUSTOM;
96
97     private static final String JavaDoc FORM_OPTION = "form-option";
98
99
100     private static final String JavaDoc[] FORM_ATTR = new String JavaDoc[] {
101         "name", "formId=form-ref", "label-property", "hidden"
102     };
103
104     private static final String JavaDoc ANYTHING = OBJECT_EDITOR + '|' + LIST_EDITOR;
105
106
107     private XmlEditorRepository editorRepository;
108
109     private BeanFactory beanFactory;
110
111     public XmlEditorRepositoryDigester(XmlEditorRepository editorRepository,
112             BeanFactory beanFactory) {
113
114         this.editorRepository = editorRepository;
115         this.beanFactory = beanFactory;
116     }
117
118     /**
119      * Initializes the repository by digesting the given document.
120      */

121     public void digest(Document JavaDoc doc, Resource resource) {
122         Element JavaDoc root = doc.getDocumentElement();
123         GroupDefinition group = editorRepository.getRootGroupDefinition();
124         if (group == null) {
125             group = new GroupDefinition(editorRepository);
126             group.setName("start"); //REVISIT
127
editorRepository.addEditorDefinition(group);
128             editorRepository.setRootGroupDefinition(group);
129         }
130         digestGroupEntries(root, group);
131     }
132
133     protected void addEditorDefinition(AbstractEditorDefinition editor) {
134         if (editor.getId() == null) {
135             editor.setId(getUniqueId(editor.getName()));
136         }
137         editorRepository.addEditorDefinition(editor);
138     }
139
140     protected void digestGroupEntries(Element JavaDoc groupElement, GroupDefinition group) {
141         Iterator JavaDoc it = XmlUtils.getChildElements(groupElement).iterator();
142         while (it.hasNext()) {
143             Element JavaDoc ele = (Element JavaDoc) it.next();
144             String JavaDoc namespace = ele.getNamespaceURI();
145             if (namespace == null || namespace.equals(NAMESPACE)) {
146                 EditorDefinition ed = digestEditorDefinition(ele);
147                 group.addEditorDefinition(ed);
148             }
149         }
150     }
151
152     /**
153      * Creates an EditorDefinition.
154      */

155     protected EditorDefinition digestEditorDefinition(Element JavaDoc ele) {
156         EditorDefinition ed = null;
157         if (isListElement(ele) || isTreeElement(ele)) {
158             ed = digestListOrTreeDefinition(ele);
159         }
160         else if (isGroupElement(ele)) {
161             ed = digestGroupDefinition(ele);
162         }
163         else if (isFormElement(ele)) {
164             ed = digestFormDefinition(ele, null);
165         }
166         else if (isCustomElement(ele)) {
167             ed = digestCustomDefinition(ele, null);
168         }
169         return ed;
170     }
171
172     /**
173      * Creates a GroupDefinition by digesting the given element. Nested
174      * EditorDefinitions will be digested by calling
175      * {@link #digestEditorDefinition(Element) digestEditorDefinition()}.
176      */

177     protected GroupDefinition digestGroupDefinition(Element JavaDoc groupElement) {
178         GroupDefinition group = new GroupDefinition(editorRepository);
179         XmlUtils.populate(group, groupElement, GROUP_ATTR);
180         addEditorDefinition(group);
181         digestGroupEntries(groupElement, group);
182         return group;
183     }
184
185     protected ListDefinition digestListOrTreeDefinition(Element JavaDoc listElement) {
186         ListDefinition listDefinition;
187         if (isTreeElement(listElement)) {
188             listDefinition = new TreeDefinition(editorRepository);
189             XmlUtils.populate(listDefinition, listElement, TREE_ATTR);
190         }
191         else {
192             listDefinition = new ListDefinition(editorRepository);
193         }
194         initListDefinition(listDefinition, listElement);
195         return listDefinition;
196     }
197
198     /**
199      * Initialized a ListDefinition by digesting the given element.
200      */

201     protected void initListDefinition(ListDefinition listDefinition,
202             Element JavaDoc listElement) {
203
204         XmlUtils.populate(listDefinition, listElement, LIST_ATTR);
205         addEditorDefinition(listDefinition);
206
207         Element JavaDoc e = XmlUtils.getFirstChildByRegex(listElement, ANYTHING);
208         if (e != null) {
209             EditorDefinition def = null;
210             if (isListElement(e) || isTreeElement(e)) {
211                 def = new IntermediateDefinition(
212                         listDefinition, digestListOrTreeDefinition(e));
213             }
214             else {
215                 def = digestObjectEditorDefinition(e, listDefinition);
216                 
217             }
218             listDefinition.setDisplayDefinition(def);
219         }
220     }
221
222
223     protected EditorDefinition digestObjectEditorDefinition(
224             Element JavaDoc ele, EditorDefinition parentDef) {
225
226         if (isFormElement(ele)) {
227             return digestFormDefinition(ele, parentDef);
228         }
229         else if (isFormChooserElement(ele)) {
230             return digestFormChooserDefinition(ele, parentDef);
231         }
232         else if (isViewElement(ele)) {
233             return digestViewDefinition(ele, parentDef);
234         }
235         else if (isCustomElement(ele)) {
236             return digestCustomDefinition(ele, parentDef);
237         }
238         else {
239             throw new RuntimeException JavaDoc("Expected " + OBJECT_EDITOR);
240         }
241     }
242
243     /**
244      * Creates a new DefaultFormDefinition by digesting the given element.
245      */

246     protected FormDefinition digestFormDefinition(Element JavaDoc formElement,
247             EditorDefinition parentDef) {
248
249         FormDefinition formDefinition = new FormDefinition(editorRepository);
250         XmlUtils.populate(formDefinition, formElement, FORM_ATTR, beanFactory);
251         formDefinition.setParentEditorDefinition(parentDef);
252         addEditorDefinition(formDefinition);
253
254         digestChildEditors(formElement, formDefinition);
255
256         return formDefinition;
257     }
258
259     protected void digestChildEditors(Element JavaDoc ele, ObjectEditorDefinition editorDef) {
260         Iterator JavaDoc it = XmlUtils.getChildElementsByRegex(
261                 ele, ANYTHING).iterator();
262
263         while (it.hasNext()) {
264             EditorDefinition childDef = digestChildEditorDefinition(
265                     (Element JavaDoc) it.next(), editorDef.getParentEditorDefinition());
266
267             editorDef.addChildEditorDefinition(childDef);
268         }
269     }
270
271     protected EditorDefinition digestChildEditorDefinition(Element JavaDoc ele,
272             EditorDefinition parentDef) {
273
274         if (isListElement(ele) || isTreeElement(ele)) {
275             return digestListOrTreeDefinition(ele);
276         }
277         return digestObjectEditorDefinition(ele, parentDef);
278     }
279
280     protected ViewDefinition digestViewDefinition(Element JavaDoc formElement,
281             EditorDefinition parentDef) {
282
283         ViewDefinition viewDefinition = new ViewDefinition(editorRepository);
284         XmlUtils.populate(viewDefinition, formElement, VIEW_ATTR);
285         viewDefinition.setParentEditorDefinition(parentDef);
286         addEditorDefinition(viewDefinition);
287         return viewDefinition;
288     }
289
290     protected String JavaDoc getUniqueId(String JavaDoc name) {
291         String JavaDoc id = name != null ? name : "editor";
292         String JavaDoc uniqueId = id;
293         EditorDefinition def = editorRepository.getEditorDefinition(uniqueId);
294         int i = 1;
295         while (def != null) {
296             uniqueId = id + i;
297             def = editorRepository.getEditorDefinition(uniqueId);
298             i++;
299         }
300         return uniqueId;
301     }
302
303
304     protected FormChooserDefinition digestFormChooserDefinition(
305             Element JavaDoc chooserElement, EditorDefinition parentDef) {
306
307         FormChooserDefinition formChooserDefinition =
308                 new FormChooserDefinition(editorRepository);
309
310         XmlUtils.populate(formChooserDefinition, chooserElement,
311                 FORM_ATTR, beanFactory);
312
313         formChooserDefinition.setParentEditorDefinition(parentDef);
314         addEditorDefinition(formChooserDefinition);
315
316         Iterator JavaDoc it = DomUtils.getChildElementsByTagName(
317                 chooserElement, FORM_OPTION).iterator();
318
319         while (it.hasNext()) {
320             FormDefinition formDefinition =
321                     digestFormDefinition((Element JavaDoc) it.next(),
322                     formChooserDefinition);
323
324             formChooserDefinition.addFormDefinition(formDefinition);
325         }
326
327         Element JavaDoc commonElement = XmlUtils.getFirstChildByTagName(
328                 chooserElement, FORM_CHOOSER_COMMON);
329
330         if (commonElement != null) {
331             digestChildEditors(commonElement, formChooserDefinition);
332         }
333         return formChooserDefinition;
334     }
335
336     protected EditorDefinition digestCustomDefinition(Element JavaDoc ele,
337             EditorDefinition parentDef) {
338
339         CustomEditorDefinition custom;
340         String JavaDoc beanName = XmlUtils.getAttribute(ele, CUSTOM_REF);
341         if (beanName != null) {
342             custom = (CustomEditorDefinition) beanFactory.getBean(
343                     beanName, CustomEditorDefinition.class);
344         }
345         else {
346             custom = new CustomEditorDefinition();
347         }
348         custom.setEditorRepository(editorRepository);
349         
350         XmlUtils.populate(custom, ele, CUSTOM_ATTR, beanFactory);
351         custom.setParentEditorDefinition(parentDef);
352         addEditorDefinition(custom);
353         
354         digestChildEditors(ele, custom);
355         
356         return custom;
357     }
358
359     private static boolean isFormElement(Element JavaDoc ele) {
360         return ele != null && DomUtils.nodeNameEquals(ele, FORM);
361     }
362
363     private static boolean isFormChooserElement(Element JavaDoc ele) {
364         return ele != null && DomUtils.nodeNameEquals(ele, FORM_CHOOSER);
365     }
366
367     private static boolean isViewElement(Element JavaDoc ele) {
368         return ele != null && DomUtils.nodeNameEquals(ele, VIEW);
369     }
370
371     private static boolean isListElement(Element JavaDoc ele) {
372         return ele != null && DomUtils.nodeNameEquals(ele, LIST);
373     }
374
375     private static boolean isTreeElement(Element JavaDoc ele) {
376         return ele != null && DomUtils.nodeNameEquals(ele, TREE);
377     }
378
379     private static boolean isGroupElement(Element JavaDoc ele) {
380         return ele != null && DomUtils.nodeNameEquals(ele, GROUP);
381     }
382
383     private static boolean isCustomElement(Element JavaDoc ele) {
384         return ele != null && DomUtils.nodeNameEquals(ele, CUSTOM);
385     }
386
387 }
388
Popular Tags