blob: 99a6ea3a9817c3ae2c7282c965db2303e670f834 [file] [log] [blame]
Humberto Cervantes Macedaa3664ba2006-04-21 16:34:36 +00001/*
2 * Copyright 2006 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 */
17package org.apache.felix.declarativeservices.scr;
18
19import java.io.Reader;
20import java.util.Properties;
21
22import org.kxml2.io.KXmlParser;
23import org.xmlpull.v1.XmlPullParser;
24import org.xmlpull.v1.XmlPullParserException;
25
26/**
27 * The KXml2SAXParser extends the XmlParser from kxml. This is a very
28 * simple parser that does not take into account the DTD
29 *
30 */
31public class KXml2SAXParser extends KXmlParser {
32
33 public String uri="uri";
34
35 private Reader reader;
36
37 /**
38 * The constructor for a parser, it receives a java.io.Reader.
39 *
40 * @param reader The reader
41 * @throws XmlPullParserException
42 */
43 public KXml2SAXParser(Reader reader) throws XmlPullParserException {
44 super();
45 this.reader=reader;
46 setInput(reader);
47 }
48
49 /**
50 * Parser from the reader provided in the constructor, and call
51 * the startElement and endElement in a KxmlHandler
52 *
53 * @param reader The reader
54 * @exception Exception thrown by the superclass
55 */
56 public void parseXML(KXml2SAXHandler handler) throws Exception {
57
58 while (next() != XmlPullParser.END_DOCUMENT) {
59 handler.setLineNumber(getLineNumber());
60 handler.setColumnNumber(getColumnNumber());
61 if (getEventType() == XmlPullParser.START_TAG) {
62 Properties props = new Properties();
63 for (int i = 0; i < getAttributeCount(); i++) {
64 props.put(getAttributeName(i), getAttributeValue(i));
65 }
66 handler.startElement(
67 getNamespace(),
68 getName(),
69 getName(),
70 props);
71 } else if (getEventType() == XmlPullParser.END_TAG) {
72 handler.endElement(getNamespace(), getName(), getName());
73 } else if (getEventType() == XmlPullParser.TEXT) {
74 String text = getText();
75 handler.characters(text.toCharArray(),0,text.length());
76 } else if (getEventType() == XmlPullParser.PROCESSING_INSTRUCTION) {
77 // TODO extract the target from the evt.getText()
78 handler.processingInstruction(null,getText());
79 } else {
80 // do nothing
81 }
82 }
83 }
84}