📄 synthparser.java
字号:
/* * @(#)SynthParser.java 1.23 05/09/12 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.swing.plaf.synth;import java.awt.Color;import java.awt.Component;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.Insets;import java.awt.Toolkit;import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import java.net.URLClassLoader;import java.text.ParseException;import java.util.ArrayList;import java.util.HashMap;import java.util.Locale;import java.util.Map;import java.util.StringTokenizer;import java.util.regex.PatternSyntaxException;import javax.swing.ImageIcon;import javax.swing.JSplitPane;import javax.swing.SwingConstants;import javax.swing.UIDefaults;import javax.swing.plaf.ColorUIResource;import javax.swing.plaf.DimensionUIResource;import javax.swing.plaf.FontUIResource;import javax.swing.plaf.InsetsUIResource;import javax.swing.plaf.UIResource;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.AttributeList;import org.xml.sax.HandlerBase;import org.xml.sax.InputSource;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import com.sun.beans.ObjectHandler;/** * @version 1.23, 09/12/05 */class SynthParser extends HandlerBase { // // Known element names // private static final String ELEMENT_SYNTH = "synth"; private static final String ELEMENT_STYLE = "style"; private static final String ELEMENT_STATE = "state"; private static final String ELEMENT_FONT = "font"; private static final String ELEMENT_COLOR = "color"; private static final String ELEMENT_IMAGE_PAINTER = "imagePainter"; private static final String ELEMENT_PAINTER = "painter"; private static final String ELEMENT_PROPERTY = "property"; private static final String ELEMENT_SYNTH_GRAPHICS = "graphicsUtils"; private static final String ELEMENT_IMAGE_ICON = "imageIcon"; private static final String ELEMENT_BIND = "bind"; private static final String ELEMENT_BIND_KEY = "bindKey"; private static final String ELEMENT_INSETS = "insets"; private static final String ELEMENT_OPAQUE = "opaque"; private static final String ELEMENT_DEFAULTS_PROPERTY = "defaultsProperty"; private static final String ELEMENT_INPUT_MAP = "inputMap"; // // Known attribute names // private static final String ATTRIBUTE_ACTION = "action"; private static final String ATTRIBUTE_ID = "id"; private static final String ATTRIBUTE_IDREF = "idref"; private static final String ATTRIBUTE_CLONE = "clone"; private static final String ATTRIBUTE_VALUE = "value"; private static final String ATTRIBUTE_NAME = "name"; private static final String ATTRIBUTE_STYLE = "style"; private static final String ATTRIBUTE_SIZE = "size"; private static final String ATTRIBUTE_TYPE = "type"; private static final String ATTRIBUTE_TOP = "top"; private static final String ATTRIBUTE_LEFT = "left"; private static final String ATTRIBUTE_BOTTOM = "bottom"; private static final String ATTRIBUTE_RIGHT = "right"; private static final String ATTRIBUTE_KEY = "key"; private static final String ATTRIBUTE_SOURCE_INSETS = "sourceInsets"; private static final String ATTRIBUTE_DEST_INSETS = "destinationInsets"; private static final String ATTRIBUTE_PATH = "path"; private static final String ATTRIBUTE_STRETCH = "stretch"; private static final String ATTRIBUTE_PAINT_CENTER = "paintCenter"; private static final String ATTRIBUTE_METHOD = "method"; private static final String ATTRIBUTE_DIRECTION = "direction"; private static final String ATTRIBUTE_CENTER = "center"; /** * Lazily created, used for anything we don't understand. */ private ObjectHandler _handler; /** * Indicates the depth of how many elements we've encountered but don't * understand. This is used when forwarding to beans persistance to know * when we hsould stop forwarding. */ private int _depth; /** * Factory that new styles are added to. */ private DefaultSynthStyleFactory _factory; /** * Array of state infos for the current style. These are pushed to the * style when </style> is received. */ private java.util.List _stateInfos; /** * Current style. */ private ParsedSynthStyle _style; /** * Current state info. */ private ParsedSynthStyle.StateInfo _stateInfo; /** * Bindings for the current InputMap */ private java.util.List _inputMapBindings; /** * ID for the input map. This is cached as * the InputMap is created AFTER the inputMapProperty has ended. */ private String _inputMapID; /** * Object references outside the scope of persistance. */ private Map<String,Object> _mapping; /** * Based URL used to resolve paths. */ private URL _urlResourceBase; /** * Based class used to resolve paths. */ private Class<?> _classResourceBase; /** * List of ColorTypes. This is populated in startColorType. */ private java.util.List _colorTypes; /** * defaultsPropertys are placed here. */ private Map _defaultsMap; /** * List of SynthStyle.Painters that will be applied to the current style. */ private java.util.List _stylePainters; /** * List of SynthStyle.Painters that will be applied to the current state. */ private java.util.List _statePainters; SynthParser() { _mapping = new HashMap<String,Object>(); _stateInfos = new ArrayList(); _colorTypes = new ArrayList(); _inputMapBindings = new ArrayList(); _stylePainters = new ArrayList(); _statePainters = new ArrayList(); } /** * Parses a set of styles from <code>inputStream</code>, adding the * resulting styles to the passed in DefaultSynthStyleFactory. * Resources are resolved either from a URL or from a Class. When calling * this method, one of the URL or the Class must be null but not both at * the same time. * * @param inputStream XML document containing the styles to read * @param factory DefaultSynthStyleFactory that new styles are added to * @param urlResourceBase the URL used to resolve any resources, such as Images * @param classResourceBase the Class used to resolve any resources, such as Images * @param defaultsMap Map that UIDefaults properties are placed in */ public void parse(InputStream inputStream, DefaultSynthStyleFactory factory, URL urlResourceBase, Class<?> classResourceBase, Map defaultsMap) throws ParseException, IllegalArgumentException { if (inputStream == null || factory == null || (urlResourceBase == null && classResourceBase == null)) { throw new IllegalArgumentException( "You must supply an InputStream, StyleFactory and Class or URL"); } assert(!(urlResourceBase != null && classResourceBase != null)); _factory = factory; _classResourceBase = classResourceBase; _urlResourceBase = urlResourceBase; _defaultsMap = defaultsMap; try { try { SAXParser saxParser = SAXParserFactory.newInstance(). newSAXParser(); saxParser.parse(new BufferedInputStream(inputStream), this); } catch (ParserConfigurationException e) { throw new ParseException("Error parsing: " + e, 0); } catch (SAXException se) { throw new ParseException("Error parsing: " + se + " " + se.getException(), 0); } catch (IOException ioe) { throw new ParseException("Error parsing: " + ioe, 0); } } finally { reset(); } } /** * Returns the path to a resource. */ private URL getResource(String path) { if (_classResourceBase != null) { return _classResourceBase.getResource(path); } else { try { return new URL(_urlResourceBase, path); } catch (MalformedURLException mue) { return null; } } } /** * Clears our internal state. */ private void reset() { _handler = null; _depth = 0; _mapping.clear(); _stateInfos.clear(); _colorTypes.clear(); _statePainters.clear(); _stylePainters.clear(); } /** * Returns true if we are forwarding to persistance. */ private boolean isForwarding() { return (_depth > 0); } /** * Handles beans persistance. */ private ObjectHandler getHandler() { if (_handler == null) { if (_urlResourceBase != null) { // getHandler() is never called before parse() so it is safe // to create a URLClassLoader with _resourceBase. // // getResource(".") is called to ensure we have the directory // containing the resources in the case the resource base is a // .class file. URL[] urls = new URL[] { getResource(".") }; ClassLoader parent = Thread.currentThread().getContextClassLoader(); ClassLoader urlLoader = new URLClassLoader(urls, parent); _handler = new ObjectHandler(null, urlLoader); } else { _handler = new ObjectHandler(null, _classResourceBase.getClassLoader()); } for (String key : _mapping.keySet()) { _handler.register(key, _mapping.get(key)); } } return _handler; } /** * If <code>value</code> is an instance of <code>type</code> it is * returned, otherwise a SAXException is thrown. */ private Object checkCast(Object value, Class type) throws SAXException { if (!type.isInstance(value)) { throw new SAXException("Expected type " + type + " got " + value.getClass()); } return value; } /** * Returns an object created with id=key. If the object is not of * type type, this will throw an exception. */ private Object lookup(String key, Class type) throws SAXException { Object value = null; if (_handler != null) { if ((value = _handler.lookup(key)) != null) { return checkCast(value, type); } } value = _mapping.get(key); if (value == null) { throw new SAXException("ID " + key + " has not been defined"); } return checkCast(value, type); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -