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.portal.util;
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  
31  import java.util.Map;
32  import java.util.concurrent.ConcurrentHashMap;
33  
34  import javax.xml.namespace.QName;
35  
36  /**
37   * <a href="QNameUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class QNameUtil {
43  
44      public static final String PUBLIC_RENDER_PARAMETER_NAMESPACE = "p_r_p_";
45  
46      public static String getKey(QName qName) {
47          return getKey(qName.getNamespaceURI(), qName.getLocalPart());
48      }
49  
50      public static String getKey(String uri, String localPart) {
51          StringBuilder sb = new StringBuilder();
52  
53          sb.append(uri);
54          sb.append(_KEY_SEPARATOR);
55          sb.append(localPart);
56  
57          return sb.toString();
58      }
59  
60      public static String getPublicRenderParameterIdentifier(
61          String publicRenderParameterName) {
62  
63          return _instance._getPublicRenderParameterIdentifier(
64              publicRenderParameterName);
65      }
66  
67      public static String getPublicRenderParameterName(QName qName) {
68          return _instance._getPublicRenderParameterName(qName);
69      }
70  
71      public static QName getQName(String publicRenderParameterName) {
72          return _instance._getQName(publicRenderParameterName);
73      }
74  
75      public static QName getQName(
76          Element qNameEl, Element nameEl, String defaultNamespace) {
77  
78          if ((qNameEl == null) && (nameEl == null)) {
79              _log.error("both qname and name elements are null");
80  
81              return null;
82          }
83  
84          if (qNameEl == null) {
85              return new QName(defaultNamespace, nameEl.getTextTrim());
86          }
87  
88          String localPart = qNameEl.getTextTrim();
89  
90          int pos = localPart.indexOf(StringPool.COLON);
91  
92          if (pos == -1) {
93              if (_log.isDebugEnabled()) {
94                  _log.debug("qname " + localPart + " does not have a prefix");
95              }
96  
97              return new QName(localPart);
98          }
99  
100         String prefix = localPart.substring(0, pos);
101 
102         Namespace namespace = qNameEl.getNamespaceForPrefix(prefix);
103 
104         if (namespace == null) {
105             if (_log.isWarnEnabled()) {
106                 _log.warn(
107                     "qname " + localPart + " does not have a valid namespace");
108             }
109 
110             return null;
111         }
112 
113         String uri = namespace.getURI();
114 
115         localPart = localPart.substring(prefix.length() + 1);
116 
117         return new QName(uri, localPart, prefix);
118     }
119 
120     public static void setPublicRenderParameterIdentifier(
121         String publicRenderParameterName, String identifier) {
122 
123         _instance._setPublicRenderParameterIdentifier(
124             publicRenderParameterName, identifier);
125     }
126 
127     private QNameUtil() {
128         _qNames = new ConcurrentHashMap<String, QName>();
129         _identifiers = new ConcurrentHashMap<String, String>();
130     }
131 
132     private String _getPublicRenderParameterIdentifier(
133         String publicRenderParameterName) {
134 
135         if (!publicRenderParameterName.startsWith(
136                 QNameUtil.PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
137 
138             return null;
139         }
140 
141         return _identifiers.get(publicRenderParameterName);
142     }
143 
144     private String _getPublicRenderParameterName(QName qName) {
145         StringBuilder sb = new StringBuilder();
146 
147         sb.append(PUBLIC_RENDER_PARAMETER_NAMESPACE);
148         sb.append(qName.getNamespaceURI().hashCode());
149         sb.append(StringPool.UNDERLINE);
150         sb.append(qName.getLocalPart());
151 
152         String publicRenderParameterName = sb.toString();
153 
154         if (!_qNames.containsKey(publicRenderParameterName)) {
155             _qNames.put(publicRenderParameterName, qName);
156         }
157 
158         return publicRenderParameterName;
159     }
160 
161     private QName _getQName(String publicRenderParameterName) {
162         if (!publicRenderParameterName.startsWith(
163                 QNameUtil.PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
164 
165             return null;
166         }
167 
168         return _qNames.get(publicRenderParameterName);
169     }
170 
171     private void _setPublicRenderParameterIdentifier(
172         String publicRenderParameterName, String identifier) {
173 
174         _identifiers.put(publicRenderParameterName, identifier);
175     }
176 
177     private static final String _KEY_SEPARATOR = "_KEY_";
178 
179     private static Log _log = LogFactoryUtil.getLog(QNameUtil.class);
180 
181     private static QNameUtil _instance = new QNameUtil();
182 
183     private Map<String, QName> _qNames;
184     private Map<String, String> _identifiers;
185 
186 }