| 1 | /* |
| 2 | * Copyright (C) 2012. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 3 or |
| 6 | * version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | package func.lib; |
| 15 | |
| 16 | import java.io.FileNotFoundException; |
| 17 | import java.io.Reader; |
| 18 | import java.io.StringReader; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Map; |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import uk.me.parabola.mkgmap.osmstyle.StyleFileLoader; |
| 24 | |
| 25 | /** |
| 26 | * A style file loader where all the files are held as strings within the loader itself. |
| 27 | * |
| 28 | * A file name and contents can be added with the {@link #addfile(String, String)} method, or by passing an |
| 29 | * array to the constructor of name,content pairs. |
| 30 | * |
| 31 | * Used for testing. |
| 32 | * |
| 33 | * @author Steve Ratcliffe |
| 34 | */ |
| 35 | public class StringStyleFileLoader extends StyleFileLoader { |
| 36 | private final Map<String, String> files = new HashMap<>(); |
| 37 | |
| 38 | /** |
| 39 | * Pass filename and file contents like so: |
| 40 | * <pre> |
| 41 | * new String[][] { |
| 42 | * {"lines", "highway=primary [0x2]"}, |
| 43 | * {"points", "amenity=doctors [0x88]"}, |
| 44 | * ... |
| 45 | * } |
| 46 | * </pre> |
| 47 | * @param files An array of filename, content pairs. |
| 48 | */ |
| 49 | public StringStyleFileLoader(String[][] files) { |
| 50 | for (String[] nameContents : files) |
| 51 | addfile(nameContents[0], nameContents[1]); |
| 52 | } |
| 53 | |
| 54 | public void addfile(String name, String contents) { |
| 55 | files.put(name, contents); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Open a file within the style. Creates a StringReader with the contents corresponding |
| 60 | * to the given filename. If the filename does not exist in the files array, then a FileNotFoundException |
| 61 | * is thrown as it would be in a regular style. |
| 62 | * |
| 63 | * @param filename The name of the file in the style. |
| 64 | * @return A StringReader with the contents of the file. |
| 65 | * @throws FileNotFoundException If the file name is not found in the files array. |
| 66 | */ |
| 67 | public Reader open(String filename) throws FileNotFoundException { |
| 68 | String contents = files.get(filename); |
| 69 | if (contents == null) |
| 70 | throw new FileNotFoundException("No such file " + filename); |
| 71 | return new StringReader(contents); |
| 72 | } |
| 73 | |
| 74 | public void close() { |
| 75 | // Nothing to do |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * List the filenames in the style. |
| 80 | * For completeness, we probably won't use this. |
| 81 | */ |
| 82 | public String[] list() { |
| 83 | Set<String> strings = files.keySet(); |
| 84 | return strings.toArray(new String[strings.size()]); |
| 85 | } |
| 86 | } |