01: /*
02: * PropertiesFormatter.java
03: *
04: * Copyright (C) 2000-2002 Peter Graves
05: * $Id: PropertiesFormatter.java,v 1.1.1.1 2002/09/24 16:07:57 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.j;
23:
24: public final class PropertiesFormatter extends Formatter {
25: private static final byte PROPERTIES_FORMAT_TEXT = 0;
26: private static final byte PROPERTIES_FORMAT_COMMENT = 1;
27: private static final byte PROPERTIES_FORMAT_SECTION = 2;
28: private static final byte PROPERTIES_FORMAT_KEY = 3;
29: private static final byte PROPERTIES_FORMAT_VALUE = 4;
30: private static final byte PROPERTIES_FORMAT_DELIMITER = 5;
31:
32: public PropertiesFormatter(Buffer buffer) {
33: this .buffer = buffer;
34: }
35:
36: public LineSegmentList formatLine(Line line) {
37: clearSegmentList();
38: final String text = getDetabbedText(line);
39: Line p = line.previous();
40: if (p != null) {
41: final int length = p.length();
42: if (length > 0 && p.charAt(length - 1) == '\\') {
43: // Continuation line.
44: addSegment(text, PROPERTIES_FORMAT_VALUE);
45: return segmentList;
46: }
47: }
48: if (text.length() > 0) {
49: switch (text.charAt(0)) {
50: case '#':
51: case ';':
52: case '!':
53: addSegment(text, PROPERTIES_FORMAT_COMMENT);
54: break;
55: case '[':
56: case '<':
57: addSegment(text, PROPERTIES_FORMAT_SECTION);
58: break;
59: default:
60: int index = text.indexOf('=');
61: if (index < 0)
62: index = text.indexOf(':');
63: if (index >= 0) {
64: addSegment(text, 0, index, PROPERTIES_FORMAT_KEY);
65: addSegment(text, index, index + 1,
66: PROPERTIES_FORMAT_DELIMITER);
67: addSegment(text, index + 1, PROPERTIES_FORMAT_VALUE);
68: } else
69: addSegment(text, PROPERTIES_FORMAT_TEXT);
70: break;
71: }
72: } else
73: addSegment(text, PROPERTIES_FORMAT_TEXT);
74: return segmentList;
75: }
76:
77: public FormatTable getFormatTable() {
78: if (formatTable == null) {
79: formatTable = new FormatTable("PropertiesMode");
80: formatTable.addEntryFromPrefs(PROPERTIES_FORMAT_TEXT,
81: "text");
82: formatTable.addEntryFromPrefs(PROPERTIES_FORMAT_COMMENT,
83: "comment");
84: formatTable.addEntryFromPrefs(PROPERTIES_FORMAT_SECTION,
85: "section");
86: formatTable.addEntryFromPrefs(PROPERTIES_FORMAT_KEY, "key",
87: "function");
88: formatTable.addEntryFromPrefs(PROPERTIES_FORMAT_VALUE,
89: "value", "text");
90: formatTable.addEntryFromPrefs(PROPERTIES_FORMAT_DELIMITER,
91: "delimiter", "operator");
92: }
93: return formatTable;
94: }
95: }
|