24 */ |
24 */ |
25 |
25 |
26 package com.sun.xml.internal.bind.api.impl; |
26 package com.sun.xml.internal.bind.api.impl; |
27 |
27 |
28 import java.util.ArrayList; |
28 import java.util.ArrayList; |
|
29 import java.util.Collections; |
29 import java.util.HashSet; |
30 import java.util.HashSet; |
30 import java.util.List; |
31 import java.util.List; |
31 import java.util.Locale; |
32 import java.util.Locale; |
32 |
33 |
33 /** |
34 /** |
322 escape(sb, s, i); |
323 escape(sb, s, i); |
323 return sb.toString(); |
324 return sb.toString(); |
324 } |
325 } |
325 return s; |
326 return s; |
326 } |
327 } |
327 |
|
328 |
|
329 /** |
|
330 * Checks if a given string is usable as a Java identifier. |
|
331 */ |
|
332 public static boolean isJavaIdentifier(String s) { |
|
333 if(s.length()==0) return false; |
|
334 if( reservedKeywords.contains(s) ) return false; |
|
335 |
|
336 if(!Character.isJavaIdentifierStart(s.charAt(0))) return false; |
|
337 |
|
338 for (int i = 1; i < s.length(); i++) |
|
339 if (!Character.isJavaIdentifierPart(s.charAt(i))) |
|
340 return false; |
|
341 |
|
342 return true; |
|
343 } |
|
344 |
|
345 /** |
|
346 * Checks if the given string is a valid Java package name. |
|
347 */ |
|
348 public static boolean isJavaPackageName(String s) { |
|
349 while(s.length()!=0) { |
|
350 int idx = s.indexOf('.'); |
|
351 if(idx==-1) idx=s.length(); |
|
352 if( !isJavaIdentifier(s.substring(0,idx)) ) |
|
353 return false; |
|
354 |
|
355 s = s.substring(idx); |
|
356 if(s.length()!=0) s = s.substring(1); // remove '.' |
|
357 } |
|
358 return true; |
|
359 } |
|
360 |
|
361 |
|
362 /** All reserved keywords of Java. */ |
|
363 private static HashSet<String> reservedKeywords = new HashSet<String>(); |
|
364 |
|
365 static { |
|
366 // see http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html |
|
367 String[] words = new String[]{ |
|
368 "abstract", |
|
369 "boolean", |
|
370 "break", |
|
371 "byte", |
|
372 "case", |
|
373 "catch", |
|
374 "char", |
|
375 "class", |
|
376 "const", |
|
377 "continue", |
|
378 "default", |
|
379 "do", |
|
380 "double", |
|
381 "else", |
|
382 "extends", |
|
383 "final", |
|
384 "finally", |
|
385 "float", |
|
386 "for", |
|
387 "goto", |
|
388 "if", |
|
389 "implements", |
|
390 "import", |
|
391 "instanceof", |
|
392 "int", |
|
393 "interface", |
|
394 "long", |
|
395 "native", |
|
396 "new", |
|
397 "package", |
|
398 "private", |
|
399 "protected", |
|
400 "public", |
|
401 "return", |
|
402 "short", |
|
403 "static", |
|
404 "strictfp", |
|
405 "super", |
|
406 "switch", |
|
407 "synchronized", |
|
408 "this", |
|
409 "throw", |
|
410 "throws", |
|
411 "transient", |
|
412 "try", |
|
413 "void", |
|
414 "volatile", |
|
415 "while", |
|
416 |
|
417 // technically these are not reserved words but they cannot be used as identifiers. |
|
418 "true", |
|
419 "false", |
|
420 "null", |
|
421 |
|
422 // and I believe assert is also a new keyword |
|
423 "assert", |
|
424 |
|
425 // and 5.0 keywords |
|
426 "enum" |
|
427 }; |
|
428 for (String word : words) |
|
429 reservedKeywords.add(word); |
|
430 } |
|
431 } |
328 } |