xref: /AOO41X/test/testcommon/source/org/openoffice/test/vcl/IDList.java (revision e6e6073ddaad3a04a985e8f05823629a884eb203)
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 org.openoffice.test.vcl;
25 
26 import java.io.BufferedReader;
27 import java.io.File;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.Comparator;
33 import java.util.HashMap;
34 
35 /**
36  *
37  * The class is used to read an from external files to replace the id in the code.
38  */
39 public class IDList {
40 
41     private HashMap<String, String> map = new HashMap<String, String>();
42 
43     private File dir = null;
44 
IDList(File dir)45     public IDList(File dir) {
46         this.dir = dir;
47         load();
48     }
49 
50 
readFile(File file, HashMap<String, String> map)51     private void readFile(File file, HashMap<String, String> map) {
52         BufferedReader reader = null;
53         try {
54             reader = new BufferedReader(new FileReader(file));
55             String line = null;
56             while ((line = reader.readLine()) != null ) {
57                 line = line.trim();
58                 if (line.length() == 0 /*|| line.startsWith("//")*/)
59                     continue;
60                 String[] parts = line.split(" ");
61                 if (parts.length != 2)
62                     continue;
63                 map.put(parts[0], parts[1]);
64             }
65 
66         } catch (IOException e) {
67             // for debug
68             e.printStackTrace();
69         } finally {
70             if (reader != null)
71                 try {
72                     reader.close();
73                 } catch (IOException e) {
74                     // ignore
75                 }
76         }
77     }
78 
79 
load()80     public void load() {
81         if (dir == null)
82             return;
83 
84         map.clear();
85         ArrayList<File> validFiles = new ArrayList<File>();
86         File[] files = dir.listFiles();
87         if (files == null)
88             return;
89 
90         for (File file : files) {
91             if (file.isFile() && file.getName().endsWith(".lst")) {
92                 validFiles.add(file);
93             }
94         }
95 
96         // Sort by file name. Maybe the sorting is redundant!?
97         Collections.sort(validFiles, new Comparator<File>() {
98             public int compare(File o1, File o2) {
99                 return o1.getName().compareTo(o2.getName());
100             }
101         });
102 
103         for (File file: validFiles) {
104             readFile(file, map);
105         }
106     }
107 
getId(String id)108     public String getId(String id) {
109         String value = map.get(id);
110         if (value == null) {
111             int i = id.indexOf("_");
112             if (i >= 0) {
113                 value = map.get(id.substring(++i));
114             }
115         }
116         if (value != null)
117             //The external definition overwrites the id.
118             id = value;
119 
120         return id;
121     }
122 }
123