A TableLoader loads CSV-like files into Tables for PCGen. The CSV files must conform to the CSV file format with some additional limitations. Embedded newlines are not permitted in quotes. This should generate an error from TableLoader.
| 38 | * generate an error from TableLoader. |
| 39 | */ |
| 40 | public class TableLoader extends LstLineFileLoader |
| 41 | { |
| 42 | /** |
| 43 | * A pattern for empty lines. This helps the LineProcessors from having to |
| 44 | * deal with this situation. |
| 45 | */ |
| 46 | private static Pattern EMPTY = Pattern.compile("^[\\s,\\\"]+$"); |
| 47 | |
| 48 | /** |
| 49 | * The active LineProcessor used to interpret the contents of the next |
| 50 | * loaded line in a Table file. |
| 51 | */ |
| 52 | private LineProcessor processor = new ExpectStartTable(); |
| 53 | |
| 54 | @Override |
| 55 | public void loadLstString(LoadContext context, URI uri, String aString) throws PersistenceLayerException |
| 56 | { |
| 57 | //Reset to ensure prior file corruption doesn't leak into a new file |
| 58 | processor = new ExpectStartTable(); |
| 59 | super.loadLstString(context, uri, aString); |
| 60 | if (!(processor instanceof ExpectStartTable)) |
| 61 | { |
| 62 | throw new PersistenceLayerException("Did not find last ENDTABLE: entry in " + uri); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void parseLine(LoadContext context, String lstLine, URI sourceURI) throws PersistenceLayerException |
| 68 | { |
| 69 | //ignore comments |
| 70 | if (lstLine.startsWith("#") || lstLine.startsWith("\"#")) |
| 71 | { |
| 72 | return; |
| 73 | } |
| 74 | //Empty line (commas, whitespace, empty quotes) |
| 75 | if (EMPTY.matcher(lstLine).find()) |
| 76 | { |
| 77 | return; |
| 78 | } |
| 79 | processor = processor.parseLine(context, lstLine, sourceURI); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * A LineProcessor interprets a line of a Table file and returns the |
| 84 | * LineProcessor that should be responsible for interpreting the next line |
| 85 | * of the file. |
| 86 | * |
| 87 | * A LineProcessor is not expected to be able to understand/comprehend |
| 88 | * either blank lines or comment lines. Both of those should be ignored |
| 89 | * prior to the line being passed to a LineProcessor. |
| 90 | */ |
| 91 | @FunctionalInterface |
| 92 | public interface LineProcessor |
| 93 | { |
| 94 | /** |
| 95 | * Processes the given line of a Table file (identified by the |
| 96 | * sourceURI). |
| 97 | * |
nothing calls this directly
no outgoing calls
no test coverage detected