001 package de.java2html.options;
002 
003 import java.io.IOException;
004 import java.io.InputStream;
005 import java.util.Properties;
006 
007 import de.java2html.properties.ConversionOptionsPropertiesReader;
008 import de.java2html.util.Ensure;
009 import de.java2html.util.IllegalConfigurationException;
010 import de.java2html.util.IoUtilities;
011 
012 /**
013  * Conversion options for customizing the output result. You can adjust the
014  * output style of a {@link de.java2html.converter.AbstractJavaSourceConverter}by
015  * changing the attributes of this object. The color and font style are defined
016  * by the {@link de.java2html.options.JavaSourceStyleTable} associated with
017  * this options.
018  
019  @see #setStyleTable(JavaSourceStyleTable)
020  @see #getStyleTable()
021  @see de.java2html.converter.AbstractJavaSourceConverter
022  
023  @author <a href="mailto:markus@jave.de">Markus Gebhard</a>
024  
025  <code>Copyright (C) Markus Gebhard 2000-2003
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.</code>
040  */
041 public class JavaSourceConversionOptions {
042 
043   public static final String DEFAULT_CHARSET = "iso-8859-1"//$NON-NLS-1$
044 
045   //Attribute names for persistence (e.g. in the eclipse plugin
046 
047   private static final String PROPERTIES_FILE_NAME = "java2html.properties"//$NON-NLS-1$
048   /**
049    @deprecated As of Dec 21, 2003 (Markus Gebhard), replaced by
050    *             {@link IConversionOptionsConstants#TAB_SIZE}
051    */
052   @Deprecated
053   public final static String TAB_SIZE = IConversionOptionsConstants.TAB_SIZE;
054 
055   /**
056    @deprecated As of Dec 21, 2003 (Markus Gebhard), replaced by
057    *             {@link IConversionOptionsConstants#SHOW_LINE_NUMBERS}
058    */
059   @Deprecated
060   public final static String SHOW_LINE_NUMBERS = IConversionOptionsConstants.SHOW_LINE_NUMBERS;
061 
062   /**
063    @deprecated As of Dec 21, 2003 (Markus Gebhard), replaced by
064    *             {@link IConversionOptionsConstants#SHOW_FILE_NAME}
065    */
066   @Deprecated
067   public final static String SHOW_FILE_NAME = IConversionOptionsConstants.SHOW_FILE_NAME;
068 
069   /**
070    @deprecated As of Dec 21, 2003 (Markus Gebhard), replaced by
071    *             {@link IConversionOptionsConstants#SHOW_TABLE_BORDER}
072    */
073   @Deprecated
074   public final static String SHOW_TABLE_BORDER = IConversionOptionsConstants.SHOW_TABLE_BORDER;
075 
076   private static JavaSourceConversionOptions defaultOptions;
077 
078   public static JavaSourceConversionOptions getRawDefault() {
079     return new JavaSourceConversionOptions();
080   }
081 
082   public static JavaSourceConversionOptions getDefault() throws IllegalConfigurationException {
083     if (defaultOptions == null) {
084       defaultOptions = createDefaultOptions();
085     }
086     return defaultOptions.getClone();
087   }
088 
089   private static JavaSourceConversionOptions createDefaultOptions() throws IllegalConfigurationException {
090     final InputStream inputStream = JavaSourceConversionOptions.class.getClassLoader().getResourceAsStream(
091         PROPERTIES_FILE_NAME);
092     if (inputStream == null) {
093       return new JavaSourceConversionOptions();
094     }
095 
096     /* TODO: ClassLoader.getSystemResourceAsStream(PROPERTIES_FILE_NAME)
097      instead of
098      Java2HtmlConversionOptions.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE_NAME);
099      Will have to find out how I can make this compatible when using Java WebStart.
100      As far as I know using ClassLoader.getSystemResourceAsStream() will propbably not find 
101      resources that are defined in the JNLP file. Maybe I should use 
102      ClassLoader.getSystemResourceAsStream as fallback if there is no file found using the other
103      classloader... */
104 
105     final Properties properties = new Properties();
106     try {
107       properties.load(inputStream);
108       return new ConversionOptionsPropertiesReader().read(properties);
109     }
110     catch (final IOException exception) {
111       throw new IllegalConfigurationException("Error loading configuration file '" //$NON-NLS-1$
112           + PROPERTIES_FILE_NAME + "' from classpath", exception)//$NON-NLS-1$
113     }
114     catch (final IllegalArgumentException exception) {
115       throw new IllegalConfigurationException("Error loading configuration file '" //$NON-NLS-1$
116           + PROPERTIES_FILE_NAME + "' from classpath", exception)//$NON-NLS-1$
117     }
118     finally {
119       IoUtilities.close(inputStream);
120     }
121   }
122 
123   private JavaSourceStyleTable styleTable = JavaSourceStyleTable.getDefault();
124   private int tabSize = 2;
125   private boolean showLineNumbers = false;
126   private boolean showFileName = false;
127   private boolean showTableBorder = false;
128 
129   /**
130    * Flag indication whether html output contains a link to the
131    * Java2Html-Homepage or not.
132    */
133   private boolean showJava2HtmlLink = false;
134   private boolean addLineAnchors = false;
135   private String lineAnchorPrefix = ""//$NON-NLS-1$
136   private HorizontalAlignment horizontalAlignment = HorizontalAlignment.LEFT;
137   private String charset = DEFAULT_CHARSET;
138   
139   private JavaSourceConversionOptions() {
140     //nothing to do
141   }
142 
143   @Override
144   public boolean equals(Object obj) {
145     if (!(obj instanceof JavaSourceConversionOptions)) {
146       return false;
147     }
148     final JavaSourceConversionOptions other = (JavaSourceConversionOptionsobj;
149     return (other.tabSize == tabSize
150         && other.styleTable.equals(styleTable)
151         && other.showFileName == showFileName
152         && other.showJava2HtmlLink == showJava2HtmlLink
153         && other.showLineNumbers == showLineNumbers
154         && other.showTableBorder == showTableBorder && other.horizontalAlignment == horizontalAlignment);
155   }
156 
157   @Override
158   public int hashCode() {
159     return styleTable.hashCode() + tabSize;
160   }
161 
162   public JavaSourceConversionOptions getClone() {
163     final JavaSourceConversionOptions options = new JavaSourceConversionOptions();
164     options.styleTable = styleTable.getClone();
165     options.tabSize = tabSize;
166     options.showLineNumbers = showLineNumbers;
167     options.showFileName = showFileName;
168     options.showJava2HtmlLink = showJava2HtmlLink;
169     options.showTableBorder = showTableBorder;
170     options.horizontalAlignment = horizontalAlignment;
171     return options;
172   }
173 
174   public void setStyleTable(JavaSourceStyleTable styleTable) {
175     Ensure.ensureArgumentNotNull(styleTable);
176     this.styleTable = styleTable;
177   }
178 
179   public JavaSourceStyleTable getStyleTable() {
180     return styleTable;
181   }
182 
183   public int getTabSize() {
184     return tabSize;
185   }
186 
187   public void setTabSize(int tabSize) {
188     this.tabSize = tabSize;
189   }
190 
191   public boolean isShowLineNumbers() {
192     return showLineNumbers;
193   }
194 
195   public void setShowLineNumbers(boolean showLineNumbers) {
196     this.showLineNumbers = showLineNumbers;
197   }
198 
199   public boolean isShowFileName() {
200     return showFileName;
201   }
202 
203   public boolean isShowTableBorder() {
204     return showTableBorder;
205   }
206 
207   public void setShowFileName(boolean showFileName) {
208     this.showFileName = showFileName;
209   }
210 
211   public void setShowTableBorder(boolean showTableBorder) {
212     this.showTableBorder = showTableBorder;
213   }
214 
215   public boolean isAddLineAnchors() {
216     return addLineAnchors;
217   }
218 
219   public String getLineAnchorPrefix() {
220     return lineAnchorPrefix;
221   }
222 
223   public void setAddLineAnchors(boolean addLineAnchors) {
224     this.addLineAnchors = addLineAnchors;
225   }
226 
227   public void setLineAnchorPrefix(String lineAnchorPrefix) {
228     this.lineAnchorPrefix = lineAnchorPrefix;
229   }
230 
231   public HorizontalAlignment getHorizontalAlignment() {
232     return horizontalAlignment;
233   }
234 
235   public void setHorizontalAlignment(HorizontalAlignment horizontalAlignment) {
236     Ensure.ensureArgumentNotNull(horizontalAlignment);
237     this.horizontalAlignment = horizontalAlignment;
238   }
239 
240   public boolean isShowJava2HtmlLink() {
241     return showJava2HtmlLink;
242   }
243 
244   public void setShowJava2HtmlLink(boolean isShowJava2HtmlLink) {
245     this.showJava2HtmlLink = isShowJava2HtmlLink;
246   }
247   
248   public void setCharset(String charset) {
249     Ensure.ensureArgumentNotNull(charset);
250     this.charset = charset;
251   }
252   
253   public String getCharset() {
254     return charset;
255   }
256 }