gnuContext.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* gnuContext.java --
  2. Copyright (C) 2005 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;
  32. import java.util.Hashtable;
  33. import java.util.Iterator;
  34. import java.util.Map;
  35. import java.util.Set;
  36. import org.omg.CORBA.Any;
  37. import org.omg.CORBA.Bounds;
  38. import org.omg.CORBA.CTX_RESTRICT_SCOPE;
  39. import org.omg.CORBA.Context;
  40. import org.omg.CORBA.NVList;
  41. /**
  42. * The working implementation of the {@link org.omg.CORBA.Context}.
  43. * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  44. */
  45. public class gnuContext
  46. extends Context
  47. {
  48. /**
  49. * The parent context.
  50. */
  51. Context parent;
  52. /**
  53. * The collection to store the context properties.
  54. */
  55. Map properties = new Hashtable();
  56. /**
  57. * The name of this context.
  58. */
  59. String name;
  60. /**
  61. * Creates the new context with the given name and parent.
  62. *
  63. * @param a_name a name of the new context.
  64. * @param a_parent a parent, used to resolve the missing references.
  65. */
  66. public gnuContext(String a_name, Context a_parent)
  67. {
  68. name = a_name;
  69. parent = a_parent;
  70. }
  71. /** {@inheritDoc} */
  72. public String context_name()
  73. {
  74. return name;
  75. }
  76. /** {@inheritDoc} */
  77. public Context create_child(String child)
  78. {
  79. return new gnuContext(child, this);
  80. }
  81. /** {@inheritDoc} */
  82. public void delete_values(String property)
  83. {
  84. boolean starts = false;
  85. if (property.endsWith("*"))
  86. {
  87. starts = true;
  88. property = property.substring(0, property.length() - 1);
  89. }
  90. Set keys = properties.keySet();
  91. Iterator iter = keys.iterator();
  92. while (iter.hasNext())
  93. {
  94. String key = (String) iter.next();
  95. if ((starts && key.startsWith(property)) ||
  96. (!starts && key.equals(property))
  97. )
  98. iter.remove();
  99. }
  100. }
  101. /** {@inheritDoc} */
  102. public NVList get_values(String start_scope, int flags, String pattern)
  103. {
  104. if (start_scope != null)
  105. {
  106. Context c = this;
  107. while (c != null && !c.context_name().equals(start_scope))
  108. c = c.parent();
  109. if (c == null)
  110. return new gnuNVList();
  111. }
  112. try
  113. {
  114. gnuNVList rt = new gnuNVList();
  115. boolean starts = false;
  116. if (pattern.endsWith("*"))
  117. {
  118. starts = true;
  119. pattern = pattern.substring(0, pattern.length() - 1);
  120. }
  121. Set keys = properties.keySet();
  122. Iterator iter = keys.iterator();
  123. while (iter.hasNext())
  124. {
  125. String key = (String) iter.next();
  126. if ((starts && key.startsWith(pattern)) ||
  127. (!starts && key.equals(pattern))
  128. )
  129. {
  130. rt.add_value(key, (Any) properties.get(key), 0);
  131. }
  132. }
  133. if ((flags & CTX_RESTRICT_SCOPE.value) == 0 && parent != null)
  134. {
  135. NVList par = parent.get_values(start_scope, flags, pattern);
  136. for (int i = 0; i < par.count(); i++)
  137. {
  138. rt.list.add(par.item(i));
  139. }
  140. }
  141. return rt;
  142. }
  143. catch (Bounds ex)
  144. {
  145. throw new Error("Report this bug.");
  146. }
  147. }
  148. /** {@inheritDoc} */
  149. public Context parent()
  150. {
  151. return parent;
  152. }
  153. /** {@inheritDoc} */
  154. public void set_one_value(String name, Any value)
  155. {
  156. properties.put(name, value);
  157. }
  158. /** {@inheritDoc} */
  159. public void set_values(NVList values)
  160. {
  161. try
  162. {
  163. for (int i = 0; i < values.count(); i++)
  164. {
  165. properties.put(values.item(i).name(), values.item(i).value());
  166. }
  167. }
  168. catch (Bounds ex)
  169. {
  170. throw new Error("Please report this bug.");
  171. }
  172. }
  173. }