1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.unitesk.atp.tree.generation;
33
34 import java.io.InputStream;
35 import java.io.IOException;
36 import java.io.File;
37
38 import java.util.Enumeration;
39 import java.util.HashMap;
40 import java.util.Iterator;
41 import java.util.Map;
42 import java.util.Properties;
43
44 import com.unitesk.atp.tree.tool.Tool;
45 import com.unitesk.atp.messages.*;
46
47 /***
48 * Token Style Manager provides token styles for html generation.
49 * It reads token styles from file (in property format).
50 *
51 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
52 * @version $Id: TokenStyleManager.java,v 1.3 2004/10/11 15:00:45 all-x Exp $
53 */
54 public class TokenStyleManager
55 {
56 protected String[] styles = null;
57
58 /***
59 * Constructs Token Style manager.
60 *
61 * @param token_types_interface Interface that defines tokens
62 * types as constants.
63 * @param tokenstyle_file File name with map from token type name
64 * to token style name.
65 */
66 public TokenStyleManager( Class token_types_interface
67 , String tokenstyle_file
68 , MessageBox mbox
69 )
70 {
71 InputStream in = Tool.findFile( tokenstyle_file );
72
73 Properties name_to_style = new Properties();
74
75 Map type_to_style = new HashMap
76
77 if( in != null )
78 {
79 try
80 {
81 name_to_style.load( in );
82 mbox.info( new FileStatusMessage( FoundFileStatus.status
83 , tokenstyle_file
84 )
85 );
86 }
87 catch( IOException e )
88 {
89 mbox.error( new IOErrorMessage( new File( tokenstyle_file ) ) );
90 }
91
92 Enumeration name_enum = name_to_style.propertyNames();
93 int max = 0;
94
95 while( name_enum.hasMoreElements() )
96 {
97 String name = (String)name_enum.nextElement();
98 String style = name_to_style.getProperty( name );
99
100 if( style != null && style.length() > 0 )
101 {
102 try
103 {
104 Integer type = (Integer) token_types_interface
105 .getField( name )
106 .get( null );
107 type_to_style.put( type, style );
108 max = Math.max( max, type.intValue() );
109 }
110 catch( NoSuchFieldException fe )
111 {
112 mbox.error( new NameNotFoundMessage
113 ( new File( tokenstyle_file )
114 , null
115 , mbox
116 .getStringManager()
117 .getString( TokenStyleManager.class
118 , "message.token"
119 )
120 , name
121 )
122 );
123 }
124 catch( IllegalAccessException fe )
125 {
126 mbox.error( new NameNotFoundMessage
127 ( new File( tokenstyle_file )
128 , null
129 , mbox
130 .getStringManager()
131 .getString( TokenStyleManager.class
132 , "message.token"
133 )
134 , name
135 )
136 );
137 }
138 }
139 }
140
141 styles = new String[max+1];
142
143 Iterator type_enum = type_to_style.entrySet().iterator();
144
145 while( type_enum.hasNext() )
146 {
147 Map.Entry entry = (Map.Entry)type_enum.next();
148 Integer type = (Integer)entry.getKey();
149 String style = (String)entry.getValue();
150 styles[type.intValue()] = style;
151 }
152 }
153 }
154
155 /***
156 * Returns token style for given token type.
157 *
158 * @param type the token type.
159 * @return token style for this token type or <code>null</code>
160 * if style is not defined.
161 */
162 public String getStyle( int type )
163 {
164 if( styles != null && 0 <= type && type < styles.length )
165 {
166 return styles[type];
167 } else {
168 return null;
169 }
170 }
171 }