1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.ByteArrayMaker;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  
31  import java.io.IOException;
32  import java.io.StringReader;
33  
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
37  import java.util.Map;
38  
39  import javax.portlet.PortletPreferences;
40  
41  import org.dom4j.Document;
42  import org.dom4j.DocumentException;
43  import org.dom4j.DocumentFactory;
44  import org.dom4j.Element;
45  import org.dom4j.io.OutputFormat;
46  import org.dom4j.io.SAXReader;
47  import org.dom4j.io.XMLWriter;
48  
49  /**
50   * <a href="PortletPreferencesSerializer.java.html"><b><i>View Source</i></b>
51   * </a>
52   *
53   * @author Brian Wing Shun Chan
54   * @author Jon Steer
55   * @author Zongliang Li
56   *
57   */
58  public class PortletPreferencesSerializer {
59  
60      public static PortletPreferences fromDefaultXML(String xml)
61          throws PortalException, SystemException {
62  
63          PortletPreferencesImpl prefs = new PortletPreferencesImpl();
64  
65          if (Validator.isNull(xml)) {
66              return prefs;
67          }
68  
69          Map preferences = prefs.getPreferences();
70  
71          try {
72              Document doc = new SAXReader().read(new StringReader(xml));
73  
74              Element root = doc.getRootElement();
75  
76              Iterator itr1 = root.elements("preference").iterator();
77  
78              while (itr1.hasNext()) {
79                  Element prefEl = (Element)itr1.next();
80  
81                  String name = prefEl.elementTextTrim("name");
82  
83                  List values = new ArrayList();
84  
85                  Iterator itr2 = prefEl.elements("value").iterator();
86  
87                  while (itr2.hasNext()) {
88                      Element valueEl = (Element)itr2.next();
89  
90                      /*if (valueEl.nodeCount() <= 0) {
91                          values.add(valueEl.getText());
92                      }
93                      else {
94                          values.add(valueEl.node(0).asXML());
95                      }*/
96  
97                      values.add(valueEl.getTextTrim());
98                  }
99  
100                 boolean readOnly = GetterUtil.getBoolean(
101                     prefEl.elementText("read-only"));
102 
103                 Preference preference = new Preference(
104                     name, (String[])values.toArray(new String[0]), readOnly);
105 
106                 preferences.put(name, preference);
107             }
108 
109             return prefs;
110         }
111         catch (DocumentException de) {
112             throw new SystemException(de);
113         }
114     }
115 
116     public static PortletPreferencesImpl fromXML(
117             long companyId, long ownerId, int ownerType, long plid,
118             String portletId, String xml)
119         throws PortalException, SystemException {
120 
121         try {
122             PortletPreferencesImpl prefs =
123                 (PortletPreferencesImpl)fromDefaultXML(xml);
124 
125             prefs = new PortletPreferencesImpl(
126                 companyId, ownerId, ownerType, plid, portletId,
127                 prefs.getPreferences());
128 
129             return prefs;
130         }
131         catch (PortalException pe) {
132             throw pe;
133         }
134         catch (SystemException se) {
135             throw se;
136         }
137     }
138 
139     public static String toXML(PortletPreferencesImpl prefs)
140         throws SystemException {
141 
142         try {
143             Map preferences = prefs.getPreferences();
144 
145             DocumentFactory docFactory = DocumentFactory.getInstance();
146 
147             Element portletPreferences =
148                 docFactory.createElement("portlet-preferences");
149 
150             Iterator itr = preferences.entrySet().iterator();
151 
152             while (itr.hasNext()) {
153                 Map.Entry entry = (Map.Entry)itr.next();
154 
155                 Preference preference = (Preference)entry.getValue();
156 
157                 Element prefEl = docFactory.createElement("preference");
158 
159                 Element nameEl = docFactory.createElement("name");
160 
161                 nameEl.addText(preference.getName());
162 
163                 prefEl.add(nameEl);
164 
165                 String[] values = preference.getValues();
166 
167                 for (int i = 0; i < values.length; i++) {
168                     Element valueEl = docFactory.createElement("value");
169 
170                     valueEl.addText(values[i]);
171 
172                     prefEl.add(valueEl);
173                 }
174 
175                 if (preference.isReadOnly()) {
176                     Element valueEl = docFactory.createElement("read-only");
177 
178                     valueEl.addText("true");
179 
180                     prefEl.add(valueEl);
181                 }
182 
183                 portletPreferences.add(prefEl);
184             }
185 
186             ByteArrayMaker bam = new ByteArrayMaker();
187 
188             XMLWriter writer = new XMLWriter(
189                 bam, OutputFormat.createCompactFormat());
190 
191             writer.write(portletPreferences);
192 
193             return bam.toString("UTF-8");
194         }
195         catch (IOException ioe) {
196             throw new SystemException(ioe);
197         }
198     }
199 
200 }