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