1 package com.sun.syndication.io.impl;
2
3 import com.sun.syndication.io.WireFeedGenerator;
4 import org.jdom.Element;
5 import org.jdom.Namespace;
6 import org.jdom.Parent;
7
8 import java.util.HashSet;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Set;
12
13 /***
14 * @author Alejandro Abdelnur
15 */
16 public abstract class BaseWireFeedGenerator implements WireFeedGenerator {
17
18 /***
19 * [TYPE].feed.ModuleParser.classes= [className] ...
20 */
21 private static final String FEED_MODULE_GENERATORS_POSFIX_KEY = ".feed.ModuleGenerator.classes";
22
23 /***
24 * [TYPE].item.ModuleParser.classes= [className] ...
25 */
26 private static final String ITEM_MODULE_GENERATORS_POSFIX_KEY = ".item.ModuleGenerator.classes";
27
28
29 private String _type;
30 private ModuleGenerators _feedModuleGenerators;
31 private ModuleGenerators _itemModuleGenerators;
32 private Namespace[] _allModuleNamespaces;
33
34 protected BaseWireFeedGenerator(String type) {
35 _type = type;
36 _feedModuleGenerators = new ModuleGenerators(type + FEED_MODULE_GENERATORS_POSFIX_KEY, this);
37 _itemModuleGenerators = new ModuleGenerators(type + ITEM_MODULE_GENERATORS_POSFIX_KEY, this);
38 Set allModuleNamespaces = new HashSet();
39 Iterator i = _feedModuleGenerators.getAllNamespaces().iterator();
40 while (i.hasNext()) {
41 allModuleNamespaces.add(i.next());
42 }
43 i = _itemModuleGenerators.getAllNamespaces().iterator();
44 while (i.hasNext()) {
45 allModuleNamespaces.add(i.next());
46 }
47 _allModuleNamespaces = new Namespace[allModuleNamespaces.size()];
48 allModuleNamespaces.toArray(_allModuleNamespaces);
49 }
50
51 public String getType() {
52 return _type;
53 }
54
55 protected void generateModuleNamespaceDefs(Element root) {
56 for (int i = 0; i < _allModuleNamespaces.length; i++) {
57 root.addNamespaceDeclaration(_allModuleNamespaces[i]);
58 }
59 }
60
61 protected void generateFeedModules(List modules, Element feed) {
62 _feedModuleGenerators.generateModules(modules, feed);
63 }
64
65 public void generateItemModules(List modules, Element item) {
66 _itemModuleGenerators.generateModules(modules, item);
67 }
68
69 protected void generateForeignMarkup(Element e, List foreignMarkup) {
70 if (foreignMarkup != null) {
71 Iterator elems = (Iterator) foreignMarkup.iterator();
72 while (elems.hasNext()) {
73 Element elem = (Element) elems.next();
74 Parent parent = elem.getParent();
75 if (parent != null) {
76 parent.removeContent(elem);
77 }
78 e.addContent(elem);
79 }
80 }
81 }
82
83 /***
84 * Purging unused declarations is less optimal, performance-wise, than never adding them in the first place. So, we
85 * should still ask the ROME guys to fix their code (not adding dozens of unnecessary module declarations). Having
86 * said that: purging them here, before XML generation, is more efficient than parsing and re-molding the XML after
87 * ROME generates it.
88 * <p/>
89 * Note that the calling app could still add declarations/modules to the Feed tree after this. Which is fine. But
90 * those modules are then responsible for crawling to the root of the tree, at generate() time, to make sure their
91 * namespace declarations are present.
92 */
93 protected static void purgeUnusedNamespaceDeclarations(Element root) {
94 java.util.Set usedPrefixes = new java.util.HashSet();
95 collectUsedPrefixes(root, usedPrefixes);
96
97 List list = root.getAdditionalNamespaces();
98 List additionalNamespaces = new java.util.ArrayList();
99 additionalNamespaces.addAll(list);
100
101 for (int i = 0; i < additionalNamespaces.size(); i++) {
102 Namespace ns = (Namespace) additionalNamespaces.get(i);
103 String prefix = ns.getPrefix();
104 if (prefix != null && prefix.length() > 0 && !usedPrefixes.contains(prefix)) {
105 root.removeNamespaceDeclaration(ns);
106 }
107 }
108 }
109
110 private static void collectUsedPrefixes(Element el, java.util.Set collector) {
111 String prefix = el.getNamespacePrefix();
112 if (prefix != null && prefix.length() > 0 && !collector.contains(prefix)) {
113 collector.add(prefix);
114 }
115 List kids = el.getChildren();
116 for (int i = 0; i < kids.size(); i++) {
117 collectUsedPrefixes((Element) kids.get(i), collector);
118 }
119 }
120
121 }