001    /**
002     * Copyright (c) 2000-2013 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 void addProp(String name, String prefix, String uri)
041                    throws Exception {
042    
043                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
044    
045                    QName qname = SAXReaderUtil.createQName(name, namespace);
046    
047                    Element root = _removeExisting(qname);
048    
049                    root.addElement(qname);
050            }
051    
052            @Override
053            public void addProp(String name, String prefix, String uri, String text)
054                    throws Exception {
055    
056                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
057    
058                    QName qname = SAXReaderUtil.createQName(name, namespace);
059    
060                    Element root = _removeExisting(qname);
061    
062                    root.addElement(qname).addText(text);
063            }
064    
065            @Override
066            public String getProps() {
067                    String props = super.getProps();
068    
069                    if (Validator.isNull(props)) {
070                            return _PROPS;
071                    }
072                    else {
073                            return props;
074                    }
075            }
076    
077            @Override
078            public Set<QName> getPropsSet() throws Exception {
079                    Set<QName> propsSet = new HashSet<QName>();
080    
081                    Document doc = _getPropsDocument();
082    
083                    Element root = doc.getRootElement();
084    
085                    for (Element el : root.elements()) {
086                            String prefix = el.getNamespacePrefix();
087                            String uri = el.getNamespaceURI();
088    
089                            Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
090    
091                            propsSet.add(SAXReaderUtil.createQName(el.getName(), namespace));
092                    }
093    
094                    return propsSet;
095            }
096    
097            @Override
098            public String getText(String name, String prefix, String uri)
099                    throws Exception {
100    
101                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
102    
103                    QName qname = SAXReaderUtil.createQName(name, namespace);
104    
105                    Document doc = _getPropsDocument();
106    
107                    Element root = doc.getRootElement();
108    
109                    Element prop = root.element(qname);
110    
111                    return prop.getText();
112            }
113    
114            @Override
115            public void removeProp(String name, String prefix, String uri)
116                    throws Exception {
117    
118                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
119    
120                    QName qname = SAXReaderUtil.createQName(name, namespace);
121    
122                    _removeExisting(qname);
123            }
124    
125            @Override
126            public void store() throws Exception {
127                    if (_document != null) {
128                            String xml = _document.formattedString(StringPool.FOUR_SPACES);
129    
130                            setProps(xml);
131    
132                            _document = null;
133                    }
134            }
135    
136            private Document _getPropsDocument() throws DocumentException {
137                    if (_document == null) {
138                            _document = SAXReaderUtil.read(getProps());
139                    }
140    
141                    return _document;
142            }
143    
144            private Element _removeExisting(QName qname) throws Exception {
145                    Document doc = _getPropsDocument();
146    
147                    Element root = doc.getRootElement();
148    
149                    Iterator<Element> itr = root.elements(qname).iterator();
150    
151                    while (itr.hasNext()) {
152                            Element el = itr.next();
153    
154                            root.remove(el);
155                    }
156    
157                    return root;
158            }
159    
160            private static final String _PROPS = "<properties />";
161    
162            private Document _document = null;
163    
164    }