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            public String getKey(QName qName) {
043                    return getKey(qName.getNamespaceURI(), qName.getLocalPart());
044            }
045    
046            public String getKey(String uri, String localPart) {
047                    return uri.concat(_KEY_SEPARATOR).concat(localPart);
048            }
049    
050            public String getPublicRenderParameterIdentifier(
051                    String publicRenderParameterName) {
052    
053                    if (!publicRenderParameterName.startsWith(
054                                    PUBLIC_RENDER_PARAMETER_NAMESPACE) &&
055                            !publicRenderParameterName.startsWith(
056                                    REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
057    
058                            return null;
059                    }
060    
061                    return _identifiers.get(publicRenderParameterName);
062            }
063    
064            public String getPublicRenderParameterName(QName qName) {
065                    StringBundler sb = new StringBundler(4);
066    
067                    sb.append(PUBLIC_RENDER_PARAMETER_NAMESPACE);
068                    sb.append(qName.getNamespaceURI().hashCode());
069                    sb.append(StringPool.UNDERLINE);
070                    sb.append(qName.getLocalPart());
071    
072                    String publicRenderParameterName = sb.toString();
073    
074                    if (!_qNames.containsKey(publicRenderParameterName)) {
075                            _qNames.put(publicRenderParameterName, qName);
076                    }
077    
078                    return publicRenderParameterName;
079            }
080    
081            public QName getQName(
082                    Element qNameEl, Element nameEl, String defaultNamespace) {
083    
084                    if ((qNameEl == null) && (nameEl == null)) {
085                            _log.error("both qname and name elements are null");
086    
087                            return null;
088                    }
089    
090                    if (qNameEl == null) {
091                            return SAXReaderUtil.createQName(
092                                    nameEl.getTextTrim(),
093                                    SAXReaderUtil.createNamespace(defaultNamespace));
094                    }
095    
096                    String localPart = qNameEl.getTextTrim();
097    
098                    int pos = localPart.indexOf(CharPool.COLON);
099    
100                    if (pos == -1) {
101                            if (_log.isDebugEnabled()) {
102                                    _log.debug("qname " + localPart + " does not have a prefix");
103                            }
104    
105                            return SAXReaderUtil.createQName(localPart);
106                    }
107    
108                    String prefix = localPart.substring(0, pos);
109    
110                    Namespace namespace = qNameEl.getNamespaceForPrefix(prefix);
111    
112                    if (namespace == null) {
113                            if (_log.isWarnEnabled()) {
114                                    _log.warn(
115                                            "qname " + localPart + " does not have a valid namespace");
116                            }
117    
118                            return null;
119                    }
120    
121                    localPart = localPart.substring(prefix.length() + 1);
122    
123                    return SAXReaderUtil.createQName(localPart, namespace);
124            }
125    
126            public QName getQName(String publicRenderParameterName) {
127                    if (!publicRenderParameterName.startsWith(
128                                    PUBLIC_RENDER_PARAMETER_NAMESPACE) &&
129                            !publicRenderParameterName.startsWith(
130                                    REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
131    
132                            return null;
133                    }
134    
135                    return _qNames.get(publicRenderParameterName);
136            }
137    
138            public String getRemovePublicRenderParameterName(QName qName) {
139                    StringBundler sb = new StringBundler(4);
140    
141                    sb.append(REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE);
142                    sb.append(qName.getNamespaceURI().hashCode());
143                    sb.append(StringPool.UNDERLINE);
144                    sb.append(qName.getLocalPart());
145    
146                    String removePublicRenderParameterName = sb.toString();
147    
148                    if (!_qNames.containsKey(removePublicRenderParameterName)) {
149                            _qNames.put(removePublicRenderParameterName, qName);
150                    }
151    
152                    return removePublicRenderParameterName;
153            }
154    
155            public void setPublicRenderParameterIdentifier(
156                    String publicRenderParameterName, String identifier) {
157    
158                    _identifiers.put(publicRenderParameterName, identifier);
159            }
160    
161            private static final String _KEY_SEPARATOR = "_KEY_";
162    
163            private static Log _log = LogFactoryUtil.getLog(PortletQNameImpl.class);
164    
165            private Map<String, String> _identifiers;
166            private Map<String, QName> _qNames;
167    
168    }