|
1 /* |
|
2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.tools.themeinstaller.odtconverter; |
|
20 |
|
21 import java.io.File; |
|
22 import java.io.FileInputStream; |
|
23 import java.io.IOException; |
|
24 import java.net.URL; |
|
25 import java.util.Properties; |
|
26 |
|
27 /** |
|
28 * Properties for ODT to XML converter |
|
29 * @author jutarhon |
|
30 */ |
|
31 public class ConverterProperties extends Properties |
|
32 { |
|
33 /** |
|
34 * Default serial version UID |
|
35 */ |
|
36 private static final long serialVersionUID = 1L; |
|
37 private static final String PROPERTY_FILE = |
|
38 "data" + File.separatorChar + "ThemeInstaller.prop"; |
|
39 private static ConverterProperties sInstance = null; |
|
40 private static String sPropFileName = null; |
|
41 |
|
42 /** |
|
43 * Default constructor, loads the properties from default place |
|
44 * @throws IOException If default file is not found or some other IO error occurs |
|
45 */ |
|
46 public ConverterProperties() throws IOException |
|
47 { |
|
48 // Get the execution location |
|
49 URL resource = |
|
50 getClass().getProtectionDomain().getCodeSource().getLocation(); |
|
51 File bin = new File( resource.getPath() ); |
|
52 |
|
53 // Load properties from the Theme Installation directory |
|
54 loadProperties( new File( |
|
55 bin.getParent() + File.separatorChar + PROPERTY_FILE ) ); |
|
56 } |
|
57 |
|
58 /** |
|
59 * Constructor with specified properties file |
|
60 * @param aFile File to read |
|
61 * @throws IOException If given file is not found or some other IO error occurs |
|
62 */ |
|
63 public ConverterProperties( File aFile ) throws IOException |
|
64 { |
|
65 loadProperties( aFile ); |
|
66 } |
|
67 |
|
68 /** |
|
69 * Constructor with default values |
|
70 * @param aDefaults Default properties values |
|
71 * @throws IOException If default file is not found or some other IO error occurs |
|
72 */ |
|
73 public ConverterProperties( Properties aDefaults ) throws IOException |
|
74 { |
|
75 super( aDefaults ); |
|
76 loadProperties( new File( PROPERTY_FILE ) ); |
|
77 } |
|
78 |
|
79 /** |
|
80 * Loads the properties from file |
|
81 * @param aFile File to read |
|
82 * @throws IOException If given file is not found or some other IO error occurs |
|
83 */ |
|
84 protected void loadProperties( File aFile ) throws IOException |
|
85 { |
|
86 FileInputStream file = null; |
|
87 try |
|
88 { |
|
89 file = new FileInputStream( aFile ); |
|
90 load( file ); |
|
91 } |
|
92 finally |
|
93 { |
|
94 if( file != null ) |
|
95 { |
|
96 file.close(); |
|
97 } |
|
98 } |
|
99 } |
|
100 |
|
101 /** |
|
102 * Singleton method for getting the ConverterProperties instance. Note that |
|
103 * the initialize method must be called before this one. |
|
104 * @return ConverterProperties instance |
|
105 * @throws IllegalStateException if this method has been called before |
|
106 * initialize has been called. |
|
107 */ |
|
108 public static ConverterProperties getInstance() |
|
109 { |
|
110 if( sInstance == null ) |
|
111 { |
|
112 throw new IllegalStateException( "Converter Properties: " + |
|
113 "Initialize must be called before the properties " + |
|
114 "can be used" ); |
|
115 } |
|
116 |
|
117 return sInstance; |
|
118 } |
|
119 |
|
120 /** |
|
121 * Initialize method for constructing and reading the properties. The |
|
122 * properties are re-initialized only if the given property file differs |
|
123 * from the one used in the previous initialization. |
|
124 * @param aPropFile Properties file |
|
125 * @throws IOException If required properties can not be read from the |
|
126 * property file. |
|
127 */ |
|
128 public static void initialize( File aPropFile ) |
|
129 throws IOException |
|
130 { |
|
131 // Already initialized if: |
|
132 // sInstance is not null AND |
|
133 // sPropFileName equals with aPropFile |
|
134 if( sInstance != null && |
|
135 ( sPropFileName != null && aPropFile != null && sPropFileName.equals( aPropFile.getPath() ) || |
|
136 ( sPropFileName == null && aPropFile == null ) ) ) |
|
137 { |
|
138 return; |
|
139 } |
|
140 |
|
141 // Create a new instance |
|
142 if( aPropFile == null ) |
|
143 { |
|
144 sInstance = new ConverterProperties(); |
|
145 sPropFileName = null; |
|
146 } |
|
147 else |
|
148 { |
|
149 sInstance = new ConverterProperties( aPropFile ); |
|
150 sPropFileName = aPropFile.getPath(); |
|
151 } |
|
152 } |
|
153 } |