public static StringBuilder generateIdPrefix(String template, String prefix) {
		NullArgumentException.checkNotNull(template, "template");
		NullArgumentException.checkNotNull(prefix, "prefix");
		assert isValidId(prefix);
		final int len = template.length();
		// First character must be [A-Za-z]
		int pos = 0;
		while(pos < len) {
			char ch = template.charAt(pos);
			if(
				(ch >= 'A' && ch <= 'Z')
				|| (ch >= 'a' && ch <= 'z')
			) {
				break;
			}
			pos++;
		}
		StringBuilder idPrefix;
		if(pos == len) {
			// No usable characters from label
			idPrefix = new StringBuilder(prefix);
		} else {
			// Get remaining usable characters from label
			idPrefix = new StringBuilder(len - pos);
			//idPrefix.append(template.charAt(pos));
			//pos++;
			// Remaining must match [A-Za-z0-9:_.-]
			while(pos < len) {
				char ch = template.charAt(pos);
				pos++;
				// Convert space to '-'
				if(ch == ' ') {
					idPrefix.append('-');
				} else if(
					(ch >= 'A' && ch <= 'Z')
					|| (ch >= 'a' && ch <= 'z')
					|| (ch >= '0' && ch <= '9')
					|| ch == ':'
					|| ch == '_'
					|| ch == '.'
					|| ch == '-'
				) {
					if(ch >= 'A' && ch <= 'Z') {
						// Works since we're only using ASCII range:
						ch = (char)(ch + ('a' - 'A'));
						// Would support Unicode, but id's don't have Unicode:
						// ch = Character.toLowerCase(ch);
					}
					idPrefix.append(ch);
				}
			}
		}
		assert isValidId(idPrefix.toString());
		return idPrefix;
	}
public static StringBuilder generateIdPrefix ( String template , String prefix ) { NullArgumentException . checkNotNull ( template , "template" ) ; NullArgumentException . checkNotNull ( prefix , "prefix" ) ; assert isValidId ( prefix ) ; final int len = template . length ( ) ; int pos = 0 ; while ( pos < len ) { char ch = template . charAt ( pos ) ; if ( ( ch >= 'A' && ch <= 'Z' ) || ( ch >= 'a' && ch <= 'z' ) ) { break ; } pos ++ ; } StringBuilder idPrefix ; if ( pos == len ) { idPrefix = new StringBuilder ( prefix ) ; } else { idPrefix = new StringBuilder ( len - pos ) ; while ( pos < len ) { char ch = template . charAt ( pos ) ; pos ++ ; if ( ch == ' ' ) { idPrefix . append ( '-' ) ; } else if ( ( ch >= 'A' && ch <= 'Z' ) || ( ch >= 'a' && ch <= 'z' ) || ( ch >= '0' && ch <= '9' ) || ch == ':' || ch == '_' || ch == '.' || ch == '-' ) { if ( ch >= 'A' && ch <= 'Z' ) { ch = ( char ) ( ch + ( 'a' - 'A' ) ) ; } idPrefix . append ( ch ) ; } } } assert isValidId ( idPrefix . toString ( ) ) ; return idPrefix ; }
Generates a valid ID from an arbitrary string.

Strip all character not matching [A-Za-z][A-Za-z0-9:_.-]*

Also converts characters to lower case.

@param  template The preferred text to base the id on
@param  prefix   The base used when template is unusable (must be a valid id or invalid ID's may be generated)

@see <a href="http://www.w3.org/TR/2002/REC-xhtml1-20020801/#C_8">http://www.w3.org/TR/2002/REC-xhtml1-20020801/#C_8</a>