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 23 /* 24 * Created on 2005 25 * by Christian Schmidt 26 */ 27 package com.sun.star.tooling.converter; 28 29 import java.io.File; 30 import java.io.FileInputStream; 31 import java.io.IOException; 32 import java.io.InputStreamReader; 33 import java.util.HashMap; 34 import java.util.Map; 35 36 import com.sun.star.tooling.languageResolver.LanguageResolver; 37 import com.sun.star.tooling.languageResolver.LanguageResolver.LanguageResolvingException; 38 39 /** 40 * @author Christian Schmidt 2005 41 * 42 */ 43 public class GSIReader extends DataReader { 44 /** 45 * A Map holding an empty GSIBlock 46 */ 47 private Map EmptyGSIBlock; 48 49 /** 50 * @see LanguageResolver 51 */ 52 LanguageResolver languageResolver; 53 54 /** 55 * The source language identifier 56 */ 57 protected String sourceLanguage; 58 59 /** 60 * The target language identifier 61 */ 62 protected String targetLanguage; 63 64 /** 65 * The number of the last block 66 */ 67 private String oldBlockNr; 68 69 /** 70 * A buffer holding one GSILine 71 */ 72 private Map GSILineBuffer; 73 74 /** 75 * Indicates whether to use the buffered line 76 */ 77 private boolean useBuffer = false; 78 79 private static final String EMPTY = new String(""); 80 81 /** 82 * An empty Map to fill with language depending data 83 */ 84 private ExtMap EmptyLanguageMap; 85 86 /** 87 * Indicates whether the first block is read 88 */ 89 private boolean isFirst = true; 90 91 private int lineCounter; 92 93 private int blockCounter; 94 /** 95 * Indicates whether the last line is read 96 */ 97 private boolean lastLineFound = false; 98 99 /** 100 * Create a new Instance of GSIReader 101 * 102 * @param source 103 * the file to read from 104 * @param sourceLanguage 105 * the sourceLanguage (must not be empty) 106 * @param targetLanguage 107 * the targetLanguage 108 * @param charset 109 * the charset used to read source 110 * @throws java.io.IOException 111 * @throws Exception 112 */ GSIReader(File source, String sourceLanguage, String targetLanguage, String charset)113 public GSIReader(File source, String sourceLanguage, String targetLanguage, 114 String charset) throws java.io.IOException { 115 super(new InputStreamReader(new FileInputStream(source), charset)); 116 this.languageResolver = new LanguageResolver(); 117 this.sourceLanguage = sourceLanguage; 118 this.targetLanguage = targetLanguage; 119 120 EmptyLanguageMap = new ExtMap(new String[0], new String[0]); 121 122 } 123 124 /** 125 * Read the next GSIBlock and return the data 126 * 127 * @return A Map containing the data of the read GSIBlock the keys for the language depending data are the language id (numeric) the 128 * single language are acessible with the keys "BlockNr", "resType", "languageNr", "status","content". 129 * 130 * @throws IOException 131 */ getGSIData()132 public Map getGSIData() throws IOException { 133 String help; 134 Map helpmap; 135 Map GSIBlock = new HashMap(); 136 GSIBlock.put(sourceLanguage, EmptyLanguageMap.clone()); 137 GSIBlock.put(targetLanguage, EmptyLanguageMap.clone()); 138 String line = EMPTY; 139 String[] splitLine; 140 Map GSILine; 141 String[] GSINames = { "BlockNr", "resType", "languageNr", "status", 142 "content" }; 143 144 while (useBuffer || (line = readLine()) != null) { 145 146 147 148 if (useBuffer) { 149 GSILine = GSILineBuffer; 150 GSIBlock.put(sourceLanguage, EmptyLanguageMap.clone()); 151 GSIBlock.put(targetLanguage, EmptyLanguageMap.clone()); 152 GSIBlock.put("BlockNr", GSILine.get("BlockNr")); 153 useBuffer = false; 154 } else { 155 this.lineCounter++; 156 if ((splitLine = split(line)) == null) { 157 continue; 158 } 159 GSILine = new ExtMap(GSINames, splitLine); 160 if (isFirst) { 161 GSIBlock.put("BlockNr", GSILine.get("BlockNr")); 162 oldBlockNr = (String) GSILine.get("BlockNr"); 163 isFirst = false; 164 } 165 } 166 if (oldBlockNr == null) { 167 oldBlockNr = (String) GSILine.get("BlockNr"); 168 } 169 if (!oldBlockNr.equals((String) GSILine.get("BlockNr"))) { 170 GSILineBuffer = GSILine; 171 oldBlockNr = (String) GSILine.get("BlockNr"); 172 useBuffer = true; 173 break; 174 } 175 String lang; 176 try { 177 // Is there the source language in this line? 178 if ((lang = languageResolver.getISOfromNr((String) GSILine 179 .get("languageNr"))).equals(this.sourceLanguage)) { 180 // ok..store it as Source String under the depending 181 // ressource type 182 183 ((Map) GSIBlock.get(sourceLanguage)).put("Source" 184 + ResTypeResolver.getInternKey((String) GSILine 185 .get("resType")), GSILine.get("content")); 186 // ..maybe the target language? 187 } else { 188 if (targetLanguage.equals(EMPTY)) { 189 // if no target language is given at command line 190 targetLanguage = lang; 191 GSIBlock.put(targetLanguage, EmptyLanguageMap.clone()); 192 } 193 if (lang.equals(this.targetLanguage)) { 194 195 // ok..store it as target String under the depending 196 // ressource type 197 ((Map) GSIBlock.get(targetLanguage)).put("Target" 198 + ResTypeResolver.getInternKey((String) GSILine 199 .get("resType")), GSILine 200 .get("content")); 201 } 202 } 203 } catch (LanguageResolvingException e) { 204 OutputHandler.out("Can not resolve the language "+e.getMessage()); 205 } 206 207 } 208 if (line == null) { 209 if (lastLineFound){ 210 OutputHandler.out("\n\n"); 211 OutputHandler.out("GSI Blocks : " + this.blockCounter); 212 OutputHandler.out("GSI Lines : " + this.lineCounter); 213 214 return null; 215 }else{ 216 lastLineFound = true; 217 this.blockCounter++; 218 return GSIBlock; 219 } 220 } else { 221 this.blockCounter++; 222 return GSIBlock; 223 } 224 225 } 226 227 /** 228 * Split a GSILine to single fields 229 * 230 * @param line 231 * The line to split 232 * @return An array containing the contents of the columns in the given line 233 */ split(String line)234 private String[] split(String line) { 235 String[] splitLine = (line.substring(0, line.length() - 1)) 236 .split("\\(\\$\\$\\)"); 237 if (splitLine.length != 5) 238 return null; 239 else 240 return splitLine; 241 } 242 243 } 244