001: /*
002: LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
003:
004:
005: Copyright (C) 2003 Together
006:
007: This library is free software; you can redistribute it and/or
008: modify it under the terms of the GNU Lesser General Public
009: License as published by the Free Software Foundation; either
010: version 2.1 of the License, or (at your option) any later version.
011:
012: This library is distributed in the hope that it will be useful,
013: but WITHOUT ANY WARRANTY; without even the implied warranty of
014: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: Lesser General Public License for more details.
016:
017: You should have received a copy of the GNU Lesser General Public
018: License along with this library; if not, write to the Free Software
019: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: package org.webdocwf.util.loader.generator;
023:
024: import java.io.File;
025: import java.io.FileOutputStream;
026:
027: import org.apache.xml.serialize.Method;
028: import org.apache.xml.serialize.OutputFormat;
029: import org.apache.xml.serialize.XMLSerializer;
030: import org.w3c.dom.Document;
031: import org.webdocwf.util.loader.LoaderException;
032: import org.webdocwf.util.loader.logging.Logger;
033: import org.webdocwf.util.loader.logging.StandardLogger;
034:
035: /**
036: *
037: * WriteXmlFile writes the xml file (ImportDefinition.xml), and creates generatorOutput
038: * directory if they don't exists.
039: * @author Radoslav Dutina
040: * @version 1.0
041: */
042: public class WriteXmlFile {
043: private Logger logger;
044:
045: /**
046: * Construct object WriteXmlFile with associated parameters.
047: * @param document is the org.w3c.dom.Document object, which is created in CreateIncludeFile class.
048: * @param generatorParameters represents the references to InputParameter object.
049: * @throws LoaderException
050: */
051:
052: public WriteXmlFile(Document document,
053: InputParameters generatorParameters) throws LoaderException {
054: setLogger();
055: this .logger.write("full", "WriteXmlFile is started.");
056: try {
057: IncludeTagAttributes includeTagAttributes = new IncludeTagAttributes();
058: File xmlFile = null;
059: if (generatorParameters.getGeneratorOutput()
060: .equalsIgnoreCase("")) {
061: xmlFile = new File("xml");
062: } else {
063: xmlFile = new File(generatorParameters
064: .getGeneratorOutput()
065: + "/xml");
066: }
067: if (!xmlFile.exists())
068: xmlFile.mkdirs();
069:
070: FileOutputStream os = new FileOutputStream(xmlFile
071: + includeTagAttributes.getHref().substring(
072: includeTagAttributes.getHref().lastIndexOf(
073: "/")));
074: OutputFormat of = new OutputFormat(); //xerces apache.xml.serializer
075: of.setIndenting(true);
076: of.setIndent(4);
077: of.setMethod(Method.XML);
078: of.setPreserveSpace(false);
079: of.setLineWidth(0);
080: of.setOmitXMLDeclaration(true);
081: XMLSerializer out = new XMLSerializer(os, of);
082: //IOException
083: out.serialize(document);
084:
085: } catch (Exception e) {
086: String msg = "Exception in class WriteXmlFile: Error has occurred when trying to write xml file!";
087: LoaderException le = new LoaderException(msg, (Throwable) e);
088: this .logger.write("full", "Exception:" + msg + "\n"
089: + le.getStackTraceAsString());
090: throw le;
091: }
092: this .logger.write("full", "WriteXmlFile is finished.");
093: }
094:
095: /**
096: * This method will set logger object
097: * @param logger
098: */
099: private void setLogger() {
100: this.logger = StandardLogger.getCentralLogger();
101: }
102: }
|