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.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
020    import com.liferay.portal.kernel.util.CharPool;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.kernel.xml.Namespace;
025    import com.liferay.portal.kernel.xml.QName;
026    import com.liferay.portal.kernel.xml.SAXReaderUtil;
027    
028    import java.util.Map;
029    import java.util.concurrent.ConcurrentHashMap;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    @DoPrivileged
035    public class PortletQNameImpl implements PortletQName {
036    
037            public PortletQNameImpl() {
038                    _qNames = new ConcurrentHashMap<String, QName>();
039                    _identifiers = new ConcurrentHashMap<String, String>();
040            }
041    
042            @Override
043            public String getKey(QName qName) {
044                    return getKey(qName.getNamespaceURI(), qName.getLocalPart());
045            }
046    
047            @Override
048            public String getKey(String uri, String localPart) {
049                    return uri.concat(_KEY_SEPARATOR).concat(localPart);
050            }
051    
052            @Override
053            public String getPublicRenderParameterIdentifier(
054                    String publicRenderParameterName) {
055    
056                    if (!publicRenderParameterName.startsWith(
057                                    PUBLIC_RENDER_PARAMETER_NAMESPACE) &&
058                            !publicRenderParameterName.startsWith(
059                                    REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
060    
061                            return null;
062                    }
063    
064                    return _identifiers.get(publicRenderParameterName);
065            }
066    
067            @Override
068            public String getPublicRenderParameterName(QName qName) {
069                    StringBundler sb = new StringBundler(4);
070    
071                    sb.append(PUBLIC_RENDER_PARAMETER_NAMESPACE);
072                    sb.append(qName.getNamespaceURI().hashCode());
073                    sb.append(StringPool.UNDERLINE);
074                    sb.append(qName.getLocalPart());
075    
076                    String publicRenderParameterName = sb.toString();
077    
078                    if (!_qNames.containsKey(publicRenderParameterName)) {
079                            _qNames.put(publicRenderParameterName, qName);
080                    }
081    
082                    return publicRenderParameterName;
083            }
084    
085            @Override
086            public QName getQName(
087                    Element qNameEl, Element nameEl, String defaultNamespace) {
088    
089                    if ((qNameEl == null) && (nameEl == null)) {
090                            _log.error("both qname and name elements are null");
091    
092                            return null;
093                    }
094    
095                    if (qNameEl == null) {
096                            return SAXReaderUtil.createQName(
097                                    nameEl.getTextTrim(),
098                                    SAXReaderUtil.createNamespace(defaultNamespace));
099                    }
100    
101                    String localPart = qNameEl.getTextTrim();
102    
103                    int pos = localPart.indexOf(CharPool.COLON);
104    
105                    if (pos == -1) {
106                            if (_log.isDebugEnabled()) {
107                                    _log.debug("qname " + localPart + " does not have a prefix");
108                            }
109    
110                            return SAXReaderUtil.createQName(localPart);
111                    }
112    
113                    String prefix = localPart.substring(0, pos);
114    
115                    Namespace namespace = qNameEl.getNamespaceForPrefix(prefix);
116    
117                    if (namespace == null) {
118                            if (_log.isWarnEnabled()) {
119                                    _log.warn(
120                                            "qname " + localPart + " does not have a valid namespace");
121                            }
122    
123                            return null;
124                    }
125    
126                    localPart = localPart.substring(prefix.length() + 1);
127    
128                    return SAXReaderUtil.createQName(localPart, namespace);
129            }
130    
131            @Override
132            public QName getQName(String publicRenderParameterName) {
133                    if (!publicRenderParameterName.startsWith(
134                                    PUBLIC_RENDER_PARAMETER_NAMESPACE) &&
135                            !publicRenderParameterName.startsWith(
136                                    REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
137    
138                            return null;
139                    }
140    
141                    return _qNames.get(publicRenderParameterName);
142            }
143    
144            @Override
145            public String getRemovePublicRenderParameterName(QName qName) {
146                    StringBundler sb = new StringBundler(4);
147    
148                    sb.append(REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE);
149                    sb.append(qName.getNamespaceURI().hashCode());
150                    sb.append(StringPool.UNDERLINE);
151                    sb.append(qName.getLocalPart());
152    
153                    String removePublicRenderParameterName = sb.toString();
154    
155                    if (!_qNames.containsKey(removePublicRenderParameterName)) {
156                            _qNames.put(removePublicRenderParameterName, qName);
157                    }
158    
159                    return removePublicRenderParameterName;
160            }
161    
162            @Override
163            public void setPublicRenderParameterIdentifier(
164                    String publicRenderParameterName, String identifier) {
165    
166                    _identifiers.put(publicRenderParameterName, identifier);
167            }
168    
169            private static final String _KEY_SEPARATOR = "_KEY_";
170    
171            private static Log _log = LogFactoryUtil.getLog(PortletQNameImpl.class);
172    
173            private Map<String, String> _identifiers;
174            private Map<String, QName> _qNames;
175    
176    }