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 
25 import java.util.*;
26 import java.io.*;
27 
28 public class SdfData
29 {
30     private String          filename;
31     private OrderedHashMap  ohm;
32     private LinkedHashSet   languagesFound;
33 
SdfData()34     public SdfData()
35     {
36         languagesFound  = new LinkedHashSet();
37         ohm             = new OrderedHashMap();
38         languagesFound  = new LinkedHashSet();
39     }
SdfData( String filename )40     public SdfData( String filename )
41     {
42         this();
43         this.filename = filename;
44     }
45 
getLanguages()46     public LinkedHashSet getLanguages()
47     {
48         return languagesFound;
49     }
get( SdfEntity obj )50     public SdfEntity get( SdfEntity obj )
51     {
52         return (SdfEntity) ohm.get( (String)obj.getId() );
53     }
get( String key )54     public SdfEntity get( String key ){
55         return (SdfEntity) ohm.get( key );
56     }
add( SdfEntity obj )57     public void add( SdfEntity obj )
58     {
59         ohm.add( obj.getId() , obj );
60     }
61 
read()62     public void read()
63     {
64         BufferedReader in;
65         try
66         {
67             in = new BufferedReader( new InputStreamReader( new FileInputStream( filename ), "UTF-8" ) );
68             SdfEntity entity;
69             while( in.ready() )
70             {
71                 String line = in.readLine();
72                 if( line.length() > 0 )
73                 {
74                     entity = new SdfEntity( line );
75                     ohm.add( entity.getId() , entity );        // test if is valid
76                     languagesFound.add( entity.getLangid() );
77                 }
78             }
79             in.close();
80         }
81         catch( IOException e )
82         {
83             System.out.println("Warning: can not read file " + filename);
84         }
85     }
write( String filename )86     public void write( String filename )
87     {
88         FileWriter out;
89         try
90         {
91             out = new FileWriter( filename , true );  // Always append
92             for( Enumeration e = ohm.elements(); e.hasMoreElements(); )
93             {
94                 out.write( ( (SdfEntity) e.nextElement() ).toString() + "\n" );
95             }
96             out.close();
97         }
98         catch( IOException e )
99         {
100             System.out.println("Error: Can't write to file " + filename);
101             System.exit( -1 );
102         }
103     }
104 }
105