1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.xml.Element;
22  import com.liferay.portal.kernel.xml.Namespace;
23  import com.liferay.portal.kernel.xml.QName;
24  import com.liferay.portal.kernel.xml.SAXReaderUtil;
25  
26  import java.util.Map;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  /**
30   * <a href="PortletQNameImpl.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class PortletQNameImpl implements PortletQName {
35  
36      public PortletQNameImpl() {
37          _qNames = new ConcurrentHashMap<String, QName>();
38          _identifiers = new ConcurrentHashMap<String, String>();
39      }
40  
41      public String getKey(QName qName) {
42          return getKey(qName.getNamespaceURI(), qName.getLocalPart());
43      }
44  
45      public String getKey(String uri, String localPart) {
46          return uri.concat(_KEY_SEPARATOR).concat(localPart);
47      }
48  
49      public String getPublicRenderParameterIdentifier(
50          String publicRenderParameterName) {
51  
52          if (!publicRenderParameterName.startsWith(
53                  PUBLIC_RENDER_PARAMETER_NAMESPACE) &&
54              !publicRenderParameterName.startsWith(
55                  REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
56  
57              return null;
58          }
59  
60          return _identifiers.get(publicRenderParameterName);
61      }
62  
63      public String getPublicRenderParameterName(QName qName) {
64          StringBundler sb = new StringBundler(4);
65  
66          sb.append(PUBLIC_RENDER_PARAMETER_NAMESPACE);
67          sb.append(qName.getNamespaceURI().hashCode());
68          sb.append(StringPool.UNDERLINE);
69          sb.append(qName.getLocalPart());
70  
71          String publicRenderParameterName = sb.toString();
72  
73          if (!_qNames.containsKey(publicRenderParameterName)) {
74              _qNames.put(publicRenderParameterName, qName);
75          }
76  
77          return publicRenderParameterName;
78      }
79  
80      public QName getQName(String publicRenderParameterName) {
81          if (!publicRenderParameterName.startsWith(
82                  PUBLIC_RENDER_PARAMETER_NAMESPACE) &&
83              !publicRenderParameterName.startsWith(
84                  REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
85  
86              return null;
87          }
88  
89          return _qNames.get(publicRenderParameterName);
90      }
91  
92      public QName getQName(
93          Element qNameEl, Element nameEl, String defaultNamespace) {
94  
95          if ((qNameEl == null) && (nameEl == null)) {
96              _log.error("both qname and name elements are null");
97  
98              return null;
99          }
100 
101         if (qNameEl == null) {
102             return SAXReaderUtil.createQName(
103                 nameEl.getTextTrim(),
104                 SAXReaderUtil.createNamespace(defaultNamespace));
105         }
106 
107         String localPart = qNameEl.getTextTrim();
108 
109         int pos = localPart.indexOf(StringPool.COLON);
110 
111         if (pos == -1) {
112             if (_log.isDebugEnabled()) {
113                 _log.debug("qname " + localPart + " does not have a prefix");
114             }
115 
116             return SAXReaderUtil.createQName(localPart);
117         }
118 
119         String prefix = localPart.substring(0, pos);
120 
121         Namespace namespace = qNameEl.getNamespaceForPrefix(prefix);
122 
123         if (namespace == null) {
124             if (_log.isWarnEnabled()) {
125                 _log.warn(
126                     "qname " + localPart + " does not have a valid namespace");
127             }
128 
129             return null;
130         }
131 
132         localPart = localPart.substring(prefix.length() + 1);
133 
134         return SAXReaderUtil.createQName(localPart, namespace);
135     }
136 
137     public String getRemovePublicRenderParameterName(QName qName) {
138         StringBundler sb = new StringBundler(4);
139 
140         sb.append(REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE);
141         sb.append(qName.getNamespaceURI().hashCode());
142         sb.append(StringPool.UNDERLINE);
143         sb.append(qName.getLocalPart());
144 
145         String removePublicRenderParameterName = sb.toString();
146 
147         if (!_qNames.containsKey(removePublicRenderParameterName)) {
148             _qNames.put(removePublicRenderParameterName, qName);
149         }
150 
151         return removePublicRenderParameterName;
152     }
153 
154     public void setPublicRenderParameterIdentifier(
155         String publicRenderParameterName, String identifier) {
156 
157         _identifiers.put(publicRenderParameterName, identifier);
158     }
159 
160     private static final String _KEY_SEPARATOR = "_KEY_";
161 
162     private static Log _log = LogFactoryUtil.getLog(PortletQNameImpl.class);
163 
164     private Map<String, QName> _qNames;
165     private Map<String, String> _identifiers;
166 
167 }