001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.search;
022:
023: import com.liferay.util.HttpUtil;
024:
025: import java.text.SimpleDateFormat;
026:
027: import java.util.Date;
028:
029: import org.dom4j.Element;
030: import org.dom4j.Namespace;
031: import org.dom4j.QName;
032:
033: /**
034: * <a href="OpenSearchUtil.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Charles May
037: * @author Brian Wing Shun Chan
038: *
039: */
040: public class OpenSearchUtil {
041:
042: public static final int DEFAULT_NAMESPACE = 0;
043:
044: public static final int OS_NAMESPACE = 1;
045:
046: public static final int RELEVANCE_NAMESPACE = 2;
047:
048: public static Element addElement(Element el, String name,
049: int namespaceType) {
050:
051: return el.addElement(getQName(name, namespaceType));
052: }
053:
054: public static Element addElement(Element el, String name,
055: int namespaceType, Date value) {
056:
057: SimpleDateFormat sdf = new SimpleDateFormat(
058: "yyyy-MM-dd'T'HH:mm:sszzz");
059:
060: return addElement(el, name, namespaceType, sdf.format(value));
061: }
062:
063: public static Element addElement(Element el, String name,
064: int namespaceType, double value) {
065:
066: return addElement(el, name, namespaceType, String
067: .valueOf(value));
068: }
069:
070: public static Element addElement(Element el, String name,
071: int namespaceType, int value) {
072:
073: return addElement(el, name, namespaceType, String
074: .valueOf(value));
075: }
076:
077: public static Element addElement(Element el, String name,
078: int namespaceType, String value) {
079:
080: Element returnElement = el.addElement(getQName(name,
081: namespaceType));
082:
083: returnElement.addCDATA(value);
084:
085: return returnElement;
086: }
087:
088: public static void addLink(Element root, String searchURL,
089: String rel, String keywords, int page, int itemsPerPage) {
090:
091: Element link = addElement(root, "link", DEFAULT_NAMESPACE);
092:
093: link.addAttribute("rel", rel);
094: link.addAttribute("href", searchURL + "?keywords="
095: + HttpUtil.encodeURL(keywords) + "&p=" + page + "&c="
096: + itemsPerPage + "&format=atom");
097: link.addAttribute("type", "application/atom+xml");
098: }
099:
100: public static Namespace getNamespace(int namespaceType) {
101: Namespace namespace = null;
102:
103: if (namespaceType == DEFAULT_NAMESPACE) {
104: namespace = new Namespace("", "http://www.w3.org/2005/Atom");
105: } else if (namespaceType == OS_NAMESPACE) {
106: namespace = new Namespace("opensearch",
107: "http://a9.com/-/spec/opensearch/1.1/");
108: } else if (namespaceType == RELEVANCE_NAMESPACE) {
109: namespace = new Namespace("relevance",
110: "http://a9.com/-/opensearch/extensions/relevance/1.0/");
111: }
112:
113: return namespace;
114: }
115:
116: public static QName getQName(String name, int namespaceType) {
117: return new QName(name, getNamespace(namespaceType));
118: }
119:
120: }
|