001: package com.anthonyeden.lib.config.sax;
002:
003: import com.anthonyeden.lib.config.Configuration;
004: import com.anthonyeden.lib.config.ConfigurationBase;
005: import com.anthonyeden.lib.config.Location;
006: import com.anthonyeden.lib.config.MutableConfiguration;
007: import org.xml.sax.Attributes;
008: import org.xml.sax.Locator;
009: import org.xml.sax.helpers.DefaultHandler;
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: import java.util.LinkedList;
014:
015: /**
016: * Handler implementation
017: *
018: * @author Anthony Eden
019: * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
020: * @since 2.0
021: */
022:
023: class SAXConfigurationHandler extends DefaultHandler {
024: protected static final Log log = LogFactory
025: .getLog(SAXConfigurationHandler.class);
026: private SAXConfigurationFactory factory;
027: private String id;
028:
029: private Locator locator;
030:
031: private LinkedList configurationStack = new LinkedList();
032: private LinkedList valueStack = new LinkedList();
033:
034: private Configuration rootConfiguration;
035:
036: private MutableConfiguration parentConfiguration;
037: private MutableConfiguration currentConfiguration;
038: private StringBuffer currentValue;
039:
040: /**
041: * Construct a new SAXConfigurationHandler with the given root configuration
042: * object.
043: *
044: * @param factory
045: * @param id
046: */
047:
048: SAXConfigurationHandler(SAXConfigurationFactory factory, String id) {
049: this .factory = factory;
050: this .id = id;
051: }
052:
053: public Configuration getConfiguration() {
054: return rootConfiguration;
055: }
056:
057: public void setDocumentLocator(Locator locator) {
058: this .locator = locator;
059: }
060:
061: public void characters(char[] ch, int start, int length) {
062: currentValue.append(ch, start, length);
063: }
064:
065: public void startElement(String uri, String localName,
066: String qName, Attributes attributes) {
067: //System.out.println("Start element: " + qName);
068:
069: if (currentValue != null) {
070: valueStack.add(currentValue);
071: }
072:
073: if (currentConfiguration != null) {
074: configurationStack.add(currentConfiguration);
075: parentConfiguration = currentConfiguration;
076: }
077:
078: Location location = new Location(id, locator.getLineNumber(),
079: locator.getColumnNumber());
080: currentConfiguration = new ConfigurationBase(qName, null,
081: parentConfiguration, location);
082: if (parentConfiguration != null) {
083: parentConfiguration.addChild(currentConfiguration);
084: }
085:
086: //System.out.println("Attributes length: " + attributes.getLength());
087: for (int i = 0; i < attributes.getLength(); i++) {
088: String value = attributes.getValue(i);
089: String name = attributes.getQName(i);
090: currentConfiguration.addAttribute(name, value);
091: }
092:
093: currentValue = new StringBuffer();
094: }
095:
096: public void endElement(String uri, String localName, String qName) {
097: //System.out.println("End element: " + qName);
098: //System.out.println("Setting the value of " +
099: // currentConfiguration.getName() + " to " + currentValue);
100:
101: currentConfiguration.setValue(currentValue.toString());
102:
103: if (configurationStack.size() > 0) {
104: currentConfiguration = (MutableConfiguration) configurationStack
105: .removeLast();
106: } else {
107: rootConfiguration = currentConfiguration;
108: }
109:
110: if (valueStack.size() > 0) {
111: currentValue = (StringBuffer) valueStack.removeLast();
112: }
113: }
114:
115: public SAXConfigurationFactory getFactory() {
116: return factory;
117: }
118:
119: }
|