1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
47
48 public SearchResultModuleGenerator() {
49 }
50
51
52
53
54 public String getNamespaceUri() {
55 return SearchResultModule.URI;
56 }
57
58
59
60
61 public Set getNamespaces() {
62 return NAMESPACES;
63 }
64
65
66
67
68 public void generate(final Module module, final Element element) {
69
70
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 }