gt:       expr = xpath.compile(temp);
context: package Library.newlib;
  
  /*Generated by MPS */
  
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.DocumentBuilder;
  import org.w3c.dom.Document;
  import javax.xml.transform.dom.DOMSource;
  import org.w3c.dom.Element;
  import java.io.File;
  import javax.xml.transform.stream.StreamResult;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.Transformer;
  import javax.xml.xpath.XPath;
  import javax.xml.xpath.XPathExpression;
  import javax.xml.xpath.XPathFactory;
  import javax.swing.JOptionPane;
  import org.w3c.dom.Node;
  import javax.xml.xpath.XPathConstants;
  import org.w3c.dom.NodeList;
  import java.net.URLDecoder;
  import java.util.ArrayList;
  import java.util.Map;
  import java.util.HashMap;
  import java.util.Set;
  import java.util.Collections;
  import javax.swing.JTable;
  import javax.swing.JFrame;
  import javax.swing.BoxLayout;
  import javax.swing.WindowConstants;
  import java.awt.Dimension;
  import javax.swing.JScrollPane;
  import javax.swing.JTextPane;
  import javax.swing.text.StyledDocument;
  import javax.swing.text.Style;
  import javax.swing.text.SimpleAttributeSet;
  import javax.swing.text.StyleConstants;
  import java.awt.Color;
  import javax.swing.text.BadLocationException;
  import javax.swing.JButton;
  import java.awt.event.ActionListener;
  import java.awt.event.ActionEvent;
  import java.awt.BorderLayout;
  import java.util.HashSet;
  
  public class XmlMediator {
    /*package*/ int Question_count;
    /*package*/ DocumentBuilderFactory documentBuilderFactory;
    /*package*/ DocumentBuilder documentBuilder;
    /*package*/ Document document;
    /*package*/ DOMSource source;
    /*package*/ Element root;
    /*package*/ File fp;
    /*package*/ StreamResult result;
    /*package*/ TransformerFactory transformerFactory;
    /*package*/ Transformer transformer;
    /*package*/ XPath xpath;
    /*package*/ XPathExpression expr;
    /*package*/ XPathFactory xpathfactory;
    /*package*/ String QBPath;
    /*package*/ String xpathQuery;
  
    public XmlMediator(String s) {
      QBPath = s;
      System.out.print(QBPath);
      fp = new File(QBPath);
    }
  
    public void CreateXMLFile() throws Exception {
      if (fp.createNewFile()) {
        doc_init();
        document = documentBuilder.newDocument();
        Element rootElement = document.createElement("QuestionBank");
        document.appendChild(rootElement);
        Element totcount = document.createElement("totCount");
        totcount.appendChild(document.createTextNode("0"));
        rootElement.appendChild(totcount);
        transform_init();
        transformer.transform(source, result);
        JOptionPane.showMessageDialog(null, "XML File created");
      }
    }
  
    public void setQuestion_count(int Question_count) {
      Node tnode = document.getElementsByTagName("totCount").item(0);
      Element totcount = (Element) tnode;
      System.out.println("" + Integer.parseInt(totcount.getTextContent()));
      System.out.println("" + Integer.parseInt(totcount.getTextContent()));
      this.Question_count = Integer.parseInt(totcount.getTextContent()) + 1;
      System.out.println("" + this.Question_count);
      tnode.setTextContent("");
      totcount.appendChild(document.createTextNode("" + this.Question_count));
    }
  
    public boolean QuesBankExists() {
      return fp.exists();
    }
  
    public void doc_init() throws Exception {
      fp = new File(QBPath);
      result = new StreamResult(fp);
      documentBuilderFactory = DocumentBuilderFactory.newInstance();
      documentBuilder = documentBuilderFactory.newDocumentBuilder();
    }
  
    public void transform_init() throws Exception {
      result = new StreamResult(fp);
      source = new DOMSource(document);
      transformerFactory = TransformerFactory.newInstance();
      transformer = transformerFactory.newTransformer();
    }
  
    public void xpath_init() {
      result = new StreamResult(fp);
      xpathfactory = XPathFactory.newInstance();
      xpath = xpathfactory.newXPath();
    }
  
    public Node getQuestionNode(int id) throws Exception {
      if (QuesBankExists()) {
        doc_init();
        document = documentBuilder.parse(fp);
        xpath_init();
        System.out.println(id);
        xpathQuery = "//question[id='" + id + "']";
        expr = xpath.compile(xpathQuery);
        Object temp = expr.evaluate(document, XPathConstants.NODESET);
        NodeList tempf = (NodeList) temp;
        if (tempf.getLength() == 0) {
          JOptionPane.showMessageDialog(null, "Question not found!!");
        } else
        if (tempf.getLength() == 1) {
          return tempf.item(0);
        } else
        return null;
      }
      return null;
    }
  
    public void removeNode(int id) throws Exception {
      Node n = getQuestionNode(id);
      while (n.hasChildNodes()) {
        n.removeChild(n.getFirstChild());
        // ***********to be checked if it will work for third level of children 
      }
      n.getParentNode().removeChild(n);
      transform_init();
      transformer.transform(source, result);
    }
  
    public Node createsubElement(Node root, String tag, String value) {
      // to be used by addnode. 
      return root.appendChild(document.createElement(tag)).appendChild(document.createTextNode(value));
    }
  
    public void addNode(Question qob) throws Exception {
      CreateXMLFile();
      if (QuesBankExists()) {
        doc_init();
        document = documentBuilder.parse(fp);
        root = document.getDocumentElement();
        Element question = document.createElement("question");
        root.appendChild(question);
        setQuestion_count(Question_count);
        qob.setId(Question_count);
        createsubElement(question, "id", "" + qob.getId() + "");
        createsubElement(question, "statement", "" + qob.getQuestionStatement() + "");
        createsubElement(question, "subject", "" + qob.getSubject() + "");
        createsubElement(question, "answer", "" + qob.getAnswer().getKeyAnswer());
        createsubElement(question, "type", "" + qob.getAnswer().getType() + "");
        createsubElement(question, "difficulty", "" + qob.getDifficulty() + "");
        if (!((qob.getMediaUrl().equalsIgnoreCase("")))) {
          createsubElement(question, "image", "" + qob.getMediaUrl() + "");
        }
        if (qob.getAnswer().getType() == "MultipleChoiceAnswer") {
          Node choices = question.appendChild(document.createElement("choices"));
          MultipleChoiceAnswer mcq = (MultipleChoiceAnswer) qob.getAnswer();
          createsubElement(choices, "optional", mcq.isOptional() + "");
          createsubElement(choices, "size", mcq.getNoOfChoices() + "");
  
          for (int i = 0; i < mcq.numberOfChoices; i++) {
            System.out.println(mcq.getChoices()[i]);
            createsubElement(choices, "choice" + i, mcq.getChoices()[i]);
          }
  
        }
        transform_init();
        transformer.transform(source, result);
      }
    }
  
    public void updateNode(int id, String tag, String new_value) throws Exception {
      doc_init();
      if (tag.equalsIgnoreCase("image")) {
        new_value = URLDecoder.decode(new_value, "UTF-8");
      }
      document = documentBuilder.parse(fp);
      root = document.getDocumentElement();
      xpath_init();
      Node qn = getQuestionNode(id);
      Element chd = (Element) qn.getChildNodes();
      if (tag.equalsIgnoreCase("image")) {
        if (chd.getElementsByTagName(tag).getLength() == 0) {
          createsubElement(qn, "image", new_value);
        }
        if (chd.getElementsByTagName(tag).getLength() == 1) {
          chd.getElementsByTagName(tag).item(0).setTextContent(new_value);
        }
      }
      if (!(tag.equalsIgnoreCase("image"))) {
        chd.getElementsByTagName(tag).item(0).setTextContent(new_value);
      }
      transform_init();
      transformer.transform(source, result);
    }
  
    public NodeList searchByCategory(String field, String value, String numb) throws Exception {
      String typ = "";
      String sub = "";
      String diff = "";
  
      if (field.toLowerCase() == "subject") {
        return search(value, "", "", numb);
      }
      if (field.toLowerCase() == "type") {
        return search("", value, "", numb);
      }
      if (field.toLowerCase() == "difficulty") {
        return search("", "", value, numb);
      }
      return search("", "", "", numb);
  
    }
  
    public NodeList search(String subject, String type, String difficulty, String numb) throws Exception {
      if (QuesBankExists()) {
        ArrayList<String> res = new ArrayList<String>();
        int num;
        int i;
        doc_init();
        Node temp1 = null;
        document = documentBuilder.parse(fp);
        String temp = null;
        xpath_init();
        if ((type.equals("")) && (subject.equals("")) && (difficulty == "")) {
          temp = "//question";
        } else {
          temp = "//question[ ";
          if (type != "") {
            temp += "type='" + type + "' ";
            if ((subject != "") || (difficulty != "")) {
              temp += "and ";
            }
          }
          if (subject != "") {
            temp += "subject='" + subject + "' ";
            if (difficulty != "") {
              temp += "and ";
            }
          }
          if (difficulty != "") {
            temp += "difficulty='" + difficulty + "' ";
          }
          temp += " ]";
        }
        System.out.println(temp);
        expr = xpath.compile(temp);
        Object temp2 = expr.evaluate(document, XPathConstants.NODESET);
        NodeList tempf = (NodeList) temp2;
        if (numb.equals("")) {
          num = tempf.getLength();
        } else {
          num = Integer.parseInt(numb);
        }
  
        if (num > tempf.getLength()) {
          JOptionPane.showMessageDialog(null, "Only " + tempf.getLength() + " questions exist!!!");
        }
  
        return tempf;
  
      }
  
      return null;
    }
  
    public NodeList fetchAll() throws Exception {
      return search("", "", "", "");
    }
  
  
  
    public NodeList readData(String typ, String sub, String diff) throws Exception {
      if (QuesBankExists()) {
        doc_init();
        String temp = null;
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(fp);
        XPathFactory xpathfactory = XPathFactory.newInstance();
        XPath xpath = xpathfactory.newXPath();
        if ((typ.equals("")) && (sub.equals("")) && (diff.equals(""))) {
          temp = "//question";
        } else {
          temp = "//question[ ";
          if (typ != "") {
            temp += "type='" + typ + "' ";
            if ((sub != "") || (diff != "")) {
              temp += "and ";
            }
          }
          if (sub != "") {
            temp += "subject='" + sub + "' ";
            if (diff != "") {
              temp += "and ";
            }
          }
          if (diff != "") {
            temp += "difficulty='" + diff + "' ";
          }
          temp += " ]";
        }
        XPathExpression expr = xpath.compile(temp);
        Object res = expr.evaluate(document, XPathConstants.NODESET);
        NodeList nodes = (NodeList) res;
        return nodes;
      }
      return null;
    }
  
  
  
  
  
    public void prepareReport(String[] category) throws Exception {
      ArrayList<String> tmp = new ArrayList<String>();
      Map<String, String> map = new HashMap<String, String>();
      Set<String> tmp1 = null;
      NodeList result = null;
      String temp;
      Element chd = null;
      result = readData("", "", "");
      int i;
      int size = category.length;
      ArrayList<String> res = new ArrayList<String>();
      i = 0;
      for (; i < result.getLength(); i++) {
        chd = (Element) result.item(i);
        if (size == 2) {
          res.add(chd.getElementsByTagName(category[0]).item(0).getTextContent() + "-" + chd.getElementsByTagName(category[1]).item(0).getTextContent());
        } else
        if (size == 1) {
          res.add(chd.getElementsByTagName(category[0]).item(0).getTextContent());
        } else
        res.add(chd.getElementsByTagName(category[0]).item(0).getTextContent() + "-" + chd.getElementsByTagName(category[1]).item(0).getTextContent() + "-" + chd.getElementsByTagName(category[2]).item(0).getTextContent());
      }
      i = 0;
      for (; i < res.size(); i++) {
        map.put(res.get(i), "" + Collections.frequency(res, res.get(i)));
      }
      tmp1 = map.keySet();
      i = 0;
      final Object[][] tableObjects = new Object[tmp1.size()][category.length + 1];
      final String[] title = new String[category.length + 1];
      System.out.println("t.....    " + category.length);
      for (int i1 = 0; i1 < category.length; i1++) {
        System.out.println("t.....    " + category[i1]);
        title[i1] = category[i1];
      }
      System.out.println("t..");
      title[category.length] = " Number of Questions";
      for (; i < tmp1.size(); i++) {
        String[] tt = new String[category.length];
        tt = tmp1.toArray()[i].toString().split("-");
        for (int j = 0; j < category.length; j++) {
          tableObjects[i][j] = tt[j];
        }
        temp = (String) tmp1.toArray()[i];
        tableObjects[i][category.length] = map.get(temp);
        tmp.add(temp);
        // System.out.println(temp);   
      }
      final JTable table = new JTable(tableObjects, title);
      JFrame frame = new JFrame();
      frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
      frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      frame.setTitle(" Generated Report ");
      table.setRowHeight(table.getRowHeight() + 10);
      table.setIntercellSpacing(new Dimension(15, 2));
      JScrollPane scrollPane = new JScrollPane(table);
      JTextPane textPane = new JTextPane();
      StyledDocument doc = textPane.getStyledDocument();
      Style style = textPane.addStyle("Style1", null);
      SimpleAttributeSet style1 = new SimpleAttributeSet();
      StyleConstants.setAlignment(style1, StyleConstants.ALIGN_CENTER);
      StyleConstants.setForeground(style1, Color.BLACK);
      StyleConstants.setFontFamily(style1, "Times New Roman");
      StyleConstants.setFontSize(style1, 28);
      textPane.setParagraphAttributes(style1, true);
      try {
        doc.insertString(doc.getLength(), "REPORT\n", style1);
      } catch (BadLocationException e) {
      }
      JButton button1 = new JButton();
      button1.setText("Save Report");
      button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent p0) {
          qpaper temp = new qpaper();
          temp.report(title, tableObjects);
          JOptionPane.showMessageDialog(null, "Report Saved");
        }
      });
      frame.add(textPane);
      frame.add(scrollPane, BorderLayout.CENTER);
      frame.add(button1);
      frame.pack();
      frame.setVisible(true);
      for (; i < tmp1.size(); i++) {
        temp = (String) tmp1.toArray()[i];
        temp += " = " + map.get(temp);
        tmp.add(temp);
        // System.out.println(temp);   
      }
    }
  
  
  
  
  
  
  
    public Set<String> prepareMenu(String type, String subject) throws Exception {
      NodeList temp3 = null;
      if (QuesBankExists()) {
        Set<String> set = new HashSet<String>();
        int num;
        int i;
        doc_init();
        Node temp1 = null;
        document = documentBuilder.parse(fp);
        String temp = null;
        xpath_init();
        if (subject.equals("") && (type.equals(""))) {
          temp = "/QuestionBank/question/type";
        } else
        if ((subject.equals("")) && (!(type.equals("")))) {
          temp = "/QuestionBank/question[type='" + type + "']/subject";
        } else
        if ((!(subject.equals("")) && (!(type.equals(""))))) {
          temp = "/QuestionBank/question[type='" + type + "' and subject='" + subject + "']/difficulty";
        }
        System.out.println(temp);
  