001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019 import com.liferay.portal.kernel.util.GetterUtil;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portal.kernel.xml.simple.Element;
022 import com.liferay.portal.xml.StAXReaderUtil;
023
024 import java.util.ArrayList;
025 import java.util.Iterator;
026 import java.util.List;
027 import java.util.Map;
028
029 import javax.portlet.PortletPreferences;
030
031 import javax.xml.stream.XMLEventReader;
032 import javax.xml.stream.XMLInputFactory;
033 import javax.xml.stream.XMLStreamException;
034 import javax.xml.stream.events.EndElement;
035 import javax.xml.stream.events.StartElement;
036 import javax.xml.stream.events.XMLEvent;
037
038
044 public class PortletPreferencesSerializer {
045
046 public static PortletPreferences fromDefaultXML(String xml)
047 throws SystemException {
048
049 PortletPreferencesImpl preferences = new PortletPreferencesImpl();
050
051 if (Validator.isNull(xml)) {
052 return preferences;
053 }
054
055 Map<String, Preference> preferencesMap = preferences.getPreferences();
056
057 XMLEventReader xmlEventReader = null;
058
059 try {
060 XMLInputFactory xmlInputFactory =
061 StAXReaderUtil.getXMLInputFactory();
062
063 xmlEventReader = xmlInputFactory.createXMLEventReader(
064 new UnsyncStringReader(xml));
065
066 while (xmlEventReader.hasNext()) {
067 XMLEvent xmlEvent = xmlEventReader.nextEvent();
068
069 if (xmlEvent.isStartElement()) {
070 StartElement startElement = xmlEvent.asStartElement();
071
072 String elementName = startElement.getName().getLocalPart();
073
074 if (elementName.equals("preference")) {
075 Preference preference = _readPreference(xmlEventReader);
076
077 preferencesMap.put(preference.getName(), preference);
078 }
079 }
080 }
081
082 return preferences;
083 }
084 catch (XMLStreamException xse) {
085 throw new SystemException(xse);
086 }
087 finally {
088 if (xmlEventReader != null) {
089 try {
090 xmlEventReader.close();
091 }
092 catch (XMLStreamException xse) {
093 }
094 }
095 }
096 }
097
098 public static PortletPreferencesImpl fromXML(
099 long companyId, long ownerId, int ownerType, long plid,
100 String portletId, String xml)
101 throws SystemException {
102
103 try {
104 PortletPreferencesImpl preferences =
105 (PortletPreferencesImpl)fromDefaultXML(xml);
106
107 preferences = new PortletPreferencesImpl(
108 companyId, ownerId, ownerType, plid, portletId,
109 preferences.getPreferences());
110
111 return preferences;
112 }
113 catch (SystemException se) {
114 throw se;
115 }
116 }
117
118 public static String toXML(PortletPreferencesImpl preferences) {
119 Map<String, Preference> preferencesMap = preferences.getPreferences();
120
121 Element portletPreferencesElement = new Element(
122 "portlet-preferences", false);
123
124 Iterator<Map.Entry<String, Preference>> itr =
125 preferencesMap.entrySet().iterator();
126
127 while (itr.hasNext()) {
128 Map.Entry<String, Preference> entry = itr.next();
129
130 Preference preference = entry.getValue();
131
132 Element preferenceElement = portletPreferencesElement.addElement(
133 "preference");
134
135 preferenceElement.addElement("name", preference.getName());
136
137 for (String value : preference.getValues()) {
138 preferenceElement.addElement("value", value);
139 }
140
141 if (preference.isReadOnly()) {
142 preferenceElement.addElement("read-only", Boolean.TRUE);
143 }
144 }
145
146 return portletPreferencesElement.toXMLString();
147 }
148
149 private static Preference _readPreference(XMLEventReader xmlEventReader)
150 throws XMLStreamException {
151
152 String name = null;
153 List<String> values = new ArrayList<String>();
154 boolean readOnly = false;
155
156 while (xmlEventReader.hasNext()) {
157 XMLEvent xmlEvent = xmlEventReader.nextEvent();
158
159 if (xmlEvent.isStartElement()) {
160 StartElement startElement = xmlEvent.asStartElement();
161
162 String elementName = startElement.getName().getLocalPart();
163
164 if (elementName.equals("name")) {
165 name = StAXReaderUtil.read(xmlEventReader);
166 }
167 else if (elementName.equals("value")) {
168 String value = StAXReaderUtil.read(xmlEventReader);
169
170 values.add(value);
171 }
172 else if (elementName.equals("read-only")) {
173 String value = StAXReaderUtil.read(xmlEventReader);
174
175 readOnly = GetterUtil.getBoolean(value);
176 }
177 }
178 else if (xmlEvent.isEndElement()){
179 EndElement endElement = xmlEvent.asEndElement();
180
181 String elementName = endElement.getName().getLocalPart();
182
183 if (elementName.equals("preference")) {
184 break;
185 }
186 }
187 }
188
189 return new Preference(
190 name, values.toArray(new String[values.size()]), readOnly);
191 }
192
193 }