View Javadoc

1   package org.apache.torque.betwixt;
2   
3   /*
4    * Copyright 2001-2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.beans.IntrospectionException;
20  import java.io.IOException;
21  import java.io.Writer;
22  
23  import org.apache.commons.betwixt.io.BeanWriter;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.xml.sax.SAXException;
27  
28  /***
29   * 
30   * Support for converting Torque (and other Betwixted mapped objects) in a 
31   * Dataset object into XML format.  It extends XMLEventNotifier, but does
32   * not make any notification calls.
33   *
34   * @author <a href="mailto:greg.monroe@dukece.com">Greg Monroe</a>
35   */
36  public class Exporter extends XMLEventNotifier 
37  {
38      
39      static Log logger = LogFactory.getLog( Exporter.class );
40      
41      public Exporter() 
42      {
43          super();
44      }
45      
46      /***
47       * Write the records in a dataset to XML
48       *
49       * @param dataset
50       *            The records to export.
51       * @param outputWriter
52       *            The writer that will get the XML.
53       * @param resolver
54       *            Resolve various information requirements.
55       * @throws IOException
56       * @throws IntrospectionException
57       * @throws SAXException
58       */
59      public void exportData( Dataset dataset, Writer outputWriter, 
60                              Resolver resolver )
61                                  throws IOException, IntrospectionException, 
62                                         SAXException, Exception 
63     {
64          BeanWriter beanWriter = createBeanWriter( outputWriter, resolver );
65          writeHeaderInfo( beanWriter, resolver );
66          writeData ( dataset, beanWriter );
67          beanWriter.close();
68      }
69      
70      /***
71       * Calls the full writeHeaderInfo method with "dataset" as the default
72       * element and uses the resolver information to create a SYSTEM Doctype
73       * heading.
74       * 
75       * @throws IOException 
76       * @throws IllegalStateException 
77       * 
78       */
79      public void writeHeaderInfo( BeanWriter beanWriter, Resolver resolver ) 
80                                        throws IOException, IllegalStateException
81      {
82          writeHeaderInfo( beanWriter, resolver, "dataset", true, null, null);
83      }
84      
85      /***
86       * Writes the standard xml version processing info and a custom DOCTYPE 
87       * header to the XML output file.  Must be called prior to writeData 
88       * for valid XML to be created.
89       * 
90       * @param beanWriter The output writer
91       * @param resolver  The resolver object with DTD info set.
92       * @param rootElement  The root element of the data that will be output,
93       *                      e.g. dataset.
94       * @param isSystem  If true, the DOCTYPE will be a "System" format and not
95       *                  a public ID format.
96       * @param publicId  The public ID to use if needed. Can be null if isSystem
97       *                  is true.
98       * @param uri       The URI to the DTD file to use for validation. If null,
99       *                  the Resolver will be used.
100      * @throws IOException
101      * @throws IllegalStateException 
102      */
103     public void writeHeaderInfo( BeanWriter beanWriter,Resolver resolver,
104                                     String rootElement, boolean isSystem,
105                                     String publicId, String uri ) 
106                                           throws IOException, 
107                                                  IllegalStateException 
108     {
109         beanWriter.writeXmlDeclaration("<?xml version='1.0' ?>");
110         StringBuffer doctype = new StringBuffer();
111         doctype.append("<!DOCTYPE ").append(rootElement).append(" ");
112         if ( isSystem ) 
113         {
114             doctype.append("SYSTEM ");
115         }
116         else
117         {
118             doctype.append("PUBLIC \"").append(publicId).append("\"\n");
119         }
120         
121         String uriOption = uri;
122         if ( uriOption == null ) 
123         {
124             uriOption = resolver.resolveDtdUri();
125         }
126         doctype.append("\"").append(uriOption).append("\" >");
127         beanWriter.writeXmlDeclaration(doctype.toString());
128         logger.debug("headerInfo written.");
129     }
130     
131     /***
132      * Write the records in this dataset to XML
133      * 
134      * @throws IOException
135      * @throws SAXException
136      * @throws IntrospectionException
137      */
138     public void writeData( Dataset dataset, BeanWriter beanWriter ) 
139                      throws IOException, IntrospectionException, SAXException 
140     {
141         beanWriter.write( dataset );
142     }
143 
144     /***
145      * Create the BeanWriter object to use to write data out.
146      * 
147      * @param outputWriter The writer that get the XML output.
148      * @param resolver A resolver object to locate needed information.
149      * @return A Betwixt BeanWriter to use in writing the XML data.
150      * @throws IOException
151      * @throws IntrospectionException
152      * @throws SAXException
153      * @throws Exception
154      */
155     public BeanWriter createBeanWriter(Writer outputWriter, 
156                                        Resolver resolver )
157                                             throws IOException, 
158                                                    IntrospectionException, 
159                                                    SAXException, Exception 
160     {
161 
162         // Create a BeanWriter which writes to our prepared stream
163         BeanWriter beanWriter = new BeanWriter(outputWriter);
164 
165         // Configure betwixt
166         beanWriter.getXMLIntrospector().register(resolver.resolveBetwixtFile());
167         beanWriter.getBindingConfiguration().setMapIDs(false);
168         beanWriter.enablePrettyPrint();
169         logger.debug("createBeanWriter - Done.");
170         return beanWriter;
171     }
172 }