09.01.2015 Views

Prof. Guido Russo - Scope - Università degli Studi di Napoli Federico II

Prof. Guido Russo - Scope - Università degli Studi di Napoli Federico II

Prof. Guido Russo - Scope - Università degli Studi di Napoli Federico II

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

format.setIndenting(true);<br />

format.setIndent(2);<br />

format.setPreserveEmptyAttributes(true);<br />

XMLSerializer serializer = new XMLSerializer(out, format);<br />

serializer.serialize(doc);<br />

}<br />

/**<br />

* Effettua le mo<strong>di</strong>fiche sulla base DOM doc al file xmlFile<br />

* @param doc<br />

* @param xmlFile<br />

* @param dtdFileName<br />

* @return Document<br />

*/<br />

protected Document saveDocument(Document doc, File xmlFile, String dtdFileName) {<br />

try {<br />

this.mutex.lock();<br />

File tmpFile = new File(xmlFile.getParentFile() + "/tmp.xml");<br />

doc.normalize();<br />

FileOutputStream tFos = new FileOutputStream(tmpFile);<br />

serialize(doc, tFos);<br />

tFos.close();<br />

this.validated = true;<br />

doc = this.builder.parse(tmpFile);<br />

if (!(this.validated)) {<br />

throw new Exception();<br />

}<br />

FileInputStream fis = new FileInputStream(tmpFile);<br />

FileOutputStream fos = new FileOutputStream(xmlFile);<br />

try {<br />

byte[] buf = new byte[1024];<br />

int i = 0;<br />

while ((i = fis.read(buf)) != -1)<br />

fos.write(buf, 0, i);<br />

}<br />

catch (Exception e)<br />

{<br />

throw e;<br />

}<br />

fis.close();<br />

fos.close();<br />

tmpFile.delete();<br />

this.mutex.unlock();<br />

}<br />

catch (Exception e) {<br />

this.mutex.unlock();<br />

return null;<br />

}<br />

return doc;<br />

}<br />

/**<br />

* Inserisce nel file <strong>di</strong> configurazione device.xml il <strong>di</strong>spositivo name<br />

* @param String name<br />

* @return boolean<br />

*/<br />

public boolean addDevice(String name) {<br />

try {<br />

if (this.device.getElementById(name) == null)<br />

{Element node = this.device.createElement("device");<br />

node.setAttribute("name", name);<br />

node.setAttribute("type", "");<br />

node.setAttribute("description", "");<br />

node.setAttribute("address", "");<br />

node.setAttribute("lock-on", "");<br />

node.setAttribute("lock-off", "");<br />

node.setAttribute("selected", "");<br />

node.setIdAttribute("name", true);<br />

Element actionElement = this.device.createElement("action");<br />

Pietro Neroni 566/2833 Pagina 85 <strong>di</strong> 117

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!