1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  /** Reads an RSS file using Betwixt's auto-digester rules then
29    * outputs it again.
30    *
31    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
32    * @version $Revision: 438373 $
33    */
34  public class RSSBeanReader extends AbstractTestCase {
35      
36      /**
37       * The set of public identifiers, and corresponding resource names,
38       * for the versions of the DTDs that we know about.
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          // Register local copies of the DTDs we understand
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