189 /**
190 * List of SynthStyle.Painters that will be applied to the current style.
191 */
192 private List<ParsedSynthStyle.PainterInfo> _stylePainters;
193
194 /**
195 * List of SynthStyle.Painters that will be applied to the current state.
196 */
197 private List<ParsedSynthStyle.PainterInfo> _statePainters;
198
199 SynthParser() {
200 _mapping = new HashMap<String,Object>();
201 _stateInfos = new ArrayList<ParsedSynthStyle.StateInfo>();
202 _colorTypes = new ArrayList<ColorType>();
203 _inputMapBindings = new ArrayList<String>();
204 _stylePainters = new ArrayList<ParsedSynthStyle.PainterInfo>();
205 _statePainters = new ArrayList<ParsedSynthStyle.PainterInfo>();
206 }
207
208 /**
209 * Parses a set of styles from <code>inputStream</code>, adding the
210 * resulting styles to the passed in DefaultSynthStyleFactory.
211 * Resources are resolved either from a URL or from a Class. When calling
212 * this method, one of the URL or the Class must be null but not both at
213 * the same time.
214 *
215 * @param inputStream XML document containing the styles to read
216 * @param factory DefaultSynthStyleFactory that new styles are added to
217 * @param urlResourceBase the URL used to resolve any resources, such as Images
218 * @param classResourceBase the Class used to resolve any resources, such as Images
219 * @param defaultsMap Map that UIDefaults properties are placed in
220 */
221 public void parse(InputStream inputStream,
222 DefaultSynthStyleFactory factory,
223 URL urlResourceBase, Class<?> classResourceBase,
224 Map<String, Object> defaultsMap)
225 throws ParseException, IllegalArgumentException {
226 if (inputStream == null || factory == null ||
227 (urlResourceBase == null && classResourceBase == null)) {
228 throw new IllegalArgumentException(
229 "You must supply an InputStream, StyleFactory and Class or URL");
302 //
303 // getResource(".") is called to ensure we have the directory
304 // containing the resources in the case the resource base is a
305 // .class file.
306 URL[] urls = new URL[] { getResource(".") };
307 ClassLoader parent = Thread.currentThread().getContextClassLoader();
308 ClassLoader urlLoader = new URLClassLoader(urls, parent);
309 _handler.setClassLoader(urlLoader);
310 } else {
311 _handler.setClassLoader(_classResourceBase.getClassLoader());
312 }
313
314 for (String key : _mapping.keySet()) {
315 _handler.setVariable(key, _mapping.get(key));
316 }
317 }
318 return _handler;
319 }
320
321 /**
322 * If <code>value</code> is an instance of <code>type</code> it is
323 * returned, otherwise a SAXException is thrown.
324 */
325 private Object checkCast(Object value, Class<?> type) throws SAXException {
326 if (!type.isInstance(value)) {
327 throw new SAXException("Expected type " + type + " got " +
328 value.getClass());
329 }
330 return value;
331 }
332
333 /**
334 * Returns an object created with id=key. If the object is not of
335 * type type, this will throw an exception.
336 */
337 private Object lookup(String key, Class<?> type) throws SAXException {
338 Object value;
339 if (_handler != null) {
340 if (_handler.hasVariable(key)) {
341 return checkCast(_handler.getVariable(key), type);
342 }
|
189 /**
190 * List of SynthStyle.Painters that will be applied to the current style.
191 */
192 private List<ParsedSynthStyle.PainterInfo> _stylePainters;
193
194 /**
195 * List of SynthStyle.Painters that will be applied to the current state.
196 */
197 private List<ParsedSynthStyle.PainterInfo> _statePainters;
198
199 SynthParser() {
200 _mapping = new HashMap<String,Object>();
201 _stateInfos = new ArrayList<ParsedSynthStyle.StateInfo>();
202 _colorTypes = new ArrayList<ColorType>();
203 _inputMapBindings = new ArrayList<String>();
204 _stylePainters = new ArrayList<ParsedSynthStyle.PainterInfo>();
205 _statePainters = new ArrayList<ParsedSynthStyle.PainterInfo>();
206 }
207
208 /**
209 * Parses a set of styles from {@code inputStream}, adding the
210 * resulting styles to the passed in DefaultSynthStyleFactory.
211 * Resources are resolved either from a URL or from a Class. When calling
212 * this method, one of the URL or the Class must be null but not both at
213 * the same time.
214 *
215 * @param inputStream XML document containing the styles to read
216 * @param factory DefaultSynthStyleFactory that new styles are added to
217 * @param urlResourceBase the URL used to resolve any resources, such as Images
218 * @param classResourceBase the Class used to resolve any resources, such as Images
219 * @param defaultsMap Map that UIDefaults properties are placed in
220 */
221 public void parse(InputStream inputStream,
222 DefaultSynthStyleFactory factory,
223 URL urlResourceBase, Class<?> classResourceBase,
224 Map<String, Object> defaultsMap)
225 throws ParseException, IllegalArgumentException {
226 if (inputStream == null || factory == null ||
227 (urlResourceBase == null && classResourceBase == null)) {
228 throw new IllegalArgumentException(
229 "You must supply an InputStream, StyleFactory and Class or URL");
302 //
303 // getResource(".") is called to ensure we have the directory
304 // containing the resources in the case the resource base is a
305 // .class file.
306 URL[] urls = new URL[] { getResource(".") };
307 ClassLoader parent = Thread.currentThread().getContextClassLoader();
308 ClassLoader urlLoader = new URLClassLoader(urls, parent);
309 _handler.setClassLoader(urlLoader);
310 } else {
311 _handler.setClassLoader(_classResourceBase.getClassLoader());
312 }
313
314 for (String key : _mapping.keySet()) {
315 _handler.setVariable(key, _mapping.get(key));
316 }
317 }
318 return _handler;
319 }
320
321 /**
322 * If {@code value} is an instance of {@code type} it is
323 * returned, otherwise a SAXException is thrown.
324 */
325 private Object checkCast(Object value, Class<?> type) throws SAXException {
326 if (!type.isInstance(value)) {
327 throw new SAXException("Expected type " + type + " got " +
328 value.getClass());
329 }
330 return value;
331 }
332
333 /**
334 * Returns an object created with id=key. If the object is not of
335 * type type, this will throw an exception.
336 */
337 private Object lookup(String key, Class<?> type) throws SAXException {
338 Object value;
339 if (_handler != null) {
340 if (_handler.hasVariable(key)) {
341 return checkCast(_handler.getVariable(key), type);
342 }
|