Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jbreport/util/XMLOutputReportVisitor.java


1   /*
2    * $Id: XMLOutputReportVisitor.java,v 1.1.1.1 2000/08/31 13:14:48 grantfin Exp $
3    *
4    * jbReport - A reporting library for Java
5    * Copyright (C) 2000 Grant Finnemore <grantfin@users.sourceforge.net>
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package jbreport.util;
22  
23  import java.io.PrintWriter;
24  
25  import jbreport.ReportElement;
26  import jbreport.ReportException;
27  import jbreport.core.ReportVisitor;
28  import jbreport.core.ReportVisitorState;
29  
30  /**
31   * This should be capable of outputting the full xml definition of the report
32   * structure.
33   *
34   * <p> At the moment it caters for the names of the elements, and their 
35   * identifiers if present.
36   *
37   * @version $Revision: 1.1.1.1 $
38   */
39  public 
40  class XMLOutputReportVisitor implements ReportVisitor {
41  
42     /** The output stream used by this visitor */
43     private PrintWriter out;
44  
45     /** Should we flush before exiting each method? */
46     private boolean autoFlush = true;
47  
48     //
49     // Constructors
50     //
51  
52     public XMLOutputReportVisitor() {
53        this(new PrintWriter(System.out), true);
54     }
55  
56     public XMLOutputReportVisitor(PrintWriter out, boolean autoFlush) {
57        this.out = out;
58        this.autoFlush = autoFlush;
59     }
60  
61     //
62     // Implementation of the ReportVisitor interface
63     //
64  
65     public void visitReportComposite(ReportElement elem, 
66                                      ReportVisitorState state) 
67        throws ReportException {
68        out.println(createOutput(elem, state));
69        if(autoFlush) {
70           out.flush();
71        }
72     }
73  
74     public void visitGroup(ReportElement elem, ReportVisitorState state)
75        throws ReportException {
76        out.println(createOutput(elem, state));
77        if(autoFlush) {
78           out.flush();
79        }
80     }
81  
82     public void visitReportSection(ReportElement elem, ReportVisitorState state)
83        throws ReportException {
84        out.println(createOutput(elem, state));
85        if(autoFlush) {
86           out.flush();
87        }
88     }
89  
90     public void visitQuery(ReportElement elem, ReportVisitorState state) 
91        throws ReportException {
92        out.println(createOutput(elem, state));
93        if(autoFlush) {
94           out.flush();
95        }
96     }
97  
98     public void visitDatasource(ReportElement elem, ReportVisitorState state)
99        throws ReportException {
100       out.println(createOutput(elem, state));
101       if(autoFlush) {
102          out.flush();
103       }
104    }
105 
106    public void visitRepository(ReportElement elem, ReportVisitorState state) 
107       throws ReportException {
108       out.println(createOutput(elem, state));
109       if(autoFlush) {
110          out.flush();
111       }
112    }
113 
114    public void visitDocument(ReportElement elem, ReportVisitorState state) 
115       throws ReportException {
116       out.println(createOutput(elem, state));
117       if(autoFlush) {
118          out.flush();
119       }
120    }
121 
122    public void visitFragment(ReportElement elem, ReportVisitorState state) 
123       throws ReportException {
124       out.println(createOutput(elem, state));
125       if(autoFlush) {
126          out.flush();
127       }
128    }
129 
130    public void visitInclude(ReportElement elem, ReportVisitorState state) 
131       throws ReportException {
132       out.println(createOutput(elem, state));
133       if(autoFlush) {
134          out.flush();
135       }
136    }
137 
138    public void visitQueryBoundResult(ReportElement elem, 
139                                      ReportVisitorState state) 
140       throws ReportException {
141       out.println(createOutput(elem, state));
142       if(autoFlush) {
143          out.flush();
144       }
145    }
146    
147    public void visitSectionBreak(ReportElement elem, ReportVisitorState state)
148       throws ReportException {
149       out.println(createOutput(elem, state));
150       if(autoFlush) {
151          out.flush();
152       }
153    }
154 
155    public void visitTLine(ReportElement elem, ReportVisitorState state) 
156       throws ReportException {
157       out.println(createOutput(elem, state));
158       if(autoFlush) {
159          out.flush();
160       }
161    }
162 
163    public void visitTable(ReportElement elem, ReportVisitorState state) 
164       throws ReportException {
165       out.println(createOutput(elem, state));
166       if(autoFlush) {
167          out.flush();
168       }
169    }
170 
171    public void visitTableHeaderRow(ReportElement elem, 
172                                    ReportVisitorState state) 
173       throws ReportException {
174       out.println(createOutput(elem, state));
175       if(autoFlush) {
176          out.flush();
177       }
178    }
179 
180    public void visitTableHeaderItem(ReportElement elem, 
181                                     ReportVisitorState state)
182       throws ReportException {
183       out.println(createOutput(elem, state));
184       if(autoFlush) {
185          out.flush();
186       }
187    }
188 
189    public void visitTableRow(ReportElement elem, ReportVisitorState state)
190       throws ReportException {
191       out.println(createOutput(elem, state));
192       if(autoFlush) {
193          out.flush();
194       }
195    }
196 
197    public void visitStylesheet(ReportElement elem, ReportVisitorState state)
198       throws ReportException {
199       out.println(createOutput(elem, state));
200       if(autoFlush) {
201          out.flush();
202       }
203    }
204 
205    public void visitStyle(ReportElement elem, ReportVisitorState state)
206       throws ReportException {
207       out.println(createOutput(elem, state));
208       if(autoFlush) {
209          out.flush();
210       }
211    }
212 
213    public void visitAggregate(ReportElement elem, ReportVisitorState state)
214       throws ReportException {
215       out.println(createOutput(elem, state));
216       if(autoFlush) {
217          out.flush();
218       }
219    }
220 
221    //
222    // Implementation methods
223    //
224 
225    private String createOutput(ReportElement elem, ReportVisitorState state) {
226       StringBuffer buf = new StringBuffer();
227       if(state != null) {
228          // Create the indentation
229          for(int i = 0; i < state.getDepth(); i++) {
230             buf.append("  ");
231          }
232          buf.append('<');
233          // Add the '/' if this is the end element
234          if(state.isEnd()) {
235             buf.append('/');
236          }
237       }
238       else {
239          buf.append('<');
240       }
241       buf.append(elem.getType());
242       String name = elem.getName();
243       if(name != null && state.isStart()) {
244          buf.append(" name=\"").append(name).append('\"');
245       }
246       buf.append('>');
247       return buf.toString();
248    }
249 
250 }