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