1   package com.atlassian.user.impl.osuser.config.xml;
2   
3   import com.atlassian.user.configuration.ConfigurationException;
4   import org.apache.log4j.Logger;
5   import org.xml.sax.Attributes;
6   import org.xml.sax.SAXException;
7   import org.xml.sax.helpers.DefaultHandler;
8   
9   import java.util.Properties;
10  
11  /**
12   * Used to parse osuser.xml files.
13   *
14   * An implementation of {@link DefaultHandler} taken from the original OSUUser implementation.
15   */
16  public class DefaultOSUConfigurationHandler extends DefaultHandler
17  {
18      private static final Logger log = Logger.getLogger(DefaultOSUConfigurationHandler.class);
19      private DefaultOSUConfigurationLoader configLoader;
20      private String currentClass;
21      private Properties currentProperties;
22  
23      public DefaultOSUConfigurationHandler(DefaultOSUConfigurationLoader configLoader)
24      {
25          this.configLoader = configLoader;
26      }
27  
28      /**
29       * SAX Handler implementation for handling tags in config file and building
30       * config objects.
31       */
32      private String _currentPropertyName;
33      private StringBuffer _currentPropertyValue;
34  
35      public void characters(char[] chars, int offset, int len) throws SAXException
36      {
37          if (_currentPropertyValue != null)
38          {
39              _currentPropertyValue.append(chars, offset, len);
40          }
41      }
42  
43      public void endElement(String uri, String localName, String qName) throws SAXException
44      {
45          if (qName.equals("provider"))
46          {
47              try
48              {
49                  configLoader.addProvider(currentClass, currentProperties);
50              }
51              catch (ConfigurationException e)
52              {
53                  log.error(e);
54              }
55              currentProperties = null;
56              currentClass = null;
57          }
58          else if (qName.equals("authenticator"))
59          {
60  //            configLoader.addAuthenticator(currentClass, currentProperties);
61              currentProperties = null;
62              currentClass = null;
63          }
64          else if (qName.equals("property"))
65          {
66              currentProperties.put(_currentPropertyName, _currentPropertyValue.toString());
67              _currentPropertyName = null;
68              _currentPropertyValue = null;
69          }
70      }
71  
72      public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
73      {
74          if (qName.equals("provider") || qName.equals("authenticator"))
75          {
76              currentClass = attributes.getValue("class");
77              currentProperties = new Properties();
78          }
79          else if (qName.equals("property"))
80          {
81              _currentPropertyName = attributes.getValue("name");
82              _currentPropertyValue = new StringBuffer();
83          }
84      }
85  }