01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: 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: // Register local copies of the DTDs we understand
61: for (int i = 0; i < registrations.length; i += 2) {
62: URL url = RSSDigester.class
63: .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: } else {
73: InputStream in = new FileInputStream(
74: 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()
88: .setAttributesForPrimitives(false);
89: writer.enablePrettyPrint();
90: writer.write(bean);
91: }
92: }
|