1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.sun.syndication.io.impl;
18
19 import com.sun.syndication.io.WireFeedParser;
20 import org.jdom.Document;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25
26 /***
27 * Parses an XML document (JDOM Document) into a Feed.
28 * <p>
29 * It accepts all flavors of RSS (0.90, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0) and
30 * Atom 0.3 feeds.
31 * <p>
32 * The WireFeedParser is a liberal parser.
33 * <p>
34 * WireFeedParser instances are thread safe.
35 * <p>
36 * Parsers for a specific type must extend this class and register in the parser list.
37 * (Right now registration is hardcoded in the WireFeedParser constructor).
38 * <p>
39 * @author Alejandro Abdelnur
40 *
41 */
42 public class FeedParsers {
43 private List PARSERS;
44 private List TYPES;
45
46 private List loadParsers(String defaultFile,String fileProperty) {
47 List list = new ArrayList();
48 PlugableClasses pClasses = new PlugableClasses(defaultFile,fileProperty,Constants.FEED_PARSERS_KEY,
49 true,FeedParsers.class.getClassLoader());
50 Object[] parsers;
51 try {
52 parsers = pClasses.createInstances();
53 }
54 catch (Exception ex) {
55 throw new RuntimeException(ex);
56 }
57 for (int i=0;i<parsers.length;i++) {
58 WireFeedParser parser = (WireFeedParser) parsers[i];
59 list.add(parser);
60 }
61 return list;
62 }
63
64 /***
65 * Creates a parser instance.
66 * <p>
67 *
68 */
69 public FeedParsers() {
70 this(Constants.DEFAULT_IMPLS_FILE,Constants.IMPLS_SYSTEM_PROPERTY);
71 }
72
73 public FeedParsers(String defaultFile,String fileProperty) {
74 PARSERS = loadParsers(defaultFile,fileProperty);
75 List types = new ArrayList();
76 for (int i=0;i<PARSERS.size();i++) {
77 types.add(((WireFeedParser)PARSERS.get(i)).getType());
78 }
79 TYPES = Collections.unmodifiableList(types);
80 }
81
82 public List getSupportedFeedTypes() {
83 return TYPES;
84 }
85
86 /***
87 * Finds the real parser type for the given document feed.
88 * <p>
89 * @param document document feed to find the parser for.
90 * @return the parser for the given document or <b>null</b> if there is no parser for that document.
91 *
92 */
93 public WireFeedParser getParserFor(Document document) {
94 WireFeedParser parser = null;
95 for (int i=0;parser==null && i<PARSERS.size();i++) {
96 parser = (WireFeedParser) PARSERS.get(i);
97 if (!parser.isMyType(document)) {
98 parser = null;
99 }
100 }
101 return parser;
102 }
103
104 }