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