001    /**
002     * Copyright (c) 2000-2012 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            public void addProp(String name, String prefix, String uri)
040                    throws Exception {
041    
042                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
043    
044                    QName qname = SAXReaderUtil.createQName(name, namespace);
045    
046                    Element root = _removeExisting(qname);
047    
048                    root.addElement(qname);
049            }
050    
051            public void addProp(String name, String prefix, String uri, String text)
052                    throws Exception {
053    
054                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
055    
056                    QName qname = SAXReaderUtil.createQName(name, namespace);
057    
058                    Element root = _removeExisting(qname);
059    
060                    root.addElement(qname).addText(text);
061            }
062    
063            @Override
064            public String getProps() {
065                    String props = super.getProps();
066    
067                    if (Validator.isNull(props)) {
068                            return _PROPS;
069                    }
070                    else {
071                            return props;
072                    }
073            }
074    
075            public Set<QName> getPropsSet() throws Exception {
076                    Set<QName> propsSet = new HashSet<QName>();
077    
078                    Document doc = _getPropsDocument();
079    
080                    Element root = doc.getRootElement();
081    
082                    for (Element el : root.elements()) {
083                            String prefix = el.getNamespacePrefix();
084                            String uri = el.getNamespaceURI();
085    
086                            Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
087    
088                            propsSet.add(SAXReaderUtil.createQName(el.getName(), namespace));
089                    }
090    
091                    return propsSet;
092            }
093    
094            public String getText(String name, String prefix, String uri)
095                    throws Exception {
096    
097                    Namespace namespace = WebDAVUtil.createNamespace(prefix, uri);
098    
099                    QName qname = SAXReaderUtil.createQName(name, namespace);
100    
101                    Document doc = _getPropsDocument();
102    
103                    Element root = doc.getRootElement();
104    
105                    Element prop = root.element(qname);
106    
107                    return prop.getText();
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    }