1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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 }