1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.xml.Element;
29  import com.liferay.portal.kernel.xml.Namespace;
30  import com.liferay.portal.kernel.xml.QName;
31  import com.liferay.portal.kernel.xml.SAXReaderUtil;
32  
33  import java.util.Map;
34  import java.util.concurrent.ConcurrentHashMap;
35  
36  /**
37   * <a href="PortletQNameImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class PortletQNameImpl implements PortletQName {
42  
43      public PortletQNameImpl() {
44          _qNames = new ConcurrentHashMap<String, QName>();
45          _identifiers = new ConcurrentHashMap<String, String>();
46      }
47  
48      public String getKey(QName qName) {
49          return getKey(qName.getNamespaceURI(), qName.getLocalPart());
50      }
51  
52      public String getKey(String uri, String localPart) {
53          StringBuilder sb = new StringBuilder();
54  
55          sb.append(uri);
56          sb.append(_KEY_SEPARATOR);
57          sb.append(localPart);
58  
59          return sb.toString();
60      }
61  
62      public String getPublicRenderParameterIdentifier(
63          String publicRenderParameterName) {
64  
65          if (!publicRenderParameterName.startsWith(
66                  PUBLIC_RENDER_PARAMETER_NAMESPACE) &&
67              !publicRenderParameterName.startsWith(
68                  REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
69  
70              return null;
71          }
72  
73          return _identifiers.get(publicRenderParameterName);
74      }
75  
76      public String getPublicRenderParameterName(QName qName) {
77          StringBuilder sb = new StringBuilder();
78  
79          sb.append(PUBLIC_RENDER_PARAMETER_NAMESPACE);
80          sb.append(qName.getNamespaceURI().hashCode());
81          sb.append(StringPool.UNDERLINE);
82          sb.append(qName.getLocalPart());
83  
84          String publicRenderParameterName = sb.toString();
85  
86          if (!_qNames.containsKey(publicRenderParameterName)) {
87              _qNames.put(publicRenderParameterName, qName);
88          }
89  
90          return publicRenderParameterName;
91      }
92  
93      public QName getQName(String publicRenderParameterName) {
94          if (!publicRenderParameterName.startsWith(
95                  PUBLIC_RENDER_PARAMETER_NAMESPACE) &&
96              !publicRenderParameterName.startsWith(
97                  REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
98  
99              return null;
100         }
101 
102         return _qNames.get(publicRenderParameterName);
103     }
104 
105     public QName getQName(
106         Element qNameEl, Element nameEl, String defaultNamespace) {
107 
108         if ((qNameEl == null) && (nameEl == null)) {
109             _log.error("both qname and name elements are null");
110 
111             return null;
112         }
113 
114         if (qNameEl == null) {
115             return SAXReaderUtil.createQName(
116                 nameEl.getTextTrim(),
117                 SAXReaderUtil.createNamespace(defaultNamespace));
118         }
119 
120         String localPart = qNameEl.getTextTrim();
121 
122         int pos = localPart.indexOf(StringPool.COLON);
123 
124         if (pos == -1) {
125             if (_log.isDebugEnabled()) {
126                 _log.debug("qname " + localPart + " does not have a prefix");
127             }
128 
129             return SAXReaderUtil.createQName(localPart);
130         }
131 
132         String prefix = localPart.substring(0, pos);
133 
134         Namespace namespace = qNameEl.getNamespaceForPrefix(prefix);
135 
136         if (namespace == null) {
137             if (_log.isWarnEnabled()) {
138                 _log.warn(
139                     "qname " + localPart + " does not have a valid namespace");
140             }
141 
142             return null;
143         }
144 
145         localPart = localPart.substring(prefix.length() + 1);
146 
147         return SAXReaderUtil.createQName(localPart, namespace);
148     }
149 
150     public String getRemovePublicRenderParameterName(QName qName) {
151         StringBuilder sb = new StringBuilder();
152 
153         sb.append(REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE);
154         sb.append(qName.getNamespaceURI().hashCode());
155         sb.append(StringPool.UNDERLINE);
156         sb.append(qName.getLocalPart());
157 
158         String removePublicRenderParameterName = sb.toString();
159 
160         if (!_qNames.containsKey(removePublicRenderParameterName)) {
161             _qNames.put(removePublicRenderParameterName, qName);
162         }
163 
164         return removePublicRenderParameterName;
165     }
166 
167     public void setPublicRenderParameterIdentifier(
168         String publicRenderParameterName, String identifier) {
169 
170         _identifiers.put(publicRenderParameterName, identifier);
171     }
172 
173     private static final String _KEY_SEPARATOR = "_KEY_";
174 
175     private static Log _log = LogFactoryUtil.getLog(PortletQNameImpl.class);
176 
177     private Map<String, QName> _qNames;
178     private Map<String, String> _identifiers;
179 
180 }