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