analyseCalc/src/nodeAC/nodeAC.java

790 lines
20 KiB
Java

package nodeAC;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Stream;
public class nodeAC implements Cloneable{
private String nomElt = "";
private ArrayList<nodeAC> Nodes = new ArrayList<nodeAC>();
private Attributs attributs = new Attributs();
private ArrayList<String> contenu = new ArrayList<String>();
private int level = 0;
private boolean close=false;
private nodeAC parent = null;
/**
* Node vide
*/
public nodeAC() {
}
/**
* Création d'un node pour l'évaluation des étudiants.<br>
* Ce node permet la réalisation du fichier d'analyse des fichier étudiants.<br>
* @param namenode Le nom du node.
* @param resultatcompare Le résultat de la comparaison.
* @param attributSujet L'attribut du sujet (celui qui est évalué).
* @param valueStudent Valeur de l'attribut de létudiant.
* @param valueSujet Valeur de l'attribut du sujet.
* @param niveau Le niveau.
* @param point Le nombre de point.
* @param nameElt Le nom de l'élément.
*/
public nodeAC(String namenode, String resultatcompare , String attributSujet , String valueStudent, String valueSujet, Integer niveau, Integer point, String nameElt) {
nomElt = namenode;
if(valueStudent==null) valueStudent="null";
attributs.put("resultat", resultatcompare);
attributs.put("attribut", attributSujet);
attributs.put("valueStudent", valueStudent);
attributs.put("valueSujet", valueSujet);
attributs.put("niveau", String.valueOf(niveau));
attributs.put("point", String.valueOf(point));
attributs.put("elt", nameElt);
}
/**
* Supprime un node enfants
* @param index
*/
public void supprimeNodeEnfant(nodeAC index) {
try {
Nodes.remove(index);
}catch (Exception e) {
System.out.println(e.toString());
}
}
/**
* Supprime un attribut du node
* @param key
*/
public void supprimeAttribut(String key) {
try {
if(attributs.get(key)!=null) {
attributs.remove(key);
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
/**
* Up node Child
* @param nodeChild
*/
public void upNodeEnfant(nodeAC nodeChild) {
int i = Nodes.indexOf(nodeChild);
if(i>0 && i!=-1) {
Nodes.remove(i);
Nodes.add(i-1,nodeChild);
}
}
/**
* Up node Child
* @param nodeChild
*/
public void downNodeEnfant(nodeAC nodeChild) {
int i = Nodes.indexOf(nodeChild);
if(i<=Nodes.size()-1 && i!=-1) {
Nodes.add(i+2,nodeChild);
Nodes.remove(i);
}
}
/**
* Retourne le nom du node.
* @return
*/
public String getNomElt() {
return nomElt;
}
/**
* Retourne tous les attributs sous forme d'un Dictionary<Strin,String>.
* @return
*/
public Attributs getAttributs() {
return attributs;
}
/**
* Retourne TRUE s'il y a au moins un attribut.
* @return
*/
public boolean isHasAttributs() {
if(attributs.size()>0) return true;
return false;
}
/**
* Retourne TRUE s'il y a au moins un node enfant.
* @return
*/
public boolean isHasEnfant() {
if(Nodes.size()>0) return true;
return false;
}
/**
* Retourne les nodes enfants sous forme d'une ArrayList<node>.
* @return
*/
public ArrayList<nodeAC> getNodes() {
return Nodes;
}
/**
* Retourne le contenu sous forme de ArrayList<String>
* @return
*/
public ArrayList<String> getContenu() {
return contenu;
}
/**
* Change le nom du node.
* @param nomElt
*/
public void setNomElt(String nomElt) {
this.nomElt = nomElt;
}
/**
* Ajoute une collection de node enfant.
* @param enfants
*/
public void setEnfants(ArrayList<nodeAC> enfants) {
this.Nodes = enfants;
}
/**
* Ajoute un node enfant à l'ArrayList<node>.
* @param enfants
*/
public void addNodeEnfants(nodeAC enfants) {
this.Nodes.add(enfants);
}
/**
* Insère un node enfant dans l'ArrayList<node>.
* @param enfant
* @param i
*/
public void insertNodeEnfant(nodeAC enfant, Integer i) {
if(i<0) i =0;
if(i>this.Nodes.size()-1) i = this.Nodes.size()-1;
this.Nodes.add(i,enfant);
}
/**
* Change le contenu de l'élément.<br>
* @param contenu Le nouveau contenu de l'élément
*/
public void setContenu(ArrayList<String> contenu) {
this.contenu = contenu;
}
/**
* Change le contenu de l'élément.<br>
*
* @param contenu Le nouveau contenu de l'élément
*/
public void setContenu(String contenu) {
this.contenu.add(contenu);
}
/**
* Ajoute du contenu au contenu.<br>
* @param newContenu Le nouveau contenu à ajouter.
*/
public void addContenu(String newContenu) {
this.contenu.add(newContenu);
}
/**
* Retourne le chemin vers ce node.
* @return
*/
public ArrayList<String> chemin() {
ArrayList<String> ListParent = new ArrayList<String>();
ListParent.add(this.nomElt);
if(parent!=null) ListParent.addAll(parent.chemin());
return ListParent;
}
/**
*
* @param attributs
*/
public void setAttributs(Attributs attributs) {
this.attributs = attributs;
}
/**
*
* @param nodes
*/
public void setNodes(ArrayList<nodeAC> nodes) {
this.Nodes = nodes;
}
/**
*
* @param n
*/
public void addNode(nodeAC n) {
if(n!=null) this.Nodes.add(n);
}
/**
*
* @param nodes
*/
public void addNode(ArrayList<nodeAC> nodes) {
for(int i = 0 ; i < nodes.size();i++) {
if(nodes.get(i)!=null) this.Nodes.add(nodes.get(i));
}
}
/**
*
* @return
*/
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
/**
* overwrite toString pour la JTree
*/
public String toString()
{
String retourneIdentifiantNode =this.nomElt;
if(retourneIdentifiantNode.equals("feuille")) {
return retourneIdentifiantNode = retourneIdentifiantNode + " * " + this.attributs.get("nomFeuille");
}
if(retourneIdentifiantNode.equals("colonne")) {
return retourneIdentifiantNode = retourneIdentifiantNode + " * " + this.attributs.get("RefColDansClasseur");
}
if(retourneIdentifiantNode.equals("ligne")) {
retourneIdentifiantNode = retourneIdentifiantNode + " * " + this.attributs.get("RefLigne");
}
if(retourneIdentifiantNode.equals("cellule")) {
retourneIdentifiantNode = retourneIdentifiantNode + " * " + this.attributs.get("RefColDansClasseur") + this.attributs.get("RefLigDansClasseur");
}
if(retourneIdentifiantNode.equals("meta:user-defined")) {
if(this.attributs.get("meta:name").contains("")) return retourneIdentifiantNode = retourneIdentifiantNode + " * " + this.attributs.get("meta:name").substring(0,this.attributs.get("meta:name").lastIndexOf(""));
retourneIdentifiantNode = retourneIdentifiantNode + " * " + this.attributs.get("meta:name");
}
if(retourneIdentifiantNode.equals("graphic")) {
if(this.attributs.get("nom")==null) retourneIdentifiantNode = retourneIdentifiantNode + " * " + this.attributs.get("nomObjet");
if(this.attributs.get("nom")!=null) retourneIdentifiantNode = retourneIdentifiantNode + " * " + this.attributs.get("nom");
}
return retourneIdentifiantNode;
}
/**
* clone
*/
public nodeAC clone() throws CloneNotSupportedException {
nodeAC b = (nodeAC)super.clone();
b.setNodes(new ArrayList<nodeAC>());
for(int i = 0 ; i < this.Nodes.size();i++) {
if(this.Nodes.get(i)!=null) b.getNodes().add(this.Nodes.get(i).clone());
}
return b;
}
/**
* Retourne le node parent.
* @return
*/
public nodeAC getParent() {
return parent;
}
/**
* Ajoute le node parent.
* @param parent
*/
public void setParent(nodeAC parent) {
if(parent.getNodes().contains(this)) {
this.parent = parent;
}
}
/**
* Etat du node fermer;
* @return
*/
public boolean isClose() {
return close;
}
/**
* Change l'état du node.
* @param close
*/
public void setClose(boolean close) {
this.close = close;
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
/**
* Trie par ordre alphabétique les clés des attributs.
*/
public Stream<Entry<String, String>> sort() {
return attributs.entrySet().stream().sorted(Map.Entry.comparingByKey());
}
//***************************************************************************************************
//***************************************************************************************************
//***************************************************************************************************
//***************************************************************************************************
//***************************************************************************************************
/**
* Retourne le premier enfant qui porte le nom "<b>nameNode</b>".<br>
* Sinon retourne un node null.<br>
*
* @param nameNode "<b>nameNode</b>" de l'enfant à retourner.
* @return retourne un nouveau node.
*/
public nodeAC retourneFirstEnfantsByName(String nameNode){
if(nameNode==null) return null;
if(nameNode.isEmpty()) return null;
nodeAC enfant = null;
if(this.nomElt.equals(nameNode)) return this;
for(int i = 0 ; i< this.Nodes.size();i++) {
if(this.Nodes.get(i)!=null) {
if(this.Nodes.get(i).getNomElt().equals(nameNode)) return this.Nodes.get(i);
enfant = this.Nodes.get(i).retourneFirstEnfantsByName(nameNode);
if(enfant!=null) return enfant;
}
}
return enfant;
}
/**
* Retourne la liste des nodes ayant tous les mêmes noms.
* @param nameNode
* @return
*/
public ArrayList<nodeAC> retourneListEnfantsByName(String nameNode){
ArrayList<nodeAC> ListeNodes = new ArrayList<nodeAC>();
for(int i = 0 ; i< this.Nodes.size();i++) {
if(Nodes.get(i)!=null) {
if(Nodes.get(i).getNomElt().equals(nameNode)) {
ListeNodes.add(Nodes.get(i));
}
ArrayList<nodeAC> B = Nodes.get(i).retourneListEnfantsByName(nameNode);
if(B.size()>0) ListeNodes.addAll(B);
}
}
return ListeNodes;
}
/**
* Retourne le premier enfant qui porte le nom "<b>nameNode</b>".<br>
* Et qui contient un attribut nommé "<b>nameAttribut</b>".<br>
* Et dont la valeur de cet attribut est "<b>valueAttribut</b>".<br>
* Sinon retourne un node null.<br>
* @param nameNode
* @param nameAttribut
* @param valueAttribut
* @return
*/
public nodeAC retourneFirstEnfantsByName(String nameNode, String nameAttribut, String valueAttribut){
if(nameNode==null) return null;
if(nameNode.isEmpty()) return null;
if(nameAttribut==null) return null;
if(nameAttribut.isEmpty()) return null;
if(valueAttribut==null) return null;
if(valueAttribut.isEmpty()) return null;
nodeAC enfant = null;
for(int i = 0 ; i< this.Nodes.size();i++) {
if(this.Nodes.get(i)!=null) {
if(this.Nodes.get(i).getNomElt().equals(nameNode)) {
if(this.Nodes.get(i).getAttributs().get(nameAttribut)!=null) {
if(this.Nodes.get(i).getAttributs().get(nameAttribut).equals(valueAttribut)) {
return this.Nodes.get(i);
}
}
}
enfant = this.Nodes.get(i).retourneFirstEnfantsByName(nameNode, nameAttribut, valueAttribut);
if(enfant!=null) return enfant;
}
}
return enfant;
}
/**
* Retourne la liste des enfants qui porte le nom "<b>nameNode</b>".<br>
* Et qui contient un attribut nommé "<b>nameAttribut</b>".<br>
* Et dont la valeur de cet attribut est "<b>valueAttribut</b>".<br>
* Sinon retourne un node null.<br>
* @param nameNode
* @return
*/
public ArrayList<nodeAC> retourneListEnfantsByName(String nameNode, String nameAttribut, String valueAttribut){
ArrayList<nodeAC> ListeNodes = new ArrayList<nodeAC>();
if(this.nomElt.equals(nameNode)) {
if(this.attributs.get(nameAttribut)!=null) {
if(this.attributs.get(nameAttribut).equals(valueAttribut)) {
ListeNodes.add(this);
}
}
}
for(int i = 0 ; i< this.Nodes.size();i++) {
if(Nodes.get(i)!=null) {
// nodeAC nod = Nodes.get(i).retourneFirstEnfantsByName(nameNode, nameAttribut, valueAttribut);
ArrayList<nodeAC> B = Nodes.get(i).retourneListEnfantsByName(nameNode, nameAttribut, valueAttribut);
if(B.size()>0) ListeNodes.addAll(B);
}
}
return ListeNodes;
}
/**
* Retourne Le premier parent ayant l'attribut demandé.<br>
* Si ne trouve pas, alors retourne un node null.<br>
* @param nameAttribut
* @return
*/
public nodeAC retourneParentAyantLAttribut(String nameAttribut) {
nodeAC nod = null;
if(this.getAttributs().get(nameAttribut)!=null) {
nod = this;
}else {
if(this.parent!=null) {
nod = this.parent.retourneParentAyantLAttribut(nameAttribut);
}
}
return nod;
}
/**
* Retourne le contenu et ceux des enfants.
* @param Elt : node
* @param contenu
* @return
*/
public String retourneLesContenusEnfants(String content) {
StringBuilder sb = new StringBuilder();
sb.append(content);
switch (this.getNomElt()) {
case "text:tab":
sb.append("[tab]");
break;
case "text:line-break":
sb.append("[text:line-break]");
break;
case "text:s":
sb.append("[text:s]");
break;
}
int indexContenu = 0 ;
if(this.getContenu().size()>indexContenu) {
sb.append(this.getContenu().get(indexContenu));
indexContenu++;
}
if(this.getNodes().size()>0) {
for(int i = 0 ; i < this.getNodes().size(); i++) {
if(this.getNodes().get(i)!=null) {
if(!this.getNodes().get(i).getNomElt().equals("draw:frame")) {
sb.append(this.getNodes().get(i).retourneLesContenusEnfants(""));
}
}
if(this.getContenu().size()>indexContenu) {
sb.append(this.getContenu().get(indexContenu));
indexContenu++;
}
}
}
return sb.toString();
}
/**
* Insère l'attribut addmenu avec la valeur logique.
* @param value : valeur logique.
*/
public void addMenu(Boolean value) {
attributs.put("addmenu", String.valueOf(value));
}
/**
* Insère l'attribut evaluer=true à ce node mais aussi aux nodes endfants.<br>
* Si ce node est de level== alors insère aussi l'attribut addMenu=true.
*/
public void evaluerAllChildTrue() {
this.attributs.put("evaluer", "true");
if(this.level==1) attributs.put("addmenu", "true");
for(int i = 0 ; i < this.getNodes().size();i++) {
if(this.getNodes().get(i)!=null) this.getNodes().get(i).evaluerAllChildTrue();
}
}
/**
* Insère l'attribut titre avec le texte.<br>
* Supprimer tous les attributs titre1, titre2 et titre3.
* @param Text : Le texte à placer.
*/
public void titre(String Text) {
attributs.put("titre", Text);
attributs.remove("titre1");
attributs.remove("titre2");
attributs.remove("titre3");
}
/**
* Insère l'attribut titre1 avec le texte.<br>
* Supprimer tous les attributs titre, titre2 et titre3.
* @param Text : Le texte à placer.
*/
public void titre1(String Text) {
attributs.remove("titre");
attributs.put("titre1", Text);
attributs.remove("titre2");
attributs.remove("titre3");
}
/**
* Insère l'attribut titre2 avec le texte.<br>
* Supprimer tous les attributs titre, titre1 et titre3.
* @param Text : Le texte à placer.
*/
public void titre2(String Text) {
attributs.remove("titre");
attributs.remove("titre1");
attributs.put("titre2", Text);
attributs.remove("titre3");
}
/**
* Insère l'attribut titre3 avec le texte.<br>
* Supprimer tous les attributs titre, titre1 et titre2.
* @param Text : Le texte à placer.
*/
public void titre3(String Text) {
attributs.remove("titre");
attributs.remove("titre1");
attributs.remove("titre2");
attributs.put("titre3", Text);
}
/**
* Insère le poids avec sa valeur.
* @param P : La valeur.
*/
public void poids(Double value ) {
attributs.put("poids", String.valueOf(value));
}
/**
* Insère l'attribut saut.
* @param value : valeur logique du saut.
*/
public void saut(Boolean value) {
attributs.put("saut", String.valueOf(value));
}
/**
* Insère l'attribut evaluer=false dans ce node uniquement.<br>
* Si ce node est level==1 alors l'attribu addMenu=false.
*/
public void evaluerFalse() {
attributs.put("evaluer", "false");
if(this.level==1) attributs.put("addmenu", "false");
}
/**
* Insère l'attribut evaluer=false dans toute la branche de la racine à la feuille.
*/
public void evaluerAllChildFalse() {
evaluerFalse();
for(int i = 0 ; i < this.getNodes().size();i++) {
if(this.getNodes().get(i)!=null) this.getNodes().get(i).evaluerAllChildFalse();
}
}
/**
* Insère l'attribut evaluer=true dans ce node uniquement.<br>
* Et celui de tous les nodes parents jusqu'à la racine.<br>
* Si un node parent est de level==1 alors insère aussi l'attribut addMenu=true.
*
*/
public void evaluerTrue() {
this.attributs.put("evaluer", "true");
if(this.level==1) this.attributs.put("addmenu", "true");
if(parent!=null) {
this.parent.evaluerTrue();
}
}
/**
* Supprime tous les nodes ayant le nom nameNode.
* @param nameNode
* @return
*/
public void supprimeTousLesNodesEnfantWithThisName(String nameNode){
List<nodeAC> listeDelete = new ArrayList<nodeAC>();
boolean trouve =false;
for(int i =0; i < this.Nodes.size();i++) {
if(this.Nodes.get(i)!=null)if(this.Nodes.get(i).getNomElt().equals(nameNode)) {
trouve=true;
listeDelete.add(this.Nodes.get(i));
}
}
if(trouve) {
this.Nodes.removeAll(listeDelete);
}
for(int i =0; i < this.Nodes.size();i++) {
if(this.Nodes.get(i)!=null) this.Nodes.get(i).supprimeTousLesNodesEnfantWithThisName(nameNode);
}
}
/**
* Supprime tous les nodes contenant l'attribut evaluer=false<br>
* ou ne contenant pas l'attribut evaluer.
*/
public void supprimeTousLesNodesEvaluerFalseOuNull() {
List<nodeAC> listeDelete = new ArrayList<nodeAC>();
boolean trouve =false;
for(int i =0; i < this.Nodes.size();i++) {
if(!this.getNodes().get(i).getNomElt().equals("setting")) {
if(this.Nodes.get(i).getAttributs().get("evaluer")!=null) {
if(!this.Nodes.get(i).getAttributs().get("evaluer").equalsIgnoreCase("true")) {
listeDelete.add(this.Nodes.get(i));
trouve=true;
}
}else {
listeDelete.add(this.Nodes.get(i));
trouve=true;
}
}
}
if(trouve) this.Nodes.removeAll(listeDelete);
for(int i =0; i < this.Nodes.size();i++) {
if(!this.Nodes.get(i).chemin().contains("setting")) {
this.Nodes.get(i).supprimeTousLesNodesEvaluerFalseOuNull();
}
}
}
/**
* Retourne true si le node ne possède pas de nom.
* @return
*/
public boolean isVide() {
if(this.nomElt.isEmpty())return true;
return true;
}
/**
*
* @param nameNode
* @return
*/
public boolean containChildByName(String nameNode) {
if(this.nomElt.equals(nameNode)) return true;
for(int i = 0 ; i < this.Nodes.size();i++) {
if(this.Nodes.get(i).containChildByName(nameNode)) return true;
}
return false;
}
/**
* Aperçu du code XMl du node.
* @return
*/
public String appercuXMLString() {
StringBuilder Text = new StringBuilder();
Text.append("<"+ this.nomElt);
Text.append(" parent=\"" + this.parent.nomElt + "\"");
Text.append(" level=\"" + this.level + "\"");
Set<String> key = this.attributs.keySet();
for (String k : key) {
Text.append(" " + k + "=\"" + this.attributs.get(k) + "\"");
}
Text.append(">\r" );
if(this.getContenu().size()>0) Text.append(this.getContenu().get(0));
for(int i = 0 ; i < this.Nodes.size();i++) {
Text.append("\r\t<" + this.Nodes.get(i).getNomElt() + "/>");
if(this.getContenu().size()>i+1) Text.append(this.getContenu().get(i+1));
}
Text.append("\r</"+ this.nomElt+">");
return Text.toString();
}
}