1 package net.sf.openrocket.gui.help.tours;
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
8 import java.util.ArrayList;
10 import java.util.Locale;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
14 import net.sf.openrocket.util.BugException;
17 * Class that loads a slide set from a file.
19 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
21 public class SlideSetLoader {
23 private static final Pattern NEW_SLIDE_PATTERN = Pattern.compile("^\\[(.*)\\]$");
25 private final String baseDir;
26 private TextLineReader source;
27 private Locale locale;
35 * @param baseDir The base directory from which to load from. It is prepended to the loaded
36 * file names and image file names.
38 public SlideSetLoader(String baseDir) {
39 this(baseDir, Locale.getDefault());
46 * @param baseDir The base directory from which to load from. It is prepended to the loaded
47 * file names and image file names.
48 * @param locale The locale for which the files are loaded.
50 public SlideSetLoader(String baseDir, Locale locale) {
51 if (baseDir.length() > 0 && !baseDir.endsWith("/")) {
52 baseDir = baseDir + "/";
54 this.baseDir = baseDir;
60 * Load a slide set from a file. The base directory is prepended to the
63 * @param filename the file to read in the base directory.
64 * @return the slide set
66 public SlideSet load(String filename) throws IOException {
67 String file = baseDir + filename;
68 InputStream in = getLocalizedFile(file);
71 InputStreamReader reader = new InputStreamReader(in, "UTF-8");
79 private InputStream getLocalizedFile(String filename) throws IOException {
80 for (String file : generateLocalizedFiles(filename)) {
81 InputStream in = ClassLoader.getSystemResourceAsStream(file);
86 throw new FileNotFoundException("File '" + filename + "' not found.");
89 private List<String> generateLocalizedFiles(String filename) {
91 int index = filename.lastIndexOf('.');
93 base = filename.substring(0, index);
94 ext = filename.substring(index);
101 List<String> list = new ArrayList<String>();
102 list.add(base + "_" + locale.getLanguage() + "_" + locale.getCountry() + "_" + locale.getVariant() + ext);
103 list.add(base + "_" + locale.getLanguage() + "_" + locale.getCountry() + ext);
104 list.add(base + "_" + locale.getLanguage() + ext);
105 list.add(base + ext);
111 * Load slide set from a reader.
113 * @param reader the reader to read from.
114 * @return the slide set.
116 public SlideSet load(Reader reader) throws IOException {
117 source = new TextLineReader(reader);
119 // Read title and description
120 String title = source.next();
121 StringBuilder desc = new StringBuilder();
122 while (!nextLineStartsSlide()) {
123 if (desc.length() > 0) {
126 desc.append(source.next());
129 // Create the slide set
130 SlideSet set = new SlideSet();
132 set.setDescription(desc.toString());
136 while (source.hasNext()) {
137 Slide s = readSlide();
145 private Slide readSlide() {
147 String imgLine = source.next();
148 Matcher matcher = NEW_SLIDE_PATTERN.matcher(imgLine);
149 if (!matcher.matches()) {
150 throw new BugException("Line did not match new slide pattern: " + imgLine);
153 String imageFile = matcher.group(1);
155 StringBuffer desc = new StringBuffer();
156 while (source.hasNext() && !nextLineStartsSlide()) {
157 if (desc.length() > 0) {
160 desc.append(source.next());
163 return new Slide(baseDir + imageFile, desc.toString());
168 private boolean nextLineStartsSlide() {
169 return NEW_SLIDE_PATTERN.matcher(source.peek()).matches();