001: /*
002:
003: ============================================================================
004: The Apache Software License, Version 1.1
005: ============================================================================
006:
007: Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modifica-
010: tion, are permitted provided that the following conditions are met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright notice,
016: this list of conditions and the following disclaimer in the documentation
017: and/or other materials provided with the distribution.
018:
019: 3. The end-user documentation included with the redistribution, if any, must
020: include the following acknowledgment: "This product includes software
021: developed by the Apache Software Foundation (http://www.apache.org/)."
022: Alternately, this acknowledgment may appear in the software itself, if
023: and wherever such third-party acknowledgments normally appear.
024:
025: 4. The names "Batik" and "Apache Software Foundation" must not be
026: used to endorse or promote products derived from this software without
027: prior written permission. For written permission, please contact
028: apache@apache.org.
029:
030: 5. Products derived from this software may not be called "Apache", nor may
031: "Apache" appear in their name, without prior written permission of the
032: Apache Software Foundation.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
035: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
036: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
037: APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
038: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
039: DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
040: OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
041: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: This software consists of voluntary contributions made by many individuals
046: on behalf of the Apache Software Foundation. For more information on the
047: Apache Software Foundation, please see <http://www.apache.org/>.
048:
049: */
050:
051: package org.apache.batik.css.engine.value.css2;
052:
053: import org.apache.batik.css.engine.CSSContext;
054: import org.apache.batik.css.engine.CSSEngine;
055: import org.apache.batik.css.engine.CSSStylableElement;
056: import org.apache.batik.css.engine.StyleMap;
057: import org.apache.batik.css.engine.value.AbstractValueManager;
058: import org.apache.batik.css.engine.value.ListValue;
059: import org.apache.batik.css.engine.value.StringMap;
060: import org.apache.batik.css.engine.value.StringValue;
061: import org.apache.batik.css.engine.value.Value;
062: import org.apache.batik.css.engine.value.ValueConstants;
063: import org.apache.batik.util.CSSConstants;
064: import org.w3c.css.sac.LexicalUnit;
065: import org.w3c.dom.DOMException;
066: import org.w3c.dom.css.CSSPrimitiveValue;
067:
068: /**
069: * This class provides a factory for the 'font-family' property values.
070: *
071: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
072: * @version $Id$
073: */
074: public class FontFamilyManager extends AbstractValueManager {
075:
076: /**
077: * The default value.
078: */
079: protected final static ListValue DEFAULT_VALUE = new ListValue();
080: static {
081: DEFAULT_VALUE.append(new StringValue(
082: CSSPrimitiveValue.CSS_STRING, "Arial"));
083: DEFAULT_VALUE.append(new StringValue(
084: CSSPrimitiveValue.CSS_STRING, "Helvetica"));
085: DEFAULT_VALUE.append(new StringValue(
086: CSSPrimitiveValue.CSS_IDENT,
087: CSSConstants.CSS_SANS_SERIF_VALUE));
088: }
089:
090: /**
091: * The identifier values.
092: */
093: protected final static StringMap values = new StringMap();
094: static {
095: values.put(CSSConstants.CSS_CURSIVE_VALUE,
096: ValueConstants.CURSIVE_VALUE);
097: values.put(CSSConstants.CSS_FANTASY_VALUE,
098: ValueConstants.FANTASY_VALUE);
099: values.put(CSSConstants.CSS_MONOSPACE_VALUE,
100: ValueConstants.MONOSPACE_VALUE);
101: values.put(CSSConstants.CSS_SERIF_VALUE,
102: ValueConstants.SERIF_VALUE);
103: values.put(CSSConstants.CSS_SANS_SERIF_VALUE,
104: ValueConstants.SANS_SERIF_VALUE);
105: }
106:
107: /**
108: * Implements {@link ValueManager#isInheritedProperty()}.
109: */
110: public boolean isInheritedProperty() {
111: return true;
112: }
113:
114: /**
115: * Implements {@link ValueManager#getPropertyName()}.
116: */
117: public String getPropertyName() {
118: return CSSConstants.CSS_FONT_FAMILY_PROPERTY;
119: }
120:
121: /**
122: * Implements {@link ValueManager#getDefaultValue()}.
123: */
124: public Value getDefaultValue() {
125: return DEFAULT_VALUE;
126: }
127:
128: /**
129: * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
130: */
131: public Value createValue(LexicalUnit lu, CSSEngine engine)
132: throws DOMException {
133: switch (lu.getLexicalUnitType()) {
134: case LexicalUnit.SAC_INHERIT:
135: return ValueConstants.INHERIT_VALUE;
136:
137: default:
138: throw createInvalidLexicalUnitDOMException(lu
139: .getLexicalUnitType(), engine);
140:
141: case LexicalUnit.SAC_IDENT:
142: case LexicalUnit.SAC_STRING_VALUE:
143: }
144: ListValue result = new ListValue();
145: for (;;) {
146: switch (lu.getLexicalUnitType()) {
147: case LexicalUnit.SAC_STRING_VALUE:
148: result.append(new StringValue(
149: CSSPrimitiveValue.CSS_STRING, lu
150: .getStringValue()));
151: lu = lu.getNextLexicalUnit();
152: break;
153:
154: case LexicalUnit.SAC_IDENT:
155: StringBuffer sb = new StringBuffer(lu.getStringValue());
156: lu = lu.getNextLexicalUnit();
157: if (lu != null
158: && lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT) {
159: do {
160: sb.append(' ');
161: sb.append(lu.getStringValue());
162: lu = lu.getNextLexicalUnit();
163: } while (lu != null
164: && lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT);
165: result
166: .append(new StringValue(
167: CSSPrimitiveValue.CSS_STRING, sb
168: .toString()));
169: } else {
170: String id = sb.toString();
171: String s = id.toLowerCase().intern();
172: Value v = (Value) values.get(s);
173: result.append((v != null) ? v : new StringValue(
174: CSSPrimitiveValue.CSS_STRING, id));
175: }
176: }
177: if (lu == null) {
178: return result;
179: }
180: if (lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
181: throw createInvalidLexicalUnitDOMException(lu
182: .getLexicalUnitType(), engine);
183: }
184: lu = lu.getNextLexicalUnit();
185: if (lu == null) {
186: throw createMalformedLexicalUnitDOMException(engine);
187: }
188: }
189: }
190:
191: /**
192: * Implements {@link
193: * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
194: */
195: public Value computeValue(CSSStylableElement elt, String pseudo,
196: CSSEngine engine, int idx, StyleMap sm, Value value) {
197: if (value == DEFAULT_VALUE) {
198: CSSContext ctx = engine.getCSSContext();
199: value = ctx.getDefaultFontFamily();
200: }
201: return value;
202: }
203: }
|