]> matita.cs.unibo.it Git - logicplayer.git/blobdiff - server/tesi/src/com/company/xmlOperation.java
Shuffling.
[logicplayer.git] / server / tesi / src / com / company / xmlOperation.java
diff --git a/server/tesi/src/com/company/xmlOperation.java b/server/tesi/src/com/company/xmlOperation.java
new file mode 100644 (file)
index 0000000..c4eff71
--- /dev/null
@@ -0,0 +1,136 @@
+package com.company;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
+
+import org.jdom2.Document;
+import org.jdom2.Element;
+import org.jdom2.JDOMException;
+import org.jdom2.input.SAXBuilder;
+import org.jdom2.output.Format;
+import org.jdom2.output.XMLOutputter;
+
+
+public class xmlOperation {
+
+    xmlOperation(){}
+    static private String pathName=global.listaEsercizi;
+
+    public void create() {
+
+        try {
+
+            Element company = new Element("esercizi");
+            Document doc = new Document(company);
+            doc.setRootElement(company);
+
+            // new XMLOutputter().output(doc, System.out);
+            XMLOutputter xmlOutput = new XMLOutputter();
+
+            // display nice nice
+            xmlOutput.setFormat(Format.getPrettyFormat());
+            xmlOutput.output(doc, new FileWriter(pathName));
+
+            System.out.println("File Saved!");
+        } catch (IOException io) {
+            System.out.println(io.getMessage());
+        }
+    }
+
+
+
+    public void add(String name)
+    {
+        try {
+
+            SAXBuilder builder = new SAXBuilder();
+            File xmlFile = new File(pathName);
+
+            Document doc = (Document) builder.build(xmlFile);
+            Element rootNode = doc.getRootElement();
+
+
+            // add new age element
+            Element newEs = new Element("esercizio").setText(name).setAttribute("md5",MD5.digest(name));
+            rootNode.addContent(newEs);
+
+            XMLOutputter xmlOutput = new XMLOutputter();
+
+            // display nice nice
+            xmlOutput.setFormat(Format.getPrettyFormat());
+            xmlOutput.output(doc, new FileWriter(pathName));
+
+            // xmlOutput.output(doc, System.out);
+
+            System.out.println("File updated!");
+        } catch (IOException io) {
+            io.printStackTrace();
+        } catch (JDOMException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public String getMD5(String name)
+    {
+        try {
+
+            SAXBuilder builder = new SAXBuilder();
+            File xmlFile = new File(pathName);
+
+            Document doc = (Document) builder.build(xmlFile);
+            Element rootNode = doc.getRootElement();
+
+            for(Element esercizio:rootNode.getChildren())
+            {
+                if(esercizio.getText().contentEquals(name))
+                {
+                    return esercizio.getAttribute("md5").toString();
+                }
+            }
+        } catch (IOException io) {
+            io.printStackTrace();
+        } catch (JDOMException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    public void remove(String name)
+    {
+        try {
+
+            SAXBuilder builder = new SAXBuilder();
+            File xmlFile = new File(pathName);
+
+            Document doc = (Document) builder.build(xmlFile);
+            Element rootNode = doc.getRootElement();
+
+            for(Element esercizio:rootNode.getChildren())
+            {
+                if(esercizio.getText().contentEquals(name))
+                {
+                    System.out.println("trovato");
+                    rootNode.removeContent(esercizio);
+                }
+            }
+
+            XMLOutputter xmlOutput = new XMLOutputter();
+
+            // display nice nice
+            xmlOutput.setFormat(Format.getPrettyFormat());
+            xmlOutput.output(doc, new FileWriter(pathName));
+
+            // xmlOutput.output(doc, System.out);
+
+            System.out.println("File updated!");
+        } catch (IOException io) {
+            io.printStackTrace();
+        } catch (JDOMException e) {
+            e.printStackTrace();
+        }
+    }
+
+
+}