KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > util > conf > Populate


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/util/conf/Populate.java,v 1.7 2004/07/28 09:34:23 ib Exp $
3  * $Revision: 1.7 $
4  * $Date: 2004/07/28 09:34:23 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.util.conf;
25
26 import java.io.IOException JavaDoc;
27
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.ContentHandler JavaDoc;
30 import org.xml.sax.ErrorHandler JavaDoc;
31 import org.xml.sax.InputSource JavaDoc;
32 import org.xml.sax.Locator JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34 import org.xml.sax.SAXParseException JavaDoc;
35 import org.xml.sax.XMLReader JavaDoc;
36
37 /**
38  */

39 public class Populate implements ContentHandler JavaDoc, ErrorHandler JavaDoc {
40     
41     private Element _root;
42     private Element _current;
43     private Locator JavaDoc _locator;
44     
45     
46     public Element load(InputSource JavaDoc is, XMLReader JavaDoc parser)
47         throws SAXException JavaDoc, IOException JavaDoc, ConfigurationException {
48         parser.setContentHandler(this);
49         parser.parse(is);
50         return _root;
51     }
52     
53     
54     /**
55      * Called to reset this object so it can be used a second time.
56      */

57     public void reset() {
58         _root = null;
59     }
60     
61     
62     public void startDocument()
63         throws SAXParseException JavaDoc {
64         // Starting a new document with the same populate object. This object
65
// is not reusable with a reset.
66
if ( _root != null )
67             throw new SAXParseException JavaDoc(
68                 "Cannot start processing a new document without a reset",
69                 _locator );
70     }
71     
72     
73     public void endDocument()
74         throws SAXParseException JavaDoc {
75         // Not all elements have been closed, but end of document has been
76
// reached. This should never happen.
77
if ( _current != null )
78             throw new SAXParseException JavaDoc(
79                 "Not all elements have been closed at end of document.",
80                 _locator);
81     }
82     
83     
84     public void startElement(String JavaDoc namespaceURI,String JavaDoc localName,
85                              String JavaDoc qName, Attributes JavaDoc attr ) {
86         int i;
87         Element parent;
88         
89         // If this is the root element, create the first element, else
90
// create a child for the current element and process it.
91
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         // Set the element's name and the attributes one by one, so the
100
// object tree will be a reflection of the XML document.
101
_current.setName( qName );
102         for ( i = 0 ; i < attr.getLength() ; ++i )
103             _current.setAttribute( attr.getQName( i ), attr.getValue( i ) );
104     }
105     
106     
107     /**
108      * Closing the element. This is the place to check special conditions about
109      * the element content. If not, just make the parent element the current
110      * element.
111      */

112     public void endElement(String JavaDoc namespaceURI,String JavaDoc localName,String JavaDoc qName)
113     throws SAXParseException JavaDoc {
114         // Attempt to close an element when the root element has already been
115
// closed. Should never happen.
116
if ( _current == null )
117             throw new SAXParseException JavaDoc( "Attempt to close the element " +
118                 qName + " when root element is already closed.", _locator );
119         // Attempt to close one element when a different element is open and
120
// waiting to be closed. Should never happen.
121
if ( ! _current.getName().equals( qName ) )
122             throw new SAXParseException JavaDoc( "Attempt to close the element " +
123                 qName + " when the element " + _current.getName() +
124                 " should be closed.", _locator );
125         
126         // All we have to do is go back to the parent.
127
_current = _current.getParent();
128     }
129     
130     
131     /**
132      * Called when there is character data (text) inside an element. Will
133      * accumulate all the character data and store it inside the object data.
134      */

135     public void characters( char[] ch, int start, int length )
136         throws SAXParseException JavaDoc {
137         Object JavaDoc data;
138         StringBuffer JavaDoc buf;
139         
140         if ( ch == null || length == 0 ) return;
141         // Attempt to place character before or after the root element.
142
// Should never happen.
143
if ( _current == null )
144             throw new SAXParseException JavaDoc(
145                 "Attempt to place character before or after the root element.",
146                 _locator );
147
148         // I am assuming that initially data may be just an empty string, or a
149
// null. I am assuming that multiple calls to character can occur
150
// inside the element and that all the character data should be
151
// collected as one.
152
data = _current.getData();
153         if ( data == null || ! ( data instanceof String JavaDoc ) ||
154             ( (String JavaDoc) data ).length() == 0 )
155             _current.setData( new String JavaDoc( ch, start, length ) );
156         else {
157             buf = new StringBuffer JavaDoc( (String JavaDoc) data );
158             buf.append( ch, start, length );
159             _current.setData( buf.toString() );
160         }
161     }
162      
163     
164     /**
165      * Ignoreable whitespace is just space inside an element that only contains
166      * other elements. We can safely ignore it.
167      */

168     public void ignorableWhitespace( char[] ch, int start, int length ) {
169     }
170     
171     
172     public void processingInstruction( String JavaDoc target, String JavaDoc pi ) {
173     }
174     
175     
176     public void startPrefixMapping(java.lang.String JavaDoc prefix,
177                                    java.lang.String JavaDoc uri)
178         throws SAXException JavaDoc {
179     }
180     
181     
182     public void endPrefixMapping(java.lang.String JavaDoc prefix)
183         throws SAXException JavaDoc {
184     }
185     
186     
187     public void skippedEntity(String JavaDoc name)
188         throws SAXException JavaDoc {
189     }
190     
191     
192     /**
193      * We can store the locate just to know where an error occurs.
194      */

195     public void setDocumentLocator( Locator JavaDoc locator ) {
196         _locator = locator;
197     }
198     
199     
200     public void error( SAXParseException JavaDoc except ) {
201         System.out.println( except.getMessage() );
202     }
203     
204     
205     public void fatalError( SAXParseException JavaDoc except ) {
206         System.out.println( except.getMessage() );
207     }
208     
209     
210     public void warning( SAXParseException JavaDoc except ) {
211         System.out.println( except.getMessage() );
212     }
213     
214     
215 }
216
217
Popular Tags