Génération d'id des composants JSF
Salut !!
J'ai un problème qui me laisse perplexe...
Ci-dessous la méthode generateUniqueId() de la classe DefaultFaceletContext :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
/*
* (non-Javadoc)
*
* @see com.sun.facelets.FaceletContext#generateUniqueId(java.lang.String)
*/
public String generateUniqueId(String base) {
if(prefix==null) {
//TODO: change to StringBuilder when JDK1.5 support is available
StringBuffer builder = new StringBuffer(faceletHierarchy.size()*30);
for(int i=0; i< faceletHierarchy.size(); i++) {
DefaultFacelet facelet = (DefaultFacelet) faceletHierarchy.get(i);
builder.append(facelet.getAlias());
}
Integer prefixInt = new Integer(builder.toString().hashCode());
Integer cnt = (Integer) prefixes.get(prefixInt);
if(cnt==null) {
this.prefixes.put(prefixInt,new Integer(0));
prefix = prefixInt.toString();
} else {
int i=cnt.intValue()+1;
this.prefixes.put(prefixInt,new Integer(i));
prefix = prefixInt+"_"+i;
}
}
Integer cnt = (Integer) this.ids.get(base);
if (cnt == null) {
this.ids.put(base, new Integer(0));
uniqueIdBuilder.delete(0,uniqueIdBuilder.length());
uniqueIdBuilder.append(prefix);
uniqueIdBuilder.append("_");
uniqueIdBuilder.append(base);
return uniqueIdBuilder.toString();
} else {
int i = cnt.intValue() + 1;
this.ids.put(base, new Integer(i));
uniqueIdBuilder.delete(0,uniqueIdBuilder.length());
uniqueIdBuilder.append(prefix);
uniqueIdBuilder.append("_");
uniqueIdBuilder.append(base);
uniqueIdBuilder.append("_");
uniqueIdBuilder.append(i);
return uniqueIdBuilder.toString();
}
} |
La construction d'un uniqueId se base sur un hashCode, qui peut donc être négatif...chelou quand même pour un id !!
Ci-dessous le code de la méthode validateId de la classe UiComponentBase :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
private static void validateId(String id) {
if (id == null) {
return;
}
int n = id.length();
if (n < 1) {
throw new IllegalArgumentException();
}
for (int i = 0; i < n; i++) {
char c = id.charAt(i);
if (i == 0) {
if (!Character.isLetter(c) && (c != '_')) {
throw new IllegalArgumentException(id);
}
} else {
if (!Character.isLetter(c) &&
!Character.isDigit(c) &&
(c != '-') && (c != '_')) {
throw new IllegalArgumentException(id);
}
}
}
} |
Si un des caractères de l'id n'est pas une lettre, un chiffre ou "_", BAM exception !!!
=> avec un id généré "négatif" qui commence par "-" par exemple, ben on a une erreur.....
Un commentaire sur le sujet ?