001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.ByteArrayMaker;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.Validator;
028:
029: import java.io.IOException;
030: import java.io.StringReader;
031:
032: import java.util.ArrayList;
033: import java.util.Iterator;
034: import java.util.List;
035: import java.util.Map;
036:
037: import javax.portlet.PortletPreferences;
038:
039: import org.dom4j.Document;
040: import org.dom4j.DocumentException;
041: import org.dom4j.DocumentFactory;
042: import org.dom4j.Element;
043: import org.dom4j.io.OutputFormat;
044: import org.dom4j.io.SAXReader;
045: import org.dom4j.io.XMLWriter;
046:
047: /**
048: * <a href="PortletPreferencesSerializer.java.html"><b><i>View Source</i></b>
049: * </a>
050: *
051: * @author Brian Wing Shun Chan
052: * @author Jon Steer
053: * @author Zongliang Li
054: *
055: */
056: public class PortletPreferencesSerializer {
057:
058: public static PortletPreferences fromDefaultXML(String xml)
059: throws PortalException, SystemException {
060:
061: PortletPreferencesImpl prefs = new PortletPreferencesImpl();
062:
063: if (Validator.isNull(xml)) {
064: return prefs;
065: }
066:
067: Map preferences = prefs.getPreferences();
068:
069: try {
070: Document doc = new SAXReader().read(new StringReader(xml));
071:
072: Element root = doc.getRootElement();
073:
074: Iterator itr1 = root.elements("preference").iterator();
075:
076: while (itr1.hasNext()) {
077: Element prefEl = (Element) itr1.next();
078:
079: String name = prefEl.elementTextTrim("name");
080:
081: List values = new ArrayList();
082:
083: Iterator itr2 = prefEl.elements("value").iterator();
084:
085: while (itr2.hasNext()) {
086: Element valueEl = (Element) itr2.next();
087:
088: /*if (valueEl.nodeCount() <= 0) {
089: values.add(valueEl.getText());
090: }
091: else {
092: values.add(valueEl.node(0).asXML());
093: }*/
094:
095: values.add(valueEl.getTextTrim());
096: }
097:
098: boolean readOnly = GetterUtil.getBoolean(prefEl
099: .elementText("read-only"));
100:
101: Preference preference = new Preference(name,
102: (String[]) values.toArray(new String[0]),
103: readOnly);
104:
105: preferences.put(name, preference);
106: }
107:
108: return prefs;
109: } catch (DocumentException de) {
110: throw new SystemException(de);
111: }
112: }
113:
114: public static PortletPreferencesImpl fromXML(long companyId,
115: long ownerId, int ownerType, long plid, String portletId,
116: String xml) throws PortalException, SystemException {
117:
118: try {
119: PortletPreferencesImpl prefs = (PortletPreferencesImpl) fromDefaultXML(xml);
120:
121: prefs = new PortletPreferencesImpl(companyId, ownerId,
122: ownerType, plid, portletId, prefs.getPreferences());
123:
124: return prefs;
125: } catch (PortalException pe) {
126: throw pe;
127: } catch (SystemException se) {
128: throw se;
129: }
130: }
131:
132: public static String toXML(PortletPreferencesImpl prefs)
133: throws SystemException {
134:
135: try {
136: Map preferences = prefs.getPreferences();
137:
138: DocumentFactory docFactory = DocumentFactory.getInstance();
139:
140: Element portletPreferences = docFactory
141: .createElement("portlet-preferences");
142:
143: Iterator itr = preferences.entrySet().iterator();
144:
145: while (itr.hasNext()) {
146: Map.Entry entry = (Map.Entry) itr.next();
147:
148: Preference preference = (Preference) entry.getValue();
149:
150: Element prefEl = docFactory.createElement("preference");
151:
152: Element nameEl = docFactory.createElement("name");
153:
154: nameEl.addText(preference.getName());
155:
156: prefEl.add(nameEl);
157:
158: String[] values = preference.getValues();
159:
160: for (int i = 0; i < values.length; i++) {
161: Element valueEl = docFactory.createElement("value");
162:
163: valueEl.addText(values[i]);
164:
165: prefEl.add(valueEl);
166: }
167:
168: if (preference.isReadOnly()) {
169: Element valueEl = docFactory
170: .createElement("read-only");
171:
172: valueEl.addText("true");
173:
174: prefEl.add(valueEl);
175: }
176:
177: portletPreferences.add(prefEl);
178: }
179:
180: ByteArrayMaker bam = new ByteArrayMaker();
181:
182: XMLWriter writer = new XMLWriter(bam, OutputFormat
183: .createCompactFormat());
184:
185: writer.write(portletPreferences);
186:
187: return bam.toString("UTF-8");
188: } catch (IOException ioe) {
189: throw new SystemException(ioe);
190: }
191: }
192:
193: }
|