View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2005 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
21  ////////////////////////////////////////////////////////////////////////////////
22  package org.millscript.millscript.render;
23  
24  import org.millscript.commons.util.IList;
25  import org.millscript.commons.util.MapIterator;
26  import org.millscript.commons.vfs.VFile;
27  import org.millscript.millscript.conf.Configuration;
28  import org.millscript.millscript.datatypes.XmlElement;
29  import org.millscript.millscript.tools.CastLibrary;
30  
31  import java.io.IOException;
32  
33  /**
34   * This class implements MillScripts datafile renderer. It renders to a plain
35   * text separated file format.
36   */
37  public final class DatafileRenderer extends AbstractRenderer {
38  
39      /**
40       * Constructs a new lines renderer, to render to the specified virtual file
41       * using the given confguration.
42       *
43       * @param conf    the configuration to get rendering parameters from
44       * @param file  the virtual output file
45       */
46      public DatafileRenderer( final Configuration conf, final VFile file ) {
47          super( conf, file );
48      }
49  
50      /**
51       * @see org.millscript.millscript.render.Renderer#append(char)
52       */
53      public void append( final char ch ) throws IOException {
54          this.appendNoEscape( ch );
55      }
56  
57      /**
58       * @see org.millscript.millscript.render.Renderer#append(java.lang.CharSequence)
59       */
60      @Override
61      public void append( final CharSequence cs ) throws IOException {
62          this.appendNoEscape( cs );
63      }
64  
65      /**
66       * @see org.millscript.millscript.render.Renderer#appendEscapeFor(char)
67       */
68      public void appendEscapeFor( final char ch ) throws IOException {
69          this.appendNoEscape( ch );
70      }
71  
72      /**
73       * @see org.millscript.millscript.render.Renderer#appendNoEscape(char)
74       */
75      public void appendNoEscape( final char ch ) throws IOException {
76          this.outputWriter.write( ch );
77      }
78  
79      /**
80       * @see org.millscript.millscript.render.Renderer#appendNoEscape(java.lang.CharSequence)
81       */
82      @Override
83      public void appendNoEscape( final CharSequence cs ) throws IOException {
84          this.outputWriter.write( cs.toString() );
85      }
86  
87      /**
88       * @see org.millscript.millscript.render.Renderer#render(java.lang.Object)
89       */
90      @Override
91      public void render( final Object o ) throws IOException {
92          if ( o == null ) {
93              return;
94          } else if ( o instanceof Renderable ) {
95              ((Renderable) o).render( this );
96          } else if ( o instanceof IList ) {
97              MapIterator lines = ((IList) o).iterator( true );
98              while ( lines.hasNext() ) {
99                  IList line = CastLibrary.toIList( lines.nextValue() );
100                 String gap = "";
101                 MapIterator items = line.iterator( true );
102                 while ( items.hasNext() ) {
103                     this.appendNoEscape( gap );
104                     gap = "|";
105                     this.renderObject( items.nextValue() );
106                 }
107                 this.appendNoEscape( '\n' );
108             }
109         } else {
110             this.renderObject( o );
111         }
112     }
113 
114     /**
115      * @see org.millscript.millscript.render.Renderer#renderDocumentFooter()
116      */
117     public void renderDocumentFooter() {
118         // There is no footer for a text file
119     }
120 
121     /**
122      * @see org.millscript.millscript.render.Renderer#renderDocumentHeader()
123      */
124     public void renderDocumentHeader() {
125         // There is no header for a text file
126     }
127 
128     /**
129      * @see org.millscript.millscript.render.Renderer#renderObject(java.lang.Object)
130      */
131     public void renderObject( final Object o ) throws IOException {
132         this.outputWriter.write( o.toString() );
133     }
134 
135     /**
136      * @see org.millscript.millscript.render.Renderer#renderXMLElement(org.millscript.millscript.datatypes.XmlElement)
137      */
138     public void renderXMLElement( final XmlElement e ) throws IOException {
139         this.outputWriter.write( e.toString() );
140     }
141 
142 }