]> matita.cs.unibo.it Git - logicplayer.git/blob - server/com/company/EsNameParser.java
shuffling + libs imported
[logicplayer.git] / server / com / company / EsNameParser.java
1 package com.company;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5
6 import javax.xml.parsers.DocumentBuilder;
7 import javax.xml.parsers.DocumentBuilderFactory;
8 import javax.xml.xpath.XPath;
9 import javax.xml.xpath.XPathFactory;
10
11 import org.w3c.dom.Document;
12
13 import java.lang.Integer;
14 import java.io.FileReader;
15 import java.io.BufferedReader;
16
17 public class EsNameParser {
18     EsNameParser(){}
19
20     static public String esName()
21     {
22         try {
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())
33                     continue;
34                 else {
35                     if(returnValue==null)
36                         returnValue=name;
37                     else
38                         returnValue = returnValue + "/" + name;
39                 }
40             }
41             file.close();
42             return returnValue;
43         } catch (Exception e) {
44             e.printStackTrace();
45         }
46         return null;
47     }
48
49     public static boolean check(String esercizio)
50     {
51         boolean value=false;
52         try
53         {
54             String cartellaEs=global.locationEsercizi;
55
56             BufferedReader br = new BufferedReader(new FileReader(cartellaEs+esercizio));
57             if (br.readLine() == null) {
58                 br.close();
59                 return false;
60             }
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]");
66             if(value)
67                 value=checkIpotesi(xmlDocument);
68             file.close();
69         } catch (Exception e) {
70             e.printStackTrace();
71         }
72         return value;
73     }
74
75     static public boolean checkFormula(Document xmlDocument, String position) {
76         try
77         {
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"))
83                     numbOfElements = 2;
84                 else if (type.contentEquals("not"))
85                     numbOfElements = 1;
86                 else if (type.contentEquals("literal") || type.contentEquals("atomic"))
87                     numbOfElements = 0;
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)
94                             return false;
95                         else
96                             return true;
97                     }
98                     else if (numbOfElements == 1)
99                     {
100                         boolean child = checkFormula(xmlDocument, position + "/formula[1]");
101                         if (!child)
102                             return false;
103                         return true;
104                     }
105                     else if (numbOfElements == 0)
106                     {
107                         String child = xPath.compile(position).evaluate(xmlDocument);
108                         if (child == null || child.isEmpty())
109                             return false;
110                         else
111                             return true;
112                     }
113                 }
114             }
115         } catch (Exception e) {
116             e.printStackTrace();
117         }
118         return false;
119     }
120
121     static public boolean checkIpotesi(Document xmlDocument)
122     {
123         XPath xPath = XPathFactory.newInstance().newXPath();
124         boolean returnValue=false;
125         try
126         {
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
130                 return true;
131             for(int i=1;i<=numberHp;i++)
132             {
133                 boolean valore = checkFormula(xmlDocument, "/esercizio/ipotesi/formula["+i+"]");
134                 if(!valore)
135                     return false;
136                 else
137                     returnValue=true;
138             }
139         }catch (Exception e) {
140             e.printStackTrace();
141         }
142         return returnValue;
143     }
144 }