001: package com.xoetrope.builder.w3c.html;
002:
003: import java.awt.Color;
004: import java.awt.Font;
005: import java.util.Hashtable;
006: import java.util.StringTokenizer;
007:
008: /**
009: * XHtmlStyle holds the attributes and properties defined as part of a CSS style
010: * sheet style.
011: * <p> Copyright (c) Xoetrope Ltd., 2002-2006</p>
012: * <p> $Revision: 1.2 $</p>
013: * <p> License: see License.txt</p>
014: */
015: public class XHtmlStyle {
016: protected String className;
017: protected Hashtable<String, String> attributes;
018: protected Hashtable<String, Object> objects;
019:
020: /**
021: * Creates a new instance of XHtmlStyle
022: * @param styleName the name of teh style as defined in the style sheet
023: */
024: public XHtmlStyle(String styleName) {
025: className = styleName;
026: attributes = new Hashtable<String, String>();
027: objects = new Hashtable<String, Object>();
028: }
029:
030: /**
031: * Get a style attribute
032: * @param attribName the attribute name
033: * @return the attribute value
034: */
035: public String getAttribute(String attribName) {
036: return attributes.get(attribName);
037: }
038:
039: /**
040: * Get the Font setup by this style
041: * @return the font
042: */
043: public Font getFont() {
044: Font f = (Font) objects.get("font");
045: if (f == null) {
046: int fontAttribs = 0;
047: String s = attributes.get("font-style");
048: if ((s != null) && s.equals("italic"))
049: fontAttribs |= Font.ITALIC;
050:
051: s = attributes.get("font-weight");
052: if ((s != null) && s.equals("bold"))
053: fontAttribs |= Font.BOLD;
054:
055: int size = 12;
056: s = attributes.get("font-size");
057: if (s != null) {
058: if (s.endsWith("pt"))
059: s = s.substring(0, s.length() - 2);
060: size = Integer.parseInt(s);
061: }
062:
063: String face = attributes.get("font-family");
064: if (face == null)
065: face = "Helvetica";
066:
067: f = new Font(face, fontAttribs, size);
068: }
069: return f;
070: }
071:
072: /**
073: * Get the foreground color defined by this style, if any. Otherwise black is
074: * returned.
075: * @param returnDefault return the default value if no attribute is specified
076: * @return the foreground color or null if no default is specified and returnDefault is false
077: */
078: public Color getForeground(boolean returnDefault) {
079: String s = attributes.get("color");
080: if (s != null)
081: return XHtmlBuilder.getColor(s);
082: return returnDefault ? Color.black : null;
083: }
084:
085: /**
086: * Get the background color defined by this style, if any. Otherwise whiye is
087: * returned.
088: * @param returnDefault return the default value if no attribute is specified
089: * @return the background color or null if no default is specified and returnDefault is false
090: */
091: public Color getBackground(boolean returnDefault) {
092: String s = attributes.get("background-color");
093: if (s == null)
094: s = attributes.get("background");
095:
096: if (s != null)
097: return XHtmlBuilder.getColor(s);
098: return returnDefault ? Color.white : null;
099: }
100:
101: /**
102: * Parse a CSS string
103: * @param the css string containing a single style style
104: */
105: public void parse(String css) {
106: // Now get the attributes of this style
107: StringTokenizer tokenizer = new StringTokenizer(css, ";");
108: while (tokenizer.hasMoreTokens()) {
109: String attrib = tokenizer.nextToken().trim();
110: int pos = attrib.indexOf(':');
111: String attrName = attrib.substring(0, pos);
112: attributes.put(attrName, attrib.substring(pos + 1).trim());
113: }
114: }
115: }
|