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: Read entities from dtd files |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.tools.themeinstaller.localisation; |
|
20 |
|
21 import java.io.BufferedReader; |
|
22 import java.io.File; |
|
23 import java.io.FileInputStream; |
|
24 import java.io.IOException; |
|
25 import java.io.InputStreamReader; |
|
26 import java.util.Vector; |
|
27 import java.util.regex.Matcher; |
|
28 import java.util.regex.Pattern; |
|
29 |
|
30 import com.nokia.tools.themeinstaller.logger.LogWriter; |
|
31 import com.nokia.tools.themeinstaller.odtconverter.ConverterProperties; |
|
32 |
|
33 /** |
|
34 * Reads entities from dtd files. |
|
35 */ |
|
36 public class DTDReader |
|
37 { |
|
38 |
|
39 /** The Regular expression for entity. */ |
|
40 private static final String ENTITY_REGEX = "<!ENTITY(\\s*)%s(.*)>"; |
|
41 |
|
42 /** The Regular expression for entity's value. */ |
|
43 private static final String VALUE_REGEX = "(\")(.*)(\")"; |
|
44 |
|
45 /** Line separator */ |
|
46 private static final String LINE_SEPARATOR = |
|
47 System.getProperty( "line.separator" ); |
|
48 |
|
49 /** Property key for character encoding of DTD files */ |
|
50 private static final String ENCODING_KEY = "dtd_encoding"; |
|
51 |
|
52 /** |
|
53 * Read DTD Entities. Reads a set of DTD files and searches |
|
54 * for localised string defined in a entity named aLocString. |
|
55 * If there are more than one instances found, the one that |
|
56 * is found first is returned. |
|
57 * |
|
58 * @param aDtdFiles DTD files containing entities |
|
59 * @param aLocString Entity's localisation string |
|
60 * |
|
61 * @return Localised string |
|
62 * |
|
63 * @throws IOException Signals that an I/O exception has occurred. |
|
64 */ |
|
65 public static String readFiles( Vector aDtdFiles, String aLocString ) |
|
66 throws IOException |
|
67 { |
|
68 String tempResult = null; |
|
69 Vector results = new Vector(); |
|
70 for ( int i = 0; i < aDtdFiles.size(); i++ ) |
|
71 { |
|
72 tempResult = readEntity( ( File ) aDtdFiles.get( i ), aLocString ); |
|
73 |
|
74 if ( tempResult != null ) |
|
75 { |
|
76 results.add( tempResult ); |
|
77 } |
|
78 } |
|
79 if ( results.isEmpty() ) |
|
80 { |
|
81 return null; |
|
82 } |
|
83 else |
|
84 { |
|
85 return ( String ) results.firstElement(); |
|
86 } |
|
87 |
|
88 } |
|
89 |
|
90 /** |
|
91 * Read localized entity from the dtd. |
|
92 * |
|
93 * @param aDTD DTD file containing the localization |
|
94 * @param aLocString Localization string |
|
95 * @return Localized entity |
|
96 * @throws IOException if FileInputStream can not be opened |
|
97 */ |
|
98 public static String readEntity( File aDTD, String aLocString ) |
|
99 throws IOException |
|
100 { |
|
101 StringBuffer strbuf = new StringBuffer(); |
|
102 |
|
103 // Read the assumed character encoding of DTD files |
|
104 String encoding = |
|
105 ConverterProperties.getInstance().getProperty( ENCODING_KEY ); |
|
106 |
|
107 BufferedReader rdr = null; |
|
108 if( encoding != null ) |
|
109 { |
|
110 // Use specified encoding |
|
111 rdr = new BufferedReader( new InputStreamReader( |
|
112 new FileInputStream( aDTD ), encoding ) ); |
|
113 } |
|
114 else |
|
115 { |
|
116 // Use default encoding |
|
117 LogWriter.getInstance().logWarning( "DTDReader: Character " + |
|
118 "encoding of DTD files is not specified in the " + |
|
119 "properties file, using system default encoding." ); |
|
120 |
|
121 rdr = new BufferedReader( new InputStreamReader( |
|
122 new FileInputStream( aDTD ) ) ); |
|
123 } |
|
124 |
|
125 // Add all lines from the DTD file to the buffer |
|
126 for ( String line = rdr.readLine(); line != null; line = rdr.readLine() ) |
|
127 { |
|
128 strbuf.append( line + LINE_SEPARATOR ); |
|
129 } |
|
130 |
|
131 String[] formatArguments = { aLocString }; |
|
132 String regex = String.format( ENTITY_REGEX, formatArguments ); |
|
133 |
|
134 // Search for an entity containing the aLocString |
|
135 String result = findString( regex, strbuf.toString() ); |
|
136 |
|
137 if ( result != null ) |
|
138 { |
|
139 // Search for the entity's value |
|
140 result = findString( VALUE_REGEX, result ); |
|
141 |
|
142 // Remove "-marks from the value |
|
143 result = result.replaceAll( "\"", "" ); |
|
144 } |
|
145 |
|
146 return result; |
|
147 } |
|
148 |
|
149 /** |
|
150 * Find a substring. Also verifies that exactly one match is found. |
|
151 * |
|
152 * @param aRegex Regular expression for matching |
|
153 * @param aSearchString String for searching |
|
154 * @return Found string |
|
155 */ |
|
156 private static String findString( String aRegex, String aSearchString ) |
|
157 { |
|
158 String result = null; |
|
159 Pattern pattern = Pattern.compile( aRegex ); |
|
160 Matcher matcher = pattern.matcher( aSearchString ); |
|
161 int count = 0; |
|
162 while ( matcher.find() ) |
|
163 { |
|
164 result = matcher.group(); |
|
165 count++; |
|
166 } |
|
167 |
|
168 if ( count > 1 ) |
|
169 { |
|
170 throw new IllegalArgumentException( "DTD parsing: " |
|
171 + "found more than one localized value for the " |
|
172 + "entity" ); |
|
173 } |
|
174 |
|
175 return result; |
|
176 } |
|
177 |
|
178 } |
|