SiteGenerator.java

/*
 * Copyright 2007 Roberto Fasciolo
 * 
 * This file is part of vsitegen.
 * 
 * vsitegen is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * vsitegen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with vsitegen; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package net.robyf.vsitegen.engine;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;

import net.robyf.vsitegen.model.Element;
import net.robyf.vsitegen.model.Navigation;
import net.robyf.vsitegen.model.Page;
import net.robyf.vsitegen.model.Section;

public final class SiteGenerator {

    private final Navigation navigation;

    private final String contentPath;

    private final String outputPath;

    private final String projectName;

    private final String template;

    public SiteGenerator(final InputStream navigationFile,
            final String contentPath,
            final String outputPath,
            final String projectName,
            final String template) {
        this.navigation = Navigation.build(navigationFile);
        this.contentPath = contentPath;
        this.outputPath = outputPath;
        this.projectName = projectName;
        this.template = template;
    }

    public void generate() {
        TemplateEvaluator evaluator = new TemplateEvaluator(this.projectName,
                                                            this.navigation,
                                                            this.contentPath);
        for (Section section : this.navigation.getSections()) {
            for (Element element : section.getElements()) {
                if (element.isPage()) {
                    Page page = (Page) element;
                    String content = getPageContent(page);
                    String generatedPage = evaluator.evaluate(this.template,
                                                              page.getId(),
                                                              content);
                    saveGeneratedPage(page, generatedPage);
                }
            }
        }
    }

    private String getPageContent(final Page page) {
        File pageFile = new File(this.contentPath + File.separator + page.getFile() + ".html");
        if (pageFile.exists()) {
            try (BufferedReader reader = new BufferedReader(new FileReader(pageFile))) {
                StringWriter strwriter = new StringWriter();
                PrintWriter writer = new PrintWriter(strwriter);

                String line = reader.readLine();
                while (line != null) {
                    writer.println(line);
                    line = reader.readLine();
                }

                return strwriter.toString();
            } catch (IOException e) {
                throw new GeneratorException("Error reading page content", e);
            }
        } else {
            return "Page content not found";
        }
    }

    private void saveGeneratedPage(final Page page, final String pageContent) {
        try {
            PrintWriter writer = new PrintWriter(new FileWriter(this.outputPath
                                                                + File.separator
                                                                + page.getFile() + ".html"));
            writer.print(pageContent);
            writer.close();
        } catch (IOException e) {
            throw new GeneratorException("Error saving generated page", e);
        }
    }
}