1   /**
2    * Copyright (c) 2000-2009 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  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.portal.kernel.xml.Namespace;
28  import com.liferay.portal.kernel.xml.QName;
29  import com.liferay.portal.kernel.xml.SAXReaderUtil;
30  
31  import java.text.SimpleDateFormat;
32  
33  import java.util.Date;
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 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          SimpleDateFormat sdf = new SimpleDateFormat(
62              "yyyy-MM-dd'T'HH:mm:sszzz");
63  
64          return addElement(el, name, namespaceType, sdf.format(value));
65      }
66  
67      public static Element addElement(
68          Element el, String name, int namespaceType, double value) {
69  
70          return addElement(el, name, namespaceType, String.valueOf(value));
71      }
72  
73      public static Element addElement(
74          Element el, String name, int namespaceType, int value) {
75  
76          return addElement(el, name, namespaceType, String.valueOf(value));
77      }
78  
79      public static Element addElement(
80          Element el, String name, int namespaceType, String value) {
81  
82          Element returnElement = el.addElement(getQName(name, namespaceType));
83  
84          returnElement.addCDATA(value);
85  
86          return returnElement;
87      }
88  
89      public static void addLink(
90          Element root, String searchURL, String rel, String keywords, int page,
91          int itemsPerPage) {
92  
93          Element link = addElement(root, "link", DEFAULT_NAMESPACE);
94  
95          link.addAttribute("rel", rel);
96          link.addAttribute(
97              "href",
98              searchURL + "?keywords=" + HttpUtil.encodeURL(keywords) + "&p=" +
99                  page + "&c=" + itemsPerPage + "&format=atom");
100         link.addAttribute("type", "application/atom+xml");
101     }
102 
103     public static Namespace getNamespace(int namespaceType) {
104         Namespace namespace = null;
105 
106         if (namespaceType == DEFAULT_NAMESPACE) {
107             namespace = SAXReaderUtil.createNamespace(
108                 "", "http://www.w3.org/2005/Atom");
109         }
110         else if (namespaceType == OS_NAMESPACE) {
111             namespace = SAXReaderUtil.createNamespace(
112                 "opensearch", "http://a9.com/-/spec/opensearch/1.1/");
113         }
114         else if (namespaceType == RELEVANCE_NAMESPACE) {
115             namespace = SAXReaderUtil.createNamespace(
116                 "relevance",
117                 "http://a9.com/-/opensearch/extensions/relevance/1.0/");
118         }
119 
120         return namespace;
121     }
122 
123     public static QName getQName(String name, int namespaceType) {
124         if (NO_NAMESPACE == namespaceType) {
125             return SAXReaderUtil.createQName(name);
126         }
127         else {
128             return SAXReaderUtil.createQName(name, getNamespace(namespaceType));
129         }
130     }
131 
132 }