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