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.kernel.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 LIFERAY_NAMESPACE = 4;
39  
40      public static final int NO_NAMESPACE = 3;
41  
42      public static final int OS_NAMESPACE = 1;
43  
44      public static final int RELEVANCE_NAMESPACE = 2;
45  
46      private static Format _dateFormat =
47          FastDateFormatFactoryUtil.getSimpleDateFormat(
48              "yyyy-MM-dd'T'HH:mm:sszzz");
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, long value) {
76  
77          return addElement(el, name, namespaceType, String.valueOf(value));
78      }
79  
80      public static Element addElement(
81          Element el, String name, int namespaceType, String value) {
82  
83          Element returnElement = el.addElement(getQName(name, namespaceType));
84  
85          returnElement.addCDATA(value);
86  
87          return returnElement;
88      }
89  
90      public static void addLink(
91          Element root, String searchURL, String rel, String keywords, int page,
92          int itemsPerPage) {
93  
94          Element link = addElement(root, "link", DEFAULT_NAMESPACE);
95  
96          link.addAttribute("rel", rel);
97          link.addAttribute(
98              "href",
99              searchURL + "?keywords=" + HttpUtil.encodeURL(keywords) + "&p=" +
100                 page + "&c=" + itemsPerPage + "&format=atom");
101         link.addAttribute("type", "application/atom+xml");
102     }
103 
104     public static Namespace getNamespace(int namespaceType) {
105         Namespace namespace = null;
106 
107         if (namespaceType == DEFAULT_NAMESPACE) {
108             namespace = SAXReaderUtil.createNamespace(
109                 "", "http://www.w3.org/2005/Atom");
110         }
111         else if (namespaceType == LIFERAY_NAMESPACE) {
112             namespace = SAXReaderUtil.createNamespace(
113                 "liferay", "http://liferay.com/spec/liferay-search/1.0/");
114         }
115         else if (namespaceType == OS_NAMESPACE) {
116             namespace = SAXReaderUtil.createNamespace(
117                 "opensearch", "http://a9.com/-/spec/opensearch/1.1/");
118         }
119         else if (namespaceType == RELEVANCE_NAMESPACE) {
120             namespace = SAXReaderUtil.createNamespace(
121                 "relevance",
122                 "http://a9.com/-/opensearch/extensions/relevance/1.0/");
123         }
124 
125         return namespace;
126     }
127 
128     public static QName getQName(String name, int namespaceType) {
129         if (NO_NAMESPACE == namespaceType) {
130             return SAXReaderUtil.createQName(name);
131         }
132         else {
133             return SAXReaderUtil.createQName(name, getNamespace(namespaceType));
134         }
135     }
136 
137 }