1 55 package org.apache.avalon.framework.configuration; 56 57 import java.util.ArrayList ; 58 import java.util.BitSet ; 59 import org.xml.sax.Attributes ; 60 import org.xml.sax.ErrorHandler ; 61 import org.xml.sax.Locator ; 62 import org.xml.sax.SAXException ; 63 import org.xml.sax.SAXParseException ; 64 import org.xml.sax.helpers.DefaultHandler ; 65 66 72 public class SAXConfigurationHandler 73 extends DefaultHandler 74 implements ErrorHandler 75 { 76 80 private static final int EXPECTED_DEPTH = 5; 81 private final ArrayList m_elements = new ArrayList ( EXPECTED_DEPTH ); 82 private final ArrayList m_values = new ArrayList ( EXPECTED_DEPTH ); 83 87 private final BitSet m_preserveSpace = new BitSet (); 88 private Configuration m_configuration; 89 private Locator m_locator; 90 91 96 public Configuration getConfiguration() 97 { 98 return m_configuration; 99 } 100 101 104 public void clear() 105 { 106 m_elements.clear(); 107 m_values.clear(); 108 m_locator = null; 109 } 110 111 116 public void setDocumentLocator( final Locator locator ) 117 { 118 m_locator = locator; 119 } 120 121 129 public void characters( final char[] ch, int start, int end ) 130 throws SAXException 131 { 132 final int depth = m_values.size() - 1; 137 final StringBuffer valueBuffer = (StringBuffer )m_values.get( depth ); 138 valueBuffer.append( ch, start, end ); 139 } 140 141 149 public void endElement( final String namespaceURI, 150 final String localName, 151 final String rawName ) 152 throws SAXException 153 { 154 final int depth = m_elements.size() - 1; 155 final DefaultConfiguration finishedConfiguration = 156 (DefaultConfiguration)m_elements.remove( depth ); 157 final String accumulatedValue = 158 ( (StringBuffer )m_values.remove( depth ) ).toString(); 159 160 if( finishedConfiguration.getChildren().length == 0 ) 161 { 162 String finishedValue; 164 if( m_preserveSpace.get( depth ) ) 165 { 166 finishedValue = accumulatedValue; 167 } 168 else if( 0 == accumulatedValue.length() ) 169 { 170 finishedValue = null; 171 } 172 else 173 { 174 finishedValue = accumulatedValue.trim(); 175 } 176 finishedConfiguration.setValue( finishedValue ); 177 } 178 else 179 { 180 final String trimmedValue = accumulatedValue.trim(); 181 if( trimmedValue.length() > 0 ) 182 { 183 throw new SAXException ( "Not allowed to define mixed content in the " 184 + "element " + finishedConfiguration.getName() + " at " 185 + finishedConfiguration.getLocation() ); 186 } 187 } 188 189 if( 0 == depth ) 190 { 191 m_configuration = finishedConfiguration; 192 } 193 } 194 195 203 protected DefaultConfiguration createConfiguration( final String localName, 204 final String location ) 205 { 206 return new DefaultConfiguration( localName, location ); 207 } 208 209 218 public void startElement( final String namespaceURI, 219 final String localName, 220 final String rawName, 221 final Attributes attributes ) 222 throws SAXException 223 { 224 final DefaultConfiguration configuration = 225 createConfiguration( rawName, getLocationString() ); 226 final int depth = m_elements.size(); 229 boolean preserveSpace = false; 231 if( depth > 0 ) 232 { 233 final DefaultConfiguration parent = 234 (DefaultConfiguration)m_elements.get( depth - 1 ); 235 parent.addChild( configuration ); 236 preserveSpace = m_preserveSpace.get( depth - 1 ); 238 } 239 240 m_elements.add( configuration ); 241 m_values.add( new StringBuffer () ); 242 243 final int attributesSize = attributes.getLength(); 244 245 for( int i = 0; i < attributesSize; i++ ) 246 { 247 final String name = attributes.getQName( i ); 248 final String value = attributes.getValue( i ); 249 250 if( !name.equals( "xml:space" ) ) 251 { 252 configuration.setAttribute( name, value ); 253 } 254 else 255 { 256 preserveSpace = value.equals( "preserve" ); 257 } 258 } 259 260 if( preserveSpace ) 261 { 262 m_preserveSpace.set( depth ); 263 } 264 else 265 { 266 m_preserveSpace.clear( depth ); 267 } 268 } 269 270 275 public void error( final SAXParseException exception ) 276 throws SAXException 277 { 278 throw exception; 279 } 280 281 286 public void warning( final SAXParseException exception ) 287 throws SAXException 288 { 289 throw exception; 290 } 291 292 297 public void fatalError( final SAXParseException exception ) 298 throws SAXException 299 { 300 throw exception; 301 } 302 303 308 protected String getLocationString() 309 { 310 if( null == m_locator ) 311 { 312 return "Unknown"; 313 } 314 else 315 { 316 return 317 m_locator.getSystemId() + ":" 318 + m_locator.getLineNumber() + ":" 319 + m_locator.getColumnNumber(); 320 } 321 } 322 } 323 | Popular Tags |