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