]> matita.cs.unibo.it Git - logicplayer.git/blob - Server/server/tesi/src/com/company/xmlOperation.java
c4eff71545ad983c2c1eac37d99fc5c56021f390
[logicplayer.git] / Server / server / tesi / src / com / company / xmlOperation.java
1 package com.company;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.security.NoSuchAlgorithmException;
7
8 import org.jdom2.Document;
9 import org.jdom2.Element;
10 import org.jdom2.JDOMException;
11 import org.jdom2.input.SAXBuilder;
12 import org.jdom2.output.Format;
13 import org.jdom2.output.XMLOutputter;
14
15
16 public class xmlOperation {
17
18     xmlOperation(){}
19     static private String pathName=global.listaEsercizi;
20
21     public void create() {
22
23         try {
24
25             Element company = new Element("esercizi");
26             Document doc = new Document(company);
27             doc.setRootElement(company);
28
29             // new XMLOutputter().output(doc, System.out);
30             XMLOutputter xmlOutput = new XMLOutputter();
31
32             // display nice nice
33             xmlOutput.setFormat(Format.getPrettyFormat());
34             xmlOutput.output(doc, new FileWriter(pathName));
35
36             System.out.println("File Saved!");
37         } catch (IOException io) {
38             System.out.println(io.getMessage());
39         }
40     }
41
42
43
44     public void add(String name)
45     {
46         try {
47
48             SAXBuilder builder = new SAXBuilder();
49             File xmlFile = new File(pathName);
50
51             Document doc = (Document) builder.build(xmlFile);
52             Element rootNode = doc.getRootElement();
53
54
55             // add new age element
56             Element newEs = new Element("esercizio").setText(name).setAttribute("md5",MD5.digest(name));
57             rootNode.addContent(newEs);
58
59             XMLOutputter xmlOutput = new XMLOutputter();
60
61             // display nice nice
62             xmlOutput.setFormat(Format.getPrettyFormat());
63             xmlOutput.output(doc, new FileWriter(pathName));
64
65             // xmlOutput.output(doc, System.out);
66
67             System.out.println("File updated!");
68         } catch (IOException io) {
69             io.printStackTrace();
70         } catch (JDOMException e) {
71             e.printStackTrace();
72         }
73     }
74
75     public String getMD5(String name)
76     {
77         try {
78
79             SAXBuilder builder = new SAXBuilder();
80             File xmlFile = new File(pathName);
81
82             Document doc = (Document) builder.build(xmlFile);
83             Element rootNode = doc.getRootElement();
84
85             for(Element esercizio:rootNode.getChildren())
86             {
87                 if(esercizio.getText().contentEquals(name))
88                 {
89                     return esercizio.getAttribute("md5").toString();
90                 }
91             }
92         } catch (IOException io) {
93             io.printStackTrace();
94         } catch (JDOMException e) {
95             e.printStackTrace();
96         }
97         return null;
98     }
99
100     public void remove(String name)
101     {
102         try {
103
104             SAXBuilder builder = new SAXBuilder();
105             File xmlFile = new File(pathName);
106
107             Document doc = (Document) builder.build(xmlFile);
108             Element rootNode = doc.getRootElement();
109
110             for(Element esercizio:rootNode.getChildren())
111             {
112                 if(esercizio.getText().contentEquals(name))
113                 {
114                     System.out.println("trovato");
115                     rootNode.removeContent(esercizio);
116                 }
117             }
118
119             XMLOutputter xmlOutput = new XMLOutputter();
120
121             // display nice nice
122             xmlOutput.setFormat(Format.getPrettyFormat());
123             xmlOutput.output(doc, new FileWriter(pathName));
124
125             // xmlOutput.output(doc, System.out);
126
127             System.out.println("File updated!");
128         } catch (IOException io) {
129             io.printStackTrace();
130         } catch (JDOMException e) {
131             e.printStackTrace();
132         }
133     }
134
135
136 }