Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2005 The Apache Software Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame^] | 17 | package org.apache.felix.bundlerepository.impl.kxmlsax; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.io.Reader; |
| 21 | import java.util.Properties; |
| 22 | |
| 23 | import org.kxml.Attribute; |
| 24 | import org.kxml.Xml; |
| 25 | import org.kxml.parser.ParseEvent; |
| 26 | import org.kxml.parser.XmlParser; |
| 27 | |
| 28 | /** |
| 29 | * The KXmlSAXParser extends the XmlParser from kxml. This is a very |
| 30 | * simple parser that does not take into account the DTD |
| 31 | * |
| 32 | * @version 1.0 08 Nov 2002 |
| 33 | * @version 1.1 24 Apr 2004 |
| 34 | * @author Humberto Cervantes, Didier Donsez |
| 35 | */ |
| 36 | public class KXmlSAXParser extends XmlParser { |
| 37 | /** |
| 38 | * The constructor for a parser, it receives a java.io.Reader. |
| 39 | * |
| 40 | * @param reader The reader |
| 41 | * @exception IOException thrown by the superclass |
| 42 | */ |
| 43 | public KXmlSAXParser(Reader r) throws IOException { |
| 44 | super(r); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Parser from the reader provided in the constructor, and call |
| 49 | * the startElement and endElement in a KxmlHandler |
| 50 | * |
| 51 | * @param reader The reader |
| 52 | * @exception Exception thrown by the superclass |
| 53 | */ |
| 54 | public void parseXML(KXmlSAXHandler handler) throws Exception { |
| 55 | ParseEvent evt = null; |
| 56 | do { |
| 57 | evt = read(); |
| 58 | if (evt.getType() == Xml.START_TAG) { |
| 59 | Properties props = new Properties(); |
| 60 | for (int i = 0; i < evt.getAttributeCount(); i++) { |
| 61 | Attribute attr = evt.getAttribute(i); |
| 62 | props.put(attr.getName(), attr.getValue()); |
| 63 | } |
| 64 | handler.startElement( |
| 65 | "uri", |
| 66 | evt.getName(), |
| 67 | evt.getName(), |
| 68 | props); |
| 69 | } else if (evt.getType() == Xml.END_TAG) { |
| 70 | handler.endElement("uri", evt.getName(), evt.getName()); |
| 71 | } else if (evt.getType() == Xml.TEXT) { |
| 72 | String text = evt.getText(); |
| 73 | handler.characters(text.toCharArray(),0,text.length()); |
| 74 | } else { |
| 75 | // do nothing |
| 76 | } |
| 77 | } while (evt.getType() != Xml.END_DOCUMENT); |
| 78 | } |
| 79 | } |