View Javadoc

1   /*
2    * Copyright (c) 2001-2004,
3    * RedVerst Group, ISP RAS http://www.ispras.ru
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    *
9    * 1. Redistributions of source code must retain the above copyright notice, this
10   *    list of conditions and the following disclaimer.
11   *
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   *
16   * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17   *    may not be used to endorse or promote products derived from this software
18   *    without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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/*Integer,String*/();
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 }