001 package de.java2html.converter;
002 
003 import java.io.BufferedWriter;
004 import java.io.IOException;
005 import java.io.Writer;
006 
007 import de.java2html.javasource.TypedSource;
008 import de.java2html.options.JavaSourceConversionOptions;
009 import de.java2html.util.Ensure;
010 
011 /**
012  * Abstract superclass for all converters for converting a {@link de.java2html.javasource.TypedSource}
013  * object to anything else.
014  
015  * For questions, suggestions, bug-reports, enhancement-requests etc.
016  * I may be contacted at:
017  *   <a href="mailto:markus@jave.de">markus@jave.de</a>
018  *
019  * The Java2html home page is located at:
020  *   <a href="http://www.java2html.de">http://www.java2html.de</a>
021  *
022  @author  <a href="mailto:markus@jave.de">Markus Gebhard</a>
023  @version 2.0, 05/07/02
024  *
025  * Copyright (C) Markus Gebhard 2000-2002
026  *
027  * This program is free software; you can redistribute it and/or
028  * modify it under the terms of the GNU General Public License
029  * as published by the Free Software Foundation; either version 2
030  * of the License, or (at your option) any later version.
031  *
032  * This program is distributed in the hope that it will be useful,
033  * but WITHOUT ANY WARRANTY; without even the implied warranty of
034  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
035  * GNU General Public License for more details.
036  
037  * You should have received a copy of the GNU General Public License
038  * along with this program; if not, write to the Free Software
039  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
040  */
041 public abstract class AbstractJavaSourceConverter implements IJavaSourceConverter {
042 
043   private final ConverterMetaData metaData;
044 
045   public AbstractJavaSourceConverter(ConverterMetaData metaData) {
046     Ensure.ensureArgumentNotNull(metaData);
047     this.metaData = metaData;
048   }
049 
050   /**
051    * Is called to convert the object 'source' to the destination
052    * format. The result is stored in 'result' and can be retrieved
053    * by calling getResult().
054    */
055   public final void convert(TypedSource source, JavaSourceConversionOptions options, Writer writer)
056       throws IOException {
057     BufferedWriter bw = null;
058     try {
059       bw = new BufferedWriter(writer);
060       convert(source, options, bw);
061       bw.flush();
062     }
063     catch (final IOException e) {
064       throw e;
065     }
066   }
067 
068   public abstract void convert(TypedSource source, JavaSourceConversionOptions options, BufferedWriter writer)
069       throws IOException;
070 
071   /**
072    * Returns a header for the result document.
073    * This one will be placed before the first block of converted
074    * code.
075    * Subclasses can return an empty String (&quot;&quot;) if there is none neccessary.
076    @param title 
077    */
078   public abstract String getDocumentHeader(JavaSourceConversionOptions options, String title);
079 
080   /**
081    * Returns a footer for the result document.
082    * This one will be placed behind the last block of converted
083    * code.
084    * Subclasses can return an empty String (&quot;&quot;) if there is none neccessary.
085    */
086   public abstract String getDocumentFooter(JavaSourceConversionOptions options);
087 
088   /**
089    * Returns the code that has to be placed between two blocks
090    * of converted code.
091    * Subclasses can return an empty String (&quot;&quot;) if there is none neccessary.
092    */
093   public abstract String getBlockSeparator(JavaSourceConversionOptions options);
094 
095   public void writeDocumentHeader(Writer writer, JavaSourceConversionOptions options, String title)
096       throws IOException {
097     writer.write(getDocumentHeader(options, title));
098   }
099 
100   public void writeDocumentFooter(Writer writer, JavaSourceConversionOptions optionsthrows IOException {
101     writer.write(getDocumentFooter(options));
102   }
103 
104   public void writeBlockSeparator(Writer writer, JavaSourceConversionOptions optionsthrows IOException {
105     writer.write(getBlockSeparator(options));
106   }
107 
108   public final String getDefaultFileExtension() {
109     return metaData.getDefaultFileExtension();
110   }
111   
112   public final ConverterMetaData getMetaData() {
113     return metaData;
114   }
115 }