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 org.w3c.dom.Element;
033:
034: import nextapp.echo2.app.FillImage;
035: import nextapp.echo2.app.Extent;
036: import nextapp.echo2.app.ImageReference;
037: import nextapp.echo2.app.componentxml.InvalidPropertyException;
038: import nextapp.echo2.app.componentxml.PropertyLoader;
039: import nextapp.echo2.app.componentxml.PropertyXmlPeer;
040: import nextapp.echo2.app.util.DomUtil;
041:
042: /**
043: * <code>PropertyXmlPeer</code> implementation for
044: * <code>nextapp.echo2.app.FillImage</code> properties.
045: */
046: public class FillImagePeer implements PropertyXmlPeer {
047:
048: /**
049: * @see nextapp.echo2.app.componentxml.PropertyXmlPeer#getValue(java.lang.ClassLoader,
050: * java.lang.Class, org.w3c.dom.Element)
051: */
052: public Object getValue(ClassLoader classLoader, Class objectClass,
053: Element propertyElement) throws InvalidPropertyException {
054: Element fillImageElement = DomUtil.getChildElementByTagName(
055: propertyElement, "fill-image");
056: if (fillImageElement == null) {
057: throw new InvalidPropertyException(
058: "Invalid FillImage property.", null);
059: }
060:
061: Extent offsetX = fillImageElement.hasAttribute("horizontal") ? ExtentPeer
062: .toExtent(fillImageElement.getAttribute("horizontal"))
063: : null;
064: Extent offsetY = fillImageElement.hasAttribute("vertical") ? ExtentPeer
065: .toExtent(fillImageElement.getAttribute("vertical"))
066: : null;
067:
068: int repeat;
069: String repeatString = fillImageElement.getAttribute("repeat");
070: if ("horizontal".equals(repeatString)) {
071: repeat = FillImage.REPEAT_HORIZONTAL;
072: } else if ("vertical".equals(repeatString)) {
073: repeat = FillImage.REPEAT_VERTICAL;
074: } else if ("none".equals(repeatString)) {
075: repeat = FillImage.NO_REPEAT;
076: } else {
077: repeat = FillImage.REPEAT;
078: }
079:
080: Element imageElement = DomUtil.getChildElementByTagName(
081: fillImageElement, "image");
082: if (imageElement == null) {
083: throw new InvalidPropertyException(
084: "Invalid FillImage property.", null);
085: }
086: String imageType = imageElement.getAttribute("type");
087: PropertyLoader propertyLoader = PropertyLoader
088: .forClassLoader(classLoader);
089:
090: Class propertyClass;
091: try {
092: propertyClass = Class.forName(imageType, true, classLoader);
093: } catch (ClassNotFoundException ex) {
094: throw new InvalidPropertyException(
095: "Invalid FillImage property (type \"" + imageType
096: + "\" not found.", ex);
097: }
098:
099: Object imagePropertyValue = propertyLoader.getPropertyValue(
100: FillImage.class, propertyClass, imageElement);
101: if (!(imagePropertyValue instanceof ImageReference)) {
102: throw new InvalidPropertyException(
103: "Invalid FillImage property (type \"" + imageType
104: + "\" is not an ImageReference.", null);
105: }
106:
107: ImageReference imageReference = (ImageReference) imagePropertyValue;
108: FillImage fillImage = new FillImage(imageReference, offsetX,
109: offsetY, repeat);
110:
111: return fillImage;
112: }
113: }
|