001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(String publicRenderParameterName) {
080                    if (!publicRenderParameterName.startsWith(
081                                    PUBLIC_RENDER_PARAMETER_NAMESPACE) &&
082                            !publicRenderParameterName.startsWith(
083                                    REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
084    
085                            return null;
086                    }
087    
088                    return _qNames.get(publicRenderParameterName);
089            }
090    
091            public QName getQName(
092                    Element qNameEl, Element nameEl, String defaultNamespace) {
093    
094                    if ((qNameEl == null) && (nameEl == null)) {
095                            _log.error("both qname and name elements are null");
096    
097                            return null;
098                    }
099    
100                    if (qNameEl == null) {
101                            return SAXReaderUtil.createQName(
102                                    nameEl.getTextTrim(),
103                                    SAXReaderUtil.createNamespace(defaultNamespace));
104                    }
105    
106                    String localPart = qNameEl.getTextTrim();
107    
108                    int pos = localPart.indexOf(CharPool.COLON);
109    
110                    if (pos == -1) {
111                            if (_log.isDebugEnabled()) {
112                                    _log.debug("qname " + localPart + " does not have a prefix");
113                            }
114    
115                            return SAXReaderUtil.createQName(localPart);
116                    }
117    
118                    String prefix = localPart.substring(0, pos);
119    
120                    Namespace namespace = qNameEl.getNamespaceForPrefix(prefix);
121    
122                    if (namespace == null) {
123                            if (_log.isWarnEnabled()) {
124                                    _log.warn(
125                                            "qname " + localPart + " does not have a valid namespace");
126                            }
127    
128                            return null;
129                    }
130    
131                    localPart = localPart.substring(prefix.length() + 1);
132    
133                    return SAXReaderUtil.createQName(localPart, namespace);
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, QName> _qNames;
164            private Map<String, String> _identifiers;
165    
166    }