KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > dottedname > DottedName


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin/mbeans/src/java/com/sun/enterprise/admin/dottedname/DottedName.java,v 1.4 2005/12/25 03:42:01 tcfujii Exp $
26  * $Revision: 1.4 $
27  * $Date: 2005/12/25 03:42:01 $
28  */

29 package com.sun.enterprise.admin.dottedname;
30
31 import java.util.Collections JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.ArrayList JavaDoc;
35
36 import com.sun.enterprise.admin.util.Tokenizer;
37 import com.sun.enterprise.admin.util.TokenizerImpl;
38
39
40 class SpecialChars
41 {
42     public final static String JavaDoc WILDCARDS = "*";
43     public final static char BACKSLASH = '\\';
44     public final static String JavaDoc SPECIALS = "(){}[];<>@$#";
45     public final static String JavaDoc LEGAL_CHARS = "abcdefghijklmnopqrstuvwxyz" +
46                                                       "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
47                                                       "0123456789" +
48                                                       "-_./" +
49                                                       BACKSLASH +
50                                                       WILDCARDS +
51                                                       SPECIALS;
52 };
53
54 /*
55  */

56 final class ParsedDottedName implements java.io.Serializable JavaDoc
57 {
58     public final String JavaDoc mDomain;
59     public final String JavaDoc mScope;
60     public final List JavaDoc mParts;
61     
62         public
63     ParsedDottedName( String JavaDoc domain, String JavaDoc scope, List JavaDoc parts )
64     {
65         if ( domain.length() != 0 )
66         {
67             checkLegalNamePart( domain );
68         }
69
70         checkLegalNamePart( scope );
71     
72         for( int i = 0; i < parts.size(); ++i )
73         {
74             checkLegalNamePart( (String JavaDoc)parts.get( i ) );
75         }
76         
77         mDomain = domain;
78         mScope = scope;
79         mParts = Collections.unmodifiableList( parts );
80     }
81     
82     
83         boolean
84     isLegalChar( final char theChar )
85     {
86         return( SpecialChars.LEGAL_CHARS.indexOf( theChar ) >= 0 );
87     }
88     
89         void
90     checkLegalChar( final char theChar )
91     {
92         if ( ! isLegalChar( theChar ) )
93         {
94             final String JavaDoc msg = DottedNameStrings.getString(
95                     DottedNameStrings.ILLEGAL_CHARACTER_KEY,
96                     "'" + theChar + "'" );
97                     
98             throw new IllegalArgumentException JavaDoc( msg );
99         }
100     }
101     
102         void
103     checkLegalNamePart( String JavaDoc part )
104     {
105         final int length = part.length();
106         
107         if ( length == 0 )
108         {
109             final String JavaDoc msg = DottedNameStrings.getString(
110                     DottedNameStrings.MISSING_EXPECTED_NAME_PART_KEY );
111                     
112             throw new IllegalArgumentException JavaDoc( msg );
113         }
114     
115         for( int i = 0; i < length; ++i )
116         {
117             final char theChar = part.charAt( i );
118             
119             checkLegalChar( theChar );
120         }
121     }
122     
123         public boolean
124     equals( Object JavaDoc other )
125     {
126         boolean equals = false;
127         
128         if ( ! (other instanceof ParsedDottedName) )
129         {
130             equals = false;
131         }
132         else if ( other == this )
133         {
134             equals = true;
135         }
136         else
137         {
138             equals = this.toString().equals( other.toString() );
139         }
140         
141         return( equals );
142     }
143     
144         public String JavaDoc
145     toString()
146     {
147         return( DottedName.toString( mDomain, mScope, mParts ) );
148     }
149 }
150
151 /*
152     Represents a dotted name. The dotted name consists of a domain (usually empty),
153     a scope (server name, config name, "domain") and parts.
154     
155     Parts is parts--it is up to the user to decide if the last part is the name of a value
156     or if the dotted name is a prefix to which value names may be appended.
157  */

158 public final class DottedName implements java.io.Serializable JavaDoc
159 {
160     final String JavaDoc mSourceString;
161     final ParsedDottedName mParsed;
162     
163     public final static String JavaDoc WILDCARDS = SpecialChars.WILDCARDS;
164     public final static char BACKSLASH = SpecialChars.BACKSLASH;
165     public final static String JavaDoc SPECIALS = SpecialChars.SPECIALS;
166     public final static String JavaDoc LEGAL_CHARS = SpecialChars.LEGAL_CHARS;
167                                                       
168         public
169     DottedName( String JavaDoc sourceString )
170     {
171         mSourceString = sourceString;
172         try
173         {
174             mParsed = parse( sourceString );
175         }
176         catch( com.sun.enterprise.admin.util.TokenizerException e )
177         {
178             final String JavaDoc msg = DottedNameStrings.getString(
179                     DottedNameStrings.MALFORMED_DOTTED_NAME_KEY,
180                     sourceString );
181                     
182             throw new IllegalArgumentException JavaDoc( msg );
183         }
184         
185         checkWellFormed( sourceString, toStringFromParsed( mParsed ) );
186     }
187     
188     /*
189         Certain malformed escape constructs will parse OK, but are not acceptable
190         as dotted names. Example: "Foo\Bar" is acceptable to the Tokenizer, but
191         must be written as "Foo\\Bar" to be a valid dotted name.
192      */

193         private void
194     checkWellFormed( String JavaDoc sourceString, String JavaDoc correctValue )
195     {
196         if ( ! sourceString.equals( correctValue ) )
197         {
198             final String JavaDoc msg = DottedNameStrings.getString(
199                     DottedNameStrings.MALFORMED_DOTTED_NAME_KEY,
200                     sourceString );
201                     
202             throw new IllegalArgumentException JavaDoc( msg );
203         }
204     }
205     
206         public static String JavaDoc
207     toString( DottedName dn, int numParts )
208     {
209         return( toString( dn.getDomain(), dn.getScope(), dn.getParts(), numParts ));
210     }
211     
212         public static String JavaDoc
213     toString( String JavaDoc domain, String JavaDoc scope, List JavaDoc parts )
214     {
215         return( toString( domain, scope, parts, parts.size() ));
216     }
217     
218         public static String JavaDoc
219     toString( String JavaDoc domain, String JavaDoc scope, List JavaDoc parts, int numParts )
220     {
221         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
222         
223         if ( domain.length() != 0 )
224         {
225             buf.append( domain + ":" );
226         }
227         
228         buf.append( escapePart( scope ) );
229         
230         for( int i = 0; i < numParts; ++ i )
231         {
232             final String JavaDoc unescapedPart = (String JavaDoc)parts.get( i );
233             
234             buf.append( "." + escapePart( unescapedPart ) );
235         }
236         
237         return( buf.toString() );
238     }
239     
240         public String JavaDoc
241     toString()
242     {
243         return( mSourceString );
244     }
245
246         public static String JavaDoc
247     toString(final String JavaDoc domain,
248              final String JavaDoc scope,
249              final List JavaDoc parts,
250              final boolean needsEscaping )
251     {
252         if(needsEscaping){
253             return toString(domain,scope,parts);
254         }
255
256         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
257
258         if ( domain.length() != 0 )
259         {
260             buf.append( domain + ":" );
261         }
262
263         buf.append( scope );
264
265         for( int i = 0; i < parts.size(); ++ i )
266         {
267             buf.append( "." + parts.get( i ) );
268         }
269
270         return( buf.toString() );
271     }
272
273         static boolean
274     needsEscaping( String JavaDoc part )
275     {
276         boolean needsEscaping = false;
277         
278         final int numChars = part.length();
279         for( int i = 0; i < numChars; ++i )
280         {
281             final char theChar = part.charAt( i );
282             
283             if ( ESCAPEABLE_CHARS.indexOf( theChar ) >= 0 )
284             {
285                 needsEscaping = true;
286                 break;
287             }
288         }
289
290         return( needsEscaping );
291     }
292     
293         public static String JavaDoc
294     escapePart( String JavaDoc part )
295     {
296         String JavaDoc result = part;
297         
298         // it is rare that escaping is needed (probably 99.9% of the cases it is not).
299
// Don't needlessly generate 2 new objects
300
if ( needsEscaping( part ) )
301         {
302             final int numChars = part.length();
303             final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
304             
305             for( int i = 0; i < numChars; ++i )
306             {
307                 final char theChar = part.charAt( i );
308                 
309                 if ( ESCAPEABLE_CHARS.indexOf( theChar ) >= 0 )
310                 {
311                     buf.append( ESCAPE_CHAR );
312                 }
313                 buf.append( theChar );
314             }
315
316             return( buf.toString() );
317         }
318         
319         return( result );
320     }
321     
322     
323     
324         static String JavaDoc
325     toStringFromParsed( ParsedDottedName pn )
326     {
327         return( toString( pn.mDomain, pn.mScope, pn.mParts, pn.mParts.size() ) );
328     }
329     
330     
331     private final static String JavaDoc NO_DOMAIN = "";
332     private final static char DOMAIN_DELIM = ':';
333     private final static char SEPARATOR = '.';
334     private final static char ESCAPE_CHAR = SpecialChars.BACKSLASH;
335     private final static String JavaDoc ESCAPEABLE_CHARS = "" + SEPARATOR + ESCAPE_CHAR;
336     
337     
338     
339         static ParsedDottedName
340     parse( String JavaDoc sourceString )
341         throws com.sun.enterprise.admin.util.TokenizerException
342     {
343         final Tokenizer tk = new TokenizerImpl( sourceString, "" + SEPARATOR,
344                 false, ESCAPE_CHAR, ESCAPEABLE_CHARS );
345         
346         final String JavaDoc [] tokens = tk.getTokens();
347         
348         if ( tokens.length == 0 )
349         {
350             final String JavaDoc msg = DottedNameStrings.getString(
351                     DottedNameStrings.DOTTED_NAME_MUST_HAVE_ONE_PART_KEY,
352                     sourceString );
353                     
354             throw new IllegalArgumentException JavaDoc( msg );
355         }
356         
357         // first token is the scope, last one is the value name, 2nd-to-last can
358
// be special case of "property" qualifier, but is still considered
359
// a name-part, not a value-name.
360

361         // if the scope contains a ':' then it's preceeded by a domain name.
362
final int domainDelimIndex = tokens[ 0 ].indexOf( DOMAIN_DELIM );
363         String JavaDoc scope = null;
364         String JavaDoc domain = NO_DOMAIN;
365         if ( domainDelimIndex >= 0 )
366         {
367             domain = tokens[ 0 ].substring( 0, domainDelimIndex );
368             scope = tokens[ 0 ].substring( domainDelimIndex + 1, tokens[ 0 ].length() );
369         }
370         else
371         {
372             scope = tokens[ 0 ];
373         }
374         
375         final ArrayList JavaDoc parts = new ArrayList JavaDoc();
376         for( int i = 1; i < tokens.length; ++i )
377         {
378             parts.add( tokens[ i ] );
379         }
380         
381         final ParsedDottedName parsedName = new ParsedDottedName( domain,
382                                                 scope, parts );
383         
384         return( parsedName );
385     }
386     
387         public String JavaDoc
388     getDomain()
389     {
390         return( mParsed.mDomain );
391     }
392     
393         public String JavaDoc
394     getScope()
395     {
396         return( mParsed.mScope );
397     }
398     
399     
400     /*
401         Return a list of the parts. Each part is the unescaped part as
402         parsed and unescaped from the original name.
403         
404         @returns List of Strings, each representing the name part.
405      */

406         public List JavaDoc
407     getParts()
408     {
409         return( Collections.unmodifiableList( mParsed.mParts ) );
410     }
411     
412     /*
413         Return the part given by its index
414         
415         @returns the part at the specified index
416      */

417         public String JavaDoc
418     getPart( int i )
419     {
420         return( (String JavaDoc)mParsed.mParts.get( i ) );
421     }
422     
423         public static boolean
424     isWildcardName( final String JavaDoc name )
425     {
426         boolean isWild = false;
427         final int numWilds = SpecialChars.WILDCARDS.length();
428         
429         for( int i = 0; i < numWilds; ++i )
430         {
431             final int wildChar = SpecialChars.WILDCARDS.charAt( i );
432             
433             if ( name.indexOf( wildChar ) >= 0 )
434             {
435                 isWild = true;
436                 break;
437             }
438
439         }
440         return( isWild );
441     }
442     
443         public boolean
444     isWildcardName()
445     {
446         return( isWildcardName( toString() ) );
447     }
448     
449     
450         public boolean
451     equals( Object JavaDoc other )
452     {
453         boolean equals = false;
454         
455         if ( ! (other instanceof DottedName) )
456         {
457             equals = false;
458         }
459         else if ( other == this )
460         {
461             equals = true;
462         }
463         else
464         {
465             equals = this.toString().equals( other.toString() );
466         }
467         
468         return( equals );
469     }
470 }
471
472
473
474
475
476
Popular Tags