|
| 1 | +/* |
| 2 | + * Copyright IBM Corp. 2018 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.ibm.g11n.pipeline.custom; |
| 17 | + |
| 18 | +import java.io.BufferedReader; |
| 19 | +import java.io.BufferedWriter; |
| 20 | +import java.io.File; |
| 21 | +import java.io.FileOutputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.io.OutputStream; |
| 26 | +import java.io.OutputStreamWriter; |
| 27 | +import java.io.PrintWriter; |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.LinkedList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Map.Entry; |
| 34 | + |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +import com.ibm.g11n.pipeline.resfilter.FilterInfo.Type; |
| 38 | +import com.ibm.g11n.pipeline.resfilter.FilterOptions; |
| 39 | +import com.ibm.g11n.pipeline.resfilter.LanguageBundle; |
| 40 | +import com.ibm.g11n.pipeline.resfilter.LanguageBundleBuilder; |
| 41 | +import com.ibm.g11n.pipeline.resfilter.ResourceFilter; |
| 42 | +import com.ibm.g11n.pipeline.resfilter.ResourceFilterException; |
| 43 | +import com.ibm.g11n.pipeline.resfilter.ResourceString; |
| 44 | + |
| 45 | +/** |
| 46 | + * Resource filter implementation for testing. |
| 47 | + * |
| 48 | + * @author Yoshito Umaoka |
| 49 | + */ |
| 50 | +public class MockResourceFilter extends ResourceFilter { |
| 51 | + |
| 52 | + public static final String ID = "MOCK"; |
| 53 | + public static final Type TYPE = Type.SINGLE; |
| 54 | + |
| 55 | + // MockResource format |
| 56 | + // |
| 57 | + // Header lines |
| 58 | + // |
| 59 | + // ! lang |
| 60 | + // # bundle_comment |
| 61 | + // @ metadata_key = meadata_value |
| 62 | + // --- |
| 63 | + // |
| 64 | + // Resource entry lines |
| 65 | + // |
| 66 | + // # bundle_comment1 |
| 67 | + // # bundle_comment2 |
| 68 | + // @ metadata_key1 = medata_value1 |
| 69 | + // @ metadata_key2 = medata_value2 |
| 70 | + // resource_key = resource_value |
| 71 | + // |
| 72 | + |
| 73 | + private static char LANGCODE_MARKER = '!'; |
| 74 | + private static char COMMENT_MARKER = '#'; |
| 75 | + private static char METADATA_MARKER = '@'; |
| 76 | + private static char KEY_VALUE_SEPARATOR_CHAR = '='; |
| 77 | + private static String HEADER_SEPARATOR = "---"; |
| 78 | + |
| 79 | + @Override |
| 80 | + public LanguageBundle parse(InputStream inStream, FilterOptions options) |
| 81 | + throws IOException, ResourceFilterException { |
| 82 | + BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, StandardCharsets.UTF_8)); |
| 83 | + |
| 84 | + LanguageBundleBuilder bb = new LanguageBundleBuilder(true); |
| 85 | + boolean inHeader = true; |
| 86 | + List<String> notes = null; |
| 87 | + Map<String, String> metadata = null; |
| 88 | + |
| 89 | + while (true) { |
| 90 | + String line = reader.readLine(); |
| 91 | + if (line == null) { |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + line = line.trim(); |
| 96 | + if (line.isEmpty()) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + int idx; |
| 101 | + if (inHeader) { |
| 102 | + if (line.charAt(0) == LANGCODE_MARKER) { |
| 103 | + // language code |
| 104 | + String langCode = line.substring(1).trim(); |
| 105 | + bb.embeddedLanguageCode(langCode); |
| 106 | + } else if (line.charAt(0) == COMMENT_MARKER) { |
| 107 | + // bundle comment |
| 108 | + bb.addNote(line.substring(1).trim()); |
| 109 | + } else if (line.charAt(0) == METADATA_MARKER) { |
| 110 | + // bundle metadata |
| 111 | + String kv = line.substring(1); |
| 112 | + idx = kv.indexOf(KEY_VALUE_SEPARATOR_CHAR); |
| 113 | + if (idx <= 0) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + String metaKey = kv.substring(0, idx).trim(); |
| 118 | + if (metaKey.isEmpty()) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + String metaVal = kv.substring(idx + 1).trim(); |
| 122 | + bb.addMetadata(metaKey, metaVal); |
| 123 | + } else if (line.equals(HEADER_SEPARATOR)) { |
| 124 | + inHeader = false; |
| 125 | + } |
| 126 | + } else { |
| 127 | + if (line.charAt(0) == COMMENT_MARKER) { |
| 128 | + if (notes == null) { |
| 129 | + notes = new LinkedList<>(); |
| 130 | + } |
| 131 | + notes.add(line.substring(1).trim()); |
| 132 | + } else if (line.charAt(0) == METADATA_MARKER) { |
| 133 | + String kv = line.substring(1); |
| 134 | + idx = kv.indexOf(KEY_VALUE_SEPARATOR_CHAR); |
| 135 | + if (idx <= 0) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + String metaKey = kv.substring(0, idx).trim(); |
| 140 | + if (metaKey.isEmpty()) { |
| 141 | + continue; |
| 142 | + } |
| 143 | + String metaVal = kv.substring(idx + 1).trim(); |
| 144 | + if (metadata == null) { |
| 145 | + metadata = new HashMap<>(); |
| 146 | + } |
| 147 | + metadata.put(metaKey, metaVal); |
| 148 | + } else { |
| 149 | + idx = line.indexOf(KEY_VALUE_SEPARATOR_CHAR); |
| 150 | + if (idx <= 0) { |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + String resKey = line.substring(0, idx).trim(); |
| 155 | + String resVal = line.substring(idx + 1).trim(); |
| 156 | + |
| 157 | + bb.addResourceString( |
| 158 | + ResourceString |
| 159 | + .with(resKey, resVal) |
| 160 | + .notes(notes) |
| 161 | + .metadata(metadata)); |
| 162 | + |
| 163 | + // reset notes/metadata |
| 164 | + notes = null; |
| 165 | + metadata = null; |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return bb.build(); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void write(OutputStream outStream, LanguageBundle languageBundle, FilterOptions options) |
| 175 | + throws IOException, ResourceFilterException { |
| 176 | + |
| 177 | + try (PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outStream, StandardCharsets.UTF_8)))) { |
| 178 | + String langCode = languageBundle.getEmbeddedLanguageCode(); |
| 179 | + if (langCode != null) { |
| 180 | + StringBuilder langCodeLine = new StringBuilder(); |
| 181 | + langCodeLine.append(LANGCODE_MARKER).append(langCode); |
| 182 | + writer.println(langCodeLine); |
| 183 | + } |
| 184 | + |
| 185 | + List<String> bundleNotes = languageBundle.getNotes(); |
| 186 | + for (String bundleNote : bundleNotes) { |
| 187 | + StringBuilder noteLine = new StringBuilder(); |
| 188 | + noteLine.append(COMMENT_MARKER).append(bundleNote); |
| 189 | + writer.println(noteLine); |
| 190 | + } |
| 191 | + |
| 192 | + Map<String, String> bundleMetadata = languageBundle.getMetadata(); |
| 193 | + for (Entry<String, String> bundleMetaKV : bundleMetadata.entrySet()) { |
| 194 | + StringBuilder metadataLine = new StringBuilder(); |
| 195 | + metadataLine.append(METADATA_MARKER) |
| 196 | + .append(bundleMetaKV.getKey()).append(KEY_VALUE_SEPARATOR_CHAR).append(bundleMetaKV.getValue()); |
| 197 | + writer.println(metadataLine); |
| 198 | + } |
| 199 | + |
| 200 | + writer.println(HEADER_SEPARATOR); |
| 201 | + |
| 202 | + List<ResourceString> resStrings = languageBundle.getSortedResourceStrings(); |
| 203 | + for (ResourceString resString : resStrings) { |
| 204 | + writer.println(); |
| 205 | + |
| 206 | + // write resource entry notes |
| 207 | + List<String> notes = resString.getNotes(); |
| 208 | + for (String note : notes) { |
| 209 | + StringBuilder noteLine = new StringBuilder(); |
| 210 | + noteLine.append(COMMENT_MARKER).append(note); |
| 211 | + writer.println(noteLine); |
| 212 | + } |
| 213 | + |
| 214 | + // write resource entry metadata |
| 215 | + Map<String, String> metadata = resString.getMetadata(); |
| 216 | + for (Entry<String, String> metaKV : metadata.entrySet()) { |
| 217 | + StringBuilder metadataLine = new StringBuilder(); |
| 218 | + metadataLine.append(METADATA_MARKER) |
| 219 | + .append(metaKV.getKey()).append(KEY_VALUE_SEPARATOR_CHAR).append(metaKV.getValue()); |
| 220 | + writer.println(metadataLine); |
| 221 | + } |
| 222 | + |
| 223 | + // write resoruce entry key and value |
| 224 | + StringBuilder resKV = new StringBuilder(); |
| 225 | + resKV.append(resString.getKey()).append(KEY_VALUE_SEPARATOR_CHAR).append(resString.getValue()); |
| 226 | + writer.println(resKV); |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments