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