package FillSurvey;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import Utilities.ProcessSingleNodes;
import Utilities.SaveXML;
public class ContinueQuestionHandler
{
private ArrayList<Element> nodelist;
private ArrayList<Element> sublist;
private int index;
private int groupID;
private int totalSub;
private final String filename;
private final String order = "abcdefghijklmnopqrstuvwxyz";
public ContinueQuestionHandler(ArrayList<Element> nodelist,int index, int groupID,String filename)
{
this.nodelist =nodelist;
this.index = index;
this.groupID = groupID;
this.filename = filename;
}
//return the index of the first question of the repeated group of questions
public int returnStartSubIndex()
{
int flag = -1;
for(int i= index; i>=0; i--)
{
ProcessSingleNodes psn = new ProcessSingleNodes(nodelist,i);
if(psn.getSub()!="")
{
if(Integer.parseInt(psn.getSub())!=0)
{
totalSub = Integer.parseInt(psn.getSub());
flag = i;
break;
}
}
}
return flag;
}
public ArrayList<Element> getSubList(int index)
{
int number = totalSub;
ArrayList<Element> sublist = new ArrayList<Element>();
ProcessSingleNodes psn = new ProcessSingleNodes(nodelist,index);
char temp;
if(order.contains(psn.QuestionNumber().substring(psn.QuestionNumber().length()-1)))
{
temp = (char) (psn.QuestionNumber().getBytes()[psn.QuestionNumber().length()-1]+1);
}
else
{
temp = 'b';
}
while(number >=0)
{
String form;
if(temp == 'b')
{
Element e = (Element)nodelist.get(index).clone();
List<Element> l = e.getChildren();
form = l.get(6).getAttributeValue("v");
form = form.replaceAll(l.get(1).getAttributeValue("v"), l.get(1).getAttributeValue("v")+temp);
l.get(6).getAttribute("v").setValue(form);
l.get(1).getAttribute("v").setValue(l.get(1).getAttributeValue("v")+temp);
//add a.b.c... to group id
//l.get(5).getAttribute("v").setValue(l.get(5).getAttributeValue("v")+temp);
l.get(7).getAttribute("v").setValue("");
sublist.add(e);
index ++;
number --;
}
else
{
Element e = (Element)nodelist.get(index).clone();
List<Element> l = e.getChildren();
form = l.get(6).getAttributeValue("v");
form = form.replaceAll(l.get(1).getAttributeValue("v"), l.get(1).getAttributeValue("v").replaceAll(l.get(1).getAttributeValue("v")
,l.get(1).getAttributeValue("v").substring(0, l.get(1).getAttributeValue("v").length()-1))+temp);
l.get(6).getAttribute("v").setValue(form);
l.get(1).getAttribute("v").setValue(l.get(1).getAttributeValue("v").replaceAll(l.get(1).getAttributeValue("v")
,l.get(1).getAttributeValue("v").substring(0, l.get(1).getAttributeValue("v").length()-1))+temp);
//add a.b.c... to group id
//l.get(5).getAttribute("v").setValue(l.get(5).getAttributeValue("v")+temp);
sublist.add(e);
index ++;
number --;
}
}
this.sublist = sublist;
return sublist;
}
//add the repeated groups of nodes into the final answer.xml
public void addExtraNodes(int startpoint)
{
for(int i =0; i<sublist.size(); i++)
{
nodelist.add(startpoint+totalSub+i+1, sublist.get(i));
}
new SaveXML(nodelist,filename);
}
//
// public char getChar(String input)
// {
// char[] idinput = input.toCharArray();
// char idchar = idinput[input.length()-1];
// if(order.contains(idchar+""))
// {
// return (char) (idchar+1);
// }
// else
// {
// return 'a';
// }
// }
}
|