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