1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.CharPool;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.xml.Element;
23  import com.liferay.portal.kernel.xml.Namespace;
24  import com.liferay.portal.kernel.xml.QName;
25  import com.liferay.portal.kernel.xml.SAXReaderUtil;
26  
27  import java.util.Map;
28  import java.util.concurrent.ConcurrentHashMap;
29  
30  /**
31   * <a href="PortletQNameImpl.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class PortletQNameImpl implements PortletQName {
36  
37      public PortletQNameImpl() {
38          _qNames = new ConcurrentHashMap<String, QName>();
39          _identifiers = new ConcurrentHashMap<String, String>();
40      }
41  
42      public String getKey(QName qName) {
43          return getKey(qName.getNamespaceURI(), qName.getLocalPart());
44      }
45  
46      public String getKey(String uri, String localPart) {
47          return uri.concat(_KEY_SEPARATOR).concat(localPart);
48      }
49  
50      public String getPublicRenderParameterIdentifier(
51          String publicRenderParameterName) {
52  
53          if (!publicRenderParameterName.startsWith(
54                  PUBLIC_RENDER_PARAMETER_NAMESPACE) &&
55              !publicRenderParameterName.startsWith(
56                  REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
57  
58              return null;
59          }
60  
61          return _identifiers.get(publicRenderParameterName);
62      }
63  
64      public String getPublicRenderParameterName(QName qName) {
65          StringBundler sb = new StringBundler(4);
66  
67          sb.append(PUBLIC_RENDER_PARAMETER_NAMESPACE);
68          sb.append(qName.getNamespaceURI().hashCode());
69          sb.append(StringPool.UNDERLINE);
70          sb.append(qName.getLocalPart());
71  
72          String publicRenderParameterName = sb.toString();
73  
74          if (!_qNames.containsKey(publicRenderParameterName)) {
75              _qNames.put(publicRenderParameterName, qName);
76          }
77  
78          return publicRenderParameterName;
79      }
80  
81      public QName getQName(String publicRenderParameterName) {
82          if (!publicRenderParameterName.startsWith(
83                  PUBLIC_RENDER_PARAMETER_NAMESPACE) &&
84              !publicRenderParameterName.startsWith(
85                  REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
86  
87              return null;
88          }
89  
90          return _qNames.get(publicRenderParameterName);
91      }
92  
93      public QName getQName(
94          Element qNameEl, Element nameEl, String defaultNamespace) {
95  
96          if ((qNameEl == null) && (nameEl == null)) {
97              _log.error("both qname and name elements are null");
98  
99              return null;
100         }
101 
102         if (qNameEl == null) {
103             return SAXReaderUtil.createQName(
104                 nameEl.getTextTrim(),
105                 SAXReaderUtil.createNamespace(defaultNamespace));
106         }
107 
108         String localPart = qNameEl.getTextTrim();
109 
110         int pos = localPart.indexOf(CharPool.COLON);
111 
112         if (pos == -1) {
113             if (_log.isDebugEnabled()) {
114                 _log.debug("qname " + localPart + " does not have a prefix");
115             }
116 
117             return SAXReaderUtil.createQName(localPart);
118         }
119 
120         String prefix = localPart.substring(0, pos);
121 
122         Namespace namespace = qNameEl.getNamespaceForPrefix(prefix);
123 
124         if (namespace == null) {
125             if (_log.isWarnEnabled()) {
126                 _log.warn(
127                     "qname " + localPart + " does not have a valid namespace");
128             }
129 
130             return null;
131         }
132 
133         localPart = localPart.substring(prefix.length() + 1);
134 
135         return SAXReaderUtil.createQName(localPart, namespace);
136     }
137 
138     public String getRemovePublicRenderParameterName(QName qName) {
139         StringBundler sb = new StringBundler(4);
140 
141         sb.append(REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE);
142         sb.append(qName.getNamespaceURI().hashCode());
143         sb.append(StringPool.UNDERLINE);
144         sb.append(qName.getLocalPart());
145 
146         String removePublicRenderParameterName = sb.toString();
147 
148         if (!_qNames.containsKey(removePublicRenderParameterName)) {
149             _qNames.put(removePublicRenderParameterName, qName);
150         }
151 
152         return removePublicRenderParameterName;
153     }
154 
155     public void setPublicRenderParameterIdentifier(
156         String publicRenderParameterName, String identifier) {
157 
158         _identifiers.put(publicRenderParameterName, identifier);
159     }
160 
161     private static final String _KEY_SEPARATOR = "_KEY_";
162 
163     private static Log _log = LogFactoryUtil.getLog(PortletQNameImpl.class);
164 
165     private Map<String, QName> _qNames;
166     private Map<String, String> _identifiers;
167 
168 }