KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > PropertiesFormatter


1 /*
2  * PropertiesFormatter.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: PropertiesFormatter.java,v 1.1.1.1 2002/09/24 16:07:57 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 {
26     private static final byte PROPERTIES_FORMAT_TEXT = 0;
27     private static final byte PROPERTIES_FORMAT_COMMENT = 1;
28     private static final byte PROPERTIES_FORMAT_SECTION = 2;
29     private static final byte PROPERTIES_FORMAT_KEY = 3;
30     private static final byte PROPERTIES_FORMAT_VALUE = 4;
31     private static final byte PROPERTIES_FORMAT_DELIMITER = 5;
32
33     public PropertiesFormatter(Buffer buffer)
34     {
35         this.buffer = buffer;
36     }
37
38     public LineSegmentList formatLine(Line line)
39     {
40         clearSegmentList();
41         final String JavaDoc text = getDetabbedText(line);
42         Line p = line.previous();
43         if (p != null) {
44             final int length = p.length();
45             if (length > 0 && p.charAt(length-1) == '\\') {
46                 // Continuation line.
47
addSegment(text, PROPERTIES_FORMAT_VALUE);
48                 return segmentList;
49             }
50         }
51         if (text.length() > 0) {
52             switch (text.charAt(0)) {
53                 case '#':
54                 case ';':
55                 case '!':
56                     addSegment(text, PROPERTIES_FORMAT_COMMENT);
57                     break;
58                 case '[':
59                 case '<':
60                     addSegment(text, PROPERTIES_FORMAT_SECTION);
61                     break;
62                 default:
63                     int index = text.indexOf('=');
64                     if (index < 0)
65                         index = text.indexOf(':');
66                     if (index >= 0) {
67                         addSegment(text, 0, index, PROPERTIES_FORMAT_KEY);
68                         addSegment(text, index, index+1, PROPERTIES_FORMAT_DELIMITER);
69                         addSegment(text, index+1, PROPERTIES_FORMAT_VALUE);
70                     } else
71                         addSegment(text, PROPERTIES_FORMAT_TEXT);
72                     break;
73             }
74         } else
75             addSegment(text, PROPERTIES_FORMAT_TEXT);
76         return segmentList;
77     }
78
79     public FormatTable getFormatTable()
80     {
81         if (formatTable == null) {
82             formatTable = new FormatTable("PropertiesMode");
83             formatTable.addEntryFromPrefs(PROPERTIES_FORMAT_TEXT, "text");
84             formatTable.addEntryFromPrefs(PROPERTIES_FORMAT_COMMENT, "comment");
85             formatTable.addEntryFromPrefs(PROPERTIES_FORMAT_SECTION, "section");
86             formatTable.addEntryFromPrefs(PROPERTIES_FORMAT_KEY, "key", "function");
87             formatTable.addEntryFromPrefs(PROPERTIES_FORMAT_VALUE, "value", "text");
88             formatTable.addEntryFromPrefs(PROPERTIES_FORMAT_DELIMITER, "delimiter", "operator");
89         }
90         return formatTable;
91     }
92 }
93
Popular Tags