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   */
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 }