Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/io/AbstractParser.java
6005 views
1
/*******************************************************************************
2
* Copyright (c) 2007, 2011 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.j9tools.om.io;
23
24
import java.io.File;
25
import java.io.InputStream;
26
import java.text.MessageFormat;
27
import java.util.HashSet;
28
import java.util.Set;
29
30
import javax.xml.XMLConstants;
31
import javax.xml.parsers.SAXParser;
32
import javax.xml.parsers.SAXParserFactory;
33
34
import org.xml.sax.InputSource;
35
import org.xml.sax.Locator;
36
import org.xml.sax.SAXException;
37
import org.xml.sax.SAXParseException;
38
import org.xml.sax.XMLReader;
39
import org.xml.sax.helpers.DefaultHandler;
40
41
/**
42
* AbstractParser provides extensions to the standard SAX DefaultHandler for
43
* error propagation, and schema lookup.
44
*/
45
public abstract class AbstractParser extends DefaultHandler {
46
47
/** The JAXP property for schema language. */
48
static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; //$NON-NLS-1$
49
50
/** The JAXP property for schema language. */
51
static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; //$NON-NLS-1$
52
53
/**
54
* Used by the SAXParser as an intermediary between start and end element handlers. Contains the value between
55
* <element>value</element> once the end handler is invoked.
56
*/
57
protected String _parsedValue = ""; //$NON-NLS-1$
58
59
/** The underlying SAX parser that does the walk. */
60
protected SAXParser _parser;
61
62
/** Parse Errors. */
63
protected Set<Throwable> _warnings = new HashSet<Throwable>();
64
protected Set<Throwable> _errors = new HashSet<Throwable>();
65
66
/**
67
* The Locator instance set via setDocumentLocator handler called once when
68
* we start parsing a document. Keep track of it in order to be able to retrieve
69
* parse location data after the initial setDocumentLocator event.
70
*/
71
protected Locator _documentLocator;
72
73
/**
74
* The currently parsed File. Used as part of the SourceLocation data stored
75
* for each relevant line of parsed XML.
76
*/
77
protected File _documentLocatorFile;
78
79
/**
80
*
81
*/
82
public AbstractParser() {
83
84
try {
85
// Create a parser factory and configure it to enable namespaces/validation.
86
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
87
saxFactory.setNamespaceAware(true);
88
saxFactory.setValidating(true);
89
90
// Create a new parser
91
_parser = saxFactory.newSAXParser();
92
_parser.setProperty(JAXP_SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI);
93
// _parser.setProperty(JAXP_SCHEMA_SOURCE, getSchemaName());
94
95
// Point all handlers at this object
96
XMLReader xmlReader = _parser.getXMLReader();
97
xmlReader.setContentHandler(this);
98
xmlReader.setErrorHandler(this);
99
xmlReader.setDTDHandler(this);
100
xmlReader.setEntityResolver(this);
101
102
} catch (Exception e) {
103
// Promote the exception to a fatal error.
104
throw new Error(e);
105
}
106
}
107
108
/**
109
* @see org.xml.sax.ContentHandler#characters(char[], int, int)
110
*/
111
@Override
112
public void characters(char[] ch, int start, int length) {
113
_parsedValue += new String(ch, start, length);
114
}
115
116
/**
117
* @return The name of the schema expected by the parser.
118
*/
119
public abstract String getSchemaName();
120
121
/**
122
* @see org.xml.sax.helpers.DefaultHandler#resolveEntity(java.lang.String, java.lang.String)
123
*/
124
@Override
125
public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
126
File schemaFile = new File(systemId);
127
InputStream stream = getClass().getResourceAsStream("/" + schemaFile.getName()); //$NON-NLS-1$
128
129
if (stream == null) {
130
_errors.add(new SAXException(MessageFormat.format(Messages.getString("AbstractParser.0"), new Object[] { schemaFile.getName() }))); //$NON-NLS-1$
131
} else {
132
_parser.setProperty(JAXP_SCHEMA_SOURCE, schemaFile.getName());
133
}
134
135
return new InputSource(stream);
136
}
137
138
@Override
139
public void setDocumentLocator(Locator locator) {
140
this._documentLocator = locator;
141
}
142
143
public void setDocumentLocatorFile(File file) {
144
this._documentLocatorFile = file;
145
}
146
147
/**
148
* @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
149
*/
150
@Override
151
public void warning(SAXParseException exception) {
152
_warnings.add(exception);
153
}
154
155
/**
156
* @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
157
*/
158
@Override
159
public void error(SAXParseException exception) {
160
_errors.add(exception);
161
}
162
163
/**
164
* @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
165
*/
166
@Override
167
public void fatalError(SAXParseException exception) {
168
_errors.add(exception);
169
}
170
171
/**
172
* Determines if there has been any SAX parse errors.
173
*
174
* @return <code>true</code> if there are SAX errors, <code>false</code> otherwise
175
*/
176
public boolean hasErrors() {
177
return _errors.size() != 0;
178
}
179
180
/**
181
* Determines if there has been any SAX parse warnings.
182
*
183
* @return <code>true</code> if there are SAX warnings, <code>false</code> otherwise
184
*/
185
public boolean hasWarnings() {
186
return _warnings.size() != 0;
187
}
188
189
/**
190
* Retrieves the SAX parse errors.
191
*
192
* @return the SAX parse errors.
193
*/
194
public Set<Throwable> getErrors() {
195
return _errors;
196
}
197
198
/**
199
* Retrieves the SAX parse warnings.
200
*
201
* @return the SAX parse warnings.
202
*/
203
public Set<Throwable> getWarnings() {
204
return _warnings;
205
}
206
}
207
208