4 import java.io.FileInputStream;
6 import javax.xml.parsers.DocumentBuilder;
7 import javax.xml.parsers.DocumentBuilderFactory;
8 import javax.xml.xpath.XPath;
9 import javax.xml.xpath.XPathFactory;
11 import org.w3c.dom.Document;
13 import java.lang.Integer;
14 import java.io.FileReader;
15 import java.io.BufferedReader;
17 public class EsNameParser {
20 static public String esName()
23 FileInputStream file = new FileInputStream(new File(global.listaEsercizi));
24 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
25 DocumentBuilder builder = builderFactory.newDocumentBuilder();
26 Document xmlDocument = builder.parse(file);
27 XPath xPath = XPathFactory.newInstance().newXPath();
28 int esNumber = Integer.parseInt(xPath.compile("count(/esercizi/esercizio)").evaluate(xmlDocument));
29 String returnValue = null;
30 for (int i = 1; i <= esNumber; i++) {
31 String name = xPath.compile("/esercizi/esercizio[" + i + "]").evaluate(xmlDocument);
32 if (name == null || name.isEmpty())
38 returnValue = returnValue + "/" + name;
43 } catch (Exception e) {
49 public static boolean check(String esercizio)
54 String cartellaEs=global.locationEsercizi;
56 BufferedReader br = new BufferedReader(new FileReader(cartellaEs+esercizio));
57 if (br.readLine() == null) {
61 FileInputStream file = new FileInputStream(new File(cartellaEs+"/"+esercizio));
62 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
63 DocumentBuilder builder = builderFactory.newDocumentBuilder();
64 Document xmlDocument = builder.parse(file);
65 value=checkFormula(xmlDocument,"/esercizio/tesi/formula[1]");
67 value=checkIpotesi(xmlDocument);
69 } catch (Exception e) {
75 static public boolean checkFormula(Document xmlDocument, String position) {
78 XPath xPath = XPathFactory.newInstance().newXPath();
79 if(Integer.parseInt(xPath.compile("count(" + position + ")").evaluate(xmlDocument))==1) {
80 String type = xPath.compile(position + "/@type").evaluate(xmlDocument);
81 int numbOfElements = 5;
82 if (type.contentEquals("and") || type.contentEquals("or") || type.contentEquals("impl"))
84 else if (type.contentEquals("not"))
86 else if (type.contentEquals("literal") || type.contentEquals("atomic"))
88 int count = Integer.parseInt(xPath.compile("count(" + position + "/formula)").evaluate(xmlDocument));
89 if (count == numbOfElements) {
90 if (numbOfElements == 2) {
91 boolean firstChild = checkFormula(xmlDocument, position + "/formula[1]");
92 boolean secondChild = checkFormula(xmlDocument, position + "/formula[2]");
93 if (!firstChild || !secondChild)
98 else if (numbOfElements == 1)
100 boolean child = checkFormula(xmlDocument, position + "/formula[1]");
105 else if (numbOfElements == 0)
107 String child = xPath.compile(position).evaluate(xmlDocument);
108 if (child == null || child.isEmpty())
115 } catch (Exception e) {
121 static public boolean checkIpotesi(Document xmlDocument)
123 XPath xPath = XPathFactory.newInstance().newXPath();
124 boolean returnValue=false;
127 int count = Integer.parseInt(xPath.compile("count(/esercizio/ipotesi)").evaluate(xmlDocument));
128 int numberHp=Integer.parseInt(xPath.compile("count(/esercizio/ipotesi/formula)").evaluate(xmlDocument));
129 if(count==1 && numberHp==0)//se non ci sono ipotesi
131 for(int i=1;i<=numberHp;i++)
133 boolean valore = checkFormula(xmlDocument, "/esercizio/ipotesi/formula["+i+"]");
139 }catch (Exception e) {