2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 package org.scilab.modules.helptools;
16 import java.io.FileOutputStream;
17 import java.io.OutputStream;
18 import java.io.IOException;
19 import java.io.OutputStreamWriter;
20 import java.io.BufferedReader;
21 import java.io.FileReader;
22 import java.nio.charset.Charset;
25 * Class to handle a template
26 * @author Calixte DENIZET
28 public class TemplateHandler {
30 private String[] array;
31 private TemplateFiller filler;
32 private String language;
34 public TemplateHandler(TemplateFiller filler, File f, String language) {
36 this.language = language;
40 public void generateFileFromTemplate(String fileName, String id, String contents) {
42 OutputStream out = new FileOutputStream(fileName);
43 OutputStreamWriter writer = new OutputStreamWriter(out, Charset.forName("UTF-8"));
45 for (int i = 0; i < array.length; i += 2) {
46 writer.append(array[i]);
48 if (i + 1 < array.length) {
49 if (array[i + 1].equals("content")) {
51 } else if (array[i + 1].equals("top")) {
52 str = filler.makeTop(id);
53 } else if (array[i + 1].equals("previous")) {
54 str = filler.makePrevious(id);
55 } else if (array[i + 1].equals("next")) {
56 str = filler.makeNext(id);
57 } else if (array[i + 1].equals("path")) {
58 str = filler.makePath(id);
59 } else if (array[i + 1].equals("title")) {
60 str = filler.makeTitle(id);
61 } else if (array[i + 1].equals("subtitle")) {
62 str = filler.makeSubtitle(id);
63 } else if (array[i + 1].equals("toclist")) {
64 str = filler.makeTocList(id);
65 } else if (array[i + 1].equals("lastmodified")) {
66 str = filler.makeLastModified(id);
67 } else if (array[i + 1].equals("generationdate")) {
68 str = filler.makeGenerationDate(id);
69 } else if (array[i + 1].equals("version")) {
70 str = filler.makeVersion(id);;
71 } else if (array[i + 1].equals("start")) {
72 str = filler.makeStart(id);
73 } else if (array[i + 1].startsWith("translate=")) {
74 String toTranslate = array[i + 1].substring("translate=".length());
75 str = TemplateLocalization.getLocalized(language, toTranslate);
85 } catch (IOException e) {
86 System.err.println(e);
90 private void parseFile(File f) {
92 BufferedReader reader = new BufferedReader(new FileReader(f));
94 StringBuilder stringBuilder = new StringBuilder();
95 while ((line = reader.readLine()) != null ) {
96 stringBuilder.append(line);
97 stringBuilder.append("\n");
100 array = stringBuilder.toString().split("(<!--<)|(>-->)");
101 } catch (IOException e) {