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