001    /**
002     * Copyright (c) 2000-2013 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            public static Element addElement(
045                    Element el, String name, int namespaceType) {
046    
047                    return el.addElement(getQName(name, namespaceType));
048            }
049    
050            public static Element addElement(
051                    Element el, String name, int namespaceType, Date value) {
052    
053                    return addElement(el, name, namespaceType, _dateFormat.format(value));
054            }
055    
056            public static Element addElement(
057                    Element el, String name, int namespaceType, double value) {
058    
059                    return addElement(el, name, namespaceType, String.valueOf(value));
060            }
061    
062            public static Element addElement(
063                    Element el, String name, int namespaceType, int value) {
064    
065                    return addElement(el, name, namespaceType, String.valueOf(value));
066            }
067    
068            public static Element addElement(
069                    Element el, String name, int namespaceType, long value) {
070    
071                    return addElement(el, name, namespaceType, String.valueOf(value));
072            }
073    
074            public static Element addElement(
075                    Element el, String name, int namespaceType, String value) {
076    
077                    Element returnElement = el.addElement(getQName(name, namespaceType));
078    
079                    returnElement.addCDATA(value);
080    
081                    return returnElement;
082            }
083    
084            public static void addLink(
085                    Element root, String searchURL, String rel, String keywords, int page,
086                    int itemsPerPage) {
087    
088                    Element link = addElement(root, "link", DEFAULT_NAMESPACE);
089    
090                    link.addAttribute("rel", rel);
091                    link.addAttribute(
092                            "href",
093                            searchURL + "?keywords=" + HttpUtil.encodeURL(keywords) + "&p=" +
094                                    page + "&c=" + itemsPerPage + "&format=atom");
095                    link.addAttribute("type", "application/atom+xml");
096            }
097    
098            public static Namespace getNamespace(int namespaceType) {
099                    Namespace namespace = null;
100    
101                    if (namespaceType == DEFAULT_NAMESPACE) {
102                            namespace = SAXReaderUtil.createNamespace(
103                                    "", "http://www.w3.org/2005/Atom");
104                    }
105                    else if (namespaceType == LIFERAY_NAMESPACE) {
106                            namespace = SAXReaderUtil.createNamespace(
107                                    "liferay", "http://liferay.com/spec/liferay-search/1.0/");
108                    }
109                    else if (namespaceType == OS_NAMESPACE) {
110                            namespace = SAXReaderUtil.createNamespace(
111                                    "opensearch", "http://a9.com/-/spec/opensearch/1.1/");
112                    }
113                    else if (namespaceType == RELEVANCE_NAMESPACE) {
114                            namespace = SAXReaderUtil.createNamespace(
115                                    "relevance",
116                                    "http://a9.com/-/opensearch/extensions/relevance/1.0/");
117                    }
118    
119                    return namespace;
120            }
121    
122            public static QName getQName(String name, int namespaceType) {
123                    if (NO_NAMESPACE == namespaceType) {
124                            return SAXReaderUtil.createQName(name);
125                    }
126                    else {
127                            return SAXReaderUtil.createQName(name, getNamespace(namespaceType));
128                    }
129            }
130    
131            private static Format _dateFormat =
132                    FastDateFormatFactoryUtil.getSimpleDateFormat(
133                            "yyyy-MM-dd'T'HH:mm:sszzz");
134    
135    }