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.search;
21  
22  import com.liferay.portal.kernel.util.HttpUtil;
23  import com.liferay.portal.kernel.xml.Element;
24  import com.liferay.portal.kernel.xml.Namespace;
25  import com.liferay.portal.kernel.xml.QName;
26  import com.liferay.portal.kernel.xml.SAXReaderUtil;
27  
28  import java.text.SimpleDateFormat;
29  
30  import java.util.Date;
31  
32  /**
33   * <a href="OpenSearchUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Charles May
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class OpenSearchUtil {
40  
41      public static final int DEFAULT_NAMESPACE = 0;
42  
43      public static final int OS_NAMESPACE = 1;
44  
45      public static final int RELEVANCE_NAMESPACE = 2;
46  
47      public static Element addElement(
48          Element el, String name, int namespaceType) {
49  
50          return el.addElement(getQName(name, namespaceType));
51      }
52  
53      public static Element addElement(
54          Element el, String name, int namespaceType, Date value) {
55  
56          SimpleDateFormat sdf = new SimpleDateFormat(
57              "yyyy-MM-dd'T'HH:mm:sszzz");
58  
59          return addElement(el, name, namespaceType, sdf.format(value));
60      }
61  
62      public static Element addElement(
63          Element el, String name, int namespaceType, double value) {
64  
65          return addElement(el, name, namespaceType, String.valueOf(value));
66      }
67  
68      public static Element addElement(
69          Element el, String name, int namespaceType, int value) {
70  
71          return addElement(el, name, namespaceType, String.valueOf(value));
72      }
73  
74      public static Element addElement(
75          Element el, String name, int namespaceType, String value) {
76  
77          Element returnElement = el.addElement(getQName(name, namespaceType));
78  
79          returnElement.addCDATA(value);
80  
81          return returnElement;
82      }
83  
84      public static void addLink(
85          Element root, String searchURL, String rel, String keywords, int page,
86          int itemsPerPage) {
87  
88          Element link = addElement(root, "link", DEFAULT_NAMESPACE);
89  
90          link.addAttribute("rel", rel);
91          link.addAttribute(
92              "href",
93              searchURL + "?keywords=" + HttpUtil.encodeURL(keywords) + "&p=" +
94                  page + "&c=" + itemsPerPage + "&format=atom");
95          link.addAttribute("type", "application/atom+xml");
96      }
97  
98      public static Namespace getNamespace(int namespaceType) {
99          Namespace namespace = null;
100 
101         if (namespaceType == DEFAULT_NAMESPACE) {
102             namespace = SAXReaderUtil.createNamespace(
103                 "", "http://www.w3.org/2005/Atom");
104         }
105         else if (namespaceType == OS_NAMESPACE) {
106             namespace = SAXReaderUtil.createNamespace(
107                 "opensearch", "http://a9.com/-/spec/opensearch/1.1/");
108         }
109         else if (namespaceType == RELEVANCE_NAMESPACE) {
110             namespace = SAXReaderUtil.createNamespace(
111                 "relevance",
112                 "http://a9.com/-/opensearch/extensions/relevance/1.0/");
113         }
114 
115         return namespace;
116     }
117 
118     public static QName getQName(String name, int namespaceType) {
119         return SAXReaderUtil.createQName(name, getNamespace(namespaceType));
120     }
121 
122 }