-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariablesReader.java
More file actions
141 lines (123 loc) · 5.63 KB
/
VariablesReader.java
File metadata and controls
141 lines (123 loc) · 5.63 KB
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
/*
* Copyright (c) 2007-2008, debug-commons team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.rubyforge.debugcommons.reader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.rubyforge.debugcommons.Util;
import org.rubyforge.debugcommons.model.RubyVariableInfo;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
public final class VariablesReader extends XmlStreamReader {
private static final Logger LOGGER = Logger.getLogger(VariablesReader.class.getName());
private RubyVariableInfo[] variables;
public VariablesReader(XmlPullParser xpp) {
super(xpp);
}
private void parse() throws XmlPullParserException, IOException {
String element = xpp.getName();
assert element.equals("variables") || element.equals("processingException");
if (element.equals("variables")) {
parseVariables();
} else if (element.equals("processingException")) {
parseProcessingException();
} else {
assert false : "Unexpected element: " + element;
}
}
private void parseVariables() throws XmlPullParserException, IOException {
List<RubyVariableInfo> _variables = new ArrayList<RubyVariableInfo>();
while (!(nextEvent() == XmlPullParser.END_TAG && "variables".equals(xpp.getName()))) {
// Seems to happen on this place from time to time.
if (xpp.getName() == null) {
throw new XmlPullParserException("xpp.getName() returned 'null'. " +
"Segmentation fault. Bug in the Ruby interpreter/VM. " +
"Please provide possibly exact steps to reproduce " +
"and file a bug against Ruby or debug-commons tracker.");
}
ErrorReader.flushPossibleMessage(xpp);
/*
* Check for empty <variables>, e.g.:
* <variables>
* <message>
* </variables>
*/
if (Util.isEndTag(xpp, "variables")) {
break;
}
_variables.add(parseVariable());
}
this.variables = _variables.toArray(new RubyVariableInfo[_variables.size()]);
}
private RubyVariableInfo parseVariable() throws XmlPullParserException, IOException {
assert xpp.getName().equals("variable") : xpp.getName() + "(type: " + Util.getType(xpp) + ") encountered";
final String name = getAttributeValue("name");
String value = getAttributeValue("value");
final String kind = getAttributeValue("kind");
if (value == null) {
ensureEndTag("variable");
return new RubyVariableInfo(name, kind);
}
final String type = getAttributeValue("type");
final boolean hasChildren = getAttributeBoolValue("hasChildren");
final String objectId = getAttributeValue("objectId");
value = readValueFromElement(value);
ensureAtEndTag(xpp, "variable");
return new RubyVariableInfo(name, kind, value, type, hasChildren, objectId);
}
/**
* Tries to read value from {@code value} sub-element, if there is no sub-element default value will be returned.
* Note: xpp moved to the next event after sub-element.
*/
private String readValueFromElement(final String defaultValue) throws IOException, XmlPullParserException {
String value = defaultValue;
final int nextTag = xpp.next();
if (nextTag == XmlPullParser.START_TAG && "value".equals(xpp.getName())) {
xpp.next();
if (xpp.getEventType() == XmlPullParser.TEXT) {
value = xpp.getText();
xpp.next();
}
ensureAtEndTag(xpp, "value");
xpp.next();
}
return value;
}
private void parseProcessingException() throws XmlPullParserException, IOException {
LOGGER.severe("Processing exception occurred." +
" exceptionMessage: " + getAttributeValue("message") +
", exceptionType: " + getAttributeValue("type"));
ensureEndTag("processingException");
}
public static RubyVariableInfo[] readVariables(final XmlPullParser xpp)
throws IOException, XmlPullParserException {
VariablesReader reader = new VariablesReader(xpp);
reader.parse();
return reader.variables;
}
public static void logProcessingException(final XmlPullParser xpp)
throws IOException, XmlPullParserException {
VariablesReader reader = new VariablesReader(xpp);
reader.parse();
}
}