X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=server%2Ftesi%2Fsrc%2Fcom%2Fcompany%2FxmlOperation.java;fp=server%2Ftesi%2Fsrc%2Fcom%2Fcompany%2FxmlOperation.java;h=c4eff71545ad983c2c1eac37d99fc5c56021f390;hb=edfa62efb21b128dce6de134a3fb0d85f77bd2b8;hp=0000000000000000000000000000000000000000;hpb=a02dc65816998ec35975339dbad1627c2e5fb444;p=logicplayer.git diff --git a/server/tesi/src/com/company/xmlOperation.java b/server/tesi/src/com/company/xmlOperation.java new file mode 100644 index 0000000..c4eff71 --- /dev/null +++ b/server/tesi/src/com/company/xmlOperation.java @@ -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(); + } + } + + +}