src/share/classes/javax/swing/plaf/synth/SynthParser.java

Print this page




 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             }
 343         }
 344         value = _mapping.get(key);
 345         if (value == null) {
 346             throw new SAXException("ID " + key + " has not been defined");
 347         }
 348         return checkCast(value, type);
 349     }
 350 
 351     /**
 352      * Registers an object by name. This will throw an exception if an
 353      * object has already been registered under the given name.
 354      */
 355     private void register(String key, Object value) throws SAXException {
 356         if (key != null) {
 357             if (_mapping.get(key) != null ||


 624                         throw new SAXException("Invalid Color value: " +value);
 625                     }
 626                 }
 627                 else {
 628                     try {
 629                         color = new ColorUIResource((Color)Color.class.
 630                               getField(value.toUpperCase()).get(Color.class));
 631                     } catch (NoSuchFieldException nsfe) {
 632                         throw new SAXException("Invalid color name: " + value);
 633                     } catch (IllegalAccessException iae) {
 634                         throw new SAXException("Invalid color name: " + value);
 635                     }
 636                 }
 637             }
 638             else if (key.equals(ATTRIBUTE_TYPE)) {
 639                 StringTokenizer tokenizer = new StringTokenizer(
 640                                    attributes.getValue(i));
 641                 while (tokenizer.hasMoreTokens()) {
 642                     String typeName = tokenizer.nextToken();
 643                     int classIndex = typeName.lastIndexOf('.');
 644                     Class typeClass;
 645 
 646                     if (classIndex == -1) {
 647                         typeClass = ColorType.class;
 648                         classIndex = 0;
 649                     }
 650                     else {
 651                         try {
 652                             typeClass = ReflectUtil.forName(typeName.substring(
 653                                                       0, classIndex));
 654                         } catch (ClassNotFoundException cnfe) {
 655                             throw new SAXException("Unknown class: " +
 656                                       typeName.substring(0, classIndex));
 657                         }
 658                         classIndex++;
 659                     }
 660                     try {
 661                         _colorTypes.add((ColorType)checkCast(typeClass.
 662                               getField(typeName.substring(classIndex)).
 663                               get(typeClass), ColorType.class));
 664                     } catch (NoSuchFieldException nsfe) {


 766                 try {
 767                     value = new Integer(Integer.parseInt(aValue));
 768                 } catch (NumberFormatException nfe) {
 769                     throw new SAXException(property + " invalid value");
 770                 }
 771                 break;
 772             case 5: //string
 773                 value = aValue;
 774                 break;
 775             }
 776         }
 777         if (value == null || key == null) {
 778             throw new SAXException(property + ": you must supply a " +
 779                                    "key and value");
 780         }
 781         if (property == ELEMENT_DEFAULTS_PROPERTY) {
 782             _defaultsMap.put(key, value);
 783         }
 784         else if (_stateInfo != null) {
 785             if (_stateInfo.getData() == null) {
 786                 _stateInfo.setData(new HashMap());
 787             }
 788             _stateInfo.getData().put(key, value);
 789         }
 790         else if (_style != null) {
 791             if (_style.getData() == null) {
 792                 _style.setData(new HashMap());
 793             }
 794             _style.getData().put(key, value);
 795         }
 796     }
 797 
 798     private void startGraphics(Attributes attributes) throws SAXException {
 799         SynthGraphicsUtils graphics = null;
 800 
 801         for(int i = attributes.getLength() - 1; i >= 0; i--) {
 802             String key = attributes.getQName(i);
 803             if (key.equals(ATTRIBUTE_IDREF)) {
 804                 graphics = (SynthGraphicsUtils)lookup(attributes.getValue(i),
 805                                                  SynthGraphicsUtils.class);
 806             }
 807         }
 808         if (graphics == null) {
 809             throw new SAXException("graphicsUtils: you must supply an idref");
 810         }
 811         if (_style != null) {
 812             _style.setGraphicsUtils(graphics);




 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             }
 343         }
 344         value = _mapping.get(key);
 345         if (value == null) {
 346             throw new SAXException("ID " + key + " has not been defined");
 347         }
 348         return checkCast(value, type);
 349     }
 350 
 351     /**
 352      * Registers an object by name. This will throw an exception if an
 353      * object has already been registered under the given name.
 354      */
 355     private void register(String key, Object value) throws SAXException {
 356         if (key != null) {
 357             if (_mapping.get(key) != null ||


 624                         throw new SAXException("Invalid Color value: " +value);
 625                     }
 626                 }
 627                 else {
 628                     try {
 629                         color = new ColorUIResource((Color)Color.class.
 630                               getField(value.toUpperCase()).get(Color.class));
 631                     } catch (NoSuchFieldException nsfe) {
 632                         throw new SAXException("Invalid color name: " + value);
 633                     } catch (IllegalAccessException iae) {
 634                         throw new SAXException("Invalid color name: " + value);
 635                     }
 636                 }
 637             }
 638             else if (key.equals(ATTRIBUTE_TYPE)) {
 639                 StringTokenizer tokenizer = new StringTokenizer(
 640                                    attributes.getValue(i));
 641                 while (tokenizer.hasMoreTokens()) {
 642                     String typeName = tokenizer.nextToken();
 643                     int classIndex = typeName.lastIndexOf('.');
 644                     Class<?> typeClass;
 645 
 646                     if (classIndex == -1) {
 647                         typeClass = ColorType.class;
 648                         classIndex = 0;
 649                     }
 650                     else {
 651                         try {
 652                             typeClass = ReflectUtil.forName(typeName.substring(
 653                                                       0, classIndex));
 654                         } catch (ClassNotFoundException cnfe) {
 655                             throw new SAXException("Unknown class: " +
 656                                       typeName.substring(0, classIndex));
 657                         }
 658                         classIndex++;
 659                     }
 660                     try {
 661                         _colorTypes.add((ColorType)checkCast(typeClass.
 662                               getField(typeName.substring(classIndex)).
 663                               get(typeClass), ColorType.class));
 664                     } catch (NoSuchFieldException nsfe) {


 766                 try {
 767                     value = new Integer(Integer.parseInt(aValue));
 768                 } catch (NumberFormatException nfe) {
 769                     throw new SAXException(property + " invalid value");
 770                 }
 771                 break;
 772             case 5: //string
 773                 value = aValue;
 774                 break;
 775             }
 776         }
 777         if (value == null || key == null) {
 778             throw new SAXException(property + ": you must supply a " +
 779                                    "key and value");
 780         }
 781         if (property == ELEMENT_DEFAULTS_PROPERTY) {
 782             _defaultsMap.put(key, value);
 783         }
 784         else if (_stateInfo != null) {
 785             if (_stateInfo.getData() == null) {
 786                 _stateInfo.setData(new HashMap<>());
 787             }
 788             _stateInfo.getData().put(key, value);
 789         }
 790         else if (_style != null) {
 791             if (_style.getData() == null) {
 792                 _style.setData(new HashMap<>());
 793             }
 794             _style.getData().put(key, value);
 795         }
 796     }
 797 
 798     private void startGraphics(Attributes attributes) throws SAXException {
 799         SynthGraphicsUtils graphics = null;
 800 
 801         for(int i = attributes.getLength() - 1; i >= 0; i--) {
 802             String key = attributes.getQName(i);
 803             if (key.equals(ATTRIBUTE_IDREF)) {
 804                 graphics = (SynthGraphicsUtils)lookup(attributes.getValue(i),
 805                                                  SynthGraphicsUtils.class);
 806             }
 807         }
 808         if (graphics == null) {
 809             throw new SAXException("graphicsUtils: you must supply an idref");
 810         }
 811         if (_style != null) {
 812             _style.setGraphicsUtils(graphics);