analyseCalc/src/nodeAC/constructionNodeAvecString....

270 lines
7.4 KiB
Java

package nodeAC;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class constructionNodeAvecString {
private nodeAC nod;
public constructionNodeAvecString() {
}
/**
* Constructeur nodeAC à partir du fichier source ODF transformé en String.
* @param code
*/
public nodeAC NewNode(String code) {
ArrayList<nodeAC> suitenodes = new ArrayList<nodeAC>();
Pattern p3 = Pattern.compile("(<[.[^ /<]]{1,}>{1}|<[.[^ /<]]{1,}/>|<[.[^ /<]]{1,}\\p{Space}[.[^<]]{1,}(>|/>){1}|</[[^< ].]{1,}>{1}|[.[^>\"<]]{1,})");
Matcher m3 = p3.matcher(code);
ArrayList<String> nom = new ArrayList<String>() ;
while(m3.find()) {
nom.add(code.substring(m3.start(), m3.end()));
}
String nomNode ="";
for(int i =0; i < nom.size(); i++) {
nodeAC No = new nodeAC();
Attributs attributsNode = new Attributs();
String codexml = nom.get(i);
// Node sans attribut sans fermeture
Pattern p = Pattern.compile("^<[.[^ /<]]{1,}>$");
Matcher m = p.matcher(codexml);
if(m.find()) {
nomNode = codexml.substring(1, m.end()-1);
if(nomNode.equals("?xml") || nomNode.equals("!--") ) continue;
No.setNomElt(nomNode);
suitenodes.add(No);
continue;
}
// Node sans attribut avec fermeture
p = Pattern.compile("^<[.[^ /<]]{1,}/>$");
m = p.matcher(codexml);
if(m.find()) {
nomNode = codexml.substring(1, m.end()-2);
if(nomNode.equals("?xml") || nomNode.equals("!--") ) continue;
No.setNomElt(nomNode);
suitenodes.add(No);
No.setClose(true);
continue;
}
// Node avec attribut sans fermeture
p = Pattern.compile("^<[.[^ /<]]{1,}\\p{Space}[.[^<]]{1,}[^/]>$"); //^<[.[^ /<]]{1,}\\p{Space}[.[^<]]{1,}>$
m = p.matcher(codexml);
if(m.find()) {
p = Pattern.compile("^<[.[^ /<]]{1,}\\p{Space}");
m = p.matcher(codexml);
if(m.find()) {
nomNode = codexml.substring(1, m.end()-1);
if(nomNode.equals("?xml") || nomNode.equals("!--") ) continue;
No.setNomElt(nomNode);
p = Pattern.compile("\\p{Space}[.[^ =]]{1,}=\"[.[^\"]]{1,}\"");
m = p.matcher(codexml);
while(m.find()) {
String attr = codexml.substring(m.start()+1, m.end()).replace("\"", "");
Pattern p2 = Pattern.compile("=");
Matcher m2 = p2.matcher(attr);
if(m2.find()) {
attributsNode.put(attr.substring(0, m2.start()), attr.substring(m2.start()+1,attr.length()));
}
}
No.setAttributs(attributsNode);
}
suitenodes.add(No);
continue;
}
// Node avec attribut et avec fermeture
p = Pattern.compile("^<[.[^ /<]]{1,}\\p{Space}[.[^<]]{1,}/>$");
m = p.matcher(codexml);
if(m.find()) {
p = Pattern.compile("^<[.[^ /<]]{1,}\\p{Space}");
m = p.matcher(codexml);
if(m.find()) {
nomNode = codexml.substring(1, m.end()-1);
if(nomNode.equals("?xml") || nomNode.equals("!--") ) continue;
No.setNomElt(nomNode);
p = Pattern.compile("\\p{Space}[.[^ =]]{1,}=\"[.[^\"]]{1,}\"");
m = p.matcher(codexml);
while(m.find()) {
String attr = codexml.substring(m.start()+1, m.end()).replace("\"", "");
Pattern p2 = Pattern.compile("=");
Matcher m2 = p2.matcher(attr);
if(m2.find()) {
attributsNode.put(attr.substring(0, m2.start()), attr.substring(m2.start()+1,attr.length()));
}
}
No.setAttributs(attributsNode);
}
suitenodes.add(No);
No.setClose(true);
continue;
}
//contenu à ajouter au node
codexml = codexml.replaceAll("(\n|\r)", "");
p = Pattern.compile("^[.[^>\"<]]{1,}$");
m = p.matcher(codexml);
if(m.find()) {
try {
int index = suitenodes.size()-1;
for(int j = index; index>=0; j--) {
if(!suitenodes.get(j).isClose()) {
if(suitenodes.get(j).getContenu().size()==index-j) {
suitenodes.get(j).setContenu(codexml.substring(m.start(), m.end()));
}else {
int nbContenuVide = index-j-suitenodes.get(j).getContenu().size();
for(int k = 0 ; k<nbContenuVide; k++) {
suitenodes.get(j).setContenu("");
}
suitenodes.get(j).setContenu(codexml.substring(m.start(), m.end()));
}
break;
}else {
}
}
continue;
}catch (Exception e) {
System.out.println(e);
System.out.println();
System.out.println("\t****************************");
System.out.println("\t** the file is corrupted **");
System.out.println("\t****************************");
System.out.println();
}
}
// poupée russe
// Fermeture de node classique </nomNode>
p = Pattern.compile("^</[[^< ].]{1,}>{1}$");
m = p.matcher(codexml);
if(m.find()) {
nomNode = codexml.substring(2, m.end()-1);
ArrayList<nodeAC> lesNodesEnfants = new ArrayList<nodeAC>();
int niveau = 0;
for(int h=suitenodes.size()-1; h >=0;h--) {
if(suitenodes.get(h).getNomElt().equals(nomNode) && !suitenodes.get(h).isClose()) {
suitenodes.get(h).setNodes(lesNodesEnfants);
suitenodes.get(h).setClose(true);
suitenodes.get(h).setLevel(niveau);
break;
}else {
if(!suitenodes.get(h).isClose()) {
System.out.println("Erreur fatale ... le node " + suitenodes.get(h).getNomElt() +" n'est pas close.");
System.out.println(codexml);
System.exit(0);
}
lesNodesEnfants.add(suitenodes.get(h));
}
}
for(int x=0; x< lesNodesEnfants.size();x++) {
suitenodes.remove(lesNodesEnfants.get(x));
}
}
}
//remettre dans l'ordre les nodes de suitenode
suitenodes = RemettreOrdreStructure(suitenodes);
suitenodes = renverseOrdre(suitenodes);
//initialisation du nodeAC;
nod = new nodeAC();
if(suitenodes.size()==1) {
nod.setNomElt(suitenodes.get(0).getNomElt());
nod.setAttributs(suitenodes.get(0).getAttributs());
nod.setNodes(suitenodes.get(0).getNodes());
nod.getContenu().addAll(suitenodes.get(0).getContenu());
}
if(suitenodes.size()>1) {
nod.setNomElt("fichier");
nod.setNodes(suitenodes);
}
setParentsAndLevel(nod,0);
return nod;
}
/**
* Très important car remet dans l'ordre le node.<br>
* <br>
* @param N
* @return
*/
private ArrayList<nodeAC> RemettreOrdreStructure(ArrayList<nodeAC> N) {
ArrayList<nodeAC> ordre = new ArrayList<nodeAC>();
for(int i = N.size()-1; i>=0; i--) {
N.get(i).setNodes(RemettreOrdreStructure(N.get(i).getNodes()));
ordre.add(N.get(i));
}
return ordre;
}
/**
* Renverse l'ordre d'ArrayList de nodes.
* @param N une ArrayList de nodes.
* @return une ArrayList de nodes.
*/
private ArrayList<nodeAC> renverseOrdre(ArrayList<nodeAC> N){
ArrayList<nodeAC> ordre = new ArrayList<nodeAC>();
for(int i = N.size()-1; i>=0; i--) {
ordre.add(N.get(i));
}
return ordre;
}
/**
* Insère les parents comme informations.
* @param N
*/
private void setParentsAndLevel(nodeAC N, Integer level) {
level++;
N.setLevel(level);
for(int i = 0 ; i < N.getNodes().size();i++) {
N.getNodes().get(i).setParent(N);
setParentsAndLevel(N.getNodes().get(i),level);
}
}
/**
* Getter pour obtenir le node construit.
* @return
*/
public nodeAC getNod() {
return nod;
}
}