001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/graphics/sld/ParameterValueType.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 and 007 lat/lon GmbH 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 package org.deegree.graphics.sld; 037 038 import static org.deegree.framework.xml.XMLTools.escape; 039 040 import java.io.Serializable; 041 import java.util.ArrayList; 042 043 import org.deegree.framework.xml.Marshallable; 044 import org.deegree.model.feature.Feature; 045 import org.deegree.model.filterencoding.Expression; 046 import org.deegree.model.filterencoding.FilterEvaluationException; 047 import org.deegree.model.filterencoding.PropertyName; 048 049 /** 050 * 051 * 052 * 053 * @version $Revision: 18332 $ 054 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 055 * @author last edited by: $Author: lbuesching $ 056 * 057 * @version $Revision: 18332 $ 058 */ 059 public class ParameterValueType implements Serializable, Marshallable { 060 061 private static final long serialVersionUID = -2232181501775429152L; 062 063 private ArrayList<Object> components = new ArrayList<Object>(); 064 065 /** 066 * Constructs a new <tt>ParameterValueType</tt>. 067 * <p> 068 * 069 * @param components 070 * <tt>String</tt>s/<tt>Expression</tt> s that make up the contents of the 071 * <tt>ParameterValueType</tt> 072 */ 073 public ParameterValueType( Object[] components ) { 074 setComponents( components ); 075 } 076 077 /** 078 * Returns the contents (mix of <tt>String</tt>/<tt>Expression</tt> -objects) of this 079 * <tt>ParameterValueType</tt>. 080 * <p> 081 * 082 * @return mix of <tt>String</tt>/<tt>Expression</tt> -objects 083 * 084 */ 085 public Object[] getComponents() { 086 return components.toArray( new Object[components.size()] ); 087 } 088 089 /** 090 * Sets the contents (mix of <tt>String</tt>/<tt>Expression</tt> -objects) of this 091 * <tt>ParameterValueType</tt>. 092 * <p> 093 * 094 * @param components 095 * mix of <tt>String</tt> and <tt>Expression</tt> -objects 096 */ 097 public void setComponents( Object[] components ) { 098 this.components.clear(); 099 100 if ( components != null ) { 101 this.components.ensureCapacity( components.length ); 102 for ( int i = 0; i < components.length; i++ ) { 103 this.components.add( components[i] ); 104 } 105 } 106 } 107 108 /** 109 * Concatenates a component (a<tt>String</tt> or an <tt>Expression</tt> -object) to this 110 * <tt>ParameterValueType</tt>. 111 * <p> 112 * 113 * @param component 114 * either a <tt>String</tt> or an <tt>Expression</tt> -object 115 */ 116 public void addComponent( Object component ) { 117 components.add( component ); 118 } 119 120 /** 121 * Removes a component (a<tt>String</tt> or an <tt>Expression</tt> -object) from this 122 * <tt>ParameterValueType</tt>. 123 * <p> 124 * 125 * @param component 126 * either a <tt>String</tt> or an <tt>Expression</tt> -object 127 */ 128 public void removeComponent( Object component ) { 129 components.remove( components.indexOf( component ) ); 130 } 131 132 /** 133 * Returns the value of the ParameterValueType as an <tt>PropertyName</tt>. 134 * <p> 135 * 136 * @return the value of the parameter as an <tt>PropertyName</tt> 137 */ 138 public PropertyName getValueAsPropertyName() { 139 for ( Object component : components ) { 140 if ( component instanceof PropertyName ) { 141 return (PropertyName) component; 142 } 143 } 144 return null; 145 } 146 147 /** 148 * Returns the actual <tt>String</tt> value of this object. Expressions are evaluated according 149 * to the given <tt>Feature</tt> -instance. 150 * <p> 151 * 152 * @param feature 153 * used for the evaluation of the underlying 'wfs:Expression'-elements 154 * @return the (evaluated) String value 155 * @throws FilterEvaluationException 156 * if the evaluation fails 157 */ 158 public String evaluate( Feature feature ) 159 throws FilterEvaluationException { 160 StringBuffer sb = new StringBuffer(); 161 162 for ( int i = 0; i < components.size(); i++ ) { 163 Object component = components.get( i ); 164 if ( component instanceof Expression ) { 165 sb.append( ( (Expression) component ).evaluate( feature ) ); 166 } else if ( component != null && component instanceof String ) { 167 sb.append( ( (String) component ).trim() ); 168 } else { 169 sb.append( component ); 170 } 171 } 172 173 return sb.toString(); 174 } 175 176 /** 177 * Exports the content of the ParameterValueType as an XML formatted String. 178 * 179 * @return xml representation of the ParameterValueType 180 */ 181 public String exportAsXML() { 182 183 StringBuffer sb = new StringBuffer(); 184 for ( int i = 0; i < components.size(); i++ ) { 185 Object component = components.get( i ); 186 if ( component instanceof Expression ) { 187 sb.append( ( (Expression) component ).toXML() ); 188 } else if ( component != null && component instanceof String ) { 189 sb.append( escape( ( (String) component ).trim() ) ); 190 } else { 191 sb.append( component ); 192 } 193 } 194 return sb.toString(); 195 } 196 197 @Override 198 public String toString() { 199 return exportAsXML(); 200 } 201 202 }