View Javadoc

1   /* Copyright (2007) Schibsted Søk AS
2    * This file is part of SESAT.
3    *
4    *   SESAT is free software: you can redistribute it and/or modify
5    *   it under the terms of the GNU Affero General Public License as published by
6    *   the Free Software Foundation, either version 3 of the License, or
7    *   (at your option) any later version.
8    *
9    *   SESAT is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU Affero General Public License for more details.
13   *
14   *   You should have received a copy of the GNU Affero General Public License
15   *   along with SESAT.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  /*
18   * SearchResultModuleGenerator.java
19   */
20  
21  package no.sesat.search.view.output.syndication.modules;
22  
23  import com.sun.syndication.feed.module.Module;
24  import com.sun.syndication.io.ModuleGenerator;
25  import org.jdom.Element;
26  import org.jdom.Namespace;
27  
28  import java.util.Collections;
29  import java.util.HashSet;
30  import java.util.Set;
31  
32  /**
33   * Syndication feed generator for the extended sesam feed syntax.
34   */
35  public class SearchResultModuleGenerator implements ModuleGenerator {
36  
37      private static Set<Namespace> NAMESPACES;
38  
39      static {
40          final Set<Namespace> nss = new HashSet<Namespace>();
41          nss.add(SearchResultModuleImpl.NS);
42          NAMESPACES = Collections.unmodifiableSet(nss);
43      }
44  
45      /**
46       * Creates a new instance of this class.
47       */
48      public SearchResultModuleGenerator() {
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      public String getNamespaceUri() {
55          return SearchResultModule.URI;
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      public Set getNamespaces() {
62          return NAMESPACES;
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      public void generate(final Module module, final Element element) {
69  
70          // this is not necessary, it is done to avoid the namespace definition in every item.
71          Element root = element;
72          while (root.getParent() != null && root.getParent() instanceof Element) {
73              root = (Element) element.getParent();
74          }
75  
76          root.addNamespaceDeclaration(SearchResultModuleImpl.NS);
77  
78          final SearchResultModule m = (SearchResultModule) module;
79  
80          if (m.getNumberOfHits() != null) {
81              element.addContent(generateSimpleElement(SearchResultModule.ELEM_NUMBER_OF_HITS, m.getNumberOfHits()));
82          }
83  
84          if (m.getArticleAge() != null) {
85              element.addContent(generateSimpleElement(SearchResultModule.ELEM_ARTICLE_AGE, m.getArticleAge()));
86          }
87  
88          if (m.getNewsSource() != null) {
89              element.addContent(generateSimpleElement(SearchResultModule.ELEM_NEWS_SOURCE, m.getNewsSource()));
90          }
91      }
92  
93      private Element generateSimpleElement(final String name, final String value) {
94          final Element element = new Element(name, SearchResultModuleImpl.NS);
95          element.addContent(value);
96          return element;
97      }
98  }