/
SchemaHandler.java
83 lines (71 loc) · 2.53 KB
/
SchemaHandler.java
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
/*
* Copyright (C) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.prism.impl.schema;
import java.io.IOException;
import org.xml.sax.*;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
/**
* Implements {@link EntityResolver} and {@link ErrorHandler} that reports sax
* errors to log.
*
* @author Vilo Repan
*/
public class SchemaHandler implements ErrorHandler, EntityResolver {
private static final Trace LOGGER = TraceManager.getTrace(SchemaHandler.class);
private final EntityResolver entityResolver;
public SchemaHandler(EntityResolver entityResolver) {
super();
this.entityResolver = entityResolver;
}
@Override
public void warning(SAXParseException e) {
print("[Warning]", e);
}
@Override
public void error(SAXParseException e) throws SAXException {
print("[Error]", e);
throw e;
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
print("[Fatal]", e);
throw e;
}
private void print(String header, SAXParseException e) {
String error = "Error occurred during schema parsing: " + header + " "
+ "on line " + e.getLineNumber() + " at " + e.getSystemId() + ", " + e.getPublicId()
+ " " + e.getMessage();
LOGGER.error(error, e);
}
/*
* (non-Javadoc)
*
* @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
* java.lang.String)
*/
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
if (entityResolver == null) {
throw new IllegalStateException("Entity resolver is not set");
}
try {
InputSource source = entityResolver.resolveEntity(publicId, systemId);
LOGGER.trace("Resolved entity '{}', '{}': '{}' (resolver: {})",
publicId, systemId, source, entityResolver);
return source;
} catch (SAXException e) {
LOGGER.error("XML error resolving entity '{}', '{}': '{}-{}'",
publicId, systemId, e.getMessage(), e);
throw e;
} catch (IOException e) {
LOGGER.error("IO error resolving entity '{}', '{}': '{}-{}'",
publicId, systemId, e.getMessage(), e);
throw e;
}
}
}