001: /*
002: * Generator.java March 2006
003: *
004: * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General
016: * Public License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
018: * Boston, MA 02111-1307 USA
019: */
020:
021: package simple.page.translate;
022:
023: import freemarker.log.Logger;
024: import simple.page.Workspace;
025: import java.io.FileWriter;
026: import java.io.File;
027:
028: /**
029: * The <code>Generator</code> object is used to generate the sources
030: * for the document definition built during parsing. This will acquire
031: * a template suitable for the runtime language specified by the JSP
032: * source. This templates is given the definition, and the output of
033: * the template is written to source file specified by the definition.
034: *
035: * @author Niall Gallagher
036: */
037: final class Generator {
038:
039: /**
040: * To ensure that logging does not cause undesired JSP output,
041: * the freemarker logging is turned off for this generator.
042: */
043: static {
044: try {
045: Logger.selectLoggerLibrary(Logger.LIBRARY_NONE);
046: } catch (Exception e) {
047: e.printStackTrace();
048: }
049: }
050:
051: /**
052: * This is used to load the template to match the source object.
053: */
054: private SchemaLoader loader;
055:
056: /**
057: * Constructor for the <code>Generator</code> object. This is used
058: * to generate a source file for the specified runtime language.
059: * By default the Java and Groovy languages can be generated.
060: *
061: * @param project this is the project used by this generator
062: */
063: public Generator(Workspace project) {
064: this .loader = new SchemaLoader(project);
065: }
066:
067: /**
068: * This is used to generate the source for the provided document
069: * definition. This will ensure that the source is written to the
070: * file suggested by the document definition. If the directory
071: * leading to the source file does not exist then it is created.
072: *
073: * @param source this is the document definition to generate
074: */
075: public void generate(Definition source) throws Exception {
076: File path = source.getDirectory();
077: File file = source.getSource();
078:
079: if (!path.exists()) {
080: path.mkdirs();
081: }
082: generate(source, file);
083: }
084:
085: /**
086: * This is used to generate the source for the provided document
087: * definition. This will ensure that the source is written to the
088: * file suggested by the document definition. If the directory
089: * leading to the source file does not exist then it is created.
090: *
091: * @param source this is the document definition to generate
092: */
093: private void generate(Definition source, File file)
094: throws Exception {
095: Schema template = loader.getSchema(source);
096: FileWriter data = new FileWriter(file);
097:
098: template.write(data);
099: data.close();
100: }
101: }
|