Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0a371fccb61d61c44665dc99397b5feb955457c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.ds.parser;

import java.util.List;
import org.eclipse.equinox.ds.Log;
import org.eclipse.equinox.ds.model.ComponentDescription;
import org.osgi.framework.BundleContext;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

/**
 * ParserHandler implements the methods for the DefaultHandler of the SAX
 * parser. Each Service Component bundle contains a set of xml files which are
 * parsed.
 * 
 * @version $Revision: 1.2 $
 */

class ParserHandler extends DefaultHandler {

	/* set this to true to compile in debug messages */
	private static final boolean DEBUG = false;

	private DefaultHandler handler;
	private List components;
	protected BundleContext bundleContext;
	private int depth;
	private boolean error;

	ParserHandler(BundleContext bundleContext, List components) {
		this.bundleContext = bundleContext;
		this.components = components;
	}

	public void setHandler(DefaultHandler handler) {
		this.handler = handler;
	}

	public void addComponentDescription(ComponentDescription component) {
		components.add(component);
	}

	public void setError(boolean error) {
		this.error = error;
	}

	public boolean isError() {
		return error;
	}

	public void logError(String msg) {
		error = true;
		Log.log(1, "[SCR] Parser Error. ", new SAXException(msg));
	}

	public void startDocument() throws SAXException {
		handler = this;
		depth = 0;
	}

	public void startPrefixMapping(String prefix, String uri) throws SAXException {

		if (DEBUG) {
			System.out.println("[startPrefixMapping:prefix]" + prefix);
			System.out.println("[startPrefixMapping:uri]" + uri);
		}
	}

	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		depth++;

		if (DEBUG) {
			System.out.println("[startElement:begin]");
			System.out.println(" [uri]" + uri);
			System.out.println(" [localName]" + localName);
			System.out.println(" [qName]" + qName);

			int size = attributes.getLength();
			for (int i = 0; i < size; i++) {
				String key = attributes.getQName(i);
				String value = attributes.getValue(i);
				System.out.println(" [attr:" + i + ":localName]" + attributes.getLocalName(i));
				System.out.println(" [attr:" + i + ":qName]" + attributes.getQName(i));
				System.out.println(" [attr:" + i + ":type]" + attributes.getType(i));
				System.out.println(" [attr:" + i + ":URI]" + attributes.getURI(i));
				System.out.println(" [attr:" + i + ":value]" + attributes.getValue(i));
			}
			System.out.println("[startElement:end]");
		}

		if (handler != this) {
			handler.startElement(uri, localName, qName, attributes);
			return;
		}

		if (localName.equals(ParserConstants.COMPONENT_ELEMENT)) {
			if (((depth == 1) && (uri.length() == 0)) || uri.equals(ParserConstants.SCR_NAMESPACE)) {
				setHandler(new ComponentElement(this, attributes));
			}
		}
	}

	public void characters(char[] ch, int start, int length) throws SAXException {

		if (DEBUG) {
			System.out.print("[characters:begin]");
			System.out.print(new String(ch, start, length));
			System.out.println("[characters:end]");
		}

		if (handler != this) {
			handler.characters(ch, start, length);
		}
	}

	public void endElement(String uri, String localName, String qName) throws SAXException {

		if (DEBUG) {
			System.out.println("[endElement:begin]");
			System.out.println(" [uri]" + uri);
			System.out.println(" [localName]" + localName);
			System.out.println(" [qName]" + qName);
			System.out.println("[endElement:end]");
		}

		if (handler != this) {
			handler.endElement(uri, localName, qName);
		}

		depth--;
	}

	public void endPrefixMapping(String prefix) throws SAXException {
		if (DEBUG) {
			System.out.println("[endPrefixMapping:prefix]" + prefix);
		}
	}

	public void endDocument() throws SAXException {
		if (DEBUG) {
			System.out.println("[endDocument]");
		}
	}

	public void warning(SAXParseException e) throws SAXException {
		if (DEBUG) {
			System.out.println("[warning]");
			e.printStackTrace();
		}
	}

	public void error(SAXParseException e) throws SAXException {
		if (DEBUG) {
			System.out.println("[error]");
			e.printStackTrace();
		}
	}

	public void fatalError(SAXParseException e) throws SAXException {
		if (DEBUG) {
			System.out.println("[fatalError]");
			e.printStackTrace();
		}
		throw e;
	}
}

Back to the top