src/share/jaxws_classes/com/sun/xml/internal/bind/api/impl/NameConverter.java

changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
equal deleted inserted replaced
366:8c0b6bccfe47 368:0989ad8c0860
23 * questions. 23 * questions.
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 javax.lang.model.SourceVersion;
28 import java.util.ArrayList; 29 import java.util.ArrayList;
29 import java.util.List; 30 import java.util.List;
30 import java.util.StringTokenizer; 31 import java.util.StringTokenizer;
31 32
32 /** 33 /**
129 scheme = nsUri.substring(0,idx); 130 scheme = nsUri.substring(0,idx);
130 if( scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("urn") ) 131 if( scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("urn") )
131 nsUri = nsUri.substring(idx+1); 132 nsUri = nsUri.substring(idx+1);
132 } 133 }
133 134
134 // issue 709; s/(.*)#(.*)/\1/
135 idx = nsUri.indexOf("#");
136 if(idx >= 0)
137 nsUri = nsUri.substring(0, idx);
138
139 // tokenize string 135 // tokenize string
140 ArrayList<String> tokens = tokenize( nsUri, "/: " ); 136 ArrayList<String> tokens = tokenize( nsUri, "/: " );
141 if( tokens.size() == 0 ) { 137 if( tokens.size() == 0 ) {
142 return null; 138 return null;
143 } 139 }
175 // get the token and remove illegal chars 171 // get the token and remove illegal chars
176 String token = tokens.get( i ); 172 String token = tokens.get( i );
177 token = removeIllegalIdentifierChars( token ); 173 token = removeIllegalIdentifierChars( token );
178 174
179 // this will check for reserved keywords 175 // this will check for reserved keywords
180 if( !NameUtil.isJavaIdentifier( token.toLowerCase() ) ) { 176 if (SourceVersion.isKeyword(token.toLowerCase())) {
181 token = '_' + token; 177 token = '_' + token;
182 } 178 }
183 179
184 tokens.set( i, token.toLowerCase() ); 180 tokens.set( i, token.toLowerCase() );
185 } 181 }
188 return combine( tokens, '.' ); 184 return combine( tokens, '.' );
189 } 185 }
190 186
191 187
192 private static String removeIllegalIdentifierChars(String token) { 188 private static String removeIllegalIdentifierChars(String token) {
193 StringBuffer newToken = new StringBuffer(); 189 StringBuilder newToken = new StringBuilder(token.length() + 1); // max expected length
194 for( int i = 0; i < token.length(); i++ ) { 190 for( int i = 0; i < token.length(); i++ ) {
195 char c = token.charAt( i ); 191 char c = token.charAt( i );
196 192 if (i == 0 && !Character.isJavaIdentifierStart(c)) { // c can't be used as FIRST char
197 if( i ==0 && !Character.isJavaIdentifierStart( c ) ) { 193 newToken.append('_');
198 // prefix an '_' if the first char is illegal 194 }
199 newToken.append('_').append(c); 195 if (!Character.isJavaIdentifierPart(c)) { // c can't be used
200 } else if( !Character.isJavaIdentifierPart( c ) ) { 196 newToken.append('_');
201 // replace the char with an '_' if it is illegal
202 newToken.append( '_' );
203 } else { 197 } else {
204 // add the legal char 198 newToken.append(c); // c is valid
205 newToken.append( c );
206 } 199 }
207 } 200 }
208 return newToken.toString(); 201 return newToken.toString();
209 } 202 }
210 203
265 * Smarter converter used for RELAX NG support. 258 * Smarter converter used for RELAX NG support.
266 */ 259 */
267 public static final NameConverter smart = new Standard() { 260 public static final NameConverter smart = new Standard() {
268 public String toConstantName( String token ) { 261 public String toConstantName( String token ) {
269 String name = super.toConstantName(token); 262 String name = super.toConstantName(token);
270 if( NameUtil.isJavaIdentifier(name) ) 263 if(!SourceVersion.isKeyword(name))
271 return name; 264 return name;
272 else 265 else
273 return '_'+name; 266 return '_'+name;
274 } 267 }
275 }; 268 };

mercurial