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.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileNotFoundException;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  /**
29   * @author <a href="mailto:ivalo@iki.fi">Markku Saarela</a>
30   *
31   */
32  public class SyntaxHighlightings
33  {
34  
35      private Map syntaxHighlightningFiles;
36      
37      public SyntaxHighlightings( String syntaxHighlightnings )
38      {
39          super();
40          
41          this.syntaxHighlightningFiles = new HashMap();
42          
43          String[] strings = syntaxHighlightnings.split( "," );
44          
45          for ( int i = 0; i < strings.length; i++ )
46          {
47              String[] highLight = strings[i].trim().split( "=" );
48              
49              addHighLight( highLight[0], highLight[1] );
50          }
51          
52      }
53      
54      public SyntaxHighlighting getSyntaxHighlightning( String fileSuffix )
55      {
56  
57          return ( SyntaxHighlighting ) 
58              this.syntaxHighlightningFiles.get( fileSuffix );
59      }
60  
61      private void addHighLight( String fileSuffix, String fileName )
62      {
63  
64          Properties properties = getProperties( fileName );
65          
66          this.syntaxHighlightningFiles.put( fileSuffix
67                  , new SyntaxHighlighting( fileName, properties ) );
68          
69      }
70      
71      private Properties getProperties( String fileName )
72      {
73          
74          InputStream is = null;
75          
76          if ( fileName.startsWith( "/" ) )
77          {
78              is = this.getClass().getResourceAsStream( fileName );
79          }
80          else
81          {
82              File file = new File(fileName);
83              
84              try
85              {
86                  is = new FileInputStream(file);
87              } 
88              catch ( FileNotFoundException e )
89              {
90                  // TODO Auto-generated catch block
91                  e.printStackTrace();
92              }
93          }
94          
95          Properties props = new Properties();
96  
97          if ( is != null ) {
98              try
99              {
100                 props.load( is );
101             } catch ( IOException e )
102             {
103                 // TODO Auto-generated catch block
104                 e.printStackTrace();
105             }
106         }
107           
108         return props;
109     }
110 
111     public String toString()
112     {
113 
114         StringBuffer sb = new StringBuffer();
115         
116         for ( Iterator iter = 
117             this.syntaxHighlightningFiles.entrySet().iterator()
118             ; iter.hasNext(); )
119         {
120             Map.Entry element = ( Map.Entry ) iter.next();
121             
122             sb.append( element.getKey() );
123             sb.append( "=" );
124             sb.append( element.getValue() );
125             if ( iter.hasNext() )
126             {
127                 sb.append( "," );
128             }
129         }
130         
131         return sb.toString();
132     }
133 }