50 private static final char compSeparator = '/';
51 private static final char escapeChar = '\\';
52 static {
53 mySyntax.put("jndi.syntax.direction", "left_to_right");
54 mySyntax.put("jndi.syntax.separator", ""+compSeparator);
55 mySyntax.put("jndi.syntax.escape", ""+escapeChar);
56 };
57
58 /**
59 * Constructs a new name parser for parsing names in INS syntax.
60 */
61 public CNNameParser() {
62 }
63
64 /**
65 * Returns a CompoundName given a string in INS syntax.
66 * @param name The non-null string representation of the name.
67 * @return a non-null CompoundName
68 */
69 public Name parse(String name) throws NamingException {
70 Vector comps = insStringToStringifiedComps(name);
71 return new CNCompoundName(comps.elements());
72 }
73
74 /**
75 * Creates a NameComponent[] from a Name structure.
76 * Used by CNCtx to convert the input Name arg into a NameComponent[].
77 * @param a CompoundName or a CompositeName;
78 * each component must be the stringified form of a NameComponent.
79 */
80 static NameComponent[] nameToCosName(Name name)
81 throws InvalidNameException {
82 int len = name.size();
83 if (len == 0) {
84 return new NameComponent[0];
85 }
86
87 NameComponent[] answer = new NameComponent[len];
88 for (int i = 0; i < len; i++) {
89 answer[i] = parseComponent(name.get(i));
90 }
111 * Used by ExceptionMapper and CNBindingEnumeration to convert
112 * a NameComponent[] into a composite name.
113 */
114 static Name cosNameToName(NameComponent[] cname) {
115 Name nm = new CompositeName();
116 for ( int i = 0; cname != null && i < cname.length; i++) {
117 try {
118 nm.add(stringifyComponent(cname[i]));
119 } catch (InvalidNameException e) {
120 // ignore
121 }
122 }
123 return nm;
124 }
125
126 /**
127 * Converts an INS-syntax string name into a Vector in which
128 * each element of the vector contains a stringified form of
129 * a NameComponent.
130 */
131 private static Vector insStringToStringifiedComps(String str)
132 throws InvalidNameException {
133
134 int len = str.length();
135 Vector components = new Vector(10);
136 char[] id = new char[len];
137 char[] kind = new char[len];
138 int idCount, kindCount;
139 boolean idMode;
140 for (int i = 0; i < len; ) {
141 idCount = kindCount = 0; // reset for new component
142 idMode = true; // always start off parsing id
143 while (i < len) {
144 if (str.charAt(i) == compSeparator) {
145 break;
146
147 } else if (str.charAt(i) == escapeChar) {
148 if (i + 1 >= len) {
149 throw new InvalidNameException(str +
150 ": unescaped \\ at end of component");
151 } else if (isMeta(str.charAt(i+1))) {
152 ++i; // skip escape and let meta through
153 if (idMode) {
154 id[idCount++] = str.charAt(i++);
155 } else {
289 }
290
291 /**
292 * In INS, there are three meta characters: '.', '/' and '\'.
293 */
294 private static boolean isMeta(char ch) {
295 switch (ch) {
296 case kindSeparator:
297 case compSeparator:
298 case escapeChar:
299 return true;
300 }
301 return false;
302 }
303
304 /**
305 * An implementation of CompoundName that bypasses the parsing
306 * and stringifying code of the default CompoundName.
307 */
308 static final class CNCompoundName extends CompoundName {
309 CNCompoundName(Enumeration enum_) {
310 super(enum_, CNNameParser.mySyntax);
311 }
312
313 public Object clone() {
314 return new CNCompoundName(getAll());
315 }
316
317 public Name getPrefix(int posn) {
318 Enumeration comps = super.getPrefix(posn).getAll();
319 return new CNCompoundName(comps);
320 }
321
322 public Name getSuffix(int posn) {
323 Enumeration comps = super.getSuffix(posn).getAll();
324 return new CNCompoundName(comps);
325 }
326
327 public String toString() {
328 try {
329 // Convert Name to NameComponent[] then stringify
330 return cosNameToInsString(nameToCosName(this));
331 } catch (InvalidNameException e) {
332 return super.toString();
333 }
334 }
335
336 private static final long serialVersionUID = -6599252802678482317L;
337 }
338
339 // for testing only
340 /*
341 private static void print(String input) {
342 try {
343 System.out.println("\n >>>>>> input: " + input);
|
50 private static final char compSeparator = '/';
51 private static final char escapeChar = '\\';
52 static {
53 mySyntax.put("jndi.syntax.direction", "left_to_right");
54 mySyntax.put("jndi.syntax.separator", ""+compSeparator);
55 mySyntax.put("jndi.syntax.escape", ""+escapeChar);
56 };
57
58 /**
59 * Constructs a new name parser for parsing names in INS syntax.
60 */
61 public CNNameParser() {
62 }
63
64 /**
65 * Returns a CompoundName given a string in INS syntax.
66 * @param name The non-null string representation of the name.
67 * @return a non-null CompoundName
68 */
69 public Name parse(String name) throws NamingException {
70 Vector<String> comps = insStringToStringifiedComps(name);
71 return new CNCompoundName(comps.elements());
72 }
73
74 /**
75 * Creates a NameComponent[] from a Name structure.
76 * Used by CNCtx to convert the input Name arg into a NameComponent[].
77 * @param a CompoundName or a CompositeName;
78 * each component must be the stringified form of a NameComponent.
79 */
80 static NameComponent[] nameToCosName(Name name)
81 throws InvalidNameException {
82 int len = name.size();
83 if (len == 0) {
84 return new NameComponent[0];
85 }
86
87 NameComponent[] answer = new NameComponent[len];
88 for (int i = 0; i < len; i++) {
89 answer[i] = parseComponent(name.get(i));
90 }
111 * Used by ExceptionMapper and CNBindingEnumeration to convert
112 * a NameComponent[] into a composite name.
113 */
114 static Name cosNameToName(NameComponent[] cname) {
115 Name nm = new CompositeName();
116 for ( int i = 0; cname != null && i < cname.length; i++) {
117 try {
118 nm.add(stringifyComponent(cname[i]));
119 } catch (InvalidNameException e) {
120 // ignore
121 }
122 }
123 return nm;
124 }
125
126 /**
127 * Converts an INS-syntax string name into a Vector in which
128 * each element of the vector contains a stringified form of
129 * a NameComponent.
130 */
131 private static Vector<String> insStringToStringifiedComps(String str)
132 throws InvalidNameException {
133
134 int len = str.length();
135 Vector<String> components = new Vector<>(10);
136 char[] id = new char[len];
137 char[] kind = new char[len];
138 int idCount, kindCount;
139 boolean idMode;
140 for (int i = 0; i < len; ) {
141 idCount = kindCount = 0; // reset for new component
142 idMode = true; // always start off parsing id
143 while (i < len) {
144 if (str.charAt(i) == compSeparator) {
145 break;
146
147 } else if (str.charAt(i) == escapeChar) {
148 if (i + 1 >= len) {
149 throw new InvalidNameException(str +
150 ": unescaped \\ at end of component");
151 } else if (isMeta(str.charAt(i+1))) {
152 ++i; // skip escape and let meta through
153 if (idMode) {
154 id[idCount++] = str.charAt(i++);
155 } else {
289 }
290
291 /**
292 * In INS, there are three meta characters: '.', '/' and '\'.
293 */
294 private static boolean isMeta(char ch) {
295 switch (ch) {
296 case kindSeparator:
297 case compSeparator:
298 case escapeChar:
299 return true;
300 }
301 return false;
302 }
303
304 /**
305 * An implementation of CompoundName that bypasses the parsing
306 * and stringifying code of the default CompoundName.
307 */
308 static final class CNCompoundName extends CompoundName {
309 CNCompoundName(Enumeration<String> enum_) {
310 super(enum_, CNNameParser.mySyntax);
311 }
312
313 public Object clone() {
314 return new CNCompoundName(getAll());
315 }
316
317 public Name getPrefix(int posn) {
318 Enumeration<String> comps = super.getPrefix(posn).getAll();
319 return new CNCompoundName(comps);
320 }
321
322 public Name getSuffix(int posn) {
323 Enumeration<String> comps = super.getSuffix(posn).getAll();
324 return new CNCompoundName(comps);
325 }
326
327 public String toString() {
328 try {
329 // Convert Name to NameComponent[] then stringify
330 return cosNameToInsString(nameToCosName(this));
331 } catch (InvalidNameException e) {
332 return super.toString();
333 }
334 }
335
336 private static final long serialVersionUID = -6599252802678482317L;
337 }
338
339 // for testing only
340 /*
341 private static void print(String input) {
342 try {
343 System.out.println("\n >>>>>> input: " + input);
|