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 package com.sun.star.tooling.converter;
25 import java.io.*;
26 import java.util.Map;
27 
28 import com.sun.star.tooling.languageResolver.LanguageResolver;
29 import com.sun.star.tooling.languageResolver.LanguageResolver.LanguageResolvingException;
30 /**
31  *
32  * @author cs156069
33  */
34 public class GSIWriter  extends DataWriter {
35     /**
36      * The seperator used to seperate GSI columns
37      */
38     final String seperator=new String("($$)");
39 
40     /**
41      * Holding the keys used by a map holding the  content of an GSI Line contianing the source language
42      */
43     final static String[] sourceLineNames= {"Project","SourceFile","Dummy","ResType","GID","LID","HID","Platform","Width","SourceLanguageID","SourceText","SourceHText","SourceQText","SourceTitle","TimeStamp"};
44     /**
45      * Holding the keys used by a map holding the  content of an GSI Line contianing the target language
46      */
47     final static String[] targetLineNames= {"Project","SourceFile","Dummy","ResType","GID","LID","HID","Platform","Width","TargetLanguageID","TargetText","TargetHText","TargetQText","TargetTitle","TimeStamp"};
48     /**
49      * Holding the keys used by a map holding the  content of an GSI Line contianing the source and the target language
50      */
51     final static String[]    outLineNames= {"BlockNr","Project","SourceFile","Dummy","ResType","GID","LID","HID","Platform","Width","SourceLanguageID","SourceText","SourceHText","SourceQText","SourceTitle","TargetLanguageID","TargetText","TargetHText","TargetQText","TargetTitle","TimeStamp"};
52 
53     /**
54      * A map holding the  content of an GSI Line contianing the source language
55      */
56     private ExtMap    sourceLine=new ExtMap(sourceLineNames,null);
57     /**
58      * A map holding the  content of an GSI Line contianing the target language
59      */
60     private ExtMap      targetLine=new ExtMap(targetLineNames,null);
61     /**
62      * A map holding the  content of an GSI Line contianing the source and the target language
63      */
64     private ExtMap outData=new ExtMap(outLineNames, null);
65 
66     private static final String EMPTY          = new String("");
67     /**
68      * The sourceLanguage to use
69      */
70     private String sourceLanguage;
71     /**
72      * The sourceLanguage to use
73      */
74     private String targetLanguage;
75 
76     /**
77      * GSILines have a special Line End
78      */
79     private final static String lineEnd="!"+'\r'+'\n';
80     //private boolean SourceIsFirst=false;
81 
82     /**
83      * The blockNr of the current line
84      */
85     private String blockNr;
86 
87 
88 
89 
90     /**
91      * Create a new Instance of GSIWriter
92      *
93      * @param bos       the Buffered Output Stream to write to
94      * @param charset   the used charset
95      * @throws java.io.UnsupportedEncodingException
96      */
GSIWriter(BufferedOutputStream bos,String charset)97     public GSIWriter(BufferedOutputStream bos,String charset) throws java.io.UnsupportedEncodingException {
98         super(bos,charset);
99 
100 
101 
102     }
103 
104     /* (non-Javadoc)
105      * @see com.sun.star.tooling.converter.DataWriter#writeData()
106      */
writeData()107     public final void writeData() throws java.io.IOException {
108 
109         StringBuffer buffer=new StringBuffer("");
110         if(this.sourceLanguage==null&&this.targetLanguage==null){
111             LanguageResolver lang =new LanguageResolver();
112             try {
113                 this.sourceLanguage=lang.getNrFromISO((String)outData.get("SourceLanguageID"));
114                 this.targetLanguage=lang.getNrFromISO((String)outData.get("TargetLanguageID"));
115             } catch (LanguageResolvingException e) {
116 
117                 OutputHandler.out(e.getMessage());
118                 System.exit(0);
119             }
120         }
121 
122 
123         this.blockNr=(String)outData.get("BlockNr");
124         // get the values of the found fields
125         //create the gsi lines
126         //
127         //at first the source language line
128         buffer.append(getSourceLine("Text"));
129         buffer.append(getSourceLine("HText"));
130         buffer.append(getSourceLine("QText"));
131         buffer.append(getSourceLine("Title"));
132 
133         //now the target language line
134         // put them together for output
135         buffer.append(getTargetLine("Text"));
136         buffer.append(getTargetLine("HText"));
137         buffer.append(getTargetLine("QText"));
138         buffer.append(getTargetLine("Title"));
139         //ok...put all to disk;
140         this.write(buffer.toString());
141 
142     }
143 
144     /**
145      * Create a line containing the source string from the data
146      * @param resType
147      * @return The StringBuffer containing the line
148      */
getSourceLine(String resType)149     private StringBuffer getSourceLine(String resType){
150         StringBuffer buffer =new StringBuffer(200);
151         String resString = "Source"+resType;
152 
153         String help;
154         if(EMPTY.equals((String)outData.get(resString))||" ".equals(outData.get(resString))) return new StringBuffer(EMPTY);
155         else {
156             // put them together for output
157             buffer.append(this.blockNr);
158             // seperate the fields with ($$)
159             buffer.append(this.seperator);
160             buffer.append(ResTypeResolver.getExternKey(resType));
161             // seperate the fields with ($$)
162             buffer.append(this.seperator);
163             buffer.append(this.sourceLanguage);
164             // seperate the fields with ($$)
165             buffer.append(this.seperator);
166             buffer.append("int");
167             // seperate the fields with ($$)
168             buffer.append(this.seperator);
169             buffer.append(outData.get(resString));
170 
171             // this line is full
172             // so close it with '! cr lf'
173             buffer.append(GSIWriter.lineEnd);
174             Converter.countLine();
175             return buffer;
176         }
177 
178     }
179     /**
180      * Create a line containing the target string from the data
181      * @param resType
182      * @return The StringBuffer containing the line
183      */
getTargetLine(String resType)184     private StringBuffer getTargetLine(String resType){
185         StringBuffer buffer =new StringBuffer(200);
186         String resString = "Target"+resType;
187 
188         if(EMPTY.equals((String)outData.get(resString))||" ".equals(outData.get(resString))) return new StringBuffer(EMPTY);
189         else {
190             // put them together for output
191             buffer.append(this.blockNr);
192             // seperate the fields with ($$)
193             buffer.append(this.seperator);
194             buffer.append(ResTypeResolver.getExternKey(resType));
195             // seperate the fields with ($$)
196             buffer.append(this.seperator);
197             buffer.append(this.targetLanguage);
198             // seperate the fields with ($$)
199             buffer.append(this.seperator);
200             buffer.append("ext");
201             // seperate the fields with ($$)
202             buffer.append(this.seperator);
203             buffer.append(outData.get(resString));
204 
205             // this line is full
206             // so close it with '! cr lf'
207             buffer.append(GSIWriter.lineEnd);
208             Converter.countLine();
209             return buffer;
210         }
211 
212     }
213 
214 
215 	/* (non-Javadoc)
216 	 * @see com.sun.star.tooling.converter.DataWriter#writeData(java.util.Map[])
217 	 */
writeData(Map[] data)218 	protected void writeData(Map[] data) throws IOException {
219 		// TODO redesign DataHandler in the way that this is not nessesary any more
220 
221 	}
222 
223 	/* (non-Javadoc)
224 	 * @see com.sun.star.tooling.converter.DataWriter#getDataFrom(com.sun.star.tooling.converter.DataHandler)
225 	 */
getDataFrom(DataHandler handler)226 	protected void getDataFrom(DataHandler handler) throws IOException {
227 
228 		handler.putDataTo(this.outData);
229 	}
230 
231 	/* (non-Javadoc)
232 	 * @see com.sun.star.tooling.converter.DataWriter#getDatafrom(com.sun.star.tooling.converter.DataHandler)
233 	 */
getDatafrom(DataHandler handler)234 	protected void getDatafrom(DataHandler handler) throws IOException {
235 
236 		handler.putDataTo(this.outData);
237 
238 	}
239 }
240