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