1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.kernel.xml.Document;
26  import com.liferay.portal.kernel.xml.DocumentException;
27  import com.liferay.portal.kernel.xml.Element;
28  import com.liferay.portal.kernel.xml.SAXReaderUtil;
29  
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Map;
34  
35  import javax.portlet.PortletPreferences;
36  
37  /**
38   * <a href="PortletPreferencesSerializer.java.html"><b><i>View Source</i></b>
39   * </a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Jon Steer
43   * @author Zongliang Li
44   *
45   */
46  public class PortletPreferencesSerializer {
47  
48      public static PortletPreferences fromDefaultXML(String xml)
49          throws SystemException {
50  
51          PortletPreferencesImpl prefs = new PortletPreferencesImpl();
52  
53          if (Validator.isNull(xml)) {
54              return prefs;
55          }
56  
57          Map<String, Preference> preferences = prefs.getPreferences();
58  
59          try {
60              Document doc = SAXReaderUtil.read(xml);
61  
62              Element root = doc.getRootElement();
63  
64              Iterator<Element> itr1 = root.elements("preference").iterator();
65  
66              while (itr1.hasNext()) {
67                  Element prefEl = itr1.next();
68  
69                  String name = prefEl.elementTextTrim("name");
70  
71                  List<String> values = new ArrayList<String>();
72  
73                  Iterator<Element> itr2 = prefEl.elements("value").iterator();
74  
75                  while (itr2.hasNext()) {
76                      Element valueEl = itr2.next();
77  
78                      /*if (valueEl.nodeCount() <= 0) {
79                          values.add(valueEl.getText());
80                      }
81                      else {
82                          values.add(valueEl.node(0).asXML());
83                      }*/
84  
85                      values.add(valueEl.getTextTrim());
86                  }
87  
88                  boolean readOnly = GetterUtil.getBoolean(
89                      prefEl.elementText("read-only"));
90  
91                  Preference preference = new Preference(
92                      name, values.toArray(new String[values.size()]), readOnly);
93  
94                  preferences.put(name, preference);
95              }
96  
97              return prefs;
98          }
99          catch (DocumentException de) {
100             throw new SystemException(de);
101         }
102     }
103 
104     public static PortletPreferencesImpl fromXML(
105             long companyId, long ownerId, int ownerType, long plid,
106             String portletId, String xml)
107         throws SystemException {
108 
109         try {
110             PortletPreferencesImpl prefs =
111                 (PortletPreferencesImpl)fromDefaultXML(xml);
112 
113             prefs = new PortletPreferencesImpl(
114                 companyId, ownerId, ownerType, plid, portletId,
115                 prefs.getPreferences());
116 
117             return prefs;
118         }
119         catch (SystemException se) {
120             throw se;
121         }
122     }
123 
124     public static String toXML(PortletPreferencesImpl prefs) {
125         Map<String, Preference> preferences = prefs.getPreferences();
126 
127         Element portletPreferences = SAXReaderUtil.createElement(
128             "portlet-preferences");
129 
130         Iterator<Map.Entry<String, Preference>> itr =
131             preferences.entrySet().iterator();
132 
133         while (itr.hasNext()) {
134             Map.Entry<String, Preference> entry = itr.next();
135 
136             Preference preference = entry.getValue();
137 
138             Element prefEl = SAXReaderUtil.createElement("preference");
139 
140             Element nameEl = SAXReaderUtil.createElement("name");
141 
142             nameEl.addText(preference.getName());
143 
144             prefEl.add(nameEl);
145 
146             String[] values = preference.getValues();
147 
148             for (int i = 0; i < values.length; i++) {
149                 Element valueEl = SAXReaderUtil.createElement("value");
150 
151                 valueEl.addText(values[i]);
152 
153                 prefEl.add(valueEl);
154             }
155 
156             if (preference.isReadOnly()) {
157                 Element valueEl = SAXReaderUtil.createElement("read-only");
158 
159                 valueEl.addText("true");
160 
161                 prefEl.add(valueEl);
162             }
163 
164             portletPreferences.add(prefEl);
165         }
166 
167         return portletPreferences.asXML();
168     }
169 
170 }