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 public abstract class BaseGrammarReaderController implements GrammarReaderController
44 {
45 /*** Loaded grammar URI. */
46 private String grammarUri;
47
48 /*** Entity resolution is delegated to this object, it can be <code>null</null>. */
49 private EntityResolver entityResolver;
50
51 /*** Used to compose information of location of errors and warnings. */
52 private StringBuilder locationMessage = new StringBuilder(64);
53
54 /***
55 * Creates a grammar reader controller.
56 *
57 * @param grammarUri URI of a loaded grammar - provided for better warning description
58 * @param entityResolver optional entity resolver.
59 */
60 public BaseGrammarReaderController(
61 String grammarUri,
62 EntityResolver entityResolver)
63 {
64 this.grammarUri = grammarUri;
65 this.entityResolver = entityResolver;
66 }
67
68 /***
69 * Creates a grammar reader controller without entity resolver.
70 *
71 * @param grammarUri URI of a loaded grammar - provided for better warning description
72 */
73 public BaseGrammarReaderController(String grammarUri)
74 {
75 this(grammarUri, null);
76 }
77
78
79
80
81 /***
82 * Resolves an entity, uses externaly set EntityResolver or returns <code>null</code>.
83 *
84 * @param publicId public id of an entity
85 * @param systemId system id of an entity
86 * @return resolved entity's input source
87 * @throws java.io.IOException on IO errors while resolving entity
88 * @throws SAXException on parsing errors while resolving entity
89 */
90 public InputSource resolveEntity( String publicId, String systemId )
91 throws java.io.IOException, SAXException
92 {
93 if(entityResolver!=null)
94 {
95 return entityResolver.resolveEntity(publicId, systemId);
96 }
97 else
98 {
99 return null;
100 }
101 }
102
103
104
105
106 /***
107 * Receives warning notification - does nothing.
108 *
109 * @param loc warning location info
110 * @param errorMessage warning message
111 */
112 public void warning(Locator[] loc, String errorMessage)
113 {
114
115 }
116
117 /***
118 * Receives error notification - does nothing.
119 *
120 * @param loc error location info
121 * @param errorMessage error message
122 * @param nestedException exception nested in error information
123 */
124 public void error( Locator[] loc, String errorMessage, Exception nestedException )
125 {
126
127 }
128
129
130
131
132 /***
133 * Prepares a location message.
134 *
135 * @param type message type - 'error' or 'warning'
136 * @param loc locator of the error or warning
137 * @param errorMessage a message
138 * @return formatted location message
139 */
140 protected String getLocationMessage(String type, Locator[] loc, String errorMessage)
141 {
142
143 locationMessage.setLength(0);
144
145
146 locationMessage.append(type);
147 locationMessage.append(" on schema '").append(grammarUri).append("' ; ");
148
149 locationMessage.append(errorMessage);
150 locationMessage.append(" ; ");
151
152 if(loc == null || loc.length == 0)
153 {
154 locationMessage.append("location unknown");
155 }
156 else
157 {
158 for( int i=0; i<loc.length; i++ )
159 {
160 getLocation(loc[i]);
161 }
162 }
163
164 return locationMessage.toString();
165 }
166
167 private void getLocation(Locator loc)
168 {
169 if(loc.getColumnNumber()>=0)
170 {
171 locationMessage.append("column:");
172 locationMessage.append(loc.getColumnNumber());
173 }
174
175 locationMessage.append(" line:");
176 locationMessage.append(loc.getLineNumber());
177 locationMessage.append(" ");
178 locationMessage.append(loc.getSystemId());
179 locationMessage.append(" ");
180 }
181 }