1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.betwixt;
17
18 import java.io.FileInputStream;
19 import java.io.InputStream;
20 import java.net.URL;
21
22 import org.apache.commons.betwixt.io.BeanReader;
23 import org.apache.commons.betwixt.io.BeanWriter;
24 import org.apache.commons.digester.rss.Channel;
25 import org.apache.commons.digester.rss.RSSDigester;
26
27 /*** Reads an RSS file using Betwixt's auto-digester rules then
28 * outputs it again.
29 *
30 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31 * @version $Revision: 155402 $
32 */
33 public class RSSBeanReader extends AbstractTestCase {
34
35 /***
36 * The set of public identifiers, and corresponding resource names,
37 * for the versions of the DTDs that we know about.
38 */
39 protected static final String registrations[] = {
40 "-//Netscape Communications//DTD RSS 0.9//EN",
41 "/org/apache/commons/digester/rss/rss-0.9.dtd",
42 "-//Netscape Communications//DTD RSS 0.91//EN",
43 "/org/apache/commons/digester/rss/rss-0.91.dtd",
44 };
45
46 public RSSBeanReader(String testName) {
47 super(testName);
48 }
49
50 public static void main(String[] args) throws Exception {
51 RSSBeanReader sample = new RSSBeanReader("RSS");
52 sample.run( args );
53 }
54
55 public void run(String[] args) throws Exception {
56 BeanReader reader = new BeanReader();
57
58 reader.registerBeanClass( Channel.class );
59
60
61 for (int i = 0; i < registrations.length; i += 2) {
62 URL url = RSSDigester.class.getResource(registrations[i + 1]);
63 if (url != null) {
64 reader.register(registrations[i], url.toString());
65 }
66 }
67
68 Object bean = null;
69 if ( args.length > 0 ) {
70 bean = reader.parse( args[0] );
71 }
72 else {
73 InputStream in = new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") );
74 bean = reader.parse( in );
75 in.close();
76 }
77
78 write( bean );
79 }
80
81 public void write(Object bean) throws Exception {
82 if ( bean == null ) {
83 throw new Exception( "No bean read from the XML document!" );
84 }
85 BeanWriter writer = new BeanWriter();
86 writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
87 writer.enablePrettyPrint();
88 writer.write( bean );
89 }
90 }
91