1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PortletPreferencesSerializer {
49  
50      public static PortletPreferences fromDefaultXML(String xml)
51          throws SystemException {
52  
53          PortletPreferencesImpl prefs = new PortletPreferencesImpl();
54  
55          if (Validator.isNull(xml)) {
56              return prefs;
57          }
58  
59          Map<String, Preference> preferences = prefs.getPreferences();
60  
61          try {
62              Document doc = SAXReaderUtil.read(xml);
63  
64              Element root = doc.getRootElement();
65  
66              Iterator<Element> itr1 = root.elements("preference").iterator();
67  
68              while (itr1.hasNext()) {
69                  Element prefEl = itr1.next();
70  
71                  String name = prefEl.elementTextTrim("name");
72  
73                  List<String> values = new ArrayList<String>();
74  
75                  Iterator<Element> itr2 = prefEl.elements("value").iterator();
76  
77                  while (itr2.hasNext()) {
78                      Element valueEl = itr2.next();
79  
80                      /*if (valueEl.nodeCount() <= 0) {
81                          values.add(valueEl.getText());
82                      }
83                      else {
84                          values.add(valueEl.node(0).asXML());
85                      }*/
86  
87                      values.add(valueEl.getTextTrim());
88                  }
89  
90                  boolean readOnly = GetterUtil.getBoolean(
91                      prefEl.elementText("read-only"));
92  
93                  Preference preference = new Preference(
94                      name, values.toArray(new String[values.size()]), readOnly);
95  
96                  preferences.put(name, preference);
97              }
98  
99              return prefs;
100         }
101         catch (DocumentException de) {
102             throw new SystemException(de);
103         }
104     }
105 
106     public static PortletPreferencesImpl fromXML(
107             long companyId, long ownerId, int ownerType, long plid,
108             String portletId, String xml)
109         throws SystemException {
110 
111         try {
112             PortletPreferencesImpl prefs =
113                 (PortletPreferencesImpl)fromDefaultXML(xml);
114 
115             prefs = new PortletPreferencesImpl(
116                 companyId, ownerId, ownerType, plid, portletId,
117                 prefs.getPreferences());
118 
119             return prefs;
120         }
121         catch (SystemException se) {
122             throw se;
123         }
124     }
125 
126     public static String toXML(PortletPreferencesImpl prefs) {
127         Map<String, Preference> preferences = prefs.getPreferences();
128 
129         Element portletPreferences = SAXReaderUtil.createElement(
130             "portlet-preferences");
131 
132         Iterator<Map.Entry<String, Preference>> itr =
133             preferences.entrySet().iterator();
134 
135         while (itr.hasNext()) {
136             Map.Entry<String, Preference> entry = itr.next();
137 
138             Preference preference = entry.getValue();
139 
140             Element prefEl = SAXReaderUtil.createElement("preference");
141 
142             Element nameEl = SAXReaderUtil.createElement("name");
143 
144             nameEl.addText(preference.getName());
145 
146             prefEl.add(nameEl);
147 
148             String[] values = preference.getValues();
149 
150             for (int i = 0; i < values.length; i++) {
151                 Element valueEl = SAXReaderUtil.createElement("value");
152 
153                 valueEl.addText(values[i]);
154 
155                 prefEl.add(valueEl);
156             }
157 
158             if (preference.isReadOnly()) {
159                 Element valueEl = SAXReaderUtil.createElement("read-only");
160 
161                 valueEl.addText("true");
162 
163                 prefEl.add(valueEl);
164             }
165 
166             portletPreferences.add(prefEl);
167         }
168 
169         return portletPreferences.asXML();
170     }
171 
172 }