View Javadoc
1   package de.surethingies.properties.io;
2   
3   import org.xml.sax.Attributes;
4   import org.xml.sax.helpers.DefaultHandler;
5   
6   import de.surethingies.properties.ParameterException;
7   import de.surethingies.properties.plain.Configuration;
8   import de.surethingies.properties.plain.Entry;
9   import de.surethingies.properties.plain.Setting;
10  
11  public class ConfigurationParser extends DefaultHandler {
12      
13      private Configuration conf = new Configuration();
14      private Setting currentSetting = null;
15      
16      @Override
17      public void startElement(String uri, String localName, String qName, Attributes attributes) {
18  /*            System.out.println("startElement" 
19                  + " (String uri=" + uri 
20                  + ", String localName=" + localName 
21                  + ", String qName=" + uri
22                  + ", Attributes attributes=" + attributes
23                  + ")");
24  */        
25          switch (Elements.valueOf(localName.toUpperCase())) {
26          case CONFIGURATION:
27              conf.version = attributes.getValue(uri, "version");
28              break;
29  
30          case SETTING:
31              currentSetting = new Setting();                                    
32              currentSetting.id = attributes.getValue(uri, "id");
33              currentSetting.group = attributes.getValue(uri, "group");
34              conf.settings.add(currentSetting);
35  
36              break;
37              
38          case ENTRIES:
39              // Ignore
40              break;
41              
42          case ENTRY:
43              Entry entry = new Entry();
44              entry.key = attributes.getValue(uri, "key");
45              entry.value = attributes.getValue(uri, "value");
46              
47              currentSetting.entries.add (entry);
48              break;
49              
50          default:
51              throw new ParameterException ("Unknown Parameter " + localName);
52                      
53          }
54      }
55      
56      public Configuration getConfiguration() {
57          return conf;
58      }
59  }