|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 suboptimal |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation; either version 2 |
| 7 | + * of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | + */ |
| 18 | +package sub.optimal.hacklace2; |
| 19 | + |
| 20 | +import java.io.BufferedReader; |
| 21 | +import java.io.BufferedWriter; |
| 22 | +import java.io.File; |
| 23 | +import java.io.FileInputStream; |
| 24 | +import java.io.FileOutputStream; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStreamReader; |
| 27 | +import java.io.OutputStreamWriter; |
| 28 | +import java.util.regex.Matcher; |
| 29 | +import java.util.regex.Pattern; |
| 30 | +import sub.optimal.util.FileUtil; |
| 31 | + |
| 32 | +/** |
| 33 | + * |
| 34 | + * @author suboptimal |
| 35 | + */ |
| 36 | +public class TemplateProcessor { |
| 37 | + |
| 38 | + /** |
| 39 | + * Pattern to match an image placeholder in the format <code>[[filename]]</code>. |
| 40 | + */ |
| 41 | + private static final Pattern placeholder = Pattern.compile("\\[\\[([^\\]]*)\\]\\]"); |
| 42 | + private Converter converter; |
| 43 | + |
| 44 | + private TemplateProcessor() { |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Return an instance of the converter. |
| 49 | + * |
| 50 | + * @return new instance of the {@link Converter} |
| 51 | + */ |
| 52 | + public static TemplateProcessor getInstance() { |
| 53 | + return new TemplateProcessor(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Process a Hacklace script template. Placeholder for existing image files will be substituted. |
| 58 | + * |
| 59 | + * @param template Hacklace template file |
| 60 | + * @throws IOException if reading from the template file or writing to the output file fails |
| 61 | + */ |
| 62 | + public void processTemplateFile(File template) throws IOException { |
| 63 | + converter = Converter.getInstance(); |
| 64 | + |
| 65 | + if (!FileUtil.isReadableFile(template)) { |
| 66 | + System.err.println(String.format("%s: cannot be accessed", template.getName())); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + String templateFileName = template.getName(); |
| 71 | + int indexLastDot = templateFileName.lastIndexOf("."); |
| 72 | + String baseName = templateFileName.substring(0, indexLastDot < 0 ? templateFileName.length() : indexLastDot); |
| 73 | + File hacklaceFile = new File(baseName + ".hl"); |
| 74 | + if (FileUtil.isReadableFile(hacklaceFile)) { |
| 75 | + System.err.println(String.format("%s: output file already exist", hacklaceFile.getName())); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + BufferedReader reader = null; |
| 80 | + BufferedWriter writer = null; |
| 81 | + try { |
| 82 | + reader = new BufferedReader(new InputStreamReader(new FileInputStream(template), "ISO-8859-1")); |
| 83 | + writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(hacklaceFile), "ISO-8859-1")); |
| 84 | + |
| 85 | + String s; |
| 86 | + while ((s = reader.readLine()) != null) { |
| 87 | + writer.write(replacePlaceholder(s)); |
| 88 | + // TODO check for DOS/UNIX lineends |
| 89 | + writer.write('\n'); |
| 90 | + } |
| 91 | + } finally { |
| 92 | + if (writer != null) { |
| 93 | + try { |
| 94 | + writer.close(); |
| 95 | + } catch (IOException ioe) { |
| 96 | + System.err.println(String.format("%s: failed to close the file - %s", hacklaceFile.getName(), ioe.getMessage())); |
| 97 | + } |
| 98 | + } |
| 99 | + if (reader != null) { |
| 100 | + try { |
| 101 | + reader.close(); |
| 102 | + } catch (IOException ioe) { |
| 103 | + System.err.println(String.format("%s: failed to close the file - %s", hacklaceFile.getName(), ioe.getMessage())); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Replace an image placeholder in the passed line by hexadecimal bytes in Hacklace 2 animation |
| 111 | + * format.<br> |
| 112 | + * The placeholder has following format: [[filename]]<br> |
| 113 | + * - if <code>filename</code> point to a readable file, the placeholder is substituted |
| 114 | + * |
| 115 | + * @param line input string |
| 116 | + * @return output line, placeholder for existing image files are substituted |
| 117 | + */ |
| 118 | + public String replacePlaceholder(String line) { |
| 119 | + Matcher matcher = placeholder.matcher(line); |
| 120 | + StringBuffer lineBuffer = new StringBuffer(); |
| 121 | + StringBuilder matchBuffer = new StringBuilder(); |
| 122 | + while (matcher.find()) { |
| 123 | + matchBuffer.setLength(0); |
| 124 | + String fileName = matcher.group(1); |
| 125 | + File file = new File(fileName); |
| 126 | + if (FileUtil.isReadableFile(file)) { |
| 127 | + String hacklaceBytes = converter.convertToHacklace(file); |
| 128 | + if (hacklaceBytes.length() != 0) { |
| 129 | + System.out.println(String.format("%s: image converted", file.getName())); |
| 130 | + matchBuffer.append(Converter.getHexByteMarker()); |
| 131 | + matchBuffer.append(hacklaceBytes); |
| 132 | + matchBuffer.append(Converter.getHexByteMarker()); |
| 133 | + } else { |
| 134 | + System.out.println(String.format("%s: image could not be not converted", file.getName())); |
| 135 | + matchBuffer.append(matcher.group()); |
| 136 | + } |
| 137 | + } else { |
| 138 | + System.out.println(String.format("%s: image skipped", file.getName())); |
| 139 | + matchBuffer.append(matcher.group()); |
| 140 | + } |
| 141 | + matcher.appendReplacement(lineBuffer, matchBuffer.toString()); |
| 142 | + } |
| 143 | + matcher.appendTail(lineBuffer); |
| 144 | + return lineBuffer.toString(); |
| 145 | + } |
| 146 | +} |
0 commit comments