NameTransformer.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* NameTransformer.java --
  2. Copyright (C) 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.CORBA.NamingService;
  32. import gnu.java.lang.CPStringBuilder;
  33. import org.omg.CORBA.IntHolder;
  34. import org.omg.CosNaming.NameComponent;
  35. import org.omg.CosNaming.NamingContextPackage.InvalidName;
  36. import java.util.ArrayList;
  37. import java.util.StringTokenizer;
  38. /**
  39. * This class converts between string and array representations of the
  40. * multi component object names.
  41. *
  42. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  43. */
  44. public class NameTransformer
  45. {
  46. /**
  47. * A string, indicating the escape character.
  48. */
  49. public static final String ESCAPE = "\\";
  50. /**
  51. * Convert the string name representation into the array name
  52. * representation. See {@link #toString(NameComponent)} for the
  53. * description of this format.
  54. *
  55. * @param a_name the string form of the name.
  56. *
  57. * @return the array form of the name.
  58. *
  59. * @throws InvalidName if the name cannot be parsed.
  60. */
  61. public NameComponent[] toName(String a_name)
  62. throws InvalidName
  63. {
  64. ArrayList components = new ArrayList();
  65. StringTokenizer st = new StringTokenizer(a_name, "./\\", true);
  66. // Create the buffer array, reserving the last element for null.
  67. String[] n = new String[ st.countTokens() + 1 ];
  68. int pp = 0;
  69. while (st.hasMoreTokens())
  70. n [ pp++ ] = st.nextToken();
  71. IntHolder p = new IntHolder();
  72. NameComponent node = readNode(p, n);
  73. while (node != null)
  74. {
  75. components.add(node);
  76. node = readNode(p, n);
  77. }
  78. NameComponent[] name = new NameComponent[ components.size() ];
  79. for (int i = 0; i < name.length; i++)
  80. {
  81. name [ i ] = (NameComponent) components.get(i);
  82. }
  83. NameValidator.check(name);
  84. return name;
  85. }
  86. /**
  87. * Converts the name into its string representation, as defined in
  88. * the specification CORBA naming service.
  89. *
  90. * A string representation for the name consists of the name components,
  91. * separated by a slash '/' character (for example, 'a/b/c'). If the
  92. * {@link NameComponent#kind} field is not empty, it is given after
  93. * period ('.'), for example 'a.b/c.d/.' .
  94. * The period alone represents node where part where both
  95. * {@link NameComponent#kind} and {@link NameComponent#id} are empty strings.
  96. *
  97. * If slash or dot are part of the name, they are escaped by backslash ('\').
  98. * If the backslash itself is part of the name, it is doubled.
  99. *
  100. * @param a_name a name to convert.
  101. * @return a string representation.
  102. */
  103. public String toString(NameComponent[] a_name)
  104. throws InvalidName
  105. {
  106. NameValidator.check(a_name);
  107. CPStringBuilder b = new CPStringBuilder();
  108. NameComponent n;
  109. for (int ni = 0; ni < a_name.length; ni++)
  110. {
  111. n = a_name [ ni ];
  112. appEscaping(b, n.id);
  113. if (n.kind.length() > 0)
  114. {
  115. b.append('.');
  116. appEscaping(b, n.kind);
  117. }
  118. if (ni < a_name.length - 1)
  119. b.append('/');
  120. }
  121. return b.toString();
  122. }
  123. /**
  124. * Append the contents of the string to this
  125. * string buffer, inserting the escape sequences, where required.
  126. *
  127. * @param b a buffer to append the contents to.
  128. * @param s a string to append.
  129. */
  130. private void appEscaping(CPStringBuilder b, String s)
  131. {
  132. char c;
  133. for (int i = 0; i < s.length(); i++)
  134. {
  135. c = s.charAt(i);
  136. switch (c)
  137. {
  138. case '.' :
  139. case '/' :
  140. case '\\' :
  141. b.append('\\');
  142. b.append(c);
  143. break;
  144. default :
  145. b.append(c);
  146. break;
  147. }
  148. }
  149. }
  150. /**
  151. * Assert the end of the current name component.
  152. */
  153. private void assertEndOfNode(IntHolder p, String[] t)
  154. throws InvalidName
  155. {
  156. if (t [ p.value ] != null)
  157. if (!t [ p.value ].equals("/"))
  158. throw new InvalidName("End of node expected at token " + p.value);
  159. }
  160. /**
  161. * Read the named component node. After reading the current positon
  162. * advances to the beginning of the next node in an array.
  163. *
  164. * @param p the current position being wrapped inside the passed
  165. * IntHolder.
  166. *
  167. * @param t the text buffer.
  168. *
  169. * @return the created node.
  170. */
  171. private NameComponent readNode(IntHolder p, String[] t)
  172. throws InvalidName
  173. {
  174. // End of stream has been reached.
  175. if (t [ p.value ] == null)
  176. return null;
  177. NameComponent n = new NameComponent();
  178. if (t [ p.value ].equals("."))
  179. {
  180. // The 'id' is missing, but the 'kind' may follow.
  181. n.id = "";
  182. p.value++;
  183. n.kind = readPart(p, t);
  184. assertEndOfNode(p, t);
  185. if (t [ p.value ] != null)
  186. p.value++;
  187. }
  188. else if (t [ p.value ].equals("/"))
  189. {
  190. // This is not allowed here and may happen only
  191. // on two subsequent slashes.
  192. throw new InvalidName("Unexpected '/' token " + p.value);
  193. }
  194. else
  195. {
  196. n.id = readPart(p, t);
  197. // If some chars follow the id.
  198. if (t [ p.value ] != null)
  199. {
  200. // Dot means that the kind part follows
  201. if (t [ p.value ].equals("."))
  202. {
  203. p.value++;
  204. n.kind = readPart(p, t);
  205. assertEndOfNode(p, t);
  206. if (t [ p.value ] != null)
  207. p.value++;
  208. }
  209. // The next name component follows - advance to
  210. // the beginning of the next name component.
  211. else if (t [ p.value ].equals("/"))
  212. {
  213. n.kind = "";
  214. p.value++;
  215. }
  216. else
  217. throw new InvalidName("Unexpected '" + t [ p.value ] +
  218. "' at token " + p.value
  219. );
  220. }
  221. else
  222. // Id, and then end of sequence.
  223. n.kind = "";
  224. }
  225. return n;
  226. }
  227. /**
  228. * Read the name part (id or kind).
  229. *
  230. * @param p the current position. After reading, advances
  231. * to the beginning of the next name fragment.
  232. *
  233. * @param t the string buffer.
  234. *
  235. * @return the name part with resolved escape sequences.
  236. */
  237. private String readPart(IntHolder p, String[] t)
  238. {
  239. CPStringBuilder part = new CPStringBuilder();
  240. while (t [ p.value ] != null && !t [ p.value ].equals(".") &&
  241. !t [ p.value ].equals("/")
  242. )
  243. {
  244. if (t [ p.value ].equals(ESCAPE))
  245. {
  246. p.value++;
  247. part.append(t [ p.value ]);
  248. }
  249. else
  250. part.append(t [ p.value ]);
  251. p.value++;
  252. }
  253. return part.toString();
  254. }
  255. public static void main(String[] args)
  256. {
  257. NameComponent a = new NameComponent("a", "ak");
  258. NameComponent b = new NameComponent("b/z", "b.k");
  259. NameComponent c = new NameComponent("c", "");
  260. NameTransformer sn = new NameTransformer();
  261. try
  262. {
  263. String s = sn.toString(new NameComponent[] { a, b, c });
  264. System.out.println(s);
  265. //NameComponent[] k = toName("a.k/b.k2/c/d/.");
  266. //NameComponent[] k = toName("a.bc/.b/c.x");
  267. NameComponent[] k = sn.toName(s);
  268. System.out.println("ToString");
  269. for (int i = 0; i < k.length; i++)
  270. {
  271. System.out.println(k [ i ].id + ":" + k [ i ].kind);
  272. }
  273. }
  274. catch (InvalidName ex)
  275. {
  276. ex.printStackTrace();
  277. }
  278. }
  279. }