| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 1999 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap; |
| 39 | |
| 40 | /** |
| 41 | * Represents sorting instructions for a particular attribute. |
| 42 | * |
| 43 | * @version 1.0 |
| 44 | */ |
| 45 | public class LDAPSortKey implements java.io.Serializable { |
| 46 | static final long serialVersionUID = -7044232342344864405L; |
| 47 | public final static int REVERSE = 0x81; |
| 48 | |
| 49 | /** |
| 50 | * Constructs a new <CODE>LDAPSortKey</CODE> object that will |
| 51 | * sort based on the specified instructions. |
| 52 | * @param keyDescription a single attribute specification by which to sort |
| 53 | * If preceded by a hyphen ("-"), the attribute is sorted in reverse order. |
| 54 | * You can also specify the object ID (OID) of a matching rule after |
| 55 | * a colon (":"). For example: |
| 56 | * <P> |
| 57 | * <UL> |
| 58 | * <LI><CODE>"cn"</CODE> (sort by the <CODE>cn</CODE> attribute) <P> |
| 59 | * <LI><CODE>"-cn"</CODE> (sort by the <CODE>cn</CODE> attribute in |
| 60 | * reverse order) <P> |
| 61 | * <LI><CODE>"-cn:1.2.3.4"</CODE> (sort by the <CODE>cn</CODE> |
| 62 | * attribute in reverse order and use the matching rule identified |
| 63 | * by the OID 1.2.3.4) <P> |
| 64 | *</UL> |
| 65 | * @see org.ietf.ldap.controls.LDAPSortControl |
| 66 | * @see org.ietf.ldap.controls.LDAPVirtualListControl |
| 67 | */ |
| 68 | public LDAPSortKey( String keyDescription ) { |
| 69 | if ( (keyDescription != null) && (keyDescription.length() > 0) ) { |
| 70 | if ( keyDescription.charAt( 0 ) == '-' ) { |
| 71 | m_reverse = true; |
| 72 | m_key = keyDescription.substring( 1 ); |
| 73 | } else { |
| 74 | m_reverse = false; |
| 75 | m_key = keyDescription; |
| 76 | } |
| 77 | int colonIndex = m_key.indexOf( ':' ); |
| 78 | if ( colonIndex == 0 ) |
| 79 | m_key = null; |
| 80 | else if ( colonIndex > 0 ) { |
| 81 | m_matchRule = m_key.substring( colonIndex+1 ); |
| 82 | m_key = m_key.substring( 0, colonIndex ); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Constructs a new <CODE>LDAPSortKey</CODE> object that will |
| 89 | * sort based on the specified attribute and sort order. |
| 90 | * @param key a single attribute by which to sort. For example: |
| 91 | * <P> |
| 92 | * <UL> |
| 93 | * <LI><CODE>"cn"</CODE> (sort by the <CODE>cn</CODE> attribute) |
| 94 | * <LI><CODE>"givenname"</CODE> (sort by the <CODE>givenname</CODE> |
| 95 | * attribute) |
| 96 | * </UL> |
| 97 | * @param reverse if <CODE>true</CODE>, the sorting is done in |
| 98 | * descending order |
| 99 | * @see org.ietf.ldap.controls.LDAPSortControl |
| 100 | * @see org.ietf.ldap.controls.LDAPVirtualListControl |
| 101 | */ |
| 102 | public LDAPSortKey( String key, |
| 103 | boolean reverse) { |
| 104 | m_key = key; |
| 105 | m_reverse = reverse; |
| 106 | m_matchRule = null; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Constructs a new <CODE>LDAPSortKey</CODE> object that will |
| 111 | * sort based on the specified attribute, sort order, and matching |
| 112 | * rule. |
| 113 | * @param key a single attribute by which to sort. For example: |
| 114 | * <P> |
| 115 | * <UL> |
| 116 | * <LI><CODE>"cn"</CODE> (sort by the <CODE>cn</CODE> attribute) |
| 117 | * <LI><CODE>"givenname"</CODE> (sort by the <CODE>givenname</CODE> |
| 118 | * attribute) |
| 119 | * </UL> |
| 120 | * @param reverse if <CODE>true</CODE>, the sorting is done in |
| 121 | * descending order |
| 122 | * @param matchRule object ID (OID) of the matching rule for |
| 123 | * the attribute (for example, <CODE>1.2.3.4</CODE>) |
| 124 | * @see org.ietf.ldap.controls.LDAPSortControl |
| 125 | * @see org.ietf.ldap.controls.LDAPVirtualListControl |
| 126 | */ |
| 127 | public LDAPSortKey( String key, |
| 128 | boolean reverse, |
| 129 | String matchRule) { |
| 130 | m_key = key; |
| 131 | m_reverse = reverse; |
| 132 | m_matchRule = matchRule; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the attribute by which to sort. |
| 137 | * @return a single attribute by which to sort. |
| 138 | */ |
| 139 | public String getKey() { |
| 140 | return m_key; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns <CODE>true</CODE> if sorting is to be done in descending order. |
| 145 | * @return <CODE>true</CODE> if sorting is to be done in descending order. |
| 146 | */ |
| 147 | public boolean getReverse() { |
| 148 | return m_reverse; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Returns the object ID (OID) of the matching rule used for sorting. |
| 153 | * If no matching rule is specified, <CODE>null</CODE> is returned. |
| 154 | * @return the object ID (OID) of the matching rule, or <CODE>null</CODE> |
| 155 | * if the sorting instructions specify no matching rule. |
| 156 | */ |
| 157 | public String getMatchRule() { |
| 158 | return m_matchRule; |
| 159 | } |
| 160 | |
| 161 | public String toString() { |
| 162 | |
| 163 | StringBuffer sb = new StringBuffer("{SortKey:"); |
| 164 | |
| 165 | sb.append(" key="); |
| 166 | sb.append(m_key); |
| 167 | |
| 168 | sb.append(" reverse="); |
| 169 | sb.append(m_reverse); |
| 170 | |
| 171 | if (m_matchRule != null) { |
| 172 | sb.append(" matchRule="); |
| 173 | sb.append(m_matchRule); |
| 174 | } |
| 175 | |
| 176 | sb.append("}"); |
| 177 | |
| 178 | return sb.toString(); |
| 179 | } |
| 180 | |
| 181 | private String m_key; |
| 182 | private boolean m_reverse; |
| 183 | private String m_matchRule; |
| 184 | } |
| 185 |