1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
42  public class PortletQNameImpl implements PortletQName {
43  
44      public PortletQNameImpl() {
45          _qNames = new ConcurrentHashMap<String, QName>();
46          _identifiers = new ConcurrentHashMap<String, String>();
47      }
48  
49      public String getKey(QName qName) {
50          return getKey(qName.getNamespaceURI(), qName.getLocalPart());
51      }
52  
53      public String getKey(String uri, String localPart) {
54          StringBuilder sb = new StringBuilder();
55  
56          sb.append(uri);
57          sb.append(_KEY_SEPARATOR);
58          sb.append(localPart);
59  
60          return sb.toString();
61      }
62  
63      public String getPublicRenderParameterIdentifier(
64          String publicRenderParameterName) {
65  
66          if (!publicRenderParameterName.startsWith(
67                  PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
68  
69              return null;
70          }
71  
72          return _identifiers.get(publicRenderParameterName);
73      }
74  
75      public String getPublicRenderParameterName(QName qName) {
76          StringBuilder sb = new StringBuilder();
77  
78          sb.append(PUBLIC_RENDER_PARAMETER_NAMESPACE);
79          sb.append(qName.getNamespaceURI().hashCode());
80          sb.append(StringPool.UNDERLINE);
81          sb.append(qName.getLocalPart());
82  
83          String publicRenderParameterName = sb.toString();
84  
85          if (!_qNames.containsKey(publicRenderParameterName)) {
86              _qNames.put(publicRenderParameterName, qName);
87          }
88  
89          return publicRenderParameterName;
90      }
91  
92      public QName getQName(String publicRenderParameterName) {
93          if (!publicRenderParameterName.startsWith(
94                  PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
95  
96              return null;
97          }
98  
99          return _qNames.get(publicRenderParameterName);
100     }
101 
102     public QName getQName(
103         Element qNameEl, Element nameEl, String defaultNamespace) {
104 
105         if ((qNameEl == null) && (nameEl == null)) {
106             _log.error("both qname and name elements are null");
107 
108             return null;
109         }
110 
111         if (qNameEl == null) {
112             return SAXReaderUtil.createQName(
113                 nameEl.getTextTrim(),
114                 SAXReaderUtil.createNamespace(defaultNamespace));
115         }
116 
117         String localPart = qNameEl.getTextTrim();
118 
119         int pos = localPart.indexOf(StringPool.COLON);
120 
121         if (pos == -1) {
122             if (_log.isDebugEnabled()) {
123                 _log.debug("qname " + localPart + " does not have a prefix");
124             }
125 
126             return SAXReaderUtil.createQName(localPart);
127         }
128 
129         String prefix = localPart.substring(0, pos);
130 
131         Namespace namespace = qNameEl.getNamespaceForPrefix(prefix);
132 
133         if (namespace == null) {
134             if (_log.isWarnEnabled()) {
135                 _log.warn(
136                     "qname " + localPart + " does not have a valid namespace");
137             }
138 
139             return null;
140         }
141 
142         localPart = localPart.substring(prefix.length() + 1);
143 
144         return SAXReaderUtil.createQName(localPart, namespace);
145     }
146 
147     public void setPublicRenderParameterIdentifier(
148         String publicRenderParameterName, String identifier) {
149 
150         _identifiers.put(publicRenderParameterName, identifier);
151     }
152 
153     private static final String _KEY_SEPARATOR = "_KEY_";
154 
155     private static Log _log = LogFactoryUtil.getLog(PortletQNameImpl.class);
156 
157     private Map<String, QName> _qNames;
158     private Map<String, String> _identifiers;
159 
160 }