Main Page | Packages | Class Hierarchy | Class List | File List | Class Members

Main.java

Go to the documentation of this file.
00001 //$Id: Main_8java-source.html 336 2010-01-12 20:03:17Z linus $
00002 //Copyright (c) 2003, Mikael Albertsson, Mattias Danielsson, Per Engström, 
00003 //Fredrik Gröndahl, Martin Gyllensten, Anna Kent, Anders Olsson, 
00004 //Mattias Sidebäck.
00005 //All rights reserved.
00006 //
00007 //Redistribution and use in source and binary forms, with or without 
00008 //modification, are permitted provided that the following conditions are met:
00009 //
00010 //* Redistributions of source code must retain the above copyright notice, 
00011 //  this list of conditions and the following disclaimer.
00012 // 
00013 //* Redistributions in binary form must reproduce the above copyright 
00014 //  notice, this list of conditions and the following disclaimer in the 
00015 //  documentation and/or other materials provided with the distribution.
00016 //
00017 //* Neither the name of the University of Linköping nor the names of its 
00018 //  contributors may be used to endorse or promote products derived from 
00019 //  this software without specific prior written permission. 
00020 //
00021 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00022 //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00023 //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00024 //ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
00025 //LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00026 //CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00027 //SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00028 //INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00029 //CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00030 //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
00031 //THE POSSIBILITY OF SUCH DAMAGE.
00032 
00033 package org.argoprint.engine;
00034 import org.argoprint.ArgoPrintDataSource;
00035 import org.argoprint.engine.interpreters.*;
00036 
00037 import org.apache.log4j.Logger;
00038 
00039 import org.argoprint.ui.Settings;
00040 import org.argoprint.uml_interface.*;
00041 
00042 import java.io.FileNotFoundException;
00043 import java.io.FileOutputStream;
00044 import java.io.IOException;
00045 import org.w3c.dom.Document;
00046 import org.xml.sax.SAXException;
00047 import org.apache.xerces.parsers.DOMParser;
00048 import org.apache.xml.serialize.*;
00049 
00050 import org.argoprint.DataSourceStub;
00051 
00052 /**
00053  * Initializes the system, starts the processing of a template and writes the output to a file.  
00054  */
00055 public class Main {
00056     private Interpreter _firstHandler;
00057     private DOMParser _parser;
00058     // TODO: remove 
00059     private String _outputFile;
00060     private ArgoPrintDataSource _dataSource;
00061     private Logger _log;
00062 
00063     public Main() {
00064 //    public Main(boolean initDataSource) {
00065     _parser = new DOMParser(); 
00066     _dataSource = new UMLInterface(); 
00067     //_dataSource = new DataSourceStub();
00068 
00069     // Initialize the CoR
00070     Interpreter iDefault = new InterpreterDefault(_dataSource);
00071     Interpreter iCall = new InterpreterCall(_dataSource);
00072     Interpreter iIterate = new InterpreterIterate(_dataSource);
00073     Interpreter iBind = new InterpreterBind(_dataSource);
00074     Interpreter iIf = new InterpreterIf(_dataSource);
00075 
00076     _firstHandler = iCall;
00077 
00078     iCall.setNextHandler(iIterate);
00079     iCall.setFirstHandler(_firstHandler);
00080     iIterate.setNextHandler(iBind);
00081     iIterate.setFirstHandler(_firstHandler);
00082     iBind.setNextHandler(iIf);
00083     iBind.setFirstHandler(_firstHandler);
00084     iIf.setNextHandler(iDefault);
00085     iIf.setFirstHandler(_firstHandler);
00086     iDefault.setFirstHandler(_firstHandler);
00087     }
00088 
00089     public Main(ArgoPrintDataSource source) {
00090 //    public Main(boolean initDataSource) {
00091     _parser = new DOMParser();
00092         _dataSource = source;
00093 
00094     // Initialize the CoR
00095     Interpreter iDefault = new InterpreterDefault(_dataSource);
00096     Interpreter iCall = new InterpreterCall(_dataSource);
00097     Interpreter iIterate = new InterpreterIterate(_dataSource);
00098     Interpreter iBind = new InterpreterBind(_dataSource);
00099     Interpreter iIf = new InterpreterIf(_dataSource);
00100 
00101     _firstHandler = iCall;
00102 
00103     iCall.setNextHandler(iIterate);
00104     iCall.setFirstHandler(_firstHandler);
00105     iIterate.setNextHandler(iBind);
00106     iIterate.setFirstHandler(_firstHandler);
00107     iBind.setNextHandler(iIf);
00108     iBind.setFirstHandler(_firstHandler);
00109     iIf.setNextHandler(iDefault);
00110     iIf.setFirstHandler(_firstHandler);
00111     iDefault.setFirstHandler(_firstHandler);
00112     }
00113 
00114 
00115     // TODO: change parameter to Settings when that class is finished
00116     public void initializeSystem(String template, 
00117                  String outputFile, 
00118                  String outputDir) 
00119     throws SAXException, IOException {
00120     
00121     _outputFile = outputFile;
00122     // TODO: set outputDir of interface
00123     _parser.parse(template);
00124     }
00125 
00126     public void initializeSystem(Settings settings, Logger log) 
00127     throws SAXException, IOException{
00128     ((UMLInterface)_dataSource).initialize(log);
00129     _log = log;
00130     _outputFile = settings.getOutputFile();
00131     // TODO: set outputDir of interface
00132     // ((UMLInterface)_dataSource).setOutputPath(settings.getOutputDir);
00133     _parser.parse(settings.getTemplate());
00134     }
00135 
00136     public void initializeSystem(Settings settings) 
00137     throws SAXException, IOException{
00138 
00139     _outputFile = settings.getOutputFile();
00140     // TODO: set outputDir of interface
00141     // ((UMLInterface)_dataSource).setOutputPath(settings.getOutputDir);
00142     _parser.parse(settings.getTemplate());
00143     }
00144     
00145     /**
00146      * Starts the processing and writes the output.
00147      */
00148     public void go()
00149     throws FileNotFoundException, IOException, Exception {
00150     Document document = _parser.getDocument();
00151     _firstHandler.handleTag(document, new Environment());
00152     FileOutputStream outputStream = new FileOutputStream(_outputFile);
00153     OutputFormat outputFormat = new OutputFormat(document);
00154     outputFormat.setIndent(4);
00155     outputFormat.setPreserveSpace(false);
00156     outputFormat.setLineWidth(80);
00157     XMLSerializer serializer = new XMLSerializer(outputStream, outputFormat);
00158     serializer.serialize(document);
00159     }
00160 }
00161 
00162  

Generated on Tue Dec 2 22:50:56 2003 for ArgoPrint by doxygen 1.3.4