KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > RSSBeanReader


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.betwixt;
17
18 import java.io.FileInputStream JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.net.URL JavaDoc;
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: 1.4 $
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 JavaDoc 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 JavaDoc testName) {
47         super(testName);
48     }
49
50     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
51         RSSBeanReader sample = new RSSBeanReader("RSS");
52         sample.run( args );
53     }
54     
55     public void run(String JavaDoc[] args) throws Exception JavaDoc {
56         BeanReader reader = new BeanReader();
57         
58         reader.registerBeanClass( Channel.class );
59         
60         // Register local copies of the DTDs we understand
61
for (int i = 0; i < registrations.length; i += 2) {
62             URL JavaDoc url = RSSDigester.class.getResource(registrations[i + 1]);
63             if (url != null) {
64                 reader.register(registrations[i], url.toString());
65             }
66         }
67         
68         Object JavaDoc bean = null;
69         if ( args.length > 0 ) {
70             bean = reader.parse( args[0] );
71         }
72         else {
73             InputStream JavaDoc in = new FileInputStream JavaDoc( 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 JavaDoc bean) throws Exception JavaDoc {
82         if ( bean == null ) {
83             throw new Exception JavaDoc( "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
92
Popular Tags