001: package org.jacorb.naming;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.util.*;
024:
025: import org.omg.CORBA.INTERNAL;
026: import org.omg.CosNaming.*;
027: import org.omg.CosNaming.NamingContextPackage.*;
028:
029: /**
030: * A convenience class for names and converting
031: * between Names and their string representation
032: *
033: * @author Gerald Brose, FU Berlin
034: * @version $Id: Name.java,v 1.14 2006/06/14 11:56:28 alphonse.bendt Exp $
035: */
036:
037: public class Name implements java.io.Serializable {
038: private NameComponent[] fullName;
039: private NameComponent baseName;
040:
041: /** context part of this Name */
042: private NameComponent[] ctxName;
043:
044: public Name() {
045: fullName = null;
046: baseName = null;
047: ctxName = null;
048: }
049:
050: /**
051: * create a name from an array of NameComponents
052: */
053:
054: public Name(NameComponent[] n) throws InvalidName {
055: if (n == null || n.length == 0)
056: throw new InvalidName();
057:
058: fullName = n;
059: baseName = n[n.length - 1];
060: if (n.length > 1) {
061: ctxName = new NameComponent[n.length - 1];
062: for (int i = 0; i < n.length - 1; i++)
063: ctxName[i] = n[i];
064: } else
065: ctxName = null;
066: }
067:
068: /**
069: * create a name from a stringified name
070: */
071:
072: public Name(String string_name)
073: throws org.omg.CosNaming.NamingContextPackage.InvalidName {
074: this (toName(string_name));
075: }
076:
077: /**
078: * create a name from a singleNameComponent
079: */
080:
081: public Name(org.omg.CosNaming.NameComponent n)
082: throws org.omg.CosNaming.NamingContextPackage.InvalidName {
083: if (n == null)
084: throw new org.omg.CosNaming.NamingContextPackage.InvalidName();
085: baseName = n;
086: fullName = new org.omg.CosNaming.NameComponent[1];
087: fullName[0] = n;
088: ctxName = null;
089: }
090:
091: /**
092: * @return a NameComponent object representing the unstructured
093: * base name of this structured name
094: */
095:
096: public org.omg.CosNaming.NameComponent baseNameComponent() {
097: return baseName;
098: }
099:
100: public String kind() {
101: return baseName.kind;
102: }
103:
104: /**
105: * @return this name as an array of org.omg.CosNaming.NameComponent,
106: * neccessary for a number of operations on naming context
107: */
108:
109: public org.omg.CosNaming.NameComponent[] components() {
110: return fullName;
111: }
112:
113: /**
114: * @return a Name object representing the name of the enclosing context
115: */
116:
117: public Name ctxName() {
118: // null if no further context
119: if (ctxName != null) {
120: try {
121: return new Name(ctxName);
122: } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
123: throw new INTERNAL(e.toString());
124: }
125: }
126: return null;
127: }
128:
129: public boolean equals(Object obj) {
130: if (obj == null)
131: return false;
132: if (!(obj instanceof Name))
133: return false;
134: return (toString().equals(obj.toString()));
135: }
136:
137: public Name fullName()
138: throws org.omg.CosNaming.NamingContextPackage.InvalidName {
139: return new Name(fullName);
140: }
141:
142: public int hashCode() {
143: return toString().hashCode();
144: }
145:
146: /**
147: * @return the string representation of this name
148: */
149:
150: public String toString() {
151: try {
152: return toString(fullName);
153: } catch (InvalidName in) {
154: return "<invalid>";
155: }
156: }
157:
158: /**
159: * @return a single NameComponent, parsed from sn
160: */
161:
162: private static org.omg.CosNaming.NameComponent getComponent(
163: String sn)
164: throws org.omg.CosNaming.NamingContextPackage.InvalidName {
165: char ch;
166: int len = sn.length();
167: boolean inKind = false;
168: StringBuffer id = new StringBuffer();
169: StringBuffer kind = new StringBuffer();
170:
171: for (int i = 0; i < len; i++) {
172: ch = sn.charAt(i);
173:
174: if (ch == '\\') {
175: // Escaped character
176:
177: i++;
178: if (i >= len) {
179: throw new InvalidName();
180: }
181: ch = sn.charAt(i);
182: } else if (ch == '.') {
183: // id/kind separator character
184:
185: if (inKind) {
186: throw new InvalidName();
187: }
188: inKind = true;
189: continue;
190: }
191: if (inKind) {
192: kind.append(ch);
193: } else {
194: id.append(ch);
195: }
196: }
197:
198: return (new org.omg.CosNaming.NameComponent(id.toString(), kind
199: .toString()));
200: }
201:
202: /**
203: *
204: * @return an a array of NameComponents
205: * @throws org.omg.CosNaming.NamingContextPackage.InvalidName
206: */
207:
208: public static org.omg.CosNaming.NameComponent[] toName(String sn)
209: throws org.omg.CosNaming.NamingContextPackage.InvalidName {
210: if (sn == null || sn.length() == 0 || sn.startsWith("/"))
211: throw new InvalidName();
212:
213: Vector v = new Vector();
214:
215: int start = 0;
216: int i = 0;
217: for (; i < sn.length(); i++) {
218: if (sn.charAt(i) == '/' && sn.charAt(i - 1) != '\\') {
219: if (i - start == 0)
220: throw new InvalidName();
221: v.addElement(getComponent(sn.substring(start, i)));
222: start = i + 1;
223: }
224: }
225: if (start < i)
226: v.addElement(getComponent(sn.substring(start, i)));
227:
228: org.omg.CosNaming.NameComponent[] result = new org.omg.CosNaming.NameComponent[v
229: .size()];
230:
231: for (int j = 0; j < result.length; j++) {
232: result[j] = (org.omg.CosNaming.NameComponent) v
233: .elementAt(j);
234: }
235: return result;
236: }
237:
238: /**
239: * @return the string representation of this NameComponent array
240: */
241:
242: public static String toString(org.omg.CosNaming.NameComponent[] n)
243: throws org.omg.CosNaming.NamingContextPackage.InvalidName {
244: if (n == null || n.length == 0)
245: throw new org.omg.CosNaming.NamingContextPackage.InvalidName();
246:
247: StringBuffer b = new StringBuffer();
248: for (int i = 0; i < n.length; i++) {
249: if (i > 0)
250: b.append("/");
251:
252: if (n[i].id.length() > 0)
253: b.append(escape(n[i].id));
254:
255: if (n[i].kind.length() > 0 || n[i].id.length() == 0)
256: b.append(".");
257:
258: if (n[i].kind.length() > 0)
259: b.append(escape(n[i].kind));
260: }
261: return b.toString();
262: }
263:
264: /**
265: * escape any occurrence of "/", "." and "\"
266: */
267:
268: private static String escape(String s) {
269: StringBuffer sb = new StringBuffer(s);
270: for (int i = 0; i < sb.length(); i++) {
271: if (sb.charAt(i) == '/' || sb.charAt(i) == '\\'
272: || sb.charAt(i) == '.') {
273: sb.insert(i, '\\');
274: i++;
275: }
276: }
277: return sb.toString();
278: }
279:
280: }
|