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 final int NO_NAMESPACE = 3;
51  
52      public static Element addElement(
53          Element el, String name, int namespaceType) {
54  
55          return el.addElement(getQName(name, namespaceType));
56      }
57  
58      public static Element addElement(
59          Element el, String name, int namespaceType, Date value) {
60  
61          return addElement(el, name, namespaceType, _dateFormat.format(value));
62      }
63  
64      public static Element addElement(
65          Element el, String name, int namespaceType, double value) {
66  
67          return addElement(el, name, namespaceType, String.valueOf(value));
68      }
69  
70      public static Element addElement(
71          Element el, String name, int namespaceType, int value) {
72  
73          return addElement(el, name, namespaceType, String.valueOf(value));
74      }
75  
76      public static Element addElement(
77          Element el, String name, int namespaceType, String value) {
78  
79          Element returnElement = el.addElement(getQName(name, namespaceType));
80  
81          returnElement.addCDATA(value);
82  
83          return returnElement;
84      }
85  
86      public static void addLink(
87          Element root, String searchURL, String rel, String keywords, int page,
88          int itemsPerPage) {
89  
90          Element link = addElement(root, "link", DEFAULT_NAMESPACE);
91  
92          link.addAttribute("rel", rel);
93          link.addAttribute(
94              "href",
95              searchURL + "?keywords=" + HttpUtil.encodeURL(keywords) + "&p=" +
96                  page + "&c=" + itemsPerPage + "&format=atom");
97          link.addAttribute("type", "application/atom+xml");
98      }
99  
100     public static Namespace getNamespace(int namespaceType) {
101         Namespace namespace = null;
102 
103         if (namespaceType == DEFAULT_NAMESPACE) {
104             namespace = SAXReaderUtil.createNamespace(
105                 "", "http://www.w3.org/2005/Atom");
106         }
107         else if (namespaceType == OS_NAMESPACE) {
108             namespace = SAXReaderUtil.createNamespace(
109                 "opensearch", "http://a9.com/-/spec/opensearch/1.1/");
110         }
111         else if (namespaceType == RELEVANCE_NAMESPACE) {
112             namespace = SAXReaderUtil.createNamespace(
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         if (NO_NAMESPACE == namespaceType) {
122             return SAXReaderUtil.createQName(name);
123         }
124         else {
125             return SAXReaderUtil.createQName(name, getNamespace(namespaceType));
126         }
127     }
128 
129     private static Format _dateFormat =
130         FastDateFormatFactoryUtil.getSimpleDateFormat(
131             "yyyy-MM-dd'T'HH:mm:sszzz");
132 
133 }