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