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