📄 synthparser.java
字号:
/** * Registers an object by name. This will throw an exception if an * object has already been registered under the given name. */ private void register(String key, Object value) throws SAXException { if (key != null) { if (_mapping.get(key) != null || (_handler != null && _handler.lookup(key) != null)) { throw new SAXException("ID " + key + " is already defined"); } if (_handler != null) { _handler.register(key, value); } else { _mapping.put(key, value); } } } /** * Convenience method to return the next int, or throw if there are no * more valid ints. */ private int nextInt(StringTokenizer tok, String errorMsg) throws SAXException { if (!tok.hasMoreTokens()) { throw new SAXException(errorMsg); } try { return Integer.parseInt(tok.nextToken()); } catch (NumberFormatException nfe) { throw new SAXException(errorMsg); } } /** * Convenience method to return an Insets object. */ private Insets parseInsets(String insets, String errorMsg) throws SAXException { StringTokenizer tokenizer = new StringTokenizer(insets); return new Insets(nextInt(tokenizer, errorMsg), nextInt(tokenizer, errorMsg), nextInt(tokenizer, errorMsg), nextInt(tokenizer, errorMsg)); } // // The following methods are invoked from startElement/stopElement // private void startStyle(AttributeList attributes) throws SAXException { String id = null; _style = null; for(int i = attributes.getLength() - 1; i >= 0; i--) { String key = attributes.getName(i); if (key.equals(ATTRIBUTE_CLONE)) { _style = (ParsedSynthStyle)((ParsedSynthStyle)lookup( attributes.getValue(i), ParsedSynthStyle.class)). clone(); } else if (key.equals(ATTRIBUTE_ID)) { id = attributes.getValue(i); } } if (_style == null) { _style = new ParsedSynthStyle(); } register(id, _style); } private void endStyle() throws SAXException { int size = _stylePainters.size(); if (size > 0) { _style.setPainters((ParsedSynthStyle.PainterInfo[]) _stylePainters.toArray(new ParsedSynthStyle. PainterInfo[size])); _stylePainters.clear(); } size = _stateInfos.size(); if (size > 0) { _style.setStateInfo((ParsedSynthStyle.StateInfo[])_stateInfos. toArray(new ParsedSynthStyle.StateInfo[size])); _stateInfos.clear(); } _style = null; } private void startState(AttributeList attributes) throws SAXException { ParsedSynthStyle.StateInfo stateInfo = null; int state = 0; String id = null; _stateInfo = null; for(int i = attributes.getLength() - 1; i >= 0; i--) { String key = attributes.getName(i); if (key.equals(ATTRIBUTE_ID)) { id = attributes.getValue(i); } else if (key.equals(ATTRIBUTE_IDREF)) { _stateInfo = (ParsedSynthStyle.StateInfo)lookup( attributes.getValue(i), ParsedSynthStyle.StateInfo.class); } else if (key.equals(ATTRIBUTE_CLONE)) { _stateInfo = (ParsedSynthStyle.StateInfo)((ParsedSynthStyle. StateInfo)lookup(attributes.getValue(i), ParsedSynthStyle.StateInfo.class)).clone(); } else if (key.equals(ATTRIBUTE_VALUE)) { StringTokenizer tokenizer = new StringTokenizer( attributes.getValue(i)); while (tokenizer.hasMoreTokens()) { String stateString = tokenizer.nextToken().toUpperCase(). intern(); if (stateString == "ENABLED") { state |= SynthConstants.ENABLED; } else if (stateString == "MOUSE_OVER") { state |= SynthConstants.MOUSE_OVER; } else if (stateString == "PRESSED") { state |= SynthConstants.PRESSED; } else if (stateString == "DISABLED") { state |= SynthConstants.DISABLED; } else if (stateString == "FOCUSED") { state |= SynthConstants.FOCUSED; } else if (stateString == "SELECTED") { state |= SynthConstants.SELECTED; } else if (stateString == "DEFAULT") { state |= SynthConstants.DEFAULT; } else if (stateString != "AND") { throw new SAXException("Unknown state: " + state); } } } } if (_stateInfo == null) { _stateInfo = new ParsedSynthStyle.StateInfo(); } _stateInfo.setComponentState(state); register(id, _stateInfo); _stateInfos.add(_stateInfo); } private void endState() throws SAXException { int size = _statePainters.size(); if (size > 0) { _stateInfo.setPainters((ParsedSynthStyle.PainterInfo[]) _statePainters.toArray(new ParsedSynthStyle. PainterInfo[size])); _statePainters.clear(); } _stateInfo = null; } private void startFont(AttributeList attributes) throws SAXException { Font font = null; int style = Font.PLAIN; int size = 0; String id = null; String name = null; for(int i = attributes.getLength() - 1; i >= 0; i--) { String key = attributes.getName(i); if (key.equals(ATTRIBUTE_ID)) { id = attributes.getValue(i); } else if (key.equals(ATTRIBUTE_IDREF)) { font = (Font)lookup(attributes.getValue(i), Font.class); } else if (key.equals(ATTRIBUTE_NAME)) { name = attributes.getValue(i); } else if (key.equals(ATTRIBUTE_SIZE)) { try { size = Integer.parseInt(attributes.getValue(i)); } catch (NumberFormatException nfe) { throw new SAXException("Invalid font size: " + attributes.getValue(i)); } } else if (key.equals(ATTRIBUTE_STYLE)) { StringTokenizer tok = new StringTokenizer( attributes.getValue(i)); while (tok.hasMoreTokens()) { String token = tok.nextToken().intern(); if (token == "BOLD") { style = ((style | Font.PLAIN) ^ Font.PLAIN) | Font.BOLD; } else if (token == "ITALIC") { style |= Font.ITALIC; } } } } if (font == null) { if (name == null) { throw new SAXException("You must define a name for the font"); } if (size == 0) { throw new SAXException("You must define a size for the font"); } font = new FontUIResource(name, style, size); } else if (name != null || size != 0 || style != Font.PLAIN) { throw new SAXException("Name, size and style are not for use " + "with idref"); } register(id, font); if (_stateInfo != null) { _stateInfo.setFont(font); } else if (_style != null) { _style.setFont(font); } } private void startColor(AttributeList attributes) throws SAXException { Color color = null; String id = null; _colorTypes.clear(); for(int i = attributes.getLength() - 1; i >= 0; i--) { String key = attributes.getName(i); if (key.equals(ATTRIBUTE_ID)) { id = attributes.getValue(i); } else if (key.equals(ATTRIBUTE_IDREF)) { color = (Color)lookup(attributes.getValue(i), Color.class); } else if (key.equals(ATTRIBUTE_NAME)) { } else if (key.equals(ATTRIBUTE_VALUE)) { String value = attributes.getValue(i); if (value.startsWith("#")) { try { int argb; boolean hasAlpha; int length = value.length(); if (length < 8) { // Just RGB, or some portion of it. argb = Integer.decode(value); hasAlpha = false; } else if (length == 8) { // Single character alpha: #ARRGGBB. argb = Integer.decode(value); hasAlpha = true; } else if (length == 9) { // Color has alpha and is of the form // #AARRGGBB. // The following split decoding is mandatory due to // Integer.decode() behavior which won't decode // hexadecimal values higher than #7FFFFFFF. // Thus, when an alpha channel is detected, it is // decoded separately from the RGB channels. int rgb = Integer.decode('#' + value.substring(3, 9)); int a = Integer.decode(value.substring(0, 3)); argb = (a << 24) | rgb; hasAlpha = true; } else { throw new SAXException("Invalid Color value: " + value); } color = new ColorUIResource(new Color(argb, hasAlpha)); } catch (NumberFormatException nfe) { throw new SAXException("Invalid Color value: " +value); } } else { try { color = new ColorUIResource((Color)Color.class. getField(value.toUpperCase()).get(Color.class)); } catch (NoSuchFieldException nsfe) { throw new SAXException("Invalid color name: " + value); } catch (IllegalAccessException iae) { throw new SAXException("Invalid color name: " + value); } } } else if (key.equals(ATTRIBUTE_TYPE)) { StringTokenizer tokenizer = new StringTokenizer( attributes.getValue(i)); while (tokenizer.hasMoreTokens()) { String typeName = tokenizer.nextToken(); int classIndex = typeName.lastIndexOf('.'); Class typeClass; if (classIndex == -1) { typeClass = ColorType.class; classIndex = 0; } else { try { typeClass = Class.forName(typeName.substring( 0, classIndex)); } catch (ClassNotFoundException cnfe) { throw new SAXException("Unknown class: " + typeName.substring(0, classIndex)); } classIndex++; } try { _colorTypes.add((ColorType)checkCast(typeClass. getField(typeName.substring(classIndex, typeName.length() - classIndex)). get(typeClass), ColorType.class)); } catch (NoSuchFieldException nsfe) { throw new SAXException("Unable to find color type: " + typeName); } catch (IllegalAccessException iae) { throw new SAXException("Unable to find color type: " + typeName); } } } } if (color == null) { throw new SAXException("color: you must specificy a value");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -