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