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
|
/*******************************************************************************
* Copyright (c) 2004, 2007 Boeing.
* 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:
* Boeing - initial API and implementation
*******************************************************************************/
package org.eclipse.osee.disposition.rest.internal.importer;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.osee.framework.jdk.core.util.io.xml.AbstractSaxHandler;
import org.xml.sax.Attributes;
/**
* XMLReader xmlReader = XMLReaderFactory.createXMLReader(); CollectionParser handler = new
* CollectionParser(collectors); xmlReader.setContentHandler(handler); xmlReader.parse(new InputSource(inputStream));
*
* @author Andrew M. Finkbeiner
*/
public class DispoSaxHandler extends AbstractSaxHandler {
Map<String, ElementHandlers> handlers;
public DispoSaxHandler() throws Exception {
handlers = new HashMap<>();
addHandlers(new TestPoint());
addHandlers(new Number());
addHandlers(new Result());
addHandlers(new Stacktrace());
addHandlers(new Actual());
addHandlers(new Expected());
addHandlers(new CheckGroup());
addHandlers(new Config());
addHandlers(new TestPointName());
addHandlers(new ScriptVersion());
addHandlers(new TestPointResults());
addHandlers(new Time());
addHandlers(new TimeSummary());
}
@Override
public void endElementFound(String uri, String localName, String name) throws Exception {
ElementHandlers handler;
handler = handlers.get("*");
if (handler != null) {
handler.endElementFound(uri, localName, name, stripCData(getContents().trim()));
}
handler = handlers.get(name);
if (handler != null) {
handler.endElementFound(uri, localName, name, stripCData(getContents().trim()));
}
}
private String stripCData(String content) {
if (content.startsWith("<![CDATA[")) {
return content.subSequence(9, content.length() - 3).toString();
} else {
return content;
}
}
@Override
public void startElementFound(String uri, String localName, String name, Attributes attributes) throws Exception {
ElementHandlers handler;
handler = handlers.get("*");
if (handler != null) {
handler.startElementFound(uri, localName, name, attributes);
}
handler = handlers.get(name);
if (handler != null) {
handler.startElementFound(uri, localName, name, attributes);
}
}
public void addHandlers(ElementHandlers handler) throws Exception {
Object obj = handlers.put(handler.getElementName(), handler);
if (obj != null) {
throw new Exception("Duplicate handler.");
}
}
public ElementHandlers getHandler(String elementName) {
return handlers.get(elementName);
}
}
|