public String getName ( String name ) { StringBuilder sb = new StringBuilder ( ) ; char ch ; /* The HTML 4 spec at http://www.w3.org/TR/html4/types.html#h-6.2 mentions * that the name/id should begin with a letter followed by other valid characters. * The HTML 5 spec (draft) is more permissive on names/ids where the only restriction * is that it should be at least one character long and should not contain spaces. * The spec draft is @ http://www.w3.org/html/wg/drafts/html/master/dom.html#the-id-attribute. * * For HTML 4, we need to check for non-characters at the beginning of the name and * substitute it accordingly, "_" and "$" can appear at the beginning of a member name. * The method substitutes "$" with "Z:Z:D" and will prefix "_" with "Z:Z". */ for ( int i = 0 ; i < name . length ( ) ; i ++ ) { ch = name . charAt ( i ) ; switch ( ch ) { case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : sb . append ( ' ' ) ; break ; case ' ' : case ' ' : break ; case ' ' : sb . append ( ":A" ) ; break ; // Any appearance of $ needs to be substituted with ":D" and not with hyphen // since a field name "P$$ and a method P(), both valid member names, can end // up as "P--". A member name beginning with $ needs to be substituted with // "Z:Z:D". case ' ' : if ( i == 0 ) sb . append ( "Z:Z" ) ; sb . append ( ":D" ) ; break ; // A member name beginning with _ needs to be prefixed with "Z:Z" since valid anchor // names can only begin with a letter. case ' ' : if ( i == 0 ) sb . append ( "Z:Z" ) ; sb . append ( ch ) ; break ; default : sb . append ( ch ) ; } } return sb . toString ( ) ; }
Convert the name to a valid HTML name .