001
014
015 package com.liferay.portal.model.impl;
016
017 import com.liferay.portal.kernel.util.StringPool;
018 import com.liferay.portal.kernel.util.Tuple;
019 import com.liferay.portal.kernel.util.Validator;
020 import com.liferay.portal.kernel.webdav.WebDAVUtil;
021 import com.liferay.portal.kernel.xml.Document;
022 import com.liferay.portal.kernel.xml.DocumentException;
023 import com.liferay.portal.kernel.xml.Element;
024 import com.liferay.portal.kernel.xml.Namespace;
025 import com.liferay.portal.kernel.xml.QName;
026 import com.liferay.portal.kernel.xml.SAXReaderUtil;
027 import com.liferay.portal.model.WebDAVProps;
028
029 import java.util.HashSet;
030 import java.util.Iterator;
031 import java.util.Set;
032
033
036 public class WebDAVPropsImpl
037 extends WebDAVPropsModelImpl implements WebDAVProps {
038
039 public WebDAVPropsImpl() {
040 }
041
042 public String getProps() {
043 String props = super.getProps();
044
045 if (Validator.isNull(props)) {
046 return _PROPS;
047 }
048 else {
049 return props;
050 }
051 }
052
053 public Set<Tuple> getPropsSet() throws Exception {
054 Set<Tuple> propsSet = new HashSet<Tuple>();
055
056 Document doc = _getPropsDocument();
057
058 Element root = doc.getRootElement();
059
060 for (Element el : root.elements()) {
061 String prefix = el.getNamespacePrefix();
062 String uri = el.getNamespaceURI();
063
064 Namespace namespace = null;
065
066 if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
067 namespace = WebDAVUtil.DAV_URI;
068 }
069 else if (Validator.isNull(prefix)) {
070 namespace = SAXReaderUtil.createNamespace(uri);
071 }
072 else {
073 namespace = SAXReaderUtil.createNamespace(prefix, uri);
074 }
075
076 propsSet.add(new Tuple(el.getName(), namespace));
077 }
078
079 return propsSet;
080 }
081
082 public String getText(String name, String prefix, String uri)
083 throws Exception {
084
085 Namespace namespace = null;
086
087 if (Validator.isNull(prefix)) {
088 namespace = SAXReaderUtil.createNamespace(uri);
089 }
090 else {
091 namespace = SAXReaderUtil.createNamespace(prefix, uri);
092 }
093
094 QName qname = SAXReaderUtil.createQName(name, namespace);
095
096 Document doc = _getPropsDocument();
097
098 Element root = doc.getRootElement();
099
100 Element prop = root.element(qname);
101
102 return prop.getText();
103 }
104
105 public void addProp(String name, String prefix, String uri)
106 throws Exception {
107
108 Namespace namespace = null;
109
110 if (Validator.isNull(prefix)) {
111 namespace = SAXReaderUtil.createNamespace(uri);
112 }
113 else {
114 namespace = SAXReaderUtil.createNamespace(prefix, uri);
115 }
116
117 QName qname = SAXReaderUtil.createQName(name, namespace);
118
119 Element root = _removeExisting(qname);
120
121 root.addElement(qname);
122 }
123
124 public void addProp(String name, String prefix, String uri, String text)
125 throws Exception {
126
127 Namespace namespace = null;
128
129 if (Validator.isNull(prefix)) {
130 namespace = SAXReaderUtil.createNamespace(uri);
131 }
132 else {
133 namespace = SAXReaderUtil.createNamespace(prefix, uri);
134 }
135
136 QName qname = SAXReaderUtil.createQName(name, namespace);
137
138 Element root = _removeExisting(qname);
139
140 root.addElement(qname).addText(text);
141 }
142
143 public void removeProp(String name, String prefix, String uri)
144 throws Exception {
145
146 Namespace namespace = null;
147
148 if (Validator.isNull(prefix)) {
149 namespace = SAXReaderUtil.createNamespace(uri);
150 }
151 else {
152 namespace = SAXReaderUtil.createNamespace(prefix, uri);
153 }
154
155 QName qname = SAXReaderUtil.createQName(name, namespace);
156
157 _removeExisting(qname);
158 }
159
160 public void store() throws Exception {
161 if (_document != null) {
162 String xml = _document.formattedString(StringPool.FOUR_SPACES);
163
164 setProps(xml);
165
166 _document = null;
167 }
168 }
169
170 private Document _getPropsDocument() throws DocumentException {
171 if (_document == null) {
172 _document = SAXReaderUtil.read(getProps());
173 }
174
175 return _document;
176 }
177
178 private Element _removeExisting(QName qname) throws Exception {
179 Document doc = _getPropsDocument();
180
181 Element root = doc.getRootElement();
182
183 Iterator<Element> itr = root.elements(qname).iterator();
184
185 while (itr.hasNext()) {
186 Element el = itr.next();
187
188 root.remove(el);
189 }
190
191 return root;
192 }
193
194 private static final String _PROPS = "<properties />";
195
196 private Document _document = null;
197
198 }