View Javadoc

1   package net.sf.tomp.xml.type;
2   
3   /***
4    * DOCUMENT ME!
5    * 
6    * @author $author$
7    * @version $Revision$
8    */
9   public class TypeContextImpl implements TypeContext {
10      /*** DOCUMENT ME! */
11      public static final TypeContextImpl ROOT = new TypeContextImpl("/");
12  
13      /*** DOCUMENT ME! */
14      protected final String context;
15  
16      /***
17       * Creates a new TypeContextImpl object.
18       * 
19       * @param s DOCUMENT ME!
20       * @throws IllegalArgumentException DOCUMENT ME!
21       */
22      public TypeContextImpl(String s) {
23          if ((s == null) || (s.length() == 0)) {
24              throw new IllegalArgumentException(
25                      "TypeContextImpl cannot have null or empty String.");
26          }
27  
28          int lastSlash = s.lastIndexOf('/');
29  
30          if ((lastSlash == (s.length() - 1)) && (lastSlash > 0)) {
31              s = s.substring(0, lastSlash);
32          }
33  
34          context = s;
35      }
36  
37      /***
38       * Creates a new TypeContextImpl object.
39       * 
40       * @param parent DOCUMENT ME!
41       * @param s DOCUMENT ME!
42       * @throws IllegalArgumentException DOCUMENT ME!
43       */
44      public TypeContextImpl(TypeContextImpl parent, String s) {
45          //System.out.println("///// creating TypeContextImpl, parent="+parent+"
46          // s="+s);
47          if ((s == null) || (s.length() == 0)) {
48              throw new IllegalArgumentException(
49                      "TypeContextImpl cannot have null or empty String.");
50          }
51  
52          int lastSlash = s.lastIndexOf('/');
53  
54          if ((lastSlash == (s.length() - 1)) && (lastSlash > 0)) {
55              s = s.substring(0, lastSlash);
56          }
57  
58          if (parent == null) {
59              context = s;
60          } else {
61              String pc = parent.context;
62  
63              if (pc.length() == 1) {
64                  context = "/" + s;
65              } else {
66                  context = parent.context + "/" + s;
67              }
68          }
69      }
70  
71      /***
72       * DOCUMENT ME!
73       * 
74       * @param c DOCUMENT ME!
75       * @return DOCUMENT ME!
76       */
77      public boolean matches(TypeContext c) {
78          if (c instanceof TypeContextImpl) {
79              TypeContextImpl ci = (TypeContextImpl) c;
80  
81              return ci.context.startsWith("/") ? equals(ci) : context
82                      .endsWith(ci.context);
83          } else {
84              return false;
85          }
86      }
87  
88      /***
89       * DOCUMENT ME!
90       * 
91       * @return DOCUMENT ME!
92       */
93      public String toString() {
94          return "TypeContextImpl[" + context + "]";
95      }
96  
97      /***
98       * DOCUMENT ME!
99       * 
100      * @return DOCUMENT ME!
101      */
102     public TypeContext getParent() {
103         if (context.length() <= 1) {
104             return null;
105         }
106 
107         int lastSlash = context.lastIndexOf('/');
108 
109         if (lastSlash == 0) {
110             return ROOT;
111         }
112 
113         return new TypeContextImpl(context.substring(0, lastSlash));
114     }
115 
116     /***
117      * DOCUMENT ME!
118      * 
119      * @return DOCUMENT ME!
120      */
121     public int hashCode() {
122         return context.hashCode();
123     }
124 
125     /***
126      * DOCUMENT ME!
127      * 
128      * @param o DOCUMENT ME!
129      * @return DOCUMENT ME!
130      */
131     public boolean equals(Object o) {
132         if (o == null) {
133             return false;
134         }
135 
136         TypeContextImpl c = (TypeContextImpl) o;
137 
138         return c.context.equals(context);
139     }
140 
141     /*
142      * public static void main(String[] args) { System.out.println(false == new
143      * TypeContextImpl("/a/b/c").matches(new TypeContextImpl("/a/b")));
144      * System.out.println(true == new TypeContextImpl("/a/b/c").matches(new
145      * TypeContextImpl("/a/b/c"))); System.out.println(false == new
146      * TypeContextImpl("a/b/c").matches(new TypeContextImpl("a/b/c/d")));
147      * System.out.println(true == new TypeContextImpl("x/a/b/c").matches(new
148      * TypeContextImpl("a/b/c"))); System.out.println(new
149      * TypeContextImpl("x/a/b/c/")); System.out.println(new
150      * TypeContextImpl("x/a/b/c").getParent()); System.out.println(new
151      * TypeContextImpl("x/a/b/c").getParent().getParent());
152      * System.out.println(new
153      * TypeContextImpl("x/a/b/c").getParent().getParent().getParent()); }
154      */
155 }
156 
157 /*
158  * The contents of this file are subject to the Mozilla Public License Version
159  * 1.1 (the "License"); you may not use this file except in compliance with the
160  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
161  * Software distributed under the License is distributed on an "AS IS" basis,
162  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
163  * the specific language governing rights and limitations under the License. The
164  * Original Code is: all this file. The Initial Developer of the Original Code
165  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
166  */