Peu de nouveauté cette fois, je me contente juste de la classe de base Hiragana. Son constructeur prend en paramètre un noeud (node) et le parcours afin d’instancier correctement les attributs. En cas de problème avec le constructeur, il jette simplement une exception générique, par exemple si le noeud passé est null ou porte un nom différent de celui attendu. Une fois initialisé les valeurs peuvent seulement être lu par les “getters”.
import org.w3c.dom.Node;
import java.lang.Exception;
//classe Hiragana
public class Hiragana {
private Integer id;
private String image;
private String romanji;
private String sound;
private Integer level;
//constructeur
public Hiragana(Node nH) throws Exception {
if (nH == null) {
throw (new Exception("Null Node"));
}// end if
if (!nH.getNodeName().equals("hiragana"))
{
throw (new Exception("Bad Node"));
}// end if
id = Integer.getInteger(nH.getAttributes().getNamedItem("id").getNodeValue());
Node child = nH.getFirstChild();
while (!(child.getNextSibling() == null)) {
if (child.getNodeName().equals("image")) {
image = child.getAttributes().getNamedItem("uri").getNodeValue();
}//end if
if (child.getNodeName().equals("romanji")) {
romanji = child.getFirstChild().getNodeValue();
}//end if
if (child.getNodeName().equals("level")) {
level = Integer.getInteger(child.getFirstChild().getNodeValue());
}//end if
if (child.getNodeName().equals("son")) {
sound = child.getAttributes().getNamedItem("uri").getNodeValue();
}// end if
child = child.getNextSibling();
}//end while
}//end constructeur
//getters
public Integer getId(){ return this.id; }
public String getImage(){ return this.image; }
public String getSound(){ return this.sound; }
public String getRomanji(){ return this.romanji; }
public Integer getLevel(){ return this.level; }
}


