001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.naming.ldap;
019:
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.util.ArrayList;
024: import java.util.Enumeration;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import javax.naming.InvalidNameException;
029: import javax.naming.Name;
030:
031: import org.apache.harmony.jndi.internal.nls.Messages;
032: import org.apache.harmony.jndi.internal.parser.LdapNameParser;
033:
034: /**
035: * TODO: JavaDoc
036: */
037: public class LdapName implements Name {
038:
039: private static final long serialVersionUID = -1595520034788997356L;
040:
041: private transient List<Rdn> rdns;
042:
043: private transient String rdnsStr;
044:
045: public LdapName(List<Rdn> rdns) {
046: if (rdns == null) {
047: throw new NullPointerException("rdns "
048: + Messages.getString("ldap.00"));
049: }
050:
051: this .rdns = new ArrayList<Rdn>(rdns);
052: }
053:
054: public LdapName(String name) throws InvalidNameException {
055: rdnsStr = name;
056: LdapNameParser parser = new LdapNameParser(rdnsStr);
057:
058: this .rdns = parser.getList();
059: }
060:
061: public Name add(int posn, Rdn comp) {
062: if (comp == null) {
063: throw new NullPointerException("comp "
064: + Messages.getString("ldap.00"));
065: }
066:
067: if (posn < 0 || posn > rdns.size()) {
068: throw new IndexOutOfBoundsException(Messages
069: .getString("ldap.05"));
070: }
071:
072: rdns.add(posn, comp);
073: rdnsStr = null;
074: return this ;
075: }
076:
077: public Name add(int posn, String comp) throws InvalidNameException {
078: return add(posn, new Rdn(comp));
079: }
080:
081: public Name add(Rdn comp) {
082: return add(rdns.size(), comp);
083: }
084:
085: public Name add(String comp) throws InvalidNameException {
086: return add(rdns.size(), comp);
087: }
088:
089: public Name addAll(int posn, List<Rdn> suffixRdns) {
090: if (suffixRdns == null) {
091: throw new NullPointerException("suffixRdns "
092: + Messages.getString("ldap.00"));
093: }
094:
095: if (posn < 0 || posn > rdns.size()) {
096: throw new IndexOutOfBoundsException(Messages
097: .getString("ldap.00"));
098: }
099:
100: rdns.addAll(posn, suffixRdns);
101: rdnsStr = null;
102: return this ;
103: }
104:
105: public Name addAll(int posn, Name suffix)
106: throws InvalidNameException {
107: if (suffix instanceof LdapName) {
108: return addAll(posn, ((LdapName) suffix).rdns);
109: }
110: List<Rdn> rdns = new ArrayList<Rdn>();
111: for (Enumeration<?> iter = suffix.getAll(); iter
112: .hasMoreElements();) {
113: rdns.add(new Rdn((String) iter.nextElement()));
114: }
115: return addAll(posn, rdns);
116: }
117:
118: public Name addAll(List<Rdn> suffixRdns) {
119: return addAll(rdns.size(), suffixRdns);
120: }
121:
122: public Name addAll(Name suffix) throws InvalidNameException {
123: return addAll(rdns.size(), suffix);
124: }
125:
126: public Object clone() {
127: try {
128: if (rdnsStr != null) {
129: return new LdapName(rdnsStr);
130: }
131: } catch (InvalidNameException e) {
132: }
133: List<Rdn> lista = new ArrayList<Rdn>();
134: for (int i = 0; i < rdns.size(); i++) {
135: lista.add(rdns.get(i));
136: }
137: return new LdapName(lista);
138: }
139:
140: public int compareTo(Object obj) {
141: if (obj == null || !(obj instanceof LdapName)) {
142: throw new ClassCastException("obj "
143: + Messages.getString("ldap.01"));
144: }
145:
146: LdapName ln = (LdapName) obj;
147:
148: Iterator<?> iter = rdns.iterator();
149: Iterator<?> iter2 = ln.rdns.iterator();
150:
151: while (iter.hasNext() && iter2.hasNext()) {
152: int c = iter.next().toString().toLowerCase().compareTo(
153: iter2.next().toString().toLowerCase());
154:
155: if (c != 0) {
156: return c;
157: }
158: }
159:
160: if (iter.hasNext())
161: return 1;
162: if (iter2.hasNext())
163: return -1;
164:
165: return 0;
166: }
167:
168: public boolean endsWith(List<Rdn> rdns) {
169: try {
170: Iterator<?> iter = rdns.iterator();
171: Iterator<?> iter2 = ((LdapName) getSuffix(this .rdns.size()
172: - rdns.size())).rdns.iterator();
173:
174: while (iter.hasNext()) {
175: if (!((Rdn) iter.next()).equals(iter2.next())) {
176: return false;
177: }
178: }
179:
180: return true;
181: } catch (RuntimeException e) {
182: return false;
183: }
184: }
185:
186: public boolean endsWith(Name n) {
187: try {
188: return n.equals(getSuffix(rdns.size() - n.size()));
189: } catch (RuntimeException e) {
190: return false;
191: }
192: }
193:
194: public boolean equals(Object obj) {
195: if (obj == null || !(obj instanceof LdapName)) {
196: return false;
197: }
198:
199: LdapName ln = (LdapName) obj;
200:
201: if (ln.rdns.size() != rdns.size()) {
202: return false;
203: }
204:
205: Iterator<Rdn> iter = ln.rdns.iterator();
206: Iterator<Rdn> iter2 = rdns.iterator();
207: while (iter.hasNext()) {
208: if (!iter.next().equals(iter2.next())) {
209: return false;
210: }
211: }
212: return true;
213: }
214:
215: public String get(int posn) {
216: return getRdn(posn).toString();
217: }
218:
219: public Enumeration<String> getAll() {
220: final Iterator<Rdn> rdns = getRdns().iterator();
221:
222: return new Enumeration<String>() {
223:
224: public boolean hasMoreElements() {
225: return rdns.hasNext();
226: }
227:
228: public String nextElement() {
229: return rdns.next().toString();
230: }
231: };
232: }
233:
234: public Name getPrefix(int posn) {
235: if (posn < 0) {
236: throw new IndexOutOfBoundsException(Messages
237: .getString("ldap.02"));
238: }
239: return new LdapName(rdns.subList(0, posn));
240: }
241:
242: public Rdn getRdn(int posn) {
243: return rdns.get(posn);
244: }
245:
246: public List<Rdn> getRdns() {
247: return rdns;
248: }
249:
250: public Name getSuffix(int posn) {
251: if (posn > rdns.size()) {
252: throw new IndexOutOfBoundsException(Messages
253: .getString("ldap.02"));
254: }
255:
256: return new LdapName(rdns.subList(posn, rdns.size()));
257: }
258:
259: public int hashCode() {
260: int sum = 0;
261: for (Iterator<?> iter = rdns.iterator(); iter.hasNext();) {
262: sum += iter.next().hashCode();
263: }
264: return sum;
265: }
266:
267: public boolean isEmpty() {
268: return rdns.size() == 0;
269: }
270:
271: public Object remove(int posn) throws InvalidNameException {
272: rdnsStr = null;
273: return rdns.remove(posn).toString();
274: }
275:
276: public int size() {
277: return rdns.size();
278: }
279:
280: public boolean startsWith(List<Rdn> rdns) {
281: try {
282: Iterator<?> iter = rdns.iterator();
283: Iterator<?> iter2 = ((LdapName) getPrefix(rdns.size())).rdns
284: .iterator();
285:
286: while (iter.hasNext()) {
287: if (!((Rdn) iter.next()).equals(iter2.next())) {
288: return false;
289: }
290: }
291: return true;
292: } catch (RuntimeException e) {
293: return false;
294: }
295: }
296:
297: public boolean startsWith(Name n) {
298: try {
299: return n.equals(getPrefix(n.size()));
300: } catch (RuntimeException e) {
301: return false;
302: }
303:
304: }
305:
306: public String toString() {
307: if (rdnsStr != null) {
308: return rdnsStr;
309: }
310: if (rdns.size() == 0) {
311: return ""; //$NON-NLS-1$
312: }
313:
314: StringBuffer sb = new StringBuffer();
315: sb.append(rdns.get(rdns.size() - 1).toString());
316: for (int i = rdns.size() - 2; i >= 0; i--) {
317: sb.append(',');
318: sb.append(rdns.get(i).toString());
319: }
320: return sb.toString();
321: }
322:
323: @SuppressWarnings("unchecked")
324: private void readObject(ObjectInputStream ois) throws IOException,
325: ClassNotFoundException, InvalidNameException {
326: ois.defaultReadObject();
327: LdapNameParser parser = new LdapNameParser((String) ois
328: .readObject());
329: this .rdns = parser.getList();
330: }
331:
332: private void writeObject(ObjectOutputStream oos) throws IOException {
333: oos.defaultWriteObject();
334: oos.writeObject(this.toString());
335: }
336:
337: }
|