001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.model.impl;
022:
023: import com.liferay.portal.kernel.util.StringPool;
024: import com.liferay.portal.kernel.util.Tuple;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.model.WebDAVProps;
027: import com.liferay.portal.webdav.WebDAVUtil;
028: import com.liferay.util.xml.XMLFormatter;
029:
030: import java.io.StringReader;
031:
032: import java.util.HashSet;
033: import java.util.Iterator;
034: import java.util.Set;
035:
036: import org.dom4j.Document;
037: import org.dom4j.DocumentException;
038: import org.dom4j.Element;
039: import org.dom4j.Namespace;
040: import org.dom4j.QName;
041: import org.dom4j.io.SAXReader;
042:
043: /**
044: * <a href="WebDAVPropsImpl.java.html"><b><i>View Source</i></b></a>
045: *
046: * @author Alexander Chow
047: *
048: */
049: public class WebDAVPropsImpl extends WebDAVPropsModelImpl implements
050: WebDAVProps {
051:
052: public WebDAVPropsImpl() {
053: }
054:
055: public String getProps() {
056: String props = super .getProps();
057:
058: if (Validator.isNull(props)) {
059: return _PROPS;
060: } else {
061: return props;
062: }
063: }
064:
065: public Set getPropsSet() throws Exception {
066: Set propsSet = new HashSet();
067:
068: Document doc = _getPropsDocument();
069:
070: Element root = doc.getRootElement();
071:
072: Iterator itr = root.elements().iterator();
073:
074: while (itr.hasNext()) {
075: Element el = (Element) itr.next();
076:
077: String prefix = el.getNamespacePrefix();
078: String uri = el.getNamespaceURI();
079:
080: Namespace namespace = null;
081:
082: if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
083: namespace = WebDAVUtil.DAV_URI;
084: } else if (Validator.isNull(prefix)) {
085: namespace = Namespace.get(uri);
086: } else {
087: namespace = Namespace.get(prefix, uri);
088: }
089:
090: propsSet.add(new Tuple(el.getName(), namespace));
091: }
092:
093: return propsSet;
094: }
095:
096: public String getText(String name, String prefix, String uri)
097: throws Exception {
098:
099: Namespace namespace = null;
100:
101: if (Validator.isNull(prefix)) {
102: namespace = Namespace.get(uri);
103: } else {
104: namespace = Namespace.get(prefix, uri);
105: }
106:
107: QName qname = new QName(name, namespace);
108:
109: Document doc = _getPropsDocument();
110:
111: Element root = doc.getRootElement();
112:
113: Element prop = root.element(qname);
114:
115: return prop.getText();
116: }
117:
118: public void addProp(String name, String prefix, String uri)
119: throws Exception {
120:
121: Namespace namespace = null;
122:
123: if (Validator.isNull(prefix)) {
124: namespace = Namespace.get(uri);
125: } else {
126: namespace = Namespace.get(prefix, uri);
127: }
128:
129: QName qname = new QName(name, namespace);
130:
131: Element root = _removeExisting(qname);
132:
133: root.addElement(qname);
134: }
135:
136: public void addProp(String name, String prefix, String uri,
137: String text) throws Exception {
138:
139: Namespace namespace = null;
140:
141: if (Validator.isNull(prefix)) {
142: namespace = Namespace.get(uri);
143: } else {
144: namespace = Namespace.get(prefix, uri);
145: }
146:
147: QName qname = new QName(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 = Namespace.get(uri);
161: } else {
162: namespace = Namespace.get(prefix, uri);
163: }
164:
165: QName qname = new QName(name, namespace);
166:
167: _removeExisting(qname);
168: }
169:
170: public void store() throws Exception {
171: if (_document != null) {
172: String xml = XMLFormatter.toString(_document,
173: 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: SAXReader reader = new SAXReader();
184:
185: _document = reader.read(new StringReader(getProps()));
186: }
187:
188: return _document;
189: }
190:
191: private Element _removeExisting(QName qname) throws Exception {
192: Document doc = _getPropsDocument();
193:
194: Element root = doc.getRootElement();
195:
196: Iterator itr = root.elements(qname).iterator();
197:
198: while (itr.hasNext()) {
199: Element el = (Element) itr.next();
200:
201: root.remove(el);
202: }
203:
204: return root;
205: }
206:
207: private static final String _PROPS = "<properties />";
208:
209: private Document _document = null;
210:
211: }
|