KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > engine > value > css2 > FontFamilyManager


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.css.engine.value.css2;
19
20 import org.apache.batik.css.engine.CSSContext;
21 import org.apache.batik.css.engine.CSSEngine;
22 import org.apache.batik.css.engine.CSSStylableElement;
23 import org.apache.batik.css.engine.StyleMap;
24 import org.apache.batik.css.engine.value.AbstractValueManager;
25 import org.apache.batik.css.engine.value.ListValue;
26 import org.apache.batik.css.engine.value.StringMap;
27 import org.apache.batik.css.engine.value.StringValue;
28 import org.apache.batik.css.engine.value.Value;
29 import org.apache.batik.css.engine.value.ValueConstants;
30 import org.apache.batik.css.engine.value.ValueManager;
31 import org.apache.batik.util.CSSConstants;
32 import org.w3c.css.sac.LexicalUnit;
33 import org.w3c.dom.DOMException JavaDoc;
34 import org.w3c.dom.css.CSSPrimitiveValue;
35
36 /**
37  * This class provides a factory for the 'font-family' property values.
38  *
39  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
40  * @version $Id: FontFamilyManager.java,v 1.6 2005/03/27 08:58:31 cam Exp $
41  */

42 public class FontFamilyManager extends AbstractValueManager {
43     
44     /**
45      * The default value.
46      */

47     protected final static ListValue DEFAULT_VALUE = new ListValue();
48     static {
49         DEFAULT_VALUE.append
50             (new StringValue(CSSPrimitiveValue.CSS_STRING,
51                              "Arial"));
52         DEFAULT_VALUE.append
53             (new StringValue(CSSPrimitiveValue.CSS_STRING,
54                              "Helvetica"));
55         DEFAULT_VALUE.append
56             (new StringValue(CSSPrimitiveValue.CSS_IDENT,
57                              CSSConstants.CSS_SANS_SERIF_VALUE));
58     }
59
60     /**
61      * The identifier values.
62      */

63     protected final static StringMap values = new StringMap();
64     static {
65     values.put(CSSConstants.CSS_CURSIVE_VALUE,
66                    ValueConstants.CURSIVE_VALUE);
67     values.put(CSSConstants.CSS_FANTASY_VALUE,
68                    ValueConstants.FANTASY_VALUE);
69     values.put(CSSConstants.CSS_MONOSPACE_VALUE,
70                    ValueConstants.MONOSPACE_VALUE);
71     values.put(CSSConstants.CSS_SERIF_VALUE,
72                    ValueConstants.SERIF_VALUE);
73     values.put(CSSConstants.CSS_SANS_SERIF_VALUE,
74                    ValueConstants.SANS_SERIF_VALUE);
75     }
76
77     /**
78      * Implements {@link ValueManager#isInheritedProperty()}.
79      */

80     public boolean isInheritedProperty() {
81     return true;
82     }
83
84     /**
85      * Implements {@link ValueManager#getPropertyName()}.
86      */

87     public String JavaDoc getPropertyName() {
88     return CSSConstants.CSS_FONT_FAMILY_PROPERTY;
89     }
90     
91     /**
92      * Implements {@link ValueManager#getDefaultValue()}.
93      */

94     public Value getDefaultValue() {
95         return DEFAULT_VALUE;
96     }
97
98     /**
99      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
100      */

101     public Value createValue(LexicalUnit lu, CSSEngine engine)
102         throws DOMException JavaDoc {
103         switch (lu.getLexicalUnitType()) {
104         case LexicalUnit.SAC_INHERIT:
105             return ValueConstants.INHERIT_VALUE;
106
107         default:
108             throw createInvalidLexicalUnitDOMException
109                 (lu.getLexicalUnitType());
110
111         case LexicalUnit.SAC_IDENT:
112         case LexicalUnit.SAC_STRING_VALUE:
113         }
114         ListValue result = new ListValue();
115         for (;;) {
116             switch (lu.getLexicalUnitType()) {
117             case LexicalUnit.SAC_STRING_VALUE:
118                 result.append(new StringValue(CSSPrimitiveValue.CSS_STRING,
119                                               lu.getStringValue()));
120                 lu = lu.getNextLexicalUnit();
121                 break;
122
123             case LexicalUnit.SAC_IDENT:
124                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(lu.getStringValue());
125                 lu = lu.getNextLexicalUnit();
126                 if (lu != null &&
127                     lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT) {
128                     do {
129                         sb.append(' ');
130                         sb.append(lu.getStringValue());
131                         lu = lu.getNextLexicalUnit();
132                     } while (lu != null &&
133                              lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT);
134                     result.append(new StringValue(CSSPrimitiveValue.CSS_STRING,
135                                                   sb.toString()));
136                 } else {
137                     String JavaDoc id = sb.toString();
138                     String JavaDoc s = id.toLowerCase().intern();
139                     Value v = (Value)values.get(s);
140                     result.append((v != null)
141                                   ? v
142                                   : new StringValue
143                                         (CSSPrimitiveValue.CSS_STRING, id));
144                 }
145             }
146             if (lu == null) {
147                 return result;
148             }
149             if (lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
150                 throw createInvalidLexicalUnitDOMException
151                     (lu.getLexicalUnitType());
152             }
153             lu = lu.getNextLexicalUnit();
154             if (lu == null) {
155                 throw createMalformedLexicalUnitDOMException();
156             }
157         }
158     }
159
160     /**
161      * Implements {@link
162      * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
163      */

164     public Value computeValue(CSSStylableElement elt,
165                               String JavaDoc pseudo,
166                               CSSEngine engine,
167                               int idx,
168                               StyleMap sm,
169                               Value value) {
170         if (value == DEFAULT_VALUE) {
171             CSSContext ctx = engine.getCSSContext();
172             value = ctx.getDefaultFontFamily();
173         }
174         return value;
175     }
176 }
177
Popular Tags