View Javadoc

1   /*
2    * Copyright 2006 Markku Saarela 
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, 
11   * software distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. 
14   * See the License for the specific language governing permissions and limitations under the License. 
15   */
16  package net.sourceforge.jivalo.editor.syntax;
17  
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  import java.util.Properties;
22  
23  /**
24   * @author <a href="mailto:ivalo@iki.fi">Markku Saarela</a>
25   *
26   */
27  public final class SyntaxHighlighting
28  {
29  
30      private static final String STRING_HIGHLIGHTING = "highligh.string";
31      private static final String COMMENTLINE_HIGHLIGHTING = "commentline.string";
32      
33      private String fileName;
34      
35      private Map highLightings;
36      
37      private JIvaloStyle[] stringHighLighting;
38  
39      private JIvaloStyle[] commentLineHighLighting;
40  
41      public SyntaxHighlighting( String fileName, Properties properties )
42      {
43          super();
44          
45          this.fileName = fileName;
46          this.highLightings = new HashMap();
47          
48          for ( Iterator iter =  properties.entrySet().iterator()
49                  ; iter.hasNext(); )
50          {
51              Map.Entry element = ( Map.Entry ) iter.next();
52              
53              String key = ( String ) element.getKey();
54                         
55              JIvaloStyle[] styles = get( (String)element.getValue() );
56              
57              if ( key.equals( STRING_HIGHLIGHTING ) )
58              {
59                  this.stringHighLighting = styles;
60              } 
61              else if ( key.equals( COMMENTLINE_HIGHLIGHTING ) )
62              {
63                  this.commentLineHighLighting = styles;
64              } 
65              else
66              {
67                  this.highLightings.put( key, styles );
68              }
69          }
70      }
71      
72      public JIvaloStyle[] getStyles( String word )
73      {
74          
75          return ( JIvaloStyle[] ) this.highLightings.get( word );
76      }
77  
78      public JIvaloStyle[] getStringHighLighting()
79      {
80          return this.stringHighLighting;
81      }
82  
83      /**
84       * @return the commentLineHighLighting
85       */
86      public JIvaloStyle[] getCommentLineHighLighting()
87      {
88          return commentLineHighLighting;
89      }
90  
91      private JIvaloStyle[] get( String string )
92      {
93  
94          String[] values = string.split( "," );
95          
96          JIvaloStyle[] styles = new JIvaloStyle[values.length];
97          
98          for ( int i = 0; i < values.length; i++ )
99          {
100             JIvaloStyleType styleType = null;
101             
102             String value = null;
103             
104             if ( i == 0 )
105             {
106                 styleType = JIvaloStyleType.FOREGROUND;
107                 value = values[ i ];
108             }
109             else
110             {
111                 styleType = JIvaloStyleType.valueOf( values[ i ] );
112             }
113             styles[i] = JIvaloStyleFactory.getInstance(styleType, value );
114         }
115         
116         return styles;
117     }
118 
119     public String toString()
120     {
121         return this.fileName;
122     }
123 
124 }