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