Changeset 5283


Ignore:
Timestamp:
2003-08-27T10:46:37+12:00 (21 years ago)
Author:
jmt12
Message:

Added the -ignore_file flag to allow a single output document to be generated

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/util/Generator.java

    r4686 r5283  
    5757public class Generator {
    5858
     59    private boolean ignore_file;
    5960    private FileOutputStream file_output_stream = null;
    60 
    6161    private TemplateManager template_manager = null;
    6262
    63     public Generator(File test_file, File template_file) {
    64     template_manager = new TemplateManager(template_file);
     63    public Generator(File test_file, File template_file, boolean ignore_file) {
     64    this.ignore_file = ignore_file;
     65    this.template_manager = new TemplateManager(template_file);
    6566    File files[] = null;
    6667    // If test_file is a file, goody.
     
    114115    System.out.println("Rapid repeated HTML generation tool.");
    115116    System.out.println("  written by John Thompson, 2002\n");
     117    boolean ignore_file = false;
    116118    boolean usage = false;
    117119    // Attempt to generate html from text
    118     if(args.length == 2) {
     120    if(2 <= args.length && args.length <= 3) {
    119121        File text_file = new File(args[0]);
    120122        File template_file = new File(args[1]);
     123        if(args.length == 3) {
     124        if(args[2].equals("-ignore_file")) {
     125            ignore_file = true;
     126        }
     127        }
    121128        if(!text_file.exists() || !template_file.exists() || !template_file.isFile()) {
    122129        usage = true;
    123130        }
    124131        else {
    125         Generator generator = new Generator(text_file, template_file);
     132        Generator generator = new Generator(text_file, template_file, ignore_file);
    126133        generator.exit();
    127134        }
     
    132139    // Print usage message
    133140    if(usage) {
    134         System.out.println("Usage: GenHelp <text_file> <template_file>");
     141        System.out.println("Usage: Generator <text_file> <template_file> [-ignore_file]");
    135142        System.out.println("text_file     - A file containing text marked up using special tags.");
    136143        System.out.println("template_file - An HTML file using tags to specify where to insert text.");
     144        System.out.println("-ignore_file  - Instructs generator to ignore $FILE tags, thus generating a single output document.");
    137145    }
    138146    }
     
    256264            // The first case to consider is a file open command tag. This tells us the next block of html should be written to a certain file.
    257265            if(line.startsWith("<$FILE")) {
    258             // Close any existing output file.
    259             if(output_file != null) {
    260                 // Write any existing block
    261                 if(block != null) {
    262                 if(paragraph != null) {
    263                     block.insert(paragraph.toString());
    264                     paragraph = null;
     266            // If we have been asked to ignore file tags, then thats what we do.
     267            if(ignore_file) {
     268                done = true;
     269            }
     270            else {
     271                // Close any existing output file.
     272                if(output_file != null) {
     273                // Write any existing block
     274                if(block != null) {
     275                    if(paragraph != null) {
     276                    block.insert(paragraph.toString());
     277                    paragraph = null;
     278                    }
     279                    out("  write: " + block.getName());
     280                    output_file.write((block.toString()).getBytes());
     281                    block = null;
    265282                }
    266                 out("  write: " + block.getName());
    267                 output_file.write((block.toString()).getBytes());
    268                 block = null;
    269                 }
    270                 output_file.close();
    271                 output_file = null;
    272             }
    273             // Determine the new file name.
    274             String name = line.substring(12, line.lastIndexOf(">"));
    275             // Create new file.
    276             output_file = new FileOutputStream(name);
    277             // Dealt with
    278             out("  writing to: " + name);
    279             name = null;
    280             done = true;
     283                // Write the file footer
     284                output_file.write((template_manager.getHTMLBlock("FILE_FOOTER")).toString().getBytes());
     285
     286                output_file.close();
     287                output_file = null;
     288                }
     289                // Determine the new file name.
     290                String name = line.substring(12, line.lastIndexOf(">"));
     291                // Create new file.
     292                output_file = new FileOutputStream(name);
     293                // Write file header
     294                output_file.write((template_manager.getHTMLBlock("FILE_HEADER")).toString().getBytes());
     295                // Dealt with
     296                out("  writing to: " + name);
     297                name = null;
     298                done = true;
     299            }
    281300            }
    282301            else if(line.startsWith("<$")) {
     
    290309                if(output_file == null) {
    291310                output_file = new FileOutputStream(text_file.getName() + ".htm");
     311                // Write file header
     312                output_file.write((template_manager.getHTMLBlock("FILE_HEADER")).toString().getBytes());
    292313                }
    293314                out("  write: " + block.getName());
     
    339360            if(output_file == null) {
    340361            output_file = new FileOutputStream(text_file.getName() + ".htm");
     362            // Write file header
     363            output_file.write((template_manager.getHTMLBlock("FILE_HEADER")).toString().getBytes());
    341364            }
    342365            out("  write: " + block.getName());
     
    346369        paragraph = null;
    347370        line = null;
    348         output_file.close();
     371        // Close any remaining output_file
     372        if(output_file != null) {
     373            // Write file footer
     374            output_file.write((template_manager.getHTMLBlock("FILE_FOOTER")).toString().getBytes());
     375            output_file.close();
     376        }
    349377        output_file = null;
    350378        }
Note: See TracChangeset for help on using the changeset viewer.