+++ /dev/null
-package com.example.furt.myapplication;
-
-import org.w3c.dom.Document;
-import org.xml.sax.SAXException;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.xpath.XPath;
-import javax.xml.xpath.XPathFactory;
-
-public class parser {
- parser(){}
-
- protected static Node root(String esercizio)
- {
- Node returnNode=null;
- try {
- String cartellaEs=global.esDir;
- FileInputStream file = new FileInputStream(new File(cartellaEs+esercizio));
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = builderFactory.newDocumentBuilder();
- Document xmlDocument = builder.parse(file);
- returnNode=new Node(createFormula(xmlDocument,"/esercizio/tesi/formula[1]"));
- returnNode.addHPList(ipotesi(xmlDocument));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return returnNode;
- }
-
- static public ArrayList<String> getLiteral(String esercizio)
- {
- String cartellaEs=global.esDir;
- ArrayList<String> listValue=new ArrayList<String>();
- ArrayList<String> returnValue=new ArrayList<String>();
- try {
- FileInputStream file = new FileInputStream(new File(cartellaEs + esercizio));
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = builderFactory.newDocumentBuilder();
- Document xmlDocument = builder.parse(file);
- listValue.add("Ț");
- listValue.add("⊥");
- listValue.addAll(getLiteralAtFormula(xmlDocument, "/esercizio/tesi/formula[1]"));
- listValue.addAll(getLiteralAtHypotesis(xmlDocument));
- for(String elem : listValue)
- {
- if(!returnValue.isEmpty())
- {
- if(!returnValue.contains(elem))
- returnValue.add(elem);
- }
- else
- returnValue.add(elem);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return returnValue;
- }
-
- static public ArrayList<String> getLiteralAtFormula(Document xmlDocument, String position)
- {
- ArrayList<String> returnList=new ArrayList<String>();
- 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) {
- returnList.addAll(getLiteralAtFormula(xmlDocument, position + "/formula[1]"));
- returnList.addAll(getLiteralAtFormula(xmlDocument, position + "/formula[2]"));
-
- return returnList;
- } else if (numbOfElements == 1) {
- return getLiteralAtFormula(xmlDocument, position + "/formula[1]");
- } else if (numbOfElements == 0) {
- String child = xPath.compile(position).evaluate(xmlDocument);
- if (child == null || child.isEmpty())
- return null;
- else if (child.contentEquals("top")) {
- returnList.add("Ț");
- return returnList;
- }
- else if (child.contentEquals("bot")){
- returnList.add("⊥");
- return returnList;
- }
- else{
- returnList.add(String.valueOf(child.charAt(0)));
- return returnList;
- }
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
-
- static public Formula createFormula(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) {
- Formula firstChild = createFormula(xmlDocument, position + "/formula[1]");
- Formula secondChild = createFormula(xmlDocument, position + "/formula[2]");
- if (firstChild == null || secondChild == null)
- return null;
- else if (type.contentEquals("and"))
- return new FormulaAnd(createFormula(xmlDocument, position + "/formula[1]"), createFormula(xmlDocument, position + "/formula[2]"));
- else if (type.contentEquals("or"))
- return new FormulaOr(createFormula(xmlDocument, position + "/formula[1]"), createFormula(xmlDocument, position + "/formula[2]"));
- else if (type.contentEquals("impl"))
- return new FormulaImpl(createFormula(xmlDocument, position + "/formula[1]"), createFormula(xmlDocument, position + "/formula[2]"));
- } else if (numbOfElements == 1) {
- Formula child = createFormula(xmlDocument, position + "/formula[1]");
- if (child == null)
- return null;
- return new FormulaNot(createFormula(xmlDocument, position + "/formula[1]"));
- } else if (numbOfElements == 0) {
- String child = xPath.compile(position).evaluate(xmlDocument);
- if (child == null || child.isEmpty())
- return null;
- else if (child.length()>=3 && child.substring(0,3).equals("top"))
- return new FormulaTOP();
- else if (child.length()>=3 && child.substring(0,3).equals("bot"))
- return new FormulaBOT();
- else
- return new Literal(child.charAt(0));
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
-
- static public List<Hypothesis> ipotesi(Document xmlDocument)
- {
- List<Hypothesis> returnValue=new ArrayList<Hypothesis>();
- XPath xPath = XPathFactory.newInstance().newXPath();
- try
- {
- int count = Integer.parseInt(xPath.compile("count(/esercizio/ipotesi/formula)").evaluate(xmlDocument));
- for(int i=1;i<=count;i++)
- {
- Formula F=createFormula(xmlDocument,"/esercizio/ipotesi/formula["+i+"]");
- Hypothesis valore;
- if(F==null)
- valore=null;
- else
- valore=new Hypothesis(F,false);
- if(valore!=null)
- returnValue.add(valore);
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- return returnValue;
- }
-
- static public ArrayList<String> getLiteralAtHypotesis(Document xmlDocument)
- {
- ArrayList<String> returnValue=new ArrayList<String>();
- XPath xPath = XPathFactory.newInstance().newXPath();
- try
- {
- int count = Integer.parseInt(xPath.compile("count(/esercizio/ipotesi/formula)").evaluate(xmlDocument));
- for(int i=1;i<=count;i++)
- returnValue.addAll(getLiteralAtFormula(xmlDocument, "/esercizio/ipotesi/formula[" + i + "]"));
- }catch (Exception e) {
- e.printStackTrace();
- }
- return returnValue;
- }
-
- static public ArrayList<String> getParameters(String esercizio)
- {
- ArrayList<String> value=new ArrayList<String>();
- try {
- String cartellaEs=global.esDir;
- FileInputStream file = new FileInputStream(new File(cartellaEs+esercizio));
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = builderFactory.newDocumentBuilder();
- Document xmlDocument = builder.parse(file);
- return parameters(xmlDocument);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return value;
- }
-
- static private ArrayList<String> parameters(Document xmlDocument)
- {
- ArrayList<String> returnValue= new ArrayList<String>();
- XPath xPath = XPathFactory.newInstance().newXPath();
- try
- {
- if( Integer.parseInt(xPath.compile("count(/esercizio/valutazione/click)").evaluate(xmlDocument))==1 &&
- Integer.parseInt(xPath.compile("count(/esercizio/valutazione/tempo)").evaluate(xmlDocument))==1 &&
- Integer.parseInt(xPath.compile("count(/esercizio/valutazione/altezza)").evaluate(xmlDocument))==1)
- {
- returnValue.add(xPath.compile("/esercizio/valutazione/click").evaluate(xmlDocument));
- returnValue.add(xPath.compile("/esercizio/valutazione/tempo").evaluate(xmlDocument));
- returnValue.add(xPath.compile("/esercizio/valutazione/altezza").evaluate(xmlDocument));
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- return returnValue;
- }
-}