1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.FastDateFormatFactoryUtil;
26  import com.liferay.portal.kernel.util.HttpUtil;
27  import com.liferay.portal.kernel.xml.Element;
28  import com.liferay.portal.kernel.xml.Namespace;
29  import com.liferay.portal.kernel.xml.QName;
30  import com.liferay.portal.kernel.xml.SAXReaderUtil;
31  
32  import java.text.Format;
33  
34  import java.util.Date;
35  
36  /**
37   * <a href="OpenSearchUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Charles May
40   * @author Brian Wing Shun Chan
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          return addElement(el, name, namespaceType, _dateFormat.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     private static Format _dateFormat =
123         FastDateFormatFactoryUtil.getSimpleDateFormat(
124             "yyyy-MM-dd'T'HH:mm:sszzz");
125 
126 }