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.StringMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  
28  import java.util.List;
29  import java.util.Map;
30  import java.util.concurrent.ConcurrentHashMap;
31  
32  import javax.xml.namespace.QName;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  import org.dom4j.Element;
38  import org.dom4j.Namespace;
39  
40  /**
41   * <a href="QNameUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class QNameUtil {
47  
48      public static final String PUBLIC_RENDER_PARAMETER_NAMESPACE = "p_r_p_";
49  
50      public static String getKey(QName qName) {
51          return getKey(qName.getNamespaceURI(), qName.getLocalPart());
52      }
53  
54      public static String getKey(String uri, String localPart) {
55          StringMaker sm = new StringMaker();
56  
57          sm.append(uri);
58          sm.append(_KEY_SEPARATOR);
59          sm.append(localPart);
60  
61          return sm.toString();
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.getText();
86  
87          List<Namespace> namespaces = qNameEl.declaredNamespaces();
88  
89          if (namespaces.size() == 0) {
90              _log.error("qname " + localPart + " does not have a namespace");
91  
92              return null;
93          }
94  
95          Namespace namespace = namespaces.get(0);
96  
97          String uri = namespace.getURI();
98          String prefix = namespace.getPrefix();
99  
100         if (localPart.startsWith(prefix + StringPool.COLON)) {
101             localPart = localPart.substring(prefix.length() + 1);
102         }
103         else {
104             if (_log.isWarnEnabled()) {
105                 _log.warn(
106                     "qname " + localPart + " is not correctly namespaced");
107             }
108         }
109 
110         return new QName(uri, localPart, prefix);
111     }
112 
113     private QNameUtil() {
114         _qNames = new ConcurrentHashMap<String, QName>();
115     }
116 
117     private String _getPublicRenderParameterName(QName qName) {
118         StringMaker sm = new StringMaker();
119 
120         sm.append(PUBLIC_RENDER_PARAMETER_NAMESPACE);
121         sm.append(qName.getNamespaceURI().hashCode());
122         sm.append(StringPool.UNDERLINE);
123         sm.append(qName.getLocalPart());
124 
125         String publicRenderParameterName = sm.toString();
126 
127         if (!_qNames.containsKey(publicRenderParameterName)) {
128             _qNames.put(publicRenderParameterName, qName);
129         }
130 
131         return publicRenderParameterName;
132     }
133 
134     private QName _getQName(String publicRenderParameterName) {
135         if (!publicRenderParameterName.startsWith(
136             QNameUtil.PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
137 
138             return null;
139         }
140 
141         return _qNames.get(publicRenderParameterName);
142     }
143 
144     private static final String _KEY_SEPARATOR = "_KEY_";
145 
146     private static Log _log = LogFactory.getLog(QNameUtil.class);
147 
148     private static QNameUtil _instance = new QNameUtil();
149 
150     private Map<String, QName> _qNames;
151 
152 }