View Javadoc

1   package com.sun.syndication.io.impl;
2   
3   import com.sun.syndication.feed.WireFeed;
4   import com.sun.syndication.feed.module.Extendable;
5   import com.sun.syndication.io.WireFeedParser;
6   import java.util.ArrayList;
7   import java.util.Iterator;
8   import org.jdom.Element;
9   
10  import java.util.List;
11  import org.jdom.Namespace;
12  import org.jdom.Attribute;
13  
14  /***
15   * @author Alejandro Abdelnur
16   */
17  public abstract class BaseWireFeedParser implements WireFeedParser {
18      /***
19       * [TYPE].feed.ModuleParser.classes=  [className] ...
20       *
21       */
22      private static final String FEED_MODULE_PARSERS_POSFIX_KEY = ".feed.ModuleParser.classes";
23  
24      /***
25       * [TYPE].item.ModuleParser.classes= [className] ...
26       *
27       */
28      private static final String ITEM_MODULE_PARSERS_POSFIX_KEY = ".item.ModuleParser.classes";
29  
30  
31      private String _type;
32      private ModuleParsers _feedModuleParsers;
33      private ModuleParsers _itemModuleParsers;
34      private Namespace _namespace;
35  
36      protected BaseWireFeedParser(String type, Namespace namespace) {
37          _type = type;
38          _namespace = namespace;
39          _feedModuleParsers = new ModuleParsers(type+FEED_MODULE_PARSERS_POSFIX_KEY, this);
40          _itemModuleParsers = new ModuleParsers(type+ITEM_MODULE_PARSERS_POSFIX_KEY, this);
41      }
42  
43      /***
44       * Returns the type of feed the parser handles.
45       * <p>
46       * @see WireFeed for details on the format of this string.
47       * <p>
48       * @return the type of feed the parser handles.
49       *
50       */
51      public String getType() {
52          return _type;
53      }
54  
55      protected List parseFeedModules(Element feedElement) {
56          return _feedModuleParsers.parseModules(feedElement);
57      }
58  
59      protected List parseItemModules(Element itemElement) {
60          return _itemModuleParsers.parseModules(itemElement);
61      }
62      
63      protected List extractForeignMarkup(Element e, Extendable ext, Namespace basens) {
64          ArrayList foreignMarkup = new ArrayList();
65          Iterator children = e.getChildren().iterator();
66          while (children.hasNext()) {
67              Element elem = (Element)children.next();
68              if  ( 
69                 // if elemet not in the RSS namespace
70                 !basens.equals(elem.getNamespace())
71                 // and elem was not handled by a module
72                 && null == ext.getModule(elem.getNamespaceURI())) {
73  
74                 // save it as foreign markup, 
75                 // but we can't detach it while we're iterating
76                 foreignMarkup.add(elem.clone()); 
77              }
78          }
79          // Now we can detach the foreign markup elements
80          Iterator fm = foreignMarkup.iterator();
81          while (fm.hasNext()) {
82              Element elem = (Element)fm.next();
83              elem.detach();
84          }
85          return foreignMarkup;
86      }
87  
88      protected Attribute getAttribute(Element e, String attributeName) {
89          Attribute attribute = e.getAttribute(attributeName);
90          if (attribute == null) {
91              attribute = e.getAttribute(attributeName, _namespace);
92          }
93          return attribute;
94      }
95  
96      protected String getAttributeValue(Element e, String attributeName) {
97          Attribute attr = getAttribute(e, attributeName);
98          return (attr != null) ? attr.getValue() : null;
99      }
100 
101 }
102