1
22
23 package com.liferay.portal.search;
24
25 import com.liferay.portal.kernel.util.HttpUtil;
26
27 import java.text.SimpleDateFormat;
28
29 import java.util.Date;
30
31 import org.dom4j.Element;
32 import org.dom4j.Namespace;
33 import org.dom4j.QName;
34
35
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 SimpleDateFormat sdf = new SimpleDateFormat(
60 "yyyy-MM-dd'T'HH:mm:sszzz");
61
62 return addElement(el, name, namespaceType, sdf.format(value));
63 }
64
65 public static Element addElement(
66 Element el, String name, int namespaceType, double value) {
67
68 return addElement(el, name, namespaceType, String.valueOf(value));
69 }
70
71 public static Element addElement(
72 Element el, String name, int namespaceType, int value) {
73
74 return addElement(el, name, namespaceType, String.valueOf(value));
75 }
76
77 public static Element addElement(
78 Element el, String name, int namespaceType, String value) {
79
80 Element returnElement = el.addElement(getQName(name, namespaceType));
81
82 returnElement.addCDATA(value);
83
84 return returnElement;
85 }
86
87 public static void addLink(
88 Element root, String searchURL, String rel, String keywords, int page,
89 int itemsPerPage) {
90
91 Element link = addElement(root, "link", DEFAULT_NAMESPACE);
92
93 link.addAttribute("rel", rel);
94 link.addAttribute(
95 "href",
96 searchURL + "?keywords=" + HttpUtil.encodeURL(keywords) + "&p=" +
97 page + "&c=" + itemsPerPage + "&format=atom");
98 link.addAttribute("type", "application/atom+xml");
99 }
100
101 public static Namespace getNamespace(int namespaceType) {
102 Namespace namespace = null;
103
104 if (namespaceType == DEFAULT_NAMESPACE) {
105 namespace = new Namespace("", "http://www.w3.org/2005/Atom");
106 }
107 else if (namespaceType == OS_NAMESPACE) {
108 namespace = new Namespace(
109 "opensearch", "http://a9.com/-/spec/opensearch/1.1/");
110 }
111 else if (namespaceType == RELEVANCE_NAMESPACE) {
112 namespace = new Namespace(
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 return new QName(name, getNamespace(namespaceType));
122 }
123
124 }