|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.jconfig.handler; |
|
8 |
| |
|
9 |
| import java.io.BufferedReader; |
|
10 |
| import java.io.IOException; |
|
11 |
| import java.io.InputStream; |
|
12 |
| import java.io.InputStreamReader; |
|
13 |
| import java.util.Vector; |
|
14 |
| |
|
15 |
| import org.jconfig.Category; |
|
16 |
| import org.jconfig.Configuration; |
|
17 |
| import org.jconfig.ConfigurationManagerException; |
|
18 |
| import org.jconfig.DefaultCategory; |
|
19 |
| import org.jconfig.ExtensibleConfiguration; |
|
20 |
| import org.jconfig.error.ErrorReporter; |
|
21 |
| import org.jconfig.utils.ResourceLocator; |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| public class ScriptHandler implements ConfigurationHandler { |
|
27 |
| |
|
28 |
2
| public ScriptHandler() {
|
|
29 |
| } |
|
30 |
| |
|
31 |
2
| public Configuration load(String configurationName) throws ConfigurationManagerException {
|
|
32 |
2
| String fileName = configurationName+"_config.script";
|
|
33 |
2
| try {
|
|
34 |
2
| ResourceLocator locator = new ResourceLocator(fileName);
|
|
35 |
2
| InputStream is = locator.findResource(fileName);
|
|
36 |
2
| BufferedReader nis = new BufferedReader(new InputStreamReader(is));
|
|
37 |
2
| String line;
|
|
38 |
2
| Vector content = new Vector();
|
|
39 |
?
| while ((line=nis.readLine()) != null ) {
|
|
40 |
24
| content.add(line);
|
|
41 |
| } |
|
42 |
2
| nis.close();
|
|
43 |
2
| int braces = 0;
|
|
44 |
2
| for ( int i = 0; i < content.size();i++) {
|
|
45 |
24
| String tmp = (String)content.get(i);
|
|
46 |
24
| if ( tmp.indexOf("{") != -1 ) {
|
|
47 |
7
| braces++;
|
|
48 |
| } |
|
49 |
17
| else if ( tmp.indexOf("}") != -1 ) {
|
|
50 |
6
| braces--;
|
|
51 |
| } |
|
52 |
| } |
|
53 |
2
| if ( braces != 0 ) {
|
|
54 |
1
| ErrorReporter.getErrorHandler().reportError("Unmatched brackets in script file");
|
|
55 |
1
| throw new ConfigurationManagerException("Unmatched brackets in script file");
|
|
56 |
| } |
|
57 |
1
| Configuration cfg = new ExtensibleConfiguration(configurationName);
|
|
58 |
1
| boolean process = false;
|
|
59 |
1
| String currentCategoryName = null;
|
|
60 |
1
| for ( int i = 0; i < content.size();i++) {
|
|
61 |
15
| String tmp = (String)content.get(i);
|
|
62 |
15
| tmp = tmp.trim();
|
|
63 |
15
| if ( process ) {
|
|
64 |
8
| if ( tmp.startsWith("}") ) {
|
|
65 |
3
| process = false;
|
|
66 |
| } |
|
67 |
| else { |
|
68 |
5
| String left = tmp.substring(0,tmp.indexOf("=")-1);
|
|
69 |
5
| left = left.trim();
|
|
70 |
5
| String right = tmp.substring(tmp.indexOf("=")+1);
|
|
71 |
5
| right = right.trim();
|
|
72 |
5
| cfg.setProperty(left,right,currentCategoryName);
|
|
73 |
| } |
|
74 |
| } |
|
75 |
15
| if ( tmp.startsWith("category") && tmp.indexOf("{") != -1 ) {
|
|
76 |
3
| process = true;
|
|
77 |
3
| String name = tmp.substring(9);
|
|
78 |
3
| name = name.substring(0,name.indexOf("{"));
|
|
79 |
3
| name = name.trim();
|
|
80 |
3
| String extend = null;
|
|
81 |
3
| if ( tmp.indexOf(" extends ") != -1 ) {
|
|
82 |
2
| extend = name.substring(name.indexOf(" extends ")+9);
|
|
83 |
2
| extend = extend.trim();
|
|
84 |
2
| name = name.substring(0,name.indexOf(" extends "));
|
|
85 |
| } |
|
86 |
3
| Category ec = new DefaultCategory(name);
|
|
87 |
3
| if ( extend != null ) {
|
|
88 |
2
| ec.setExtendsCategory(extend);
|
|
89 |
| } |
|
90 |
3
| cfg.setCategory(ec);
|
|
91 |
3
| currentCategoryName = name;
|
|
92 |
| } |
|
93 |
| } |
|
94 |
1
| return cfg;
|
|
95 |
| } |
|
96 |
| catch (IOException e) { |
|
97 |
0
| ErrorReporter.getErrorHandler().reportError("Error while trying to read file",e);
|
|
98 |
0
| throw new ConfigurationManagerException("Error while trying to read file");
|
|
99 |
| } |
|
100 |
| } |
|
101 |
| |
|
102 |
0
| public void store(Configuration configuration) throws ConfigurationManagerException {
|
|
103 |
| } |
|
104 |
| |
|
105 |
| } |