1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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 final int NO_NAMESPACE = 3;
43  
44      public static Element addElement(
45          Element el, String name, int namespaceType) {
46  
47          return el.addElement(getQName(name, namespaceType));
48      }
49  
50      public static Element addElement(
51          Element el, String name, int namespaceType, Date value) {
52  
53          return addElement(el, name, namespaceType, _dateFormat.format(value));
54      }
55  
56      public static Element addElement(
57          Element el, String name, int namespaceType, double value) {
58  
59          return addElement(el, name, namespaceType, String.valueOf(value));
60      }
61  
62      public static Element addElement(
63          Element el, String name, int namespaceType, int 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, String value) {
70  
71          Element returnElement = el.addElement(getQName(name, namespaceType));
72  
73          returnElement.addCDATA(value);
74  
75          return returnElement;
76      }
77  
78      public static void addLink(
79          Element root, String searchURL, String rel, String keywords, int page,
80          int itemsPerPage) {
81  
82          Element link = addElement(root, "link", DEFAULT_NAMESPACE);
83  
84          link.addAttribute("rel", rel);
85          link.addAttribute(
86              "href",
87              searchURL + "?keywords=" + HttpUtil.encodeURL(keywords) + "&p=" +
88                  page + "&c=" + itemsPerPage + "&format=atom");
89          link.addAttribute("type", "application/atom+xml");
90      }
91  
92      public static Namespace getNamespace(int namespaceType) {
93          Namespace namespace = null;
94  
95          if (namespaceType == DEFAULT_NAMESPACE) {
96              namespace = SAXReaderUtil.createNamespace(
97                  "", "http://www.w3.org/2005/Atom");
98          }
99          else if (namespaceType == OS_NAMESPACE) {
100             namespace = SAXReaderUtil.createNamespace(
101                 "opensearch", "http://a9.com/-/spec/opensearch/1.1/");
102         }
103         else if (namespaceType == RELEVANCE_NAMESPACE) {
104             namespace = SAXReaderUtil.createNamespace(
105                 "relevance",
106                 "http://a9.com/-/opensearch/extensions/relevance/1.0/");
107         }
108 
109         return namespace;
110     }
111 
112     public static QName getQName(String name, int namespaceType) {
113         if (NO_NAMESPACE == namespaceType) {
114             return SAXReaderUtil.createQName(name);
115         }
116         else {
117             return SAXReaderUtil.createQName(name, getNamespace(namespaceType));
118         }
119     }
120 
121     private static Format _dateFormat =
122         FastDateFormatFactoryUtil.getSimpleDateFormat(
123             "yyyy-MM-dd'T'HH:mm:sszzz");
124 
125 }