View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.util;
25  
26  import java.util.*;
27  
28  /*** 
29      bug fixed 20/06/01 - call to init should initialise data to new string when no 
30      tokens are found
31      */
32  
33  public class MsgContentHandler {
34    protected String data = null;
35    protected String tag = null;
36  
37    public MsgContentHandler(String s ) {
38      /***
39        Split the input string into tokens delimited by spaces,
40        i.e. "\n\t\r "
41      */
42  
43      Assert.notNull(s);
44      
45      StringTokenizer st = new StringTokenizer(s," \t\n\r",true);
46      
47      tag = st.nextToken();
48      while( tag.equals(" ")  || tag.equals("\t") || 
49  	   tag.equals("\n") || tag.equals("\r") )
50        tag = st.nextToken();
51      
52      if ( tag != null ) tag = tag.trim();
53      
54      if ( st.hasMoreTokens() )
55        data = new String();
56      
57      while( st.hasMoreTokens() ) 
58        data += st.nextToken();
59      
60      if ( data != null ) data = data.trim();
61      // fix below... 
62      if (data == null ) data = new String(); 
63    }
64  
65    public String tag()  {
66       /***
67          return first token
68       */
69       return tag;
70    }
71    public String data() {
72       /*** 
73          return the remainder of the input (i.e. input -tag)
74       */
75       return data;
76    }
77  
78    public String data(int position) {
79       /*** 
80           return the token at position pos
81           where position zero refers to the first token after the tag.
82           For example: given input = "x y z, u, v w"
83  
84           tag()   returns "x"
85           data(0) returns "y"
86           data(1) returns "z"
87           rest(0) returns "z, u, v w"
88           rest(1) returns "u, v w"
89  
90       */
91       try {
92          StringTokenizer st = new StringTokenizer(data," \t\n\r");
93          for(int i = 0; i < position && st.hasMoreTokens(); i++ )
94             st.nextToken();
95          return st.nextToken();
96       }
97       catch(NoSuchElementException e) {
98          return null;
99       }
100   }
101   
102   
103   public String rest(int position) {
104      try {
105         StringTokenizer st = new StringTokenizer(data," \t\n\r");
106         for(int i = 0; i <= position && st.hasMoreTokens(); i++ )
107            st.nextToken();
108           
109         String result = st.nextToken();
110         while( st.hasMoreTokens() )
111            result += " " + st.nextToken();
112         return result;
113      }
114      catch(NoSuchElementException e) {
115         return null;
116      }
117   }
118   
119   
120   public static void main(String[] args) {
121      String input = "x y z, u, v w";
122      MsgContentHandler hd = new MsgContentHandler(input);
123      System.err.println("Input = " + input);
124      System.err.println("Tag = " + hd.tag());
125      System.err.println("data(0) = " + hd.data(0));
126      System.err.println("data(1) = " + hd.data(1));
127      System.err.println("data(2) = " + hd.data(2));
128      System.err.println("rest(0) = " + hd.rest(0));
129      System.err.println("rest(1) = " + hd.rest(1));
130      System.err.println("rest(2) = " + hd.rest(2));
131   }
132 }