1 23 24 package org.apache.slide.util.conf; 25 26 import java.io.IOException ; 27 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.ContentHandler ; 30 import org.xml.sax.ErrorHandler ; 31 import org.xml.sax.InputSource ; 32 import org.xml.sax.Locator ; 33 import org.xml.sax.SAXException ; 34 import org.xml.sax.SAXParseException ; 35 import org.xml.sax.XMLReader ; 36 37 39 public class Populate implements ContentHandler , ErrorHandler { 40 41 private Element _root; 42 private Element _current; 43 private Locator _locator; 44 45 46 public Element load(InputSource is, XMLReader parser) 47 throws SAXException , IOException , ConfigurationException { 48 parser.setContentHandler(this); 49 parser.parse(is); 50 return _root; 51 } 52 53 54 57 public void reset() { 58 _root = null; 59 } 60 61 62 public void startDocument() 63 throws SAXParseException { 64 if ( _root != null ) 67 throw new SAXParseException ( 68 "Cannot start processing a new document without a reset", 69 _locator ); 70 } 71 72 73 public void endDocument() 74 throws SAXParseException { 75 if ( _current != null ) 78 throw new SAXParseException ( 79 "Not all elements have been closed at end of document.", 80 _locator); 81 } 82 83 84 public void startElement(String namespaceURI,String localName, 85 String qName, Attributes attr ) { 86 int i; 87 Element parent; 88 89 if ( _current == null ) { 92 _current = new Element( qName, null ); 93 _root = _current; 94 } else { 95 parent = _current; 96 _current = new Element( qName, parent ); 97 parent.addChild( _current ); 98 } 99 _current.setName( qName ); 102 for ( i = 0 ; i < attr.getLength() ; ++i ) 103 _current.setAttribute( attr.getQName( i ), attr.getValue( i ) ); 104 } 105 106 107 112 public void endElement(String namespaceURI,String localName,String qName) 113 throws SAXParseException { 114 if ( _current == null ) 117 throw new SAXParseException ( "Attempt to close the element " + 118 qName + " when root element is already closed.", _locator ); 119 if ( ! _current.getName().equals( qName ) ) 122 throw new SAXParseException ( "Attempt to close the element " + 123 qName + " when the element " + _current.getName() + 124 " should be closed.", _locator ); 125 126 _current = _current.getParent(); 128 } 129 130 131 135 public void characters( char[] ch, int start, int length ) 136 throws SAXParseException { 137 Object data; 138 StringBuffer buf; 139 140 if ( ch == null || length == 0 ) return; 141 if ( _current == null ) 144 throw new SAXParseException ( 145 "Attempt to place character before or after the root element.", 146 _locator ); 147 148 data = _current.getData(); 153 if ( data == null || ! ( data instanceof String ) || 154 ( (String ) data ).length() == 0 ) 155 _current.setData( new String ( ch, start, length ) ); 156 else { 157 buf = new StringBuffer ( (String ) data ); 158 buf.append( ch, start, length ); 159 _current.setData( buf.toString() ); 160 } 161 } 162 163 164 168 public void ignorableWhitespace( char[] ch, int start, int length ) { 169 } 170 171 172 public void processingInstruction( String target, String pi ) { 173 } 174 175 176 public void startPrefixMapping(java.lang.String prefix, 177 java.lang.String uri) 178 throws SAXException { 179 } 180 181 182 public void endPrefixMapping(java.lang.String prefix) 183 throws SAXException { 184 } 185 186 187 public void skippedEntity(String name) 188 throws SAXException { 189 } 190 191 192 195 public void setDocumentLocator( Locator locator ) { 196 _locator = locator; 197 } 198 199 200 public void error( SAXParseException except ) { 201 System.out.println( except.getMessage() ); 202 } 203 204 205 public void fatalError( SAXParseException except ) { 206 System.out.println( except.getMessage() ); 207 } 208 209 210 public void warning( SAXParseException except ) { 211 System.out.println( except.getMessage() ); 212 } 213 214 215 } 216 217 | Popular Tags |