001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.search;
016    
017    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
018    import com.liferay.portal.kernel.util.HttpUtil;
019    import com.liferay.portal.kernel.xml.Element;
020    import com.liferay.portal.kernel.xml.Namespace;
021    import com.liferay.portal.kernel.xml.QName;
022    import com.liferay.portal.kernel.xml.SAXReaderUtil;
023    
024    import java.text.Format;
025    
026    import java.util.Date;
027    
028    /**
029     * @author Charles May
030     * @author Brian Wing Shun Chan
031     */
032    public class OpenSearchUtil {
033    
034            public static final int DEFAULT_NAMESPACE = 0;
035    
036            public static final int LIFERAY_NAMESPACE = 4;
037    
038            public static final int NO_NAMESPACE = 3;
039    
040            public static final int OS_NAMESPACE = 1;
041    
042            public static final int RELEVANCE_NAMESPACE = 2;
043    
044            private static Format _dateFormat =
045                    FastDateFormatFactoryUtil.getSimpleDateFormat(
046                            "yyyy-MM-dd'T'HH:mm:sszzz");
047    
048            public static Element addElement(
049                    Element el, String name, int namespaceType) {
050    
051                    return el.addElement(getQName(name, namespaceType));
052            }
053    
054            public static Element addElement(
055                    Element el, String name, int namespaceType, Date value) {
056    
057                    return addElement(el, name, namespaceType, _dateFormat.format(value));
058            }
059    
060            public static Element addElement(
061                    Element el, String name, int namespaceType, double value) {
062    
063                    return addElement(el, name, namespaceType, String.valueOf(value));
064            }
065    
066            public static Element addElement(
067                    Element el, String name, int namespaceType, int value) {
068    
069                    return addElement(el, name, namespaceType, String.valueOf(value));
070            }
071    
072            public static Element addElement(
073                    Element el, String name, int namespaceType, long value) {
074    
075                    return addElement(el, name, namespaceType, String.valueOf(value));
076            }
077    
078            public static Element addElement(
079                    Element el, String name, int namespaceType, String value) {
080    
081                    Element returnElement = el.addElement(getQName(name, namespaceType));
082    
083                    returnElement.addCDATA(value);
084    
085                    return returnElement;
086            }
087    
088            public static void addLink(
089                    Element root, String searchURL, String rel, String keywords, int page,
090                    int itemsPerPage) {
091    
092                    Element link = addElement(root, "link", DEFAULT_NAMESPACE);
093    
094                    link.addAttribute("rel", rel);
095                    link.addAttribute(
096                            "href",
097                            searchURL + "?keywords=" + HttpUtil.encodeURL(keywords) + "&p=" +
098                                    page + "&c=" + itemsPerPage + "&format=atom");
099                    link.addAttribute("type", "application/atom+xml");
100            }
101    
102            public static Namespace getNamespace(int namespaceType) {
103                    Namespace namespace = null;
104    
105                    if (namespaceType == DEFAULT_NAMESPACE) {
106                            namespace = SAXReaderUtil.createNamespace(
107                                    "", "http://www.w3.org/2005/Atom");
108                    }
109                    else if (namespaceType == LIFERAY_NAMESPACE) {
110                            namespace = SAXReaderUtil.createNamespace(
111                                    "liferay", "http://liferay.com/spec/liferay-search/1.0/");
112                    }
113                    else if (namespaceType == OS_NAMESPACE) {
114                            namespace = SAXReaderUtil.createNamespace(
115                                    "opensearch", "http://a9.com/-/spec/opensearch/1.1/");
116                    }
117                    else if (namespaceType == RELEVANCE_NAMESPACE) {
118                            namespace = SAXReaderUtil.createNamespace(
119                                    "relevance",
120                                    "http://a9.com/-/opensearch/extensions/relevance/1.0/");
121                    }
122    
123                    return namespace;
124            }
125    
126            public static QName getQName(String name, int namespaceType) {
127                    if (NO_NAMESPACE == namespaceType) {
128                            return SAXReaderUtil.createQName(name);
129                    }
130                    else {
131                            return SAXReaderUtil.createQName(name, getNamespace(namespaceType));
132                    }
133            }
134    
135    }