Skip to content

Commit

Permalink
#37 Gestion cas complexe
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelFOURQUET committed Mar 11, 2013
1 parent e54db9c commit 95fe532
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 177 deletions.
107 changes: 88 additions & 19 deletions projet/src/main/java/xmltojson/jsonWriter/ToJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void toJson(List<Element> questions) {
//save.indempter();

}

/**
* Traduire en objet Json et ecriture dans le fichier.
* @param element traite une element Jdom2
Expand All @@ -92,6 +93,7 @@ public void toJson(List<Element> questions) {
public Map toJson(Element element) {
List<Attribute> attList = new ArrayList<Attribute>();
List<Element> childrens = new ArrayList<Element>();
List<String> isDone = new ArrayList<String>();
Map courante = new LinkedHashMap();

//recupere les attributs de la question
Expand All @@ -110,37 +112,81 @@ public Map toJson(Element element) {
//CARE:Selon code une balise ne peut avoir texte ET(attribut ou sous balise).
// i : compteur elem courant
int i = 0;
int cpt = 0;
for (Element children : childrens) {
if (children.getName().equals("shuffleanswers")) {
System.out.println("Debug");
// Point arret debug
}

if (cpt == 0) { //non traité si deja fait
if (!isDone.contains(children.getName())) { //non traité si deja fait
//CARE les fils identiques a gerer en Array doivent se suivre.
cpt = cptEquals(childrens, children, i);
if (cpt != 0) {
cpt++; //maj pour compter le fils courant
List<Map> l = creerListes(childrens, cpt, i);
if (getEqualsChildrens(childrens, children, i)) { //plus d'un fils de même nom
isDone.add(children.getName());
List<Map> l = creerListes(element, children);
courante.put(childrens.get(i).getName(), l);
}
else {
if (addChild(children) || addAttributes(children)) {
courante.put(children.getName(), toJson(children));
} else {
String text = children.getValue();
if (text != null) {
if (!text.equals("")) {
courante.put(children.getName(), text);
}
} else { courante.put(children.getName(), null); }
}
}
}
else cpt--;
i++;
}
} else {
String text = element.getValue();
if (!text.equals("")) {
courante.put(element.getName(), text);
} else { courante.put(element.getName(), null); }
}
return courante;
}

/**
* Traite le contenu d'un element.
* @param element elem courant
* @return Map du contenu de l'element
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public Map toJsonContenu(Element element) {
List<Element> childrens = new ArrayList<Element>();
List<Attribute> attList = new ArrayList<Attribute>();
Map courante = new LinkedHashMap();

//recupere les attributs de la question
attList = element.getAttributes();

if (!attList.isEmpty()) {
for (Attribute att : attList) {
courante.put(att.getName(), att.getValue());
}
}

//On recupere les fils
childrens = element.getChildren();
if (!childrens.isEmpty()) {
//traiter chaque fils en profondeur.
//CARE:Selon code une balise ne peut avoir texte ET(attribut ou sous balise).
for (Element children : childrens) {
//appel au toJson normal
courante.put(children.getName(), toJson(children));
}
} else {
String text = element.getValue();
if (!text.equals("")) {
courante.put(text, null);
} //else { courante.put(element.getName(), null); }
}
return courante;
}

/**
* Cree les listes dans le cas de traduction en Json Array.
* Cree les listes dans un cas particulier de traduction en Json Array.
* @param childrens Liste des fils de l'element courant
* @param cpt Nombre de fils identiques
* @param j position premier fils dans liste
Expand All @@ -154,25 +200,42 @@ public List<Map> creerListes(List<Element> childrens, int cpt, int j) {
}
return list;
}

/**
* Creer une liste dans le cas général de trad JsonArray.
* @param father element pere
* @param children element courant
* @return La liste de map du courant
*/
@SuppressWarnings("rawtypes")
public List<Map> creerListes(Element father, Element children) {
List<Map> list = new ArrayList<Map>();
List<Element> sameChildrens = new ArrayList<Element>();
sameChildrens = father.getChildren(children.getName());
for (Element child : sameChildrens) {
list.add(toJsonContenu(child));
}
return list;
}


/**
* Compare suivants à partir de courant et renvoie le nombre de balises identiques.
* @param childrens Liste des fils de l'element
* @param courant fils etudie
* @param i position de ce fils
* @return nombre de fils identiques a courant dans la suite de la liste childrens
* Teste si la liste possede plusieurs elements courant
* @param childrens liste des fils
* @param courant element à tester
* @param i position de courant dans la liste
* @return boolean possede plusieurs elements courants.
*/
public int cptEquals(List<Element> childrens, Element courant, int i) {
public boolean getEqualsChildrens(List<Element> childrens, Element courant, int i) {
int cpt = 0;
for (int j = i + 1; j < childrens.size(); j++)
if (courant.getName().equals(childrens.get(j).getName()))
cpt++;
return cpt;
return cpt > 0;
}

/**
* teste si l element courant possede un fils.
* Teste si l element courant possede un fils.
* @param e element courant
* @return boolean a un fils?
*/
Expand All @@ -198,7 +261,13 @@ public static void main(String[] args) {
tj.toJson("src/test/resources/USE_TrueFalse_RSC.xml");

ToJson tj2 = new ToJson("src/test/resources/USE_exemple_AUTOGEN.json");
tj2.toJson("src/test/resources/USE_EXEMPLE_RSC.xml");
tj2.toJson("src/test/resources/USE_Exemple_RSC.xml");

ToJson tj3 = new ToJson("src/test/resources/USE_TestLimite_AUTOGEN.json");
tj3.toJson("src/test/resources/USE_TestLimite_RSC.xml");
//
ToJson tj4 = new ToJson("src/test/resources/USE_XmlWriterTest_AUTOGEN.json");
tj4.toJson("src/test/resources/USE_XmlWriterTest_RSC.xml");
}

}
55 changes: 14 additions & 41 deletions projet/src/test/resources/TrueFalse.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,15 @@
{"quiz": {"question": [
{
"type": "truefalse",
"name": {"text": "Name of question"},
"questiontext": {
"format": "html",
"text": "What is the answer to this question?"
{"quiz": {"question": {
"type": "truefalse",
"answer": [
{
"fraction": "100",
"text": "true",
"feedback": {"text": "Correct!"}
},
"answer": [
{
"fraction": "100",
"text": "true",
"feedback": {"text": "Correct!"}
},
{
"fraction": "0",
"text": "false",
"feedback": {"text": "Ooops!"}
}
]
},
{
"type": "truefalse",
"name": {"text": "Name of question"},
"questiontext": {
"format": "html",
"text": "What is the answer to this question?"
},
"answer": [
{
"fraction": "100",
"text": "true",
"feedback": {"text": "Correct!"}
},
{
"fraction": "0",
"text": "false",
"feedback": {"text": "Ooops!"}
}
]
}
]}}
{
"fraction": "0",
"text": "false",
"feedback": {"text": "Ooops!"}
}
]
}}}
15 changes: 15 additions & 0 deletions projet/src/test/resources/USE_TestLimite_AUTOGEN.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{"quiz": {"question": {
"type": "truefalse",
"answer": [
{
"fraction": "100",
"text": "true",
"feedback": {"text": "Correct!"}
},
{
"fraction": "0",
"text": null,
"feedback": {"text": "Ooops!"}
}
]
}}}
14 changes: 14 additions & 0 deletions projet/src/test/resources/USE_TestLimite_RSC.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<question type="truefalse">
<answer fraction="100">
<text>true</text>
<feedback><text>Correct!</text></feedback>
</answer>
<answer fraction="0">
<text></text>
<feedback><text>Ooops!</text></feedback>
</answer>
</question>
</quiz>

38 changes: 21 additions & 17 deletions projet/src/test/resources/USE_TrueFalseOne_RSC.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<question type="truefalse">
<name>
<text>Consigne dispositif électronique</text>
</name>
<questiontext format="moodle_auto_format">
<text>Quel est le premier langage Orienté Objet ?</text>
</questiontext>
<answer fraction="100">
<text>true</text>
<feedback><text>Correct!</text></feedback>
</answer>
<answer fraction="0">
<text>false</text>
<feedback><text>Ooops!</text></feedback>
</answer>
</question>
</quiz>
<question type="truefalse">
<name>
<text>Consigne dispositif électronique</text>
</name>
<questiontext format="moodle_auto_format">
<text>Quel est le premier langage Orienté Objet ?</text>
</questiontext>
<answer fraction="100">
<text>true</text>
<feedback>
<text>Correct!</text>
</feedback>
</answer>
<answer fraction="0">
<text>false</text>
<feedback>
<text>Ooops!</text>
</feedback>
</answer>
</question>
</quiz>
55 changes: 14 additions & 41 deletions projet/src/test/resources/USE_TrueFalse_AUTOGEN.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,15 @@
{"quiz": {"question": [
{
"type": "truefalse",
"name": {"text": "Name of question"},
"questiontext": {
"format": "html",
"text": "What is the answer to this question?"
{"quiz": {"question": {
"type": "truefalse",
"answer": [
{
"fraction": "100",
"text": "true",
"feedback": {"text": "Correct!"}
},
"answer": [
{
"fraction": "100",
"text": "true",
"feedback": {"text": "Correct!"}
},
{
"fraction": "0",
"text": "false",
"feedback": {"text": "Ooops!"}
}
]
},
{
"type": "truefalse",
"name": {"text": "Name of question"},
"questiontext": {
"format": "html",
"text": "What is the answer to this question?"
},
"answer": [
{
"fraction": "100",
"text": "true",
"feedback": {"text": "Correct!"}
},
{
"fraction": "0",
"text": "false",
"feedback": {"text": "Ooops!"}
}
]
}
]}}
{
"fraction": "0",
"text": "false",
"feedback": {"text": "Ooops!"}
}
]
}}}
Loading

0 comments on commit 95fe532

Please sign in to comment.