1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package org.apache.openoffice.ooxml.parser; 23 24 import java.io.BufferedReader; 25 import java.io.File; 26 import java.io.FileReader; 27 import java.util.HashMap; 28 import java.util.Map; 29 import java.util.Vector; 30 31 /** A simple reader for the parse table data that allows simple filtering on the 32 * first word in each line. 33 * 34 * Lines that only contain comments or whitespace are ignored. 35 * 36 */ 37 public class ParseTableReader 38 { ParseTableReader(final File aFile)39 public ParseTableReader (final File aFile) 40 { 41 maSections = new HashMap<>(); 42 43 try 44 { 45 final BufferedReader aReader = new BufferedReader(new FileReader(aFile)); 46 47 while (true) 48 { 49 final String sLine = aReader.readLine(); 50 if (sLine == null) 51 break; 52 else if (sLine.startsWith("#")) 53 continue; 54 else if (sLine.isEmpty()) 55 continue; 56 57 final String[] aLineParts = sLine.split("\\s+"); 58 for (int nIndex=0; nIndex<aLineParts.length; ++nIndex) 59 { 60 final String sPart = aLineParts[nIndex]; 61 if (sPart.isEmpty()) 62 { 63 throw new RuntimeException(); 64 } 65 else if (sPart.charAt(0) == '"') 66 { 67 // Remove leading and trailing quotes, unquote spaces. 68 aLineParts[nIndex] = sPart.substring(1, sPart.length()-1).replace("%20", " ").replace(""", "\""); 69 } 70 } 71 GetSection(aLineParts[0]).add(aLineParts); 72 } 73 74 aReader.close(); 75 } 76 catch (final Exception aException) 77 { 78 throw new RuntimeException(aException); 79 } 80 } 81 82 83 84 GetSection(final String sSectionName)85 public Vector<String[]> GetSection (final String sSectionName) 86 { 87 Vector<String[]> aSection = maSections.get(sSectionName); 88 if (aSection == null) 89 { 90 aSection = new Vector<>(); 91 maSections.put(sSectionName, aSection); 92 } 93 return aSection; 94 } 95 96 97 98 99 private final Map<String,Vector<String[]>> maSections; 100 } 101