1 2 22 23 package org.webdocwf.util.loader.generator; 24 25 import java.io.File ; 26 import java.io.FileOutputStream ; 27 28 import org.apache.xml.serialize.Method; 29 import org.apache.xml.serialize.OutputFormat; 30 import org.apache.xml.serialize.XMLSerializer; 31 import org.w3c.dom.Document ; 32 import org.webdocwf.util.loader.LoaderException; 33 import org.webdocwf.util.loader.logging.Logger; 34 import org.webdocwf.util.loader.logging.StandardLogger; 35 36 43 public class WriteXmlFile { 44 private Logger logger; 45 51 52 public WriteXmlFile(Document document, InputParameters generatorParameters) throws LoaderException { 53 setLogger(); 54 this.logger.write("full", "WriteXmlFile is started."); 55 try { 56 IncludeTagAttributes includeTagAttributes = new IncludeTagAttributes(); 57 File xmlFile = null; 58 if (generatorParameters.getGeneratorOutput().equalsIgnoreCase("")) { 59 xmlFile = new File ("xml"); 60 } else { 61 xmlFile = new File (generatorParameters.getGeneratorOutput() + "/xml"); 62 } 63 if (!xmlFile.exists()) 64 xmlFile.mkdirs(); 65 66 FileOutputStream os = new FileOutputStream (xmlFile + includeTagAttributes.getHref().substring(includeTagAttributes.getHref().lastIndexOf("/"))); 67 OutputFormat of = new OutputFormat(); of.setIndenting(true); 69 of.setIndent(4); 70 of.setMethod(Method.XML); 71 of.setPreserveSpace(false); 72 of.setLineWidth(0); 73 of.setOmitXMLDeclaration(true); 74 XMLSerializer out = new XMLSerializer(os, of); 75 out.serialize(document); 77 78 } catch (Exception e) { 79 String msg = "Exception in class WriteXmlFile: Error has occurred when trying to write xml file!"; 80 LoaderException le = new LoaderException(msg, (Throwable ) e); 81 this.logger.write("full","Exception:"+ msg + "\n" + le.getStackTraceAsString()); 82 throw le; 83 } 84 this.logger.write("full", "WriteXmlFile is finished."); 85 } 86 90 private void setLogger() { 91 this.logger = StandardLogger.getCentralLogger(); 92 } 93 } | Popular Tags |