📄 stylesheet.java
字号:
*/ public void removeStyleSheet(StyleSheet ss) { synchronized(this) { if (linkedStyleSheets != null) { int index = linkedStyleSheets.indexOf(ss); if (index != -1) { linkedStyleSheets.removeElementAt(index); unlinkStyleSheet(ss, index); if (index == 0 && linkedStyleSheets.size() == 0) { linkedStyleSheets = null; } } } } } // // The following is used to import style sheets. // /** * Returns an array of the linked StyleSheets. Will return null * if there are no linked StyleSheets. * * @since 1.3 */ public StyleSheet[] getStyleSheets() { StyleSheet[] retValue; synchronized(this) { if (linkedStyleSheets != null) { retValue = new StyleSheet[linkedStyleSheets.size()]; linkedStyleSheets.copyInto(retValue); } else { retValue = null; } } return retValue; } /** * Imports a style sheet from <code>url</code>. The resulting rules * are directly added to the receiver. If you do not want the rules * to become part of the receiver, create a new StyleSheet and use * addStyleSheet to link it in. * * @since 1.3 */ public void importStyleSheet(URL url) { try { InputStream is; is = url.openStream(); Reader r = new BufferedReader(new InputStreamReader(is)); CssParser parser = new CssParser(); parser.parse(url, r, false, true); r.close(); is.close(); } catch (Throwable e) { // on error we simply have no styles... the html // will look mighty wrong but still function. } } /** * Sets the base. All import statements that are relative, will be * relative to <code>base</code>. * * @since 1.3 */ public void setBase(URL base) { this.base = base; } /** * Returns the base. * * @since 1.3 */ public URL getBase() { return base; } /** * Adds a CSS attribute to the given set. * * @since 1.3 */ public void addCSSAttribute(MutableAttributeSet attr, CSS.Attribute key, String value) { css.addInternalCSSValue(attr, key, value); } /** * Adds a CSS attribute to the given set. * * @since 1.3 */ public boolean addCSSAttributeFromHTML(MutableAttributeSet attr, CSS.Attribute key, String value) { Object iValue = css.getCssValue(key, value); if (iValue != null) { attr.addAttribute(key, iValue); return true; } return false; } // ---- Conversion functionality --------------------------------- /** * Converts a set of HTML attributes to an equivalent * set of CSS attributes. * * @param htmlAttrSet AttributeSet containing the HTML attributes. */ public AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet) { AttributeSet cssAttrSet = css.translateHTMLToCSS(htmlAttrSet); MutableAttributeSet cssStyleSet = addStyle(null, null); cssStyleSet.addAttributes(cssAttrSet); return cssStyleSet; } /** * Adds an attribute to the given set, and returns * the new representative set. This is reimplemented to * convert StyleConstant attributes to CSS prior to forwarding * to the superclass behavior. The StyleConstants attribute * has no corresponding CSS entry, the StyleConstants attribute * is stored (but will likely be unused). * * @param old the old attribute set * @param key the non-null attribute key * @param value the attribute value * @return the updated attribute set * @see MutableAttributeSet#addAttribute */ public AttributeSet addAttribute(AttributeSet old, Object key, Object value) { if (css == null) { // supers constructor will call this before returning, // and we need to make sure CSS is non null. css = new CSS(); } if (key instanceof StyleConstants) { HTML.Tag tag = HTML.getTagForStyleConstantsKey( (StyleConstants)key); if (tag != null && old.isDefined(tag)) { old = removeAttribute(old, tag); } Object cssValue = css.styleConstantsValueToCSSValue ((StyleConstants)key, value); if (cssValue != null) { Object cssKey = css.styleConstantsKeyToCSSKey ((StyleConstants)key); if (cssKey != null) { return super.addAttribute(old, cssKey, cssValue); } } } return super.addAttribute(old, key, value); } /** * Adds a set of attributes to the element. If any of these attributes * are StyleConstants attributes, they will be converted to CSS prior * to forwarding to the superclass behavior. * * @param old the old attribute set * @param attr the attributes to add * @return the updated attribute set * @see MutableAttributeSet#addAttribute */ public AttributeSet addAttributes(AttributeSet old, AttributeSet attr) { if (!(attr instanceof HTMLDocument.TaggedAttributeSet)) { old = removeHTMLTags(old, attr); } return super.addAttributes(old, convertAttributeSet(attr)); } /** * Removes an attribute from the set. If the attribute is a StyleConstants * attribute, the request will be converted to a CSS attribute prior to * forwarding to the superclass behavior. * * @param old the old set of attributes * @param key the non-null attribute name * @return the updated attribute set * @see MutableAttributeSet#removeAttribute */ public AttributeSet removeAttribute(AttributeSet old, Object key) { if (key instanceof StyleConstants) { HTML.Tag tag = HTML.getTagForStyleConstantsKey( (StyleConstants)key); if (tag != null) { old = super.removeAttribute(old, tag); } Object cssKey = css.styleConstantsKeyToCSSKey((StyleConstants)key); if (cssKey != null) { return super.removeAttribute(old, cssKey); } } return super.removeAttribute(old, key); } /** * Removes a set of attributes for the element. If any of the attributes * is a StyleConstants attribute, the request will be converted to a CSS * attribute prior to forwarding to the superclass behavior. * * @param old the old attribute set * @param names the attribute names * @return the updated attribute set * @see MutableAttributeSet#removeAttributes */ public AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names) { // PENDING: Should really be doing something similar to // removeHTMLTags here, but it is rather expensive to have to // clone names return super.removeAttributes(old, names); } /** * Removes a set of attributes. If any of the attributes * is a StyleConstants attribute, the request will be converted to a CSS * attribute prior to forwarding to the superclass behavior. * * @param old the old attribute set * @param attrs the attributes * @return the updated attribute set * @see MutableAttributeSet#removeAttributes */ public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs) { if (old != attrs) { old = removeHTMLTags(old, attrs); } return super.removeAttributes(old, convertAttributeSet(attrs)); } /** * Creates a compact set of attributes that might be shared. * This is a hook for subclasses that want to alter the * behavior of SmallAttributeSet. This can be reimplemented * to return an AttributeSet that provides some sort of * attribute conversion. * * @param a The set of attributes to be represented in the * the compact form. */ protected SmallAttributeSet createSmallAttributeSet(AttributeSet a) { return new SmallConversionSet(a); } /** * Creates a large set of attributes that should trade off * space for time. This set will not be shared. This is * a hook for subclasses that want to alter the behavior * of the larger attribute storage format (which is * SimpleAttributeSet by default). This can be reimplemented * to return a MutableAttributeSet that provides some sort of * attribute conversion. * * @param a The set of attributes to be represented in the * the larger form. */ protected MutableAttributeSet createLargeAttributeSet(AttributeSet a) { return new LargeConversionSet(a); } /** * For any StyleConstants key in attr that has an associated HTML.Tag, * it is removed from old. The resulting AttributeSet is then returned. */ private AttributeSet removeHTMLTags(AttributeSet old, AttributeSet attr) { if (!(attr instanceof LargeConversionSet) && !(attr instanceof SmallConversionSet)) { Enumeration names = attr.getAttributeNames(); while (names.hasMoreElements()) { Object key = names.nextElement(); if (key instanceof StyleConstants) { HTML.Tag tag = HTML.getTagForStyleConstantsKey( (StyleConstants)key); if (tag != null && old.isDefined(tag)) { old = super.removeAttribute(old, tag); } } } } return old; } /** * Converts a set of attributes (if necessary) so that * any attributes that were specified as StyleConstants * attributes and have a CSS mapping, will be converted * to CSS attributes. */ AttributeSet convertAttributeSet(AttributeSet a) { if ((a instanceof LargeConversionSet) || (a instanceof SmallConversionSet)) { // known to be converted. return a; } // in most cases, there are no StyleConstants attributes // so we iterate the collection of keys to avoid creating // a new set. Enumeration names = a.getAttributeNames(); while (names.hasMoreElements()) { Object name = names.nextElement(); if (name instanceof StyleConstants) { // we really need to do a conversion, iterate again // building a new set. MutableAttributeSet converted = new LargeConversionSet(); Enumeration keys = a.getAttributeNames(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); Object cssValue = null; if (key instanceof StyleConstants) { // convert the StyleConstants attribute if possible Object cssKey = css.styleConstantsKeyToCSSKey ((StyleConstants)key); if (cssKey != null) { Object value = a.getAttribute(key); cssValue = css.styleConstantsValueToCSSValue ((StyleConstants)key, value); if (cssValue != null) { converted.addAttribute(cssKey, cssValue); } } } if (cssValue == null) { converted.addAttribute(key, a.getAttribute(key)); } } return converted; } } return a; } /** * Large set of attributes that does conversion of requests * for attributes of type StyleConstants. */ class LargeConversionSet extends SimpleAttributeSet { /** * Creates a new attribute set based on a supplied set of attributes. * * @param source the set of attributes */ public LargeConversionSet(AttributeSet source) { super(source); } public LargeConversionSet() { super(); } /** * Checks whether a given attribute is defined. * * @param key the attribute key * @return true if the attribute is defined * @see AttributeSet#isDefined */ public boolean isDefined(Object key) { if (key instanceof StyleConstants) { Object cssKey = css.styleConstantsKeyToCSSKey ((StyleConstants)key); if (cssKey != null) { return super.isDefined(cssKey); } } return super.isDefined(key); } /** * Gets the value of an attribute. * * @param key the attribute name * @return the attribute value * @see AttributeSet#getAttribute */ public Object getAttribute(Object key) { if (key instanceof StyleConstants) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -