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