001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcbase/PropertyPath.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2008 by:
006 EXSE, Department of Geography, University of Bonn
007 http://www.giub.uni-bonn.de/deegree/
008 lat/lon GmbH
009 http://www.lat-lon.de
010
011 This library is free software; you can redistribute it and/or
012 modify it under the terms of the GNU Lesser General Public
013 License as published by the Free Software Foundation; either
014 version 2.1 of the License, or (at your option) any later version.
015
016 This library is distributed in the hope that it will be useful,
017 but WITHOUT ANY WARRANTY; without even the implied warranty of
018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 Lesser General Public License for more details.
020
021 You should have received a copy of the GNU Lesser General Public
022 License along with this library; if not, write to the Free Software
023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024
025 Contact:
026
027 Andreas Poth
028 lat/lon GmbH
029 Aennchenstraße 19
030 53177 Bonn
031 Germany
032 E-Mail: poth@lat-lon.de
033
034 Prof. Dr. Klaus Greve
035 Department of Geography
036 University of Bonn
037 Meckenheimer Allee 166
038 53115 Bonn
039 Germany
040 E-Mail: greve@giub.uni-bonn.de
041
042 ---------------------------------------------------------------------------*/
043 package org.deegree.ogcbase;
044
045 import java.util.List;
046
047 import org.deegree.datatypes.QualifiedName;
048 import org.deegree.framework.xml.NamespaceContext;
049
050 /**
051 * Represents a subset of the XPath expression language as described in section 7.4.2 of the Web
052 * Feature Implementation Specification 1.1.0 (but is used by other OGC specifications as well).
053 * <p>
054 * This specification does not require a WFS implementation to support the full XPath language. In
055 * order to keep the implementation entry cost as low as possible, this specification mandates that
056 * a WFS implementation <b>must</b> support the following subset of the XPath language:
057 * <ol>
058 * <li>A WFS implementation <b>must</b> support <i>abbreviated relative location</i> paths.</li>
059 * <li>Relative location paths are composed of one or more <i>steps</i> separated by the path
060 * separator '/'.</li>
061 * <li>The first step of a relative location path <b>may</b> correspond to the root element of the
062 * feature property being referenced <b>or</b> to the root element of the feature type with the
063 * next step corresponding to the root element of the feature property being referenced</li>
064 * <li>Each subsequent step in the path <b>must</b> be composed of the abbreviated form of the
065 * <i>child::</i> axis specifier and the name of the feature property encoded as the principal node
066 * type of <i>element</i>. The abbreviated form of the <i>child::</i> axis specifier is to simply
067 * omit the specifier from the location step.</li>
068 * <li>Each step in the path may optionally contain a predicate composed of the predicate
069 * delimiters '[' and ']' and a number indicating which child of the context node is to be selected.
070 * This allows feature properties that may be repeated to be specifically referenced.</li>
071 * <li>The final step in a path may optionally be composed of the abbreviated form of the
072 * <i>attribute::</i> axis specifier, '@', and the name of a feature property encoded as the
073 * principal node type of <i>attribute::</i>.</li>
074 * </ol>
075 * <p>
076 *
077 * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
078 * @author last edited by: $Author: apoth $
079 *
080 * @version $Revision: 9344 $, $Date: 2007-12-27 17:21:56 +0100 (Do, 27 Dez 2007) $
081 *
082 * @see PropertyPathStep
083 */
084 public class PropertyPath implements Comparable<PropertyPath> {
085
086 private List<PropertyPathStep> steps;
087
088 /**
089 * Creates a new instance of <code>PropertyPath</code> with the specified steps.
090 *
091 * @param steps
092 * property path steps, may not be null
093 */
094 public PropertyPath( List<PropertyPathStep> steps ) {
095 if ( steps.size() < 1 ) {
096 throw new IllegalArgumentException( "PropertyPath must contain at least one step." );
097 }
098 this.steps = steps;
099 }
100
101 /**
102 * Returns the namespace bindings for the prefices that are used by this property path.
103 *
104 * @return the namespace bindings
105 */
106 public NamespaceContext getNamespaceContext() {
107 NamespaceContext nsContext = new NamespaceContext();
108 for ( PropertyPathStep step : steps ) {
109 QualifiedName elementName = step.getPropertyName();
110 if ( elementName.getPrefix() != null && elementName.getNamespace() != null ) {
111 nsContext.addNamespace( elementName.getPrefix(), elementName.getNamespace() );
112 }
113 }
114 return nsContext;
115 }
116
117 /**
118 * Returns the number of steps.
119 *
120 * @return the number of steps.
121 */
122 public int getSteps() {
123 return this.steps.size();
124 }
125
126 /**
127 * Returns the canonical string representation.
128 *
129 * @return canonical string representation
130 */
131 public String getAsString() {
132 StringBuffer sb = new StringBuffer( 500 );
133 for ( int i = 0; i < steps.size(); i++ ) {
134 sb.append( steps.get( i ).toString() );
135 if ( i < steps.size() - 1 ) {
136 sb.append( '/' );
137 }
138 }
139 return sb.toString();
140 }
141
142 /**
143 * Returns the <code>PropertyPathStep</code> at the given index.
144 *
145 * @param i
146 * @return the <code>PropertyPathStep</code> at the given index
147 */
148 public PropertyPathStep getStep( int i ) {
149 return this.steps.get( i );
150 }
151
152 /**
153 * Returns all steps of the <code>PropertyPath</code>.
154 *
155 * @return all steps of the <code>PropertyPath</code>
156 */
157 public List<PropertyPathStep> getAllSteps() {
158 return this.steps;
159 }
160
161 /**
162 * Adds the given <code>PropertyPathStep</code> to the end of the path.
163 *
164 * @param last
165 * <code>PropertyPathStep</code> to add
166 */
167 public void append( PropertyPathStep last ) {
168 this.steps.add( last );
169 }
170
171 /**
172 * Adds the given <code>PropertyPathStep</code> to the beginning of the path.
173 *
174 * @param first
175 * <code>PropertyPathStep</code> to add
176 */
177 public void prepend( PropertyPathStep first ) {
178 this.steps.add( 0, first );
179 }
180
181 @Override
182 public int hashCode() {
183 int hashCode = 0;
184 for ( PropertyPathStep step : steps ) {
185 hashCode += step.hashCode();
186 }
187 return hashCode;
188 }
189
190 @Override
191 public boolean equals( Object obj ) {
192 if ( !( obj instanceof PropertyPath ) ) {
193 return false;
194 }
195 PropertyPath that = (PropertyPath) obj;
196 if ( this.getSteps() != that.getSteps() ) {
197 return false;
198 }
199 for ( int i = 0; i < this.getSteps(); i++ ) {
200 if ( !this.getStep( i ).equals( that.getStep( i ) ) ) {
201 return false;
202 }
203 }
204 return true;
205 }
206
207 @Override
208 public String toString() {
209 StringBuffer sb = new StringBuffer();
210 for ( int i = 0; i < getSteps(); i++ ) {
211 sb.append( getStep( i ) );
212 if ( i != getSteps() - 1 ) {
213 sb.append( "/" );
214 }
215 }
216 return sb.toString();
217 }
218
219 /**
220 * Compares this object with the specified object for order.
221 * <p>
222 * TODO use really unique string representations (namespaces!) + cache them
223 *
224 * @param that
225 * the PropertyPath to be compared
226 * @return a negative integer, zero, or a positive integer as this object is less than, equal
227 * to, or greater than the specified object
228 */
229 public int compareTo( PropertyPath that ) {
230 return this.toString().compareTo( that.toString() );
231 }
232 }