1   /**
2    * Copyright (c) 2000-2008 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.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  
32  import java.io.IOException;
33  import java.io.StringReader;
34  
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.portlet.PortletPreferences;
41  
42  import org.dom4j.Document;
43  import org.dom4j.DocumentException;
44  import org.dom4j.DocumentFactory;
45  import org.dom4j.Element;
46  import org.dom4j.io.OutputFormat;
47  import org.dom4j.io.SAXReader;
48  import org.dom4j.io.XMLWriter;
49  
50  /**
51   * <a href="PortletPreferencesSerializer.java.html"><b><i>View Source</i></b>
52   * </a>
53   *
54   * @author Brian Wing Shun Chan
55   * @author Jon Steer
56   * @author Zongliang Li
57   *
58   */
59  public class PortletPreferencesSerializer {
60  
61      public static PortletPreferences fromDefaultXML(String xml)
62          throws PortalException, SystemException {
63  
64          PortletPreferencesImpl prefs = new PortletPreferencesImpl();
65  
66          if (Validator.isNull(xml)) {
67              return prefs;
68          }
69  
70          Map<String, Preference> preferences = prefs.getPreferences();
71  
72          try {
73              Document doc = new SAXReader().read(new StringReader(xml));
74  
75              Element root = doc.getRootElement();
76  
77              Iterator<Element> itr1 = root.elements("preference").iterator();
78  
79              while (itr1.hasNext()) {
80                  Element prefEl = itr1.next();
81  
82                  String name = prefEl.elementTextTrim("name");
83  
84                  List<String> values = new ArrayList<String>();
85  
86                  Iterator<Element> itr2 = prefEl.elements("value").iterator();
87  
88                  while (itr2.hasNext()) {
89                      Element valueEl = itr2.next();
90  
91                      /*if (valueEl.nodeCount() <= 0) {
92                          values.add(valueEl.getText());
93                      }
94                      else {
95                          values.add(valueEl.node(0).asXML());
96                      }*/
97  
98                      values.add(valueEl.getTextTrim());
99                  }
100 
101                 boolean readOnly = GetterUtil.getBoolean(
102                     prefEl.elementText("read-only"));
103 
104                 Preference preference = new Preference(
105                     name, values.toArray(new String[values.size()]), readOnly);
106 
107                 preferences.put(name, preference);
108             }
109 
110             return prefs;
111         }
112         catch (DocumentException de) {
113             throw new SystemException(de);
114         }
115     }
116 
117     public static PortletPreferencesImpl fromXML(
118             long companyId, long ownerId, int ownerType, long plid,
119             String portletId, String xml)
120         throws PortalException, SystemException {
121 
122         try {
123             PortletPreferencesImpl prefs =
124                 (PortletPreferencesImpl)fromDefaultXML(xml);
125 
126             prefs = new PortletPreferencesImpl(
127                 companyId, ownerId, ownerType, plid, portletId,
128                 prefs.getPreferences());
129 
130             return prefs;
131         }
132         catch (PortalException pe) {
133             throw pe;
134         }
135         catch (SystemException se) {
136             throw se;
137         }
138     }
139 
140     public static String toXML(PortletPreferencesImpl prefs)
141         throws SystemException {
142 
143         try {
144             Map<String, Preference> preferences = prefs.getPreferences();
145 
146             DocumentFactory docFactory = DocumentFactory.getInstance();
147 
148             Element portletPreferences =
149                 docFactory.createElement("portlet-preferences");
150 
151             Iterator<Map.Entry<String, Preference>> itr =
152                 preferences.entrySet().iterator();
153 
154             while (itr.hasNext()) {
155                 Map.Entry<String, Preference> entry = itr.next();
156 
157                 Preference preference = entry.getValue();
158 
159                 Element prefEl = docFactory.createElement("preference");
160 
161                 Element nameEl = docFactory.createElement("name");
162 
163                 nameEl.addText(preference.getName());
164 
165                 prefEl.add(nameEl);
166 
167                 String[] values = preference.getValues();
168 
169                 for (int i = 0; i < values.length; i++) {
170                     Element valueEl = docFactory.createElement("value");
171 
172                     valueEl.addText(values[i]);
173 
174                     prefEl.add(valueEl);
175                 }
176 
177                 if (preference.isReadOnly()) {
178                     Element valueEl = docFactory.createElement("read-only");
179 
180                     valueEl.addText("true");
181 
182                     prefEl.add(valueEl);
183                 }
184 
185                 portletPreferences.add(prefEl);
186             }
187 
188             ByteArrayMaker bam = new ByteArrayMaker();
189 
190             XMLWriter writer = new XMLWriter(
191                 bam, OutputFormat.createCompactFormat());
192 
193             writer.write(portletPreferences);
194 
195             return bam.toString(StringPool.UTF8);
196         }
197         catch (IOException ioe) {
198             throw new SystemException(ioe);
199         }
200     }
201 
202 }