001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcbase/SortProperty.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.ogcbase;
037
038 import static org.deegree.ogcbase.PropertyPathFactory.createPropertyPath;
039
040 import java.net.URI;
041 import java.util.Map;
042
043 import org.deegree.datatypes.QualifiedName;
044 import org.deegree.framework.xml.NamespaceContext;
045 import org.deegree.framework.xml.XMLParsingException;
046 import org.deegree.framework.xml.XMLTools;
047 import org.deegree.ogcwebservices.InvalidParameterValueException;
048 import org.w3c.dom.Element;
049 import org.w3c.dom.Node;
050 import org.w3c.dom.Text;
051
052 /**
053 * Java incarnation of the <code>SortPropertyType</code> defined in <code>sort.xsd</code> from the Filter Encoding
054 * Specification.
055 *
056 * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
057 * @author last edited by: $Author: aionita $
058 *
059 * @version $Revision: 23723 $, $Date: 2010-04-21 14:05:33 +0200 (Mi, 21 Apr 2010) $
060 */
061 public class SortProperty {
062
063 private static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
064
065 // property that will act as sort criterion
066 private PropertyPath sortProperty;
067
068 // true -> ascending, false -> descending
069 private boolean sortOrder;
070
071 private SortProperty( PropertyPath sortProperty, boolean sortOrder ) {
072 this.sortProperty = sortProperty;
073 this.sortOrder = sortOrder;
074 }
075
076 /**
077 * Returns the property that will act as sort criterion.
078 *
079 * @return the property that will act as sort criterion
080 */
081 public PropertyPath getSortProperty() {
082 return this.sortProperty;
083 }
084
085 /**
086 * Returns the sort order.
087 *
088 * @return true, if the sort order is ascending, false if it is descending
089 */
090 public boolean getSortOrder() {
091 return this.sortOrder;
092 }
093
094 /**
095 * Creates a new <code>SortProperty</code> instance.
096 *
097 * @param sortProperty
098 * @param sortOrderString
099 * must be "ASC" or "DESC"
100 * @return SortProperty instance
101 * @throws InvalidParameterValueException
102 * if sortOrderString is not "ASC" or "DESC"
103 */
104 public static SortProperty create( PropertyPath sortProperty, String sortOrderString )
105 throws InvalidParameterValueException {
106 boolean sortOrder;
107 if ( "DESC".equals( sortOrderString ) ) {
108 sortOrder = false;
109 } else if ( "ASC".equals( sortOrderString ) ) {
110 sortOrder = true;
111 } else {
112 String msg = "Invalid value '" + sortOrderString
113 + "' for 'SortOrder'. The only possible values are 'DESC' or 'ASC'";
114 throw new InvalidParameterValueException( msg );
115 }
116 return new SortProperty( sortProperty, sortOrder );
117 }
118
119 /**
120 * Creates an array of <code>SortProperty</code> instances from the giving string encoding (as described in OGC
121 * 04-021r2, page 136).
122 *
123 * @param sortBy
124 * @param nscontext
125 * @return array of <code>SortProperty</code> instances
126 */
127 public static SortProperty[] create( String sortBy, Map<String, URI> nscontext ) {
128
129 SortProperty[] sortProperties = null;
130
131 if ( sortBy != null ) {
132 String[] parts = sortBy.split( "," );
133 sortProperties = new SortProperty[parts.length];
134 for ( int i = 0; i < parts.length; i++ ) {
135 boolean sortOrder = false;
136
137 String name;
138
139 if ( parts[i].endsWith( " A" ) ) {
140 sortOrder = true;
141 name = parts[i].substring( 0, parts[i].length() - 2 );
142 } else if ( parts[i].endsWith( " D" ) ) {
143 sortOrder = false;
144 name = parts[i].substring( 0, parts[i].length() - 2 );
145 } else {
146 sortOrder = true; // it's the default
147 name = parts[i];
148 }
149
150 String[] nameps = name.split( ":" );
151
152 PropertyPath pp;
153 if ( nameps.length >= 2 ) {
154 pp = createPropertyPath( new QualifiedName( nameps[0], nameps[1], nscontext.get( nameps[0] ) ) );
155 } else {
156 pp = createPropertyPath( new QualifiedName( name ) );
157 }
158 sortProperties[i] = new SortProperty( pp, sortOrder );
159 }
160 }
161 return sortProperties;
162 }
163
164 /**
165 * Parses the given <code>SortProperty</code> element.
166 *
167 * @param element
168 * 'ogc:SortProperty'-element
169 * @return corresponding <code>SortProperty</code> instance
170 * @throws XMLParsingException
171 */
172 public static SortProperty create( Element element )
173 throws XMLParsingException {
174
175 Node node = XMLTools.getRequiredNode( element, "ogc:PropertyName/text()", nsContext );
176 PropertyPath propertyName = OGCDocument.parsePropertyPath( (Text) node );
177 String sortOrderString = XMLTools.getNodeAsString( element, "ogc:SortOrder/text()", nsContext, "ASC" );
178 boolean sortOrder;
179 if ( "ASC".equals( sortOrderString ) ) {
180 sortOrder = true;
181 } else if ( "DESC".equals( sortOrderString ) ) {
182 sortOrder = false;
183 } else {
184 String msg = "Invalid value ('" + sortOrderString + "') for 'SortOrder'. The only "
185 + "valid values are 'ASC' or 'DESC'.";
186 throw new XMLParsingException( msg );
187 }
188
189 SortProperty sortProperty = new SortProperty( propertyName, sortOrder );
190 return sortProperty;
191 }
192 }