| 113 | } |
| 114 | |
| 115 | public Messages parse() throws PoParserException { |
| 116 | // A keyword is one of msgctxt, msgid, msgid_plural, msgstr or msgstr[$N] |
| 117 | // (Where $N is a number) |
| 118 | String keyword = null; |
| 119 | StringBuilder keywordArguments = new StringBuilder(); |
| 120 | while (true) { |
| 121 | // Read lines |
| 122 | String line; |
| 123 | try { |
| 124 | mLineNumber++; |
| 125 | line = mReader.readLine(); |
| 126 | } catch (IOException e) { |
| 127 | e.printStackTrace(); |
| 128 | throw new PoParserException(mLineNumber, e.toString()); |
| 129 | } |
| 130 | if (line == null) { |
| 131 | break; |
| 132 | } |
| 133 | line = line.trim(); |
| 134 | |
| 135 | // Early-process continuation lines |
| 136 | if (line.startsWith("\"")) { |
| 137 | if (keyword == null) { |
| 138 | throw new PoParserException( |
| 139 | mLineNumber, "Expected keyword, got continuation line"); |
| 140 | } |
| 141 | keywordArguments.append(parseString(line)); |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | // If we reach this point, we know the line is not a continuation line. If we have been |
| 146 | // accumulating the lines of a keyword argument, it is now complete, we can process it |
| 147 | if (keyword != null) { |
| 148 | processKeyword(keyword, keywordArguments.toString()); |
| 149 | keyword = null; |
| 150 | } |
| 151 | |
| 152 | // Is the line an interesting comment? |
| 153 | if (line.startsWith(FUZZY_COMMENT)) { |
| 154 | mCurrentEntryIsFuzzy = true; |
| 155 | continue; |
| 156 | } |
| 157 | // Is the line something we can ignore? |
| 158 | if (line.isEmpty() || line.charAt(0) == '#') { |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | // If we reach this point, we are at the start of a new keyword |
| 163 | String[] tokens = line.split("\\s+", 2); |
| 164 | if (tokens.length != 2) { |
| 165 | throw new PoParserException(mLineNumber, "Invalid line, could not find a space"); |
| 166 | } |
| 167 | keyword = tokens[0]; |
| 168 | keywordArguments.setLength(0); |
| 169 | keywordArguments.append(parseString(tokens[1])); |
| 170 | } |
| 171 | |
| 172 | // We finished reading the file, process the last keyword |