1
22
23 package com.liferay.portal.model.impl;
24
25 import com.liferay.portal.kernel.util.StringPool;
26 import com.liferay.portal.kernel.util.Tuple;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.model.WebDAVProps;
29 import com.liferay.portal.webdav.WebDAVUtil;
30 import com.liferay.util.xml.XMLFormatter;
31
32 import java.io.StringReader;
33
34 import java.util.HashSet;
35 import java.util.Iterator;
36 import java.util.Set;
37
38 import org.dom4j.Document;
39 import org.dom4j.DocumentException;
40 import org.dom4j.Element;
41 import org.dom4j.Namespace;
42 import org.dom4j.QName;
43 import org.dom4j.io.SAXReader;
44
45
51 public class WebDAVPropsImpl
52 extends WebDAVPropsModelImpl implements WebDAVProps {
53
54 public WebDAVPropsImpl() {
55 }
56
57 public String getProps() {
58 String props = super.getProps();
59
60 if (Validator.isNull(props)) {
61 return _PROPS;
62 }
63 else {
64 return props;
65 }
66 }
67
68 public Set<Tuple> getPropsSet() throws Exception {
69 Set<Tuple> propsSet = new HashSet<Tuple>();
70
71 Document doc = _getPropsDocument();
72
73 Element root = doc.getRootElement();
74
75 Iterator<Element> itr = root.elements().iterator();
76
77 while (itr.hasNext()) {
78 Element el = itr.next();
79
80 String prefix = el.getNamespacePrefix();
81 String uri = el.getNamespaceURI();
82
83 Namespace namespace = null;
84
85 if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
86 namespace = WebDAVUtil.DAV_URI;
87 }
88 else if (Validator.isNull(prefix)) {
89 namespace = Namespace.get(uri);
90 }
91 else {
92 namespace = Namespace.get(prefix, uri);
93 }
94
95 propsSet.add(new Tuple(el.getName(), namespace));
96 }
97
98 return propsSet;
99 }
100
101 public String getText(String name, String prefix, String uri)
102 throws Exception {
103
104 Namespace namespace = null;
105
106 if (Validator.isNull(prefix)) {
107 namespace = Namespace.get(uri);
108 }
109 else {
110 namespace = Namespace.get(prefix, uri);
111 }
112
113 QName qname = new QName(name, namespace);
114
115 Document doc = _getPropsDocument();
116
117 Element root = doc.getRootElement();
118
119 Element prop = root.element(qname);
120
121 return prop.getText();
122 }
123
124 public void addProp(String name, String prefix, String uri)
125 throws Exception {
126
127 Namespace namespace = null;
128
129 if (Validator.isNull(prefix)) {
130 namespace = Namespace.get(uri);
131 }
132 else {
133 namespace = Namespace.get(prefix, uri);
134 }
135
136 QName qname = new QName(name, namespace);
137
138 Element root = _removeExisting(qname);
139
140 root.addElement(qname);
141 }
142
143 public void addProp(String name, String prefix, String uri, String text)
144 throws Exception {
145
146 Namespace namespace = null;
147
148 if (Validator.isNull(prefix)) {
149 namespace = Namespace.get(uri);
150 }
151 else {
152 namespace = Namespace.get(prefix, uri);
153 }
154
155 QName qname = new QName(name, namespace);
156
157 Element root = _removeExisting(qname);
158
159 root.addElement(qname).addText(text);
160 }
161
162 public void removeProp(String name, String prefix, String uri)
163 throws Exception {
164
165 Namespace namespace = null;
166
167 if (Validator.isNull(prefix)) {
168 namespace = Namespace.get(uri);
169 }
170 else {
171 namespace = Namespace.get(prefix, uri);
172 }
173
174 QName qname = new QName(name, namespace);
175
176 _removeExisting(qname);
177 }
178
179 public void store() throws Exception {
180 if (_document != null) {
181 String xml = XMLFormatter.toString(
182 _document, StringPool.FOUR_SPACES);
183
184 setProps(xml);
185
186 _document = null;
187 }
188 }
189
190 private Document _getPropsDocument() throws DocumentException {
191 if (_document == null) {
192 SAXReader reader = new SAXReader();
193
194 _document = reader.read(new StringReader(getProps()));
195 }
196
197 return _document;
198 }
199
200 private Element _removeExisting(QName qname) throws Exception {
201 Document doc = _getPropsDocument();
202
203 Element root = doc.getRootElement();
204
205 Iterator<Element> itr = root.elements(qname).iterator();
206
207 while (itr.hasNext()) {
208 Element el = itr.next();
209
210 root.remove(el);
211 }
212
213 return root;
214 }
215
216 private static final String _PROPS = "<properties />";
217
218 private Document _document = null;
219
220 }