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.IOException;
31 import java.util.HashMap;
32 import java.util.Map;
33 
34 /**
35  * Merge GSIFiles back to to the original (!) SDFFile
36  *
37  *
38  * @author Christian Schmidt 2005
39  *
40  */
41 public class GSIandSDFMerger extends SDFReader {
42 
43     int lineCounter=0;
44 
45     GSIReader gsiReader;
46 
47     private Map temp=new HashMap();
48     private int j;
49     private boolean skip=true;
50     Map gsiBlock=null;
51     Map sdfBlock=null;
52 
53     private boolean dontLoadGSI=false;
54 
55     private int count;
56     /**
57      * Merge the GSIFile back to the original(!) SDFFile
58      *
59      * @param source the file to read from
60      * @param sourceLanguage the source language in the source file
61      * @param targetLanguage the target language in the source file
62      * @param charset   the charset of the files
63      * @throws java.io.IOException
64      * @throws Exception
65      */
GSIandSDFMerger(File source, File secondSource,String sourceLanguage, String targetLanguage, String charset)66     public GSIandSDFMerger(File source, File secondSource,String sourceLanguage,
67             String targetLanguage, String charset) throws IOException {
68         // merging GSI and SDF requieres two Sources
69         //this. is the SDF source
70         super(secondSource, sourceLanguage, targetLanguage, charset);
71         //create the GSI Source
72 
73         gsiReader=new GSIReader(source,sourceLanguage,targetLanguage,charset);
74 
75     }
76 
77     /* (non-Javadoc)
78      * @see com.sun.star.tooling.converter.DataReader#getData()
79      */
getData()80     public Map getData()throws java.io.IOException{
81         do{
82             skip=false;
83             this.temp=matchGSI();
84         }while(skip);
85         if(temp==null){
86             OutputHandler.out("Blocks merged :         "+this.lineCounter);
87         }
88         return temp;
89 
90     }
91 
92     /**
93      * Read each block of the GSIFile and check whether there is a matching
94      * block in the SDFFile. Match depends on the BlockNr and BlockId.
95      *
96      * @return A Map that contains the source language content
97      *          and  the target language content.
98      * @throws IOException
99      * @throws ConverterException
100      */
matchGSI()101     public Map matchGSI() throws IOException{
102 
103 
104         try {
105             //System.out.println("Start...");
106 
107             if (dontLoadGSI||(gsiBlock=gsiReader.getGSIData())!=null){
108                 dontLoadGSI=false;
109                 //check if we must update this block
110                 //if so its BlockNr is in the gsi file
111                 if((sdfBlock = super.getData())!=null){
112 
113                     if(((String)sdfBlock.get("BlockNr")).equals((String)gsiBlock.get("BlockNr"))){
114 
115                         gsiBlock.remove(EMPTY);
116                          //if the target language string is empty this may be caused by an error in the source sdf File
117                          //I don't want to overwrite a possibly correct translation with an empty string
118                          // so remove the target part from the gsiBlock
119                         Map mp=(Map)gsiBlock.get(gsiReader.targetLanguage);
120                         if (mp.size()!=0&&!((String)mp.get("TargetText")).equals("")){
121 
122                              // target language part in this gsiBlock
123 //                             if(((String)mp.get("TargetText")).equals("")){
124 //                                 gsiBlock.remove(targetLanguage);
125 //                             }
126                             // count the merged blocks
127                              lineCounter++;
128                              Map helpMap = (Map)gsiBlock.get(super.targetLanguage);//"ja"
129                              sdfBlock.putAll(helpMap);
130                              skip=false;
131                         }else{
132                             //no target language part in this gsiBlock
133                              skip=true;
134 //
135 //                             return null;
136                          }
137                     }else{
138 //                        skip=true;
139 //
140 //                    // we cant match this gsi block to the current sdf block
141                       // try matching the next sdf block with this gsi line
142                        dontLoadGSI=true;
143                     }
144                 }
145                 return sdfBlock;
146             }
147 
148         } catch (IOException e) {
149 
150             e.printStackTrace();
151         }
152         return null;
153     }
154 
155 
156 
157 
158 
159 
160 }
161