1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.model.impl;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  import com.liferay.portal.kernel.util.Tuple;
19  import com.liferay.portal.kernel.util.Validator;
20  import com.liferay.portal.kernel.xml.Document;
21  import com.liferay.portal.kernel.xml.DocumentException;
22  import com.liferay.portal.kernel.xml.Element;
23  import com.liferay.portal.kernel.xml.Namespace;
24  import com.liferay.portal.kernel.xml.QName;
25  import com.liferay.portal.kernel.xml.SAXReaderUtil;
26  import com.liferay.portal.model.WebDAVProps;
27  import com.liferay.portal.webdav.WebDAVUtil;
28  
29  import java.util.HashSet;
30  import java.util.Iterator;
31  import java.util.Set;
32  
33  /**
34   * <a href="WebDAVPropsImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Alexander Chow
37   */
38  public class WebDAVPropsImpl
39      extends WebDAVPropsModelImpl implements WebDAVProps {
40  
41      public WebDAVPropsImpl() {
42      }
43  
44      public String getProps() {
45          String props = super.getProps();
46  
47          if (Validator.isNull(props)) {
48              return _PROPS;
49          }
50          else {
51              return props;
52          }
53      }
54  
55      public Set<Tuple> getPropsSet() throws Exception {
56          Set<Tuple> propsSet = new HashSet<Tuple>();
57  
58          Document doc = _getPropsDocument();
59  
60          Element root = doc.getRootElement();
61  
62          for (Element el : root.elements()) {
63              String prefix = el.getNamespacePrefix();
64              String uri = el.getNamespaceURI();
65  
66              Namespace namespace = null;
67  
68              if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
69                  namespace = WebDAVUtil.DAV_URI;
70              }
71              else if (Validator.isNull(prefix)) {
72                  namespace = SAXReaderUtil.createNamespace(uri);
73              }
74              else {
75                  namespace = SAXReaderUtil.createNamespace(prefix, uri);
76              }
77  
78              propsSet.add(new Tuple(el.getName(), namespace));
79          }
80  
81          return propsSet;
82      }
83  
84      public String getText(String name, String prefix, String uri)
85          throws Exception {
86  
87          Namespace namespace = null;
88  
89          if (Validator.isNull(prefix)) {
90              namespace = SAXReaderUtil.createNamespace(uri);
91          }
92          else {
93              namespace = SAXReaderUtil.createNamespace(prefix, uri);
94          }
95  
96          QName qname = SAXReaderUtil.createQName(name, namespace);
97  
98          Document doc = _getPropsDocument();
99  
100         Element root = doc.getRootElement();
101 
102         Element prop = root.element(qname);
103 
104         return prop.getText();
105     }
106 
107     public void addProp(String name, String prefix, String uri)
108         throws Exception {
109 
110         Namespace namespace = null;
111 
112         if (Validator.isNull(prefix)) {
113             namespace = SAXReaderUtil.createNamespace(uri);
114         }
115         else {
116             namespace = SAXReaderUtil.createNamespace(prefix, uri);
117         }
118 
119         QName qname = SAXReaderUtil.createQName(name, namespace);
120 
121         Element root = _removeExisting(qname);
122 
123         root.addElement(qname);
124     }
125 
126     public void addProp(String name, String prefix, String uri, String text)
127         throws Exception {
128 
129         Namespace namespace = null;
130 
131         if (Validator.isNull(prefix)) {
132             namespace = SAXReaderUtil.createNamespace(uri);
133         }
134         else {
135             namespace = SAXReaderUtil.createNamespace(prefix, uri);
136         }
137 
138         QName qname = SAXReaderUtil.createQName(name, namespace);
139 
140         Element root = _removeExisting(qname);
141 
142         root.addElement(qname).addText(text);
143     }
144 
145     public void removeProp(String name, String prefix, String uri)
146         throws Exception {
147 
148         Namespace namespace = null;
149 
150         if (Validator.isNull(prefix)) {
151             namespace = SAXReaderUtil.createNamespace(uri);
152         }
153         else {
154             namespace = SAXReaderUtil.createNamespace(prefix, uri);
155         }
156 
157         QName qname = SAXReaderUtil.createQName(name, namespace);
158 
159         _removeExisting(qname);
160     }
161 
162     public void store() throws Exception {
163         if (_document != null) {
164             String xml = _document.formattedString(StringPool.FOUR_SPACES);
165 
166             setProps(xml);
167 
168             _document = null;
169         }
170     }
171 
172     private Document _getPropsDocument() throws DocumentException {
173         if (_document == null) {
174             _document = SAXReaderUtil.read(getProps());
175         }
176 
177         return _document;
178     }
179 
180     private Element _removeExisting(QName qname) throws Exception {
181         Document doc = _getPropsDocument();
182 
183         Element root = doc.getRootElement();
184 
185         Iterator<Element> itr = root.elements(qname).iterator();
186 
187         while (itr.hasNext()) {
188             Element el = itr.next();
189 
190             root.remove(el);
191         }
192 
193         return root;
194     }
195 
196     private static final String _PROPS = "<properties />";
197 
198     private Document _document = null;
199 
200 }