ConfigFormat.java
5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package com.sitech.ismp.ice.syslogd;
import com.sitech.ismp.ice.syslog.SyslogDefs;
import com.sitech.ismp.ice.util.StringUtilities;
import java.lang.*;
import java.text.*;
import java.util.*;
/**
* The ConfigFormat class implements the code necessary
* to format and parse syslog configuration entries.
*
* @version $Revision: 1.1.1.1 $
* @author Timothy Gerard Endres,
* <a href="mailto:time@ice.com">time@ice.com</a>.
* @see SyslogServer
*/
public class
ConfigFormat extends Format
{
public static final String RCS_ID = "$Id: ConfigFormat.java,v 1.1.1.1 1998/02/22 05:47:54 time Exp $";
public static final String RCS_REV = "$Revision: 1.1.1.1 $";
public static final String RCS_NAME = "$Name: $";
static public final char configSeparator = '.';
static public final String paramSeparator = " \t";
static public final String facilitySeparator = ",";
static public final ConfigFormat
getInstance()
{
return new ConfigFormat();
}
public
ConfigFormat()
{
super();
}
public String
format( ConfigEntry entry )
throws IllegalArgumentException
{
StringBuffer result =
new StringBuffer();
result.append
( "UNIMPLEMENTED: ConfigFormat.format( ConfigEntry )" );
return result.toString();
}
public StringBuffer
format( Object entry, StringBuffer appendTo, FieldPosition fieldPos )
throws IllegalArgumentException
{
// UNDONE - handle fieldPos!
String tmpFormat = this.format( (ConfigEntry)entry );
appendTo.append( tmpFormat );
return appendTo;
}
public ConfigEntry
parse( String source )
throws ParseException
{
return this.parseEntry( source );
}
public Object
parseObject( String source, ParsePosition pos )
{
ConfigEntry entry = null;
try {
entry = this.parseEntry( source );
}
catch ( ParseException ex )
{
entry = null;
}
return (Object) entry;
}
// UNDONE - all the positions in ParseExceptions are zero.
public ConfigEntry
parseEntry( String source )
throws ParseException
{
String entryStr = source;
String facilityStr = null;
String priorityStr = null;
String actionStr = null;
String matchStr = null;
String parameterStr = null;
String[] params = null;
int index, tabIdx, slashIdx;
int facility, priority;
int tokeCount;
StringTokenizer toker;
boolean hasMatch = false;
index = entryStr.indexOf( ConfigFormat.configSeparator );
if ( index < 0 )
{
throw new ParseException
( "configuration entry has no facility field", 0 );
}
facilityStr = entryStr.substring( 0, index );
entryStr = entryStr.substring( index + 1 );
index = entryStr.indexOf( ConfigFormat.configSeparator );
if ( index < 0 )
{
throw new ParseException
( "configuration entry has no priority field", 0 );
}
priorityStr = entryStr.substring( 0, index );
entryStr = entryStr.substring( index + 1 );
index = entryStr.indexOf( ' ' );
tabIdx = entryStr.indexOf( '\t' );
slashIdx = entryStr.indexOf( '/' );
if ( slashIdx > 0
&& ( index < 0 || slashIdx < index )
&& ( tabIdx < 0 || slashIdx < tabIdx ) )
{
// We have a regeular expression to match...
actionStr = entryStr.substring( 0, slashIdx );
entryStr = entryStr.substring( slashIdx + 1 );
slashIdx = entryStr.indexOf( '/' );
if ( slashIdx < 0 )
{
throw new ParseException
( "configuration entry has bad match expression", 0 );
}
else
{
hasMatch = true;
matchStr = entryStr.substring( 0, slashIdx );
if ( (slashIdx + 1) < entryStr.length() )
parameterStr = entryStr.substring( slashIdx + 1 );
else
parameterStr = null;
}
}
else if ( index < 0 && tabIdx < 0 )
{
actionStr = entryStr;
parameterStr = null;
}
else
{
if ( tabIdx >= 0 && tabIdx < index )
index = tabIdx;
actionStr = entryStr.substring( 0, index );
parameterStr = entryStr.substring( index + 1 );
}
if ( parameterStr != null )
{
params =
com.sitech.ismp.ice.util.StringUtilities.parseArgumentString
( parameterStr );
}
ConfigEntry result = new ConfigEntry();
SyslogAction action =
new SyslogAction( actionStr, params );
result.setAction( action );
if ( hasMatch )
{
result.setMatchExpr( matchStr );
}
if ( priorityStr.equals( "*" ) )
{
priority = SyslogDefs.LOG_ALL;
}
else
{
priority = SyslogDefs.getPriority( priorityStr );
}
for ( int i = 0 ; i < SyslogDefs.LOG_NFACILITIES ; ++i )
{
result.setFacilityLevel( i, -1 ); // UNDONE !!!
}
if ( facilityStr.equals( "*" ) )
{
for ( int i = 0 ; i < SyslogDefs.LOG_NFACILITIES ; ++i )
{
result.setFacilityLevel( i, priority );
}
}
else
{
toker = new StringTokenizer
( facilityStr, ConfigFormat.facilitySeparator );
tokeCount = toker.countTokens();
if ( tokeCount > 0 )
{
for ( int pIdx = 0 ; pIdx < tokeCount ; ++pIdx )
{
String facilityName = null;
try { facilityName = toker.nextToken(); }
catch ( NoSuchElementException ex )
{ break; }
facility = SyslogDefs.getFacility( facilityName );
result.setFacilityLevel( facility, priority );
}
}
}
return result;
}
}