⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 css.java

📁 Mobile 应用程序使用 Java Micro Edition (Java ME) 平台
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	private String name;	static final Value[] allValues = {	    INHERITED, NONE, DOTTED, DASHED, SOLID, DOUBLE, GROOVE,	    RIDGE, INSET, OUTSET, DISC, CIRCLE, SQUARE, DECIMAL,	    LOWER_ROMAN, UPPER_ROMAN, LOWER_ALPHA, UPPER_ALPHA,	    BLANK_LIST_ITEM, BACKGROUND_NO_REPEAT, BACKGROUND_REPEAT,	    BACKGROUND_REPEAT_X, BACKGROUND_REPEAT_Y,	    BACKGROUND_FIXED, BACKGROUND_FIXED	};    }    public CSS() {	baseFontSize = baseFontSizeIndex + 1;	// setup the css conversion table	valueConvertor = new Hashtable();	valueConvertor.put(CSS.Attribute.FONT_SIZE, new FontSize());	valueConvertor.put(CSS.Attribute.FONT_FAMILY, new FontFamily());	valueConvertor.put(CSS.Attribute.FONT_WEIGHT, new FontWeight());	valueConvertor.put(CSS.Attribute.BORDER_STYLE, new BorderStyle());	Object cv = new ColorValue();	valueConvertor.put(CSS.Attribute.COLOR, cv);	valueConvertor.put(CSS.Attribute.BACKGROUND_COLOR, cv);	valueConvertor.put(CSS.Attribute.BORDER_COLOR, cv);	Object lv = new LengthValue();	valueConvertor.put(CSS.Attribute.MARGIN_TOP, lv);	valueConvertor.put(CSS.Attribute.MARGIN_BOTTOM, lv);	valueConvertor.put(CSS.Attribute.MARGIN_LEFT, lv);	valueConvertor.put(CSS.Attribute.MARGIN_LEFT_LTR, lv);	valueConvertor.put(CSS.Attribute.MARGIN_LEFT_RTL, lv);	valueConvertor.put(CSS.Attribute.MARGIN_RIGHT, lv);	valueConvertor.put(CSS.Attribute.MARGIN_RIGHT_LTR, lv);	valueConvertor.put(CSS.Attribute.MARGIN_RIGHT_RTL, lv);	valueConvertor.put(CSS.Attribute.PADDING_TOP, lv);	valueConvertor.put(CSS.Attribute.PADDING_BOTTOM, lv);	valueConvertor.put(CSS.Attribute.PADDING_LEFT, lv);	valueConvertor.put(CSS.Attribute.PADDING_RIGHT, lv);	Object bv = new BorderWidthValue(null, 0);	valueConvertor.put(CSS.Attribute.BORDER_WIDTH, lv);	valueConvertor.put(CSS.Attribute.BORDER_TOP_WIDTH, bv);	valueConvertor.put(CSS.Attribute.BORDER_BOTTOM_WIDTH, bv);	valueConvertor.put(CSS.Attribute.BORDER_LEFT_WIDTH, bv);	valueConvertor.put(CSS.Attribute.BORDER_RIGHT_WIDTH, bv);	Object nlv = new LengthValue(true);	valueConvertor.put(CSS.Attribute.TEXT_INDENT, nlv);	valueConvertor.put(CSS.Attribute.WIDTH, lv);	valueConvertor.put(CSS.Attribute.HEIGHT, lv);	valueConvertor.put(CSS.Attribute.BORDER_SPACING, lv);	Object sv = new StringValue();	valueConvertor.put(CSS.Attribute.FONT_STYLE, sv);	valueConvertor.put(CSS.Attribute.TEXT_DECORATION, sv);	valueConvertor.put(CSS.Attribute.TEXT_ALIGN, sv);	valueConvertor.put(CSS.Attribute.VERTICAL_ALIGN, sv);	Object valueMapper = new CssValueMapper();	valueConvertor.put(CSS.Attribute.LIST_STYLE_TYPE,			   valueMapper);	valueConvertor.put(CSS.Attribute.BACKGROUND_IMAGE,			   new BackgroundImage());	valueConvertor.put(CSS.Attribute.BACKGROUND_POSITION,			   new BackgroundPosition());	valueConvertor.put(CSS.Attribute.BACKGROUND_REPEAT,			   valueMapper);	valueConvertor.put(CSS.Attribute.BACKGROUND_ATTACHMENT,			   valueMapper);	Object generic = new CssValue();	int n = CSS.Attribute.allAttributes.length;	for (int i = 0; i < n; i++) {	    CSS.Attribute key = CSS.Attribute.allAttributes[i];	    if (valueConvertor.get(key) == null) {		valueConvertor.put(key, generic);	    }	}    }    /**     * Sets the base font size. <code>sz</code> is a CSS value, and is     * not necessarily the point size. Use getPointSize to determine the     * point size corresponding to <code>sz</code>.     */    void setBaseFontSize(int sz) {	if (sz < 1)	  baseFontSize = 0;	else if (sz > 7)	  baseFontSize = 7;	else	  baseFontSize = sz;    }    /**     * Sets the base font size from the passed in string.     */    void setBaseFontSize(String size) {	int relSize, absSize, diff;	if (size != null) {	    if (size.startsWith("+")) {		relSize = Integer.valueOf(size.substring(1)).intValue();		setBaseFontSize(baseFontSize + relSize);	    } else if (size.startsWith("-")) {		relSize = -Integer.valueOf(size.substring(1)).intValue();		setBaseFontSize(baseFontSize + relSize);	    } else {		setBaseFontSize(Integer.valueOf(size).intValue());	    }	}    }    /**     * Returns the base font size.     */    int getBaseFontSize() {	return baseFontSize;    }    /**     * Parses the CSS property <code>key</code> with value     * <code>value</code> placing the result in <code>att</code>.     */    void addInternalCSSValue(MutableAttributeSet attr,			     CSS.Attribute key, String value) {	if (key == CSS.Attribute.FONT) {	    ShorthandFontParser.parseShorthandFont(this, value, attr);	}	else if (key == CSS.Attribute.BACKGROUND) {	    ShorthandBackgroundParser.parseShorthandBackground		               (this, value, attr);	}	else if (key == CSS.Attribute.MARGIN) {	    ShorthandMarginParser.parseShorthandMargin(this, value, attr,					   CSS.Attribute.ALL_MARGINS);	}	else if (key == CSS.Attribute.PADDING) {	    ShorthandMarginParser.parseShorthandMargin(this, value, attr,					   CSS.Attribute.ALL_PADDING);	}	else if (key == CSS.Attribute.BORDER_WIDTH) {	    ShorthandMarginParser.parseShorthandMargin(this, value, attr,					   CSS.Attribute.ALL_BORDER_WIDTHS);	}	else {	    Object iValue = getInternalCSSValue(key, value);	    if (iValue != null) {		attr.addAttribute(key, iValue);	    }	}    }    /**     * Gets the internal CSS representation of <code>value</code> which is     * a CSS value of the CSS attribute named <code>key</code>. The receiver     * should not modify <code>value</code>, and the first <code>count</code>     * strings are valid.     */    Object getInternalCSSValue(CSS.Attribute key, String value) {	CssValue conv = (CssValue) valueConvertor.get(key);        Object r = conv.parseCssValue(value);        return r != null ? r : conv.parseCssValue(key.getDefaultValue());    }    /**     * Maps from a StyleConstants to a CSS Attribute.     */    Attribute styleConstantsKeyToCSSKey(StyleConstants sc) {	return (Attribute)styleConstantToCssMap.get(sc);    }    /**     * Maps from a StyleConstants value to a CSS value.     */    Object styleConstantsValueToCSSValue(StyleConstants sc,					 Object styleValue) {	Object cssKey = styleConstantsKeyToCSSKey(sc);	if (cssKey != null) {	    CssValue conv = (CssValue)valueConvertor.get(cssKey);	    return conv.fromStyleConstants(sc, styleValue);	}	return null;    }    /**     * Converts the passed in CSS value to a StyleConstants value.     * <code>key</code> identifies the CSS attribute being mapped.     */    Object cssValueToStyleConstantsValue(StyleConstants key, Object value) {	if (value instanceof CssValue) {	    return ((CssValue)value).toStyleConstants((StyleConstants)key,						      null);	}	return null;    }    /**     * Returns the font for the values in the passed in AttributeSet.     * It is assumed the keys will be CSS.Attribute keys.     * <code>sc</code> is the StyleContext that will be messaged to get     * the font once the size, name and style have been determined.     */    Font getFont(StyleContext sc, AttributeSet a, int defaultSize, StyleSheet ss) {        ss = getStyleSheet(ss);	int size = getFontSize(a, defaultSize, ss);	/*	 * If the vertical alignment is set to either superscirpt or	 * subscript we reduce the font size by 2 points.	 */	StringValue vAlignV = (StringValue)a.getAttribute	                      (CSS.Attribute.VERTICAL_ALIGN);	if ((vAlignV != null)) {	    String vAlign = vAlignV.toString();	    if ((vAlign.indexOf("sup") >= 0) ||		(vAlign.indexOf("sub") >= 0)) {		size -= 2;	    }	}		FontFamily familyValue = (FontFamily)a.getAttribute	                                    (CSS.Attribute.FONT_FAMILY);	String family = (familyValue != null) ? familyValue.getValue() :	                          Font.SANS_SERIF;	int style = Font.PLAIN;	FontWeight weightValue = (FontWeight) a.getAttribute	                          (CSS.Attribute.FONT_WEIGHT);	if ((weightValue != null) && (weightValue.getValue() > 400)) {	    style |= Font.BOLD;	}	Object fs = a.getAttribute(CSS.Attribute.FONT_STYLE);	if ((fs != null) && (fs.toString().indexOf("italic") >= 0)) {	    style |= Font.ITALIC;	}        if (family.equalsIgnoreCase("monospace")) {            family = Font.MONOSPACED;        }	Font f = sc.getFont(family, style, size);        if (f == null             || (f.getFamily().equals(Font.DIALOG)                && ! family.equalsIgnoreCase(Font.DIALOG))) {            family = Font.SANS_SERIF;            f = sc.getFont(family, style, size);        }	return f;    }    static int getFontSize(AttributeSet attr, int defaultSize, StyleSheet ss) {	// PENDING(prinz) this is a 1.1 based implementation, need to also	// have a 1.2 version.	FontSize sizeValue = (FontSize)attr.getAttribute(CSS.Attribute.							 FONT_SIZE);	return (sizeValue != null) ? sizeValue.getValue(attr, ss)                                   : defaultSize;    }    /**     * Takes a set of attributes and turn it into a color     * specification.  This might be used to specify things     * like brighter, more hue, etc.     * This will return null if there is no value for <code>key</code>.     *     * @param key CSS.Attribute identifying where color is stored.     * @param a the set of attributes     * @return the color     */    Color getColor(AttributeSet a, CSS.Attribute key) {	ColorValue cv = (ColorValue) a.getAttribute(key);	if (cv != null) {	    return cv.getValue();	}	return null;    }    /**     * Returns the size of a font from the passed in string.     *     * @param size CSS string describing font size     * @param baseFontSize size to use for relative units.     */    float getPointSize(String size, StyleSheet ss) {	int relSize, absSize, diff, index;        ss = getStyleSheet(ss);	if (size != null) {	    if (size.startsWith("+")) {		relSize = Integer.valueOf(size.substring(1)).intValue();		return getPointSize(baseFontSize + relSize, ss);	    } else if (size.startsWith("-")) {		relSize = -Integer.valueOf(size.substring(1)).intValue();		return getPointSize(baseFontSize + relSize, ss);	    } else {		absSize = Integer.valueOf(size).intValue();		return getPointSize(absSize, ss);	    }	}	return 0;    }    /**     * Returns the length of the attribute in <code>a</code> with     * key <code>key</code>.     */    float getLength(AttributeSet a, CSS.Attribute key, StyleSheet ss) {        ss = getStyleSheet(ss);	LengthValue lv = (LengthValue) a.getAttribute(key);        boolean isW3CLengthUnits = (ss == null) ? false : ss.isW3CLengthUnits();        float len = (lv != null) ? lv.getValue(isW3CLengthUnits) : 0;	return len;    }    /**     * Convert a set of HTML attributes to an equivalent     * set of CSS attributes.     *     * @param AttributeSet containing the HTML attributes.     * @return AttributeSet containing the corresponding CSS attributes.     *        The AttributeSet will be empty if there are no mapping     *        CSS attributes.     */    AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet) {	MutableAttributeSet cssAttrSet = new SimpleAttributeSet();	Element elem = (Element)htmlAttrSet;	HTML.Tag tag = getHTMLTag(htmlAttrSet);	if ((tag == HTML.Tag.TD) || (tag == HTML.Tag.TH)) {	    // translate border width into the cells	    AttributeSet tableAttr = elem.getParentElement().		                     getParentElement().getAttributes();	    translateAttribute(HTML.Attribute.BORDER, tableAttr, cssAttrSet);	    String pad = (String)tableAttr.getAttribute(HTML.Attribute.CELLPADDING);	    if (pad != null) {		LengthValue v = 		    (LengthValue)getInternalCSSValue(CSS.Attribute.PADDING_TOP, pad);		v.span = (v.span < 0) ? 0 : v.span;		cssAttrSet.addAttribute(CSS.Attribute.PADDING_TOP, v);		cssAttrSet.addAttribute(CSS.Attribute.PADDING_BOTTOM, v);		cssAttrSet.addAttribute(CSS.Attribute.PADDING_LEFT, v);		cssAttrSet.addAttribute(CSS.Attribute.PADDING_RIGHT, v);	    }	}	if (elem.isLeaf()) {	    translateEmbeddedAttributes(htmlAttrSet, cssAttrSet);	} else {	    translateAttributes(tag, htmlAttrSet, cssAttrSet);	}	if (tag == HTML.Tag.CAPTION) {	    /* 	     * Navigator uses ALIGN for caption placement and IE uses VALIGN.	     */	    Object v = htmlAttrSet.getAttribute(HTML.Attribute.ALIGN);	    if ((v != null) && (v.equals("top") || v.equals("bottom"))) {		cssAttrSet.addAttribute(CSS.Attribute.CAPTION_SIDE, v);		cssAttrSet.removeAttribute(CSS.Attribute.TEXT_ALIGN);	    } else {		v = htmlAttrSet.getAttribute(HTML.Attribute.VALIGN);		if (v != null) {		    cssAttrSet.addAttribute(CSS.Attribute.CAPTION_SIDE, v);		}	    }	}	return cssAttrSet;    }    private static final Hashtable attributeMap = new Hashtable();    private static final Hashtable valueMap = new Hashtable();    /**     * The hashtable and the static initalization block below,     * set up a mapping from well-known HTML attributes to     * CSS attributes.  For the most part, there is a 1-1 mapping     * between the two.  However in the case of certain HTML     * attributes for example HTML.Attribute.VSPACE or     * HTML.Attribute.HSPACE, end up mapping to two CSS.Attribute's.     * Therefore, the value associated with each HTML.Attribute.     * key ends up being an array of CSS.Attribute.* objects.     */    private static final Hashtable htmlAttrToCssAttrMap = new Hashtable(20);    /**     * The hashtable and static initialization that follows sets     * up a translation from StyleConstants (i.e. the <em>well known</em>     * attributes) to the associated CSS attributes.     */    private static final Hashtable styleConstantToCssMap = new Hashtable(17);    /** Maps from HTML value to a CSS value. Used in internal mapping. */    private static final Hashtable htmlValueToCssValueMap = new Hashtable(8);    /** Maps from CSS value (string) to internal value. */    private static final Hashtable cssValueToInternalValueMap = new Hashtable(13);    static {	// load the attribute map	for (int i = 0; i < Attribute.allAttributes.length; i++ ) {	    attributeMap.put(Attribute.allAttributes[i].toString(),			     Attribute.allAttributes[i]);	}	// load the value map	for (int i = 0; i < Value.allValues.length; i++ ) {	    valueMap.put(Value.allValues[i].toString(), 			     Value.allValues[i]);	}	htmlAttrToCssAttrMap.put(HTML.Attribute.COLOR,				 new CSS.Attribute[]{CSS.Attribute.COLOR});	htmlAttrToCssAttrMap.put(HTML.Attribute.TEXT,				 new CSS.Attribute[]{CSS.Attribute.COLOR});	htmlAttrToCssAttrMap.put(HTML.Attribute.CLEAR,				 new CSS.Attribute[]{CSS.Attribute.CLEAR});

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -