1 19 package org.jahia.data; 20 21 import java.io.FileOutputStream ; 22 23 import javax.xml.transform.OutputKeys ; 24 import javax.xml.transform.Transformer ; 25 import javax.xml.transform.TransformerFactory ; 26 import javax.xml.transform.dom.DOMSource ; 27 import javax.xml.transform.stream.StreamResult ; 28 29 import org.jahia.exceptions.JahiaException; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 33 34 40 public abstract class JahiaDOMObject { 41 42 43 protected Document xmlDoc = null; 44 45 46 52 public void save (String destFileName) throws JahiaException{ 53 saveFile(destFileName); 54 } 55 56 62 public Document getDocument () { 63 return xmlDoc; 64 } 65 66 72 public String toString () { 73 Element el = (Element )xmlDoc.getDocumentElement(); 74 if ( el != null ){ 75 return el.toString(); 76 } 77 return null; 78 } 79 80 private void saveFile(String destinationFileName) 82 throws JahiaException { 83 84 try { 85 86 xmlDoc.normalize(); 88 TransformerFactory tfactory = TransformerFactory.newInstance(); 89 90 Transformer serializer = tfactory.newTransformer(); 93 94 serializer.setOutputProperty(OutputKeys.METHOD, "xml"); 95 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 96 FileOutputStream fileStream = new FileOutputStream (destinationFileName); 97 serializer.transform(new DOMSource (xmlDoc), 98 new StreamResult (fileStream)); 99 100 } catch ( Throwable t ){ 101 throw new JahiaException( "XMLPortlets", 102 "Exception " + t.getMessage(), 103 JahiaException.ERROR_SEVERITY, 104 JahiaException.SERVICE_ERROR); 105 } 106 107 } 108 109 } 110 | Popular Tags |