comparison visualizer/Data/src/com/sun/hotspot/igv/data/serialization/XMLParser.java @ 4512:015fb895586b

Moved visualizer to new directory.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Tue, 07 Feb 2012 22:41:09 +0100
parents
children
comparison
equal deleted inserted replaced
4511:6cb549627941 4512:015fb895586b
1 /*
2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 package com.sun.hotspot.igv.data.serialization;
25
26 import com.sun.hotspot.igv.data.Properties;
27 import java.util.HashMap;
28 import java.util.Stack;
29 import org.xml.sax.Attributes;
30 import org.xml.sax.ContentHandler;
31 import org.xml.sax.Locator;
32 import org.xml.sax.SAXException;
33
34 /**
35 *
36 * @author Thomas Wuerthinger
37 */
38 public class XMLParser implements ContentHandler {
39
40 public static interface ParseMonitor {
41
42 public void setProgress(double d);
43
44 public void setState(String state);
45 }
46
47 public static class MissingAttributeException extends SAXException {
48
49 private String name;
50
51 public MissingAttributeException(String name) {
52 super("Missing attribute \"" + name + "\"");
53 this.name = name;
54 }
55
56 public String getAttributeName() {
57 return this.getMessage();
58 }
59 }
60
61 public static class HandoverElementHandler<P> extends ElementHandler<P, P> {
62
63 @Override
64 protected P start() throws SAXException {
65 return getParentObject();
66 }
67
68 public HandoverElementHandler(String name) {
69 super(name);
70 }
71
72 public HandoverElementHandler(String name, boolean needsText) {
73 super(name, needsText);
74 }
75 }
76
77 public static class TopElementHandler<P> extends ElementHandler<P, Object> {
78
79 public TopElementHandler() {
80 super(null);
81 }
82 }
83
84 public static class ElementHandler<T, P> {
85
86 private String name;
87 private Stack<T> object = new Stack<>();
88 private Attributes attr;
89 private StringBuilder currentText;
90 private ParseMonitor monitor;
91 private HashMap<String, ElementHandler<?, ? super T>> hashtable;
92 private boolean needsText;
93 private Stack<ElementHandler<P, ?>> parentElement = new Stack<>();
94 private Stack<P> parentObject = new Stack<>();
95
96 public ElementHandler(String name) {
97 this(name, false);
98 }
99
100 public ElementHandler<P, ?> getParentElement() {
101 return parentElement.peek();
102 }
103
104 public P getParentObject() {
105 return parentObject.peek();
106 }
107
108 protected boolean needsText() {
109 return needsText;
110 }
111
112 public ElementHandler(String name, boolean needsText) {
113 this.hashtable = new HashMap<>();
114 this.name = name;
115 this.needsText = needsText;
116 }
117
118 public ParseMonitor getMonitor() {
119 return monitor;
120 }
121
122 public ElementHandler<?, ? super T> getChild(String name) {
123 return hashtable.get(name);
124 }
125
126 public void addChild(ElementHandler<?, ? super T> handler) {
127 assert handler != null;
128 hashtable.put(handler.getName(), handler);
129 }
130
131 public String getName() {
132 return name;
133 }
134
135 public T getObject() {
136 return object.size() == 0 ? null : object.peek();
137 }
138
139 public String readAttribute(String name) {
140 return attr.getValue(name);
141 }
142
143 public String readRequiredAttribute(String name) throws SAXException {
144 String s = readAttribute(name);
145 if (s == null) {
146 throw new MissingAttributeException(name);
147 }
148 return s;
149 }
150
151 public void processAttributesAsProperties(Properties p) {
152 int length = attr.getLength();
153 for (int i = 0; i < length; i++) {
154 String val = attr.getValue(i);
155 String localName = attr.getLocalName(i);
156 p.setProperty(val, localName);
157 }
158 }
159
160 public void startElement(ElementHandler<P, ?> parentElement, Attributes attr, ParseMonitor monitor) throws SAXException {
161 this.currentText = new StringBuilder();
162 this.attr = attr;
163 this.monitor = monitor;
164 this.parentElement.push(parentElement);
165 parentObject.push(parentElement.getObject());
166 object.push(start());
167 }
168
169 protected T start() throws SAXException {
170 return null;
171 }
172
173 protected void end(String text) throws SAXException {
174
175 }
176
177 public void endElement() throws SAXException {
178 end(currentText.toString());
179 object.pop();
180 parentElement.pop();
181 parentObject.pop();
182 }
183
184 protected void text(char[] c, int start, int length) {
185 assert currentText != null;
186 currentText.append(c, start, length);
187 }
188 }
189 private Stack<ElementHandler> stack;
190 private ParseMonitor monitor;
191
192 public XMLParser(TopElementHandler rootHandler, ParseMonitor monitor) {
193 this.stack = new Stack<>();
194 this.monitor = monitor;
195 this.stack.push(rootHandler);
196 }
197
198 @Override
199 public void setDocumentLocator(Locator locator) {
200 if (monitor != null) {
201 monitor.setState("Starting parsing");
202 }
203 }
204
205 @Override
206 public void startDocument() throws SAXException {
207 }
208
209 @Override
210 public void endDocument() throws SAXException {
211 }
212
213 @Override
214 public void startPrefixMapping(String prefix, String uri) throws SAXException {
215 }
216
217 @Override
218 public void endPrefixMapping(String prefix) throws SAXException {
219 }
220
221 @SuppressWarnings("unchecked")
222 @Override
223 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
224 assert !stack.isEmpty();
225
226 ElementHandler parent = stack.peek();
227 if (parent != null) {
228 ElementHandler child = parent.getChild(qName);
229 if (child != null) {
230 child.startElement(parent, atts, monitor);
231 stack.push(child);
232 return;
233 }
234 }
235
236 stack.push(null);
237 }
238
239 @Override
240 public void endElement(String uri, String localName, String qName) throws SAXException {
241 ElementHandler handler = stack.pop();
242 if (handler != null) {
243 handler.endElement();
244 }
245 }
246
247 @Override
248 public void characters(char[] ch, int start, int length) throws SAXException {
249
250 assert !stack.isEmpty();
251
252
253 ElementHandler top = stack.peek();
254 if (top != null && top.needsText()) {
255 top.text(ch, start, length);
256 }
257 }
258
259 @Override
260 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
261 }
262
263 @Override
264 public void processingInstruction(String target, String data) throws SAXException {
265 }
266
267 @Override
268 public void skippedEntity(String name) throws SAXException {
269 }
270 }