1 7 package org.jboss.xml.binding; 8 9 import org.jboss.util.Classes; 10 11 17 public final class Util 18 { 19 23 public static final char HYPHEN_MINUS = '\u002D'; 24 public static final char FULL_STOP = '\u002E'; 25 public static final char COLLON = '\u003A'; 26 public static final char LOW_LINE = '\u005F'; 27 public static final char MIDDLE_DOT = '\u00B7'; 28 public static final char GREEK_ANO_TELEIA = '\u0387'; 29 public static final char ARABIC_END_OF_AYAH = '\u06DD'; 30 public static final char ARABIC_START_OF_RUB_EL_HIZB = '\u06DE'; 31 32 42 public static String xmlNameToClassName(String name, boolean ignoreLowLine) 43 { 44 return XMLNameToJavaIdentifierConverter.PARSER.parse(XMLNameToJavaIdentifierConverter.CLASS_NAME, 45 name, 46 ignoreLowLine 47 ); 48 } 49 50 60 public static String xmlNameToGetMethodName(String name, boolean ignoreLowLine) 61 { 62 return "get" + xmlNameToClassName(name, ignoreLowLine); 63 } 64 65 75 public static String xmlNameToSetMethodName(String name, boolean ignoreLowLine) 76 { 77 return "set" + xmlNameToClassName(name, ignoreLowLine); 78 } 79 80 89 public static String xmlNameToConstantName(String name) 90 { 91 return XMLNameToJavaIdentifierConverter.PARSER.parse(XMLNameToJavaIdentifierConverter.CONSTANT_NAME, 92 name, 93 true 94 ); 95 } 96 97 104 public static String xmlNamespaceToJavaPackage(String namespace) 105 { 106 if(namespace.length() == 0) 107 { 108 return namespace; 109 } 110 111 char[] src = namespace.toLowerCase().toCharArray(); 112 char[] dst = new char[namespace.length()]; 113 114 int srcInd = 0; 115 while(src[srcInd++] != ':') 117 { 118 ; 119 } 120 121 while(src[srcInd] == '/') 122 { 123 ++srcInd; 124 } 125 126 if(src[srcInd] == 'w' && src[srcInd + 1] == 'w' && src[srcInd + 2] == 'w') 128 { 129 srcInd += 4; 130 } 131 132 int domainStart = srcInd; 134 while(srcInd < src.length && src[srcInd] != '/') 135 { 136 ++srcInd; 137 } 138 139 int dstInd = 0; 140 for(int start = srcInd - 1, end = srcInd; true; --start) 142 { 143 if(start == domainStart) 144 { 145 System.arraycopy(src, start, dst, dstInd, end - start); 146 dstInd += end - start; 147 break; 148 } 149 150 if(src[start] == '.') 151 { 152 System.arraycopy(src, start + 1, dst, dstInd, end - start - 1); 153 dstInd += end - start; 154 dst[dstInd - 1] = '.'; 155 end = start; 156 } 157 } 158 159 while(srcInd < src.length) 161 { 162 char c = src[srcInd++]; 163 if(c == '/') 164 { 165 if(srcInd < src.length) 166 { 167 dst = append(dst, dstInd++, '.'); 168 if(!Character.isJavaIdentifierStart(src[srcInd])) 169 { 170 dst = append(dst, dstInd++, '_'); 171 } 172 } 173 } 174 else if(c == '.') 175 { 176 break; 178 } 179 else 180 { 181 dst = append(dst, dstInd++, Character.isJavaIdentifierPart(c) ? c : '_'); 182 } 183 } 184 185 return String.valueOf(dst, 0, dstInd); 186 } 187 188 196 public static String xmlNameToClassName(String namespaceUri, String localName, boolean ignoreLowLine) 197 { 198 return namespaceUri == null || namespaceUri.length() == 0 ? 199 xmlNameToClassName(localName, ignoreLowLine) : 200 xmlNamespaceToJavaPackage(namespaceUri) + '.' + xmlNameToClassName(localName, ignoreLowLine); 201 } 202 203 public static boolean isAttributeType(final Class type) 204 { 205 return Classes.isPrimitive(type) || 206 type == String .class || 207 type == java.util.Date .class; 208 } 209 210 212 223 private static char[] append(char[] buf, int index, char ch) 224 { 225 if(index >= buf.length) 226 { 227 char[] tmp = buf; 228 buf = new char[index + 4]; 229 System.arraycopy(tmp, 0, buf, 0, tmp.length); 230 } 231 buf[index] = ch; 232 return buf; 233 } 234 235 237 250 interface XMLNameToJavaIdentifierConverter 251 { 252 byte IGNORE = 0; 254 byte APPEND = 1; 255 byte APPEND_WITH_LOW_LINE = 2; 256 byte APPEND_UPPER_CASED = 3; 257 byte APPEND_UPPER_CASED_WITH_LOW_LINE = 4; 258 259 267 byte commandForNext(char prev, char next, boolean ignoreLowLine); 268 269 273 final class PARSER 274 { 275 285 static String parse(XMLNameToJavaIdentifierConverter converter, String xmlName, boolean ignoreLowLine) 286 { 287 if(xmlName == null || xmlName.length() == 0) 288 { 289 throw new IllegalArgumentException ("Bad XML name: " + xmlName); 290 } 291 292 char c = xmlName.charAt(0); 293 int i = 1; 294 if(!Character.isJavaIdentifierStart(c) || (c == LOW_LINE && ignoreLowLine)) 295 { 296 while(i < xmlName.length()) 297 { 298 c = xmlName.charAt(i++); 299 if(Character.isJavaIdentifierStart(c) && !(c == LOW_LINE && ignoreLowLine)) 300 { 301 break; 302 } 303 } 304 305 if(i == xmlName.length()) 306 { 307 throw new IllegalArgumentException ( 308 "XML name contains no valid character to start Java identifier: " + xmlName 309 ); 310 } 311 } 312 313 char[] buf = new char[xmlName.length() - i + 1]; 314 buf[0] = Character.toUpperCase(c); 315 int bufInd = 1; 316 while(i < xmlName.length()) 317 { 318 char prev = c; 319 c = xmlName.charAt(i++); 320 byte command = converter.commandForNext(prev, c, ignoreLowLine); 321 switch(command) 322 { 323 case IGNORE: 324 break; 325 case APPEND: 326 buf = Util.append(buf, bufInd++, c); 327 break; 328 case APPEND_WITH_LOW_LINE: 329 buf = Util.append(buf, bufInd++, LOW_LINE); 330 buf = Util.append(buf, bufInd++, c); 331 break; 332 case APPEND_UPPER_CASED: 333 buf = Util.append(buf, bufInd++, Character.toUpperCase(c)); 334 break; 335 case APPEND_UPPER_CASED_WITH_LOW_LINE: 336 buf = Util.append(buf, bufInd++, LOW_LINE); 337 buf = Util.append(buf, bufInd++, Character.toUpperCase(c)); 338 break; 339 default: 340 throw new IllegalArgumentException ("Unexpected command: " + command); 341 } 342 } 343 344 return new String (buf, 0, bufInd); 345 } 346 } 347 348 351 XMLNameToJavaIdentifierConverter CLASS_NAME = new XMLNameToJavaIdentifierConverter() 352 { 353 public byte commandForNext(char prev, char next, boolean ignoreLowLine) 354 { 355 byte command; 356 if(Character.isDigit(next)) 357 { 358 command = APPEND; 359 } 360 else if(next == LOW_LINE) 361 { 362 command = ignoreLowLine ? IGNORE : APPEND; 363 } 364 else if(Character.isJavaIdentifierPart(next)) 365 { 366 if(Character.isJavaIdentifierPart(prev) && !Character.isDigit(prev)) 367 { 368 command = prev == LOW_LINE ? APPEND_UPPER_CASED : APPEND; 369 } 370 else 371 { 372 command = APPEND_UPPER_CASED; 373 } 374 } 375 else 376 { 377 command = IGNORE; 378 } 379 return command; 380 } 381 }; 382 383 386 XMLNameToJavaIdentifierConverter CONSTANT_NAME = new XMLNameToJavaIdentifierConverter() 387 { 388 public byte commandForNext(char prev, char next, boolean ignoreLowLine) 389 { 390 byte command; 391 if(Character.isDigit(next)) 392 { 393 command = Character.isDigit(prev) ? APPEND : APPEND_UPPER_CASED_WITH_LOW_LINE; 394 } 395 else if(Character.isJavaIdentifierPart(next)) 396 { 397 if(Character.isDigit(prev)) 398 { 399 command = APPEND_UPPER_CASED_WITH_LOW_LINE; 400 } 401 else if(Character.isJavaIdentifierPart(prev)) 402 { 403 command = Character.isUpperCase(next) ? APPEND_WITH_LOW_LINE : APPEND_UPPER_CASED; 404 } 405 else 406 { 407 command = APPEND_UPPER_CASED_WITH_LOW_LINE; 408 } 409 } 410 else 411 { 412 command = IGNORE; 413 } 414 return command; 415 } 416 }; 417 } 418 } 419 | Popular Tags |