KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > export > XmlExport


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) 2007
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.components.export;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.riotfamily.common.xml.XmlUtils;
31 import org.riotfamily.components.ComponentList;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34
35 /**
36  * Exports {@link ComponentList ComponentLists} as XML.
37  *
38  * @author Felix Gnass [fgnass at neteye dot de]
39  * @since 6.5
40  */

41 public class XmlExport {
42
43     private boolean useCustomTagNames = true;
44     
45     private SimpleListBuilder builder = new SimpleListBuilder();
46     
47     /**
48      * Sets whether component-types and property-names should be used as tag
49      * names. If set to <code>false</code>, the tags will be called
50      * &lt;component&gt; and &lt;property&gt;. The default is <code>true</code>.
51      */

52     public void setUseCustomTagNames(boolean useCustomTagNames) {
53         this.useCustomTagNames = useCustomTagNames;
54     }
55     
56     public Element JavaDoc createElement(Collection JavaDoc lists, boolean preview) {
57         Document JavaDoc doc = XmlUtils.createDocument();
58         Element JavaDoc ele = doc.createElement("lists");
59         Iterator JavaDoc it = lists.iterator();
60         while (it.hasNext()) {
61             ComponentList list = (ComponentList) it.next();
62             SimpleComponentList simpleList = builder.buildSimpleList(list, preview);
63             ele.appendChild(createListElement(simpleList, doc));
64         }
65         return ele;
66     }
67     
68     public Element JavaDoc createElement(ComponentList list, boolean preview) {
69         SimpleComponentList simpleList = builder.buildSimpleList(list, preview);
70         Document JavaDoc doc = XmlUtils.createDocument();
71         return createListElement(simpleList, doc);
72     }
73     
74     private Element JavaDoc createListElement(SimpleComponentList list, Document JavaDoc doc) {
75         Element JavaDoc ele = doc.createElement("list");
76         ele.setAttribute("path", list.getLocation().getPath());
77         ele.setAttribute("slot", list.getLocation().getSlot());
78         ele.setAttribute("type", list.getLocation().getType());
79         Iterator JavaDoc it = list.getComponents().iterator();
80         while (it.hasNext()) {
81             SimpleComponent component = (SimpleComponent) it.next();
82             ele.appendChild(createComponentElement(component, doc));
83         }
84         return ele;
85     }
86     
87     private Element JavaDoc createComponentElement(SimpleComponent component, Document JavaDoc doc) {
88         Element JavaDoc ele;
89         if (useCustomTagNames) {
90             ele = doc.createElement(component.getType());
91         }
92         else {
93             ele = doc.createElement("component");
94             ele.setAttribute("type", component.getType());
95         }
96         Iterator JavaDoc it = component.getStringProperties().entrySet().iterator();
97         while (it.hasNext()) {
98             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
99             String JavaDoc key = (String JavaDoc) entry.getKey();
100             String JavaDoc value = (String JavaDoc) entry.getValue();
101             ele.appendChild(createPropertyElement(key, value, doc));
102         }
103         return ele;
104     }
105     
106     private Element JavaDoc createPropertyElement(String JavaDoc name, String JavaDoc value, Document JavaDoc doc) {
107         Element JavaDoc ele;
108         if (useCustomTagNames) {
109             ele = doc.createElement(name);
110         }
111         else {
112             ele = doc.createElement("property");
113             ele.setAttribute("name", name);
114         }
115         ele.appendChild(doc.createCDATASection(value));
116         return ele;
117     }
118 }
119
Popular Tags