1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
46   * <a href="WebDAVPropsImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Alexander Chow
49   *
50   */
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 }