01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.jndi.internal.parser;
19:
20: import java.util.ArrayList;
21: import java.util.Collections;
22: import java.util.List;
23:
24: import javax.naming.InvalidNameException;
25: import javax.naming.Name;
26: import javax.naming.NameParser;
27: import javax.naming.NamingException;
28: import javax.naming.ldap.LdapName;
29: import javax.naming.ldap.Rdn;
30:
31: /**
32: * A Distinguised Name Parser according with the RFC2253 and RFC1779 for use
33: * with LdapName only
34: */
35: public class LdapNameParser implements NameParser, LdapParser {
36: private String s = null;
37:
38: /**
39: * Constructor
40: *
41: * @param s
42: * the string to parse
43: */
44: public LdapNameParser(String s) {
45: this .s = s;
46: }
47:
48: /**
49: * For using with the NameParser interface
50: *
51: * @return an LdapName if possible
52: */
53: public Name parse(String arg0) throws NamingException {
54: return new LdapName(arg0);
55: }
56:
57: /**
58: * Returns the parsed list of Rdns
59: */
60: public List getList() throws InvalidNameException {
61: List list = new ArrayList();
62: int from = 0;
63: char[] c = s.toCharArray();
64:
65: if (s.equals("")) {
66: return list;
67: }
68:
69: if (s.startsWith(",") || s.startsWith(";")) {
70: throw new InvalidNameException("Invalid name: " + s);
71: }
72:
73: for (int i = 0; i < c.length; i++) {
74: if ((c[i] == ',' || c[i] == ';') && c[i - 1] != '\\') {
75:
76: String sub = s.substring(from, i);
77: if (sub.equals("")) {
78: throw new InvalidNameException("Invalid name: " + s);
79: }
80: list.add(new Rdn(sub));
81: from = i + 1;
82: }
83: }
84: list.add(new Rdn(s.substring(from, s.length())));
85: Collections.reverse(list);
86: return list;
87: }
88: }
|