xref: /trunk/main/l10ntools/java/jpropex/java/SdfData.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 import java.util.*;
30 import java.io.*;
31 
32 public class SdfData
33 {
34     private String          filename;
35     private OrderedHashMap  ohm;
36     private LinkedHashSet   languagesFound;
37 
38     public SdfData()
39     {
40         languagesFound  = new LinkedHashSet();
41         ohm             = new OrderedHashMap();
42         languagesFound  = new LinkedHashSet();
43     }
44     public SdfData( String filename )
45     {
46         this();
47         this.filename = filename;
48     }
49 
50     public LinkedHashSet getLanguages()
51     {
52         return languagesFound;
53     }
54     public SdfEntity get( SdfEntity obj )
55     {
56         return (SdfEntity) ohm.get( (String)obj.getId() );
57     }
58     public SdfEntity get( String key ){
59         return (SdfEntity) ohm.get( key );
60     }
61     public void add( SdfEntity obj )
62     {
63         ohm.add( obj.getId() , obj );
64     }
65 
66     public void read()
67     {
68         BufferedReader in;
69         try
70         {
71             in = new BufferedReader( new InputStreamReader( new FileInputStream( filename ), "UTF-8" ) );
72             SdfEntity entity;
73             while( in.ready() )
74             {
75                 String line = in.readLine();
76                 if( line.length() > 0 )
77                 {
78                     entity = new SdfEntity( line );
79                     ohm.add( entity.getId() , entity );        // test if is valid
80                     languagesFound.add( entity.getLangid() );
81                 }
82             }
83             in.close();
84         }
85         catch( IOException e )
86         {
87             System.out.println("Warning: can not read file " + filename);
88         }
89     }
90     public void write( String filename )
91     {
92         FileWriter out;
93         try
94         {
95             out = new FileWriter( filename , true );  // Always append
96             for( Enumeration e = ohm.elements(); e.hasMoreElements(); )
97             {
98                 out.write( ( (SdfEntity) e.nextElement() ).toString() + "\n" );
99             }
100             out.close();
101         }
102         catch( IOException e )
103         {
104             System.out.println("Error: Can't write to file " + filename);
105             System.exit( -1 );
106         }
107     }
108 }
109