001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.componentxml.propertypeer;
031:
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: import org.w3c.dom.Element;
037:
038: import nextapp.echo2.app.Extent;
039: import nextapp.echo2.app.Font;
040: import nextapp.echo2.app.componentxml.InvalidPropertyException;
041: import nextapp.echo2.app.componentxml.PropertyXmlPeer;
042: import nextapp.echo2.app.util.DomUtil;
043:
044: /**
045: * <code>PropertyXmlPeer</code> implementation for
046: * <code>nextapp.echo2.app.Font</code> properties.
047: */
048: public class FontPeer implements PropertyXmlPeer {
049:
050: private static final Map TYPEFACE_TEXT_TO_CONSTANT;
051: static {
052: Map constantMap = new HashMap();
053: constantMap.put("Helvetica", Font.HELVETICA);
054: constantMap.put("Arial", Font.ARIAL);
055: constantMap.put("Verdana", Font.VERDANA);
056: constantMap.put("Times", Font.TIMES);
057: constantMap.put("Times Roman", Font.TIMES_ROMAN);
058: constantMap.put("Times New Roman", Font.TIMES_NEW_ROMAN);
059: constantMap.put("Courier", Font.COURIER);
060: constantMap.put("Courier New", Font.COURIER_NEW);
061: TYPEFACE_TEXT_TO_CONSTANT = Collections
062: .unmodifiableMap(constantMap);
063: }
064:
065: private Font.Typeface getSimpleTypeface(String name) {
066: if (TYPEFACE_TEXT_TO_CONSTANT.containsKey(name)) {
067: return (Font.Typeface) TYPEFACE_TEXT_TO_CONSTANT.get(name);
068: } else {
069: return new Font.Typeface(name);
070: }
071: }
072:
073: /**
074: * @see nextapp.echo2.app.componentxml.PropertyXmlPeer#getValue(java.lang.ClassLoader,
075: * java.lang.Class, org.w3c.dom.Element)
076: */
077: public Object getValue(ClassLoader classLoader, Class objectClass,
078: Element propertyElement) throws InvalidPropertyException {
079: Element fontElement = DomUtil.getChildElementByTagName(
080: propertyElement, "font");
081: Extent size = null;
082: if (fontElement.hasAttribute("size")) {
083: String sizeString = fontElement.getAttribute("size");
084: size = ExtentPeer.toExtent(sizeString);
085: }
086: int style = 0;
087: if ("true".equals(fontElement.getAttribute("bold"))) {
088: style |= Font.BOLD;
089: }
090: if ("true".equals(fontElement.getAttribute("italic"))) {
091: style |= Font.ITALIC;
092: }
093: if ("true".equals(fontElement.getAttribute("underline"))) {
094: style |= Font.UNDERLINE;
095: }
096: if ("true".equals(fontElement.getAttribute("overline"))) {
097: style |= Font.OVERLINE;
098: }
099: if ("true".equals(fontElement.getAttribute("line-through"))) {
100: style |= Font.LINE_THROUGH;
101: }
102: Element[] typefaces = DomUtil.getChildElementsByTagName(
103: fontElement, "typeface");
104: Font.Typeface typeface;
105: if (typefaces.length == 0) {
106: if (fontElement.hasAttribute("typeface")) {
107: typeface = getSimpleTypeface(fontElement
108: .getAttribute("typeface"));
109: } else {
110: typeface = null;
111: }
112: } else if (typefaces.length == 1) {
113: typeface = getSimpleTypeface(typefaces[0]
114: .getAttribute("name"));
115: } else {
116: typeface = new Font.Typeface(
117: typefaces[typefaces.length - 1]
118: .getAttribute("name"));
119: for (int i = typefaces.length - 2; i >= 0; --i) {
120: typeface = new Font.Typeface(typefaces[i]
121: .getAttribute("name"), typeface);
122: }
123: }
124: return new Font(typeface, style, size);
125: }
126: }
|