001: package de.java2html.properties;
002:
003: import java.util.Properties;
004: import java.util.StringTokenizer;
005:
006: import de.java2html.javasource.JavaSourceType;
007: import de.java2html.options.ConversionOptionsUtilities;
008: import de.java2html.options.HorizontalAlignment;
009: import de.java2html.options.IConversionOptionsConstants;
010: import de.java2html.options.JavaSourceConversionOptions;
011: import de.java2html.options.JavaSourceStyleEntry;
012: import de.java2html.options.JavaSourceStyleTable;
013: import de.java2html.util.RGB;
014:
015: /**
016: * @author Markus Gebhard
017: */
018: public class ConversionOptionsPropertiesReader {
019:
020: public JavaSourceConversionOptions read(Properties properties)
021: throws IllegalPropertyValueException {
022: JavaSourceConversionOptions conversionOptions = JavaSourceConversionOptions
023: .getRawDefault();
024: adjustStyleTable(properties, conversionOptions);
025: adjustShowFileName(properties, conversionOptions);
026: adjustBorder(properties, conversionOptions);
027: adjustLineNumbers(properties, conversionOptions);
028: adjustShowJava2HtmlLink(properties, conversionOptions);
029: adjustTabSize(properties, conversionOptions);
030: adjustAlignment(properties, conversionOptions);
031: adjustStyleEntries(properties, conversionOptions);
032: return conversionOptions;
033: }
034:
035: private void adjustAlignment(Properties properties,
036: JavaSourceConversionOptions conversionOptions)
037: throws IllegalPropertyValueException {
038: String alignmentValue = properties
039: .getProperty(IConversionOptionsConstants.HORIZONTAL_ALIGNMENT);
040: if (alignmentValue != null) {
041: HorizontalAlignment alignment = HorizontalAlignment
042: .getByName(alignmentValue);
043: if (alignment == null) {
044: throw new IllegalPropertyValueException(
045: IConversionOptionsConstants.HORIZONTAL_ALIGNMENT,
046: alignmentValue, ConversionOptionsUtilities
047: .getAvailableHorizontalAlignmentNames());
048: }
049: conversionOptions.setHorizontalAlignment(alignment);
050: }
051: }
052:
053: private void adjustTabSize(Properties properties,
054: JavaSourceConversionOptions conversionOptions)
055: throws IllegalPropertyValueException {
056: String tabSizeValue = properties
057: .getProperty(IConversionOptionsConstants.TAB_SIZE);
058: if (tabSizeValue != null) {
059: try {
060: int tabSize = Integer.parseInt(tabSizeValue);
061: if (tabSize < 0) {
062: throw new NumberFormatException();
063: }
064: conversionOptions.setTabSize(tabSize);
065: } catch (NumberFormatException e) {
066: throw new IllegalPropertyValueException(
067: IConversionOptionsConstants.TAB_SIZE,
068: tabSizeValue);
069: }
070: }
071: }
072:
073: private void adjustShowFileName(Properties properties,
074: JavaSourceConversionOptions conversionOptions)
075: throws IllegalPropertyValueException {
076: String value = properties
077: .getProperty(IConversionOptionsConstants.SHOW_FILE_NAME);
078: if (value != null) {
079: conversionOptions.setShowFileName(parseBooleanValue(
080: IConversionOptionsConstants.SHOW_FILE_NAME, value));
081: }
082: }
083:
084: private void adjustShowJava2HtmlLink(Properties properties,
085: JavaSourceConversionOptions conversionOptions)
086: throws IllegalPropertyValueException {
087: String value = properties
088: .getProperty(IConversionOptionsConstants.SHOW_JAVA2HTML_LINK);
089: if (value != null) {
090: conversionOptions.setShowJava2HtmlLink(parseBooleanValue(
091: IConversionOptionsConstants.SHOW_JAVA2HTML_LINK,
092: value));
093: }
094: }
095:
096: private void adjustLineNumbers(Properties properties,
097: JavaSourceConversionOptions conversionOptions)
098: throws IllegalPropertyValueException {
099: String value = properties
100: .getProperty(IConversionOptionsConstants.SHOW_LINE_NUMBERS);
101: if (value != null) {
102: conversionOptions.setShowLineNumbers(parseBooleanValue(
103: IConversionOptionsConstants.SHOW_LINE_NUMBERS,
104: value));
105: }
106: }
107:
108: private void adjustBorder(Properties properties,
109: JavaSourceConversionOptions conversionOptions)
110: throws IllegalPropertyValueException {
111: String value = properties
112: .getProperty(IConversionOptionsConstants.SHOW_TABLE_BORDER);
113: if (value != null) {
114: conversionOptions.setShowTableBorder(parseBooleanValue(
115: IConversionOptionsConstants.SHOW_TABLE_BORDER,
116: value));
117: }
118: }
119:
120: private void adjustStyleTable(Properties properties,
121: JavaSourceConversionOptions conversionOptions)
122: throws IllegalPropertyValueException {
123: String defaultTableName = properties
124: .getProperty(IConversionOptionsConstants.DEFAULT_STYLE_NAME);
125: if (defaultTableName != null) {
126: JavaSourceStyleTable table = JavaSourceStyleTable
127: .getPredefinedTable(defaultTableName);
128: if (table == null) {
129: throw new IllegalPropertyValueException(
130: IConversionOptionsConstants.DEFAULT_STYLE_NAME,
131: defaultTableName, ConversionOptionsUtilities
132: .getPredefinedStyleTableNames());
133: }
134: conversionOptions.setStyleTable(table);
135: }
136: }
137:
138: private boolean parseBooleanValue(String propertyName, String value)
139: throws IllegalPropertyValueException {
140: if (value.equalsIgnoreCase("true")
141: || value.equalsIgnoreCase("yes")) {
142: return true;
143: }
144: if (value.equalsIgnoreCase("false")
145: || value.equalsIgnoreCase("no")) {
146: return false;
147: }
148: throw new IllegalPropertyValueException(propertyName, value,
149: new String[] { "true", "false" });
150: }
151:
152: private RGB parseColorValue(String propertyName, String colorValue)
153: throws IllegalPropertyValueException {
154: StringTokenizer tokenizer = new StringTokenizer(colorValue, ","); //$NON-NLS-1$
155: if (tokenizer.countTokens() != 3) {
156: throw new IllegalPropertyValueException(propertyName,
157: colorValue);
158: }
159: String redValue = tokenizer.nextToken();
160: String greenValue = tokenizer.nextToken();
161: String blueValue = tokenizer.nextToken();
162: int red = 0, green = 0, blue = 0;
163: try {
164: red = Integer.parseInt(redValue);
165: green = Integer.parseInt(greenValue);
166: blue = Integer.parseInt(blueValue);
167: } catch (NumberFormatException e) {
168: throw new IllegalPropertyValueException(propertyName,
169: colorValue);
170: }
171: if (red > 255 || red < 0 || green > 255 || green < 0
172: || blue > 255 || blue < 0) {
173: throw new IllegalPropertyValueException(propertyName,
174: colorValue);
175: }
176: return new RGB(red, green, blue);
177: }
178:
179: private void adjustStyleEntries(Properties properties,
180: JavaSourceConversionOptions conversionOptions)
181: throws IllegalPropertyValueException {
182: JavaSourceType[] sourceTypes = JavaSourceType.getAll();
183: for (int i = 0; i < sourceTypes.length; i++) {
184: adjustStyleEntry(properties, conversionOptions,
185: sourceTypes[i]);
186: }
187: }
188:
189: private void adjustStyleEntry(Properties properties,
190: JavaSourceConversionOptions conversionOptions,
191: JavaSourceType type) throws IllegalPropertyValueException {
192: RGB color = getColor(properties, conversionOptions, type);
193: boolean bold = getBold(properties, conversionOptions, type);
194: boolean italic = getItalic(properties, conversionOptions, type);
195: conversionOptions.getStyleTable().put(type,
196: new JavaSourceStyleEntry(color, bold, italic));
197: }
198:
199: private boolean getItalic(Properties properties,
200: JavaSourceConversionOptions conversionOptions,
201: JavaSourceType type) throws IllegalPropertyValueException {
202:
203: String italicPropertyName = type.getName()
204: + IConversionOptionsConstants.POSTFIX_ITALIC;
205: String italicValue = properties.getProperty(italicPropertyName);
206: if (italicValue != null) {
207: return parseBooleanValue(italicPropertyName, italicValue);
208: }
209: return conversionOptions.getStyleTable().get(type).isItalic();
210: }
211:
212: private boolean getBold(Properties properties,
213: JavaSourceConversionOptions conversionOptions,
214: JavaSourceType type) throws IllegalPropertyValueException {
215: String boldPropertyName = type.getName()
216: + IConversionOptionsConstants.POSTFIX_BOLD;
217: String boldValue = properties.getProperty(boldPropertyName);
218: if (boldValue != null) {
219: return parseBooleanValue(boldPropertyName, boldValue);
220: }
221: return conversionOptions.getStyleTable().get(type).isBold();
222: }
223:
224: private RGB getColor(Properties properties,
225: JavaSourceConversionOptions conversionOptions,
226: JavaSourceType type) throws IllegalPropertyValueException {
227: String colorPropertyName = type.getName()
228: + IConversionOptionsConstants.POSTFIX_COLOR;
229: String colorValue = properties.getProperty(colorPropertyName);
230: if (colorValue != null) {
231: return parseColorValue(colorPropertyName, colorValue);
232: }
233: return conversionOptions.getStyleTable().get(type).getColor();
234: }
235: }
|