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  * DataHandler.java
25  *
26  * take the data from the reader
27  * and put it to the Writer
28  *
29  */
30 
31 package com.sun.star.tooling.converter;
32 
33 import java.util.*;
34 
35 /**
36  * Handle the Data to get it from the Source
37  * readable to the Target
38  *
39  * @author Christian Schmidt
40  */
41 public class DataHandler {
42 
43     /**
44      * An arrays that holds the names that will be
45      * keys for the HashMap containing the data
46      *
47      */
48     private final String[]      dataNames = { "BlockNr", "Project",
49             "SourceFile", "Dummy", "ResType", "GID", "LID", "HID", "Platform",
50             "Width", "SourceLanguageID", "SourceText", "SourceHText",
51             "SourceQText", "SourceTitle", "TargetLanguageID", "TargetText",
52             "TargetHText", "TargetQText", "TargetTitle", "TimeStamp" };
53 
54     private static final String EMPTY     = new String("");
55 
56     /**
57      * The HashMap containing the data
58      */
59     private final Map           data      = new ExtMap(dataNames, null);
60 
61     /** Creates a new instance of DataHandler */
DataHandler()62     public DataHandler() {
63     }
64 
65     /**
66      * fill the data from the desired source
67      *
68      * @param source where to get the data from
69      * @return true if data is read and false if null is read
70      * @throws IOException
71      * @throws ConverterException
72      */
fillDataFrom(DataReader source)73     public boolean fillDataFrom(DataReader source) throws java.io.IOException,
74             ConverterException {
75 
76         Map line = null;
77 
78         line = source.getData();
79         if (line == null){
80 
81             return false;
82         }else{
83 
84             this.data.putAll(line);
85             return true;
86         }
87 
88 
89     }
90 
91     /**
92      * fill this data with the inData
93      *
94      * @param inData the data to handle by this handler
95      */
fillDataWith(Map inData)96     public void fillDataWith(Map inData) {
97         data.putAll(inData);
98     }
99 
100 //    public void transfer(DataWriter target, DataReader source) {
101 //
102 //        source.setHandler(this);
103 //
104 //    }
105 
106     /**
107      * The designated output is filled with the content of this handler
108      *
109      * @param output an array of Maps [0] should hold the source language data [1] the target language data
110      * @throws java.io.IOException
111      */
putDataTo(Map[] output)112     public void putDataTo(Map[] output) throws java.io.IOException {
113         String aKey = EMPTY;
114         for (int j = 0; j < output.length; j++) {
115             Set keys = output[j].keySet();
116             Iterator iter = keys.iterator();
117             while (iter.hasNext()) {
118                 aKey = (String) iter.next();
119                 output[j].put(aKey, data.get(aKey));
120             }
121         }
122 
123     }
124     /**
125      * The designated output is filled with the content of this handler
126      *
127      * @param output a Map that should hold the source language data and the target language data
128      * @throws java.io.IOException
129      */
putDataTo(Map output)130     public void putDataTo(Map output) throws java.io.IOException {
131         String aKey = EMPTY;
132 
133         Set keys = output.keySet();
134         Iterator iter = keys.iterator();
135         while (iter.hasNext()) {
136             aKey = (String) iter.next();
137             output.put(aKey, data.get(aKey));
138         }
139 
140     }
141 
142 }