+package com.company;
+
+import java.io.File;
+import java.io.FileInputStream;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathFactory;
+
+import org.w3c.dom.Document;
+
+import java.lang.Integer;
+import java.io.FileReader;
+import java.io.BufferedReader;
+
+public class EsNameParser {
+ EsNameParser(){}
+
+ static public String esName()
+ {
+ try {
+ FileInputStream file = new FileInputStream(new File(global.listaEsercizi));
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder = builderFactory.newDocumentBuilder();
+ Document xmlDocument = builder.parse(file);
+ XPath xPath = XPathFactory.newInstance().newXPath();
+ int esNumber = Integer.parseInt(xPath.compile("count(/esercizi/esercizio)").evaluate(xmlDocument));
+ String returnValue = null;
+ for (int i = 1; i <= esNumber; i++) {
+ String name = xPath.compile("/esercizi/esercizio[" + i + "]").evaluate(xmlDocument);
+ if (name == null || name.isEmpty())
+ continue;
+ else {
+ if(returnValue==null)
+ returnValue=name;
+ else
+ returnValue = returnValue + "/" + name;
+ }
+ }
+ file.close();
+ return returnValue;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public static boolean check(String esercizio)
+ {
+ boolean value=false;
+ try
+ {
+ String cartellaEs=global.locationEsercizi;
+
+ BufferedReader br = new BufferedReader(new FileReader(cartellaEs+esercizio));
+ if (br.readLine() == null) {
+ br.close();
+ return false;
+ }
+ FileInputStream file = new FileInputStream(new File(cartellaEs+"/"+esercizio));
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder = builderFactory.newDocumentBuilder();
+ Document xmlDocument = builder.parse(file);
+ value=checkFormula(xmlDocument,"/esercizio/tesi/formula[1]");
+ if(value)
+ value=checkIpotesi(xmlDocument);
+ file.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return value;
+ }
+
+ static public boolean checkFormula(Document xmlDocument, String position) {
+ try
+ {
+ XPath xPath = XPathFactory.newInstance().newXPath();
+ if(Integer.parseInt(xPath.compile("count(" + position + ")").evaluate(xmlDocument))==1) {
+ String type = xPath.compile(position + "/@type").evaluate(xmlDocument);
+ int numbOfElements = 5;
+ if (type.contentEquals("and") || type.contentEquals("or") || type.contentEquals("impl"))
+ numbOfElements = 2;
+ else if (type.contentEquals("not"))
+ numbOfElements = 1;
+ else if (type.contentEquals("literal") || type.contentEquals("atomic"))
+ numbOfElements = 0;
+ int count = Integer.parseInt(xPath.compile("count(" + position + "/formula)").evaluate(xmlDocument));
+ if (count == numbOfElements) {
+ if (numbOfElements == 2) {
+ boolean firstChild = checkFormula(xmlDocument, position + "/formula[1]");
+ boolean secondChild = checkFormula(xmlDocument, position + "/formula[2]");
+ if (!firstChild || !secondChild)
+ return false;
+ else
+ return true;
+ }
+ else if (numbOfElements == 1)
+ {
+ boolean child = checkFormula(xmlDocument, position + "/formula[1]");
+ if (!child)
+ return false;
+ return true;
+ }
+ else if (numbOfElements == 0)
+ {
+ String child = xPath.compile(position).evaluate(xmlDocument);
+ if (child == null || child.isEmpty())
+ return false;
+ else
+ return true;
+ }
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ static public boolean checkIpotesi(Document xmlDocument)
+ {
+ XPath xPath = XPathFactory.newInstance().newXPath();
+ boolean returnValue=false;
+ try
+ {
+ int count = Integer.parseInt(xPath.compile("count(/esercizio/ipotesi)").evaluate(xmlDocument));
+ int numberHp=Integer.parseInt(xPath.compile("count(/esercizio/ipotesi/formula)").evaluate(xmlDocument));
+ if(count==1 && numberHp==0)//se non ci sono ipotesi
+ return true;
+ for(int i=1;i<=numberHp;i++)
+ {
+ boolean valore = checkFormula(xmlDocument, "/esercizio/ipotesi/formula["+i+"]");
+ if(!valore)
+ return false;
+ else
+ returnValue=true;
+ }
+ }catch (Exception e) {
+ e.printStackTrace();
+ }
+ return returnValue;
+ }
+}