SvrlContentHandler.java 4.17 KB
Newer Older
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
/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.checks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import de.hft.stuttgart.citydoctor2.check.error.SchematronError;

/**
 * This is the content handler for parsing the Svrl xml files created by a
 * Schematron run. They are parsed and error objects are created and possibly
 * associated to GML-IDs.
 * 
 * @author Matthias Betz
 *
 */
public class SvrlContentHandler implements ContentHandler {

	private boolean nextElementIsErrorText = false;
	private boolean nextIsTextContent = false;

	private StringBuilder buffer;

48
	private Map<String, List<SchematronError>> featureErrors;
49
50
51
52
53
54
55
	private List<SchematronError> generalErrors;

	public SvrlContentHandler() {
		featureErrors = new HashMap<>();
		generalErrors = new ArrayList<>();
	}

56
	public Map<String, List<SchematronError>> getFeatureErrors() {
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
		return featureErrors;
	}

	public List<SchematronError> getGeneralErrors() {
		return generalErrors;
	}

	@Override
	public void setDocumentLocator(Locator locator) {
		// not needed
	}

	@Override
	public void startDocument() throws SAXException {
		// not needed
	}

	@Override
	public void endDocument() throws SAXException {
		// not needed
	}

	@Override
	public void startPrefixMapping(String prefix, String uri) throws SAXException {
		// not needed
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// not needed
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
		if ("failed-assert".equals(localName)) {
			nextElementIsErrorText = true;
		} else if (nextElementIsErrorText && "text".equals(localName)) {
			nextIsTextContent = true;
			buffer = new StringBuilder();
			nextElementIsErrorText = false;
		} else {
			nextElementIsErrorText = false;
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		// not needed
		if (nextIsTextContent && "text".equals(localName)) {
			String text = buffer.toString();
107
108
109
110
111
112
113
114
115
116
117
118
			String[] split = text.split("\\|\\|");
			if (split.length != 5) {
				throw new IllegalStateException(
						"Schematron File is not formed according to specification for CityDoctor.");
			}
			String gmlId = split[0];
			String childId = split[1];
			String errorId = split[2];
			String nameOfAttribute = split[3];
			boolean generic = Boolean.parseBoolean(split[4]);
			SchematronError err = new SchematronError(errorId, gmlId, childId, nameOfAttribute, generic);
			if (gmlId == null || gmlId.isEmpty()) {
119
				// general error
120
				generalErrors.add(err);
121
			} else {
122
123
				List<SchematronError> errors = featureErrors.computeIfAbsent(gmlId, k -> new ArrayList<>());
				errors.add(err);
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
			}
			buffer = null;
			nextIsTextContent = false;
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		if (nextIsTextContent) {
			buffer.append(ch, start, length);
		}
	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
		// not needed
	}

	@Override
	public void processingInstruction(String target, String data) throws SAXException {
		// not needed
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// not needed
	}

}