001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.model.impl;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.kernel.webdav.WebDAVUtil;
020    import com.liferay.portal.kernel.xml.Document;
021    import com.liferay.portal.kernel.xml.DocumentException;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.kernel.xml.Namespace;
024    import com.liferay.portal.kernel.xml.QName;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    
027    import java.util.HashSet;
028    import java.util.Iterator;
029    import java.util.Set;
030    
031    /**
032     * @author Alexander Chow
033     */
034    public class WebDAVPropsImpl extends WebDAVPropsBaseImpl {
035    
036            public WebDAVPropsImpl() {
037            }
038    
039            @Override
040            public String getProps() {
041                    String props = super.getProps();
042    
043                    if (Validator.isNull(props)) {
044                            return _PROPS;
045                    }
046                    else {
047                            return props;
048                    }
049            }
050    
051            public Set<QName> getPropsSet() throws Exception {
052                    Set<QName> propsSet = new HashSet<QName>();
053    
054                    Document doc = _getPropsDocument();
055    
056                    Element root = doc.getRootElement();
057    
058                    for (Element el : root.elements()) {
059                            String prefix = el.getNamespacePrefix();
060                            String uri = el.getNamespaceURI();
061    
062                            Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
063    
064                            propsSet.add(SAXReaderUtil.createQName(el.getName(), namespace));
065                    }
066    
067                    return propsSet;
068            }
069    
070            public String getText(String name, String prefix, String uri)
071                    throws Exception {
072    
073                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
074    
075                    QName qname = SAXReaderUtil.createQName(name, namespace);
076    
077                    Document doc = _getPropsDocument();
078    
079                    Element root = doc.getRootElement();
080    
081                    Element prop = root.element(qname);
082    
083                    return prop.getText();
084            }
085    
086            public void addProp(String name, String prefix, String uri)
087                    throws Exception {
088    
089                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
090    
091                    QName qname = SAXReaderUtil.createQName(name, namespace);
092    
093                    Element root = _removeExisting(qname);
094    
095                    root.addElement(qname);
096            }
097    
098            public void addProp(String name, String prefix, String uri, String text)
099                    throws Exception {
100    
101                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
102    
103                    QName qname = SAXReaderUtil.createQName(name, namespace);
104    
105                    Element root = _removeExisting(qname);
106    
107                    root.addElement(qname).addText(text);
108            }
109    
110            public void removeProp(String name, String prefix, String uri)
111                    throws Exception {
112    
113                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
114    
115                    QName qname = SAXReaderUtil.createQName(name, namespace);
116    
117                    _removeExisting(qname);
118            }
119    
120            public void store() throws Exception {
121                    if (_document != null) {
122                            String xml = _document.formattedString(StringPool.FOUR_SPACES);
123    
124                            setProps(xml);
125    
126                            _document = null;
127                    }
128            }
129    
130            private Document _getPropsDocument() throws DocumentException {
131                    if (_document == null) {
132                            _document = SAXReaderUtil.read(getProps());
133                    }
134    
135                    return _document;
136            }
137    
138            private Element _removeExisting(QName qname) throws Exception {
139                    Document doc = _getPropsDocument();
140    
141                    Element root = doc.getRootElement();
142    
143                    Iterator<Element> itr = root.elements(qname).iterator();
144    
145                    while (itr.hasNext()) {
146                            Element el = itr.next();
147    
148                            root.remove(el);
149                    }
150    
151                    return root;
152            }
153    
154            private static final String _PROPS = "<properties />";
155    
156            private Document _document = null;
157    
158    }