1 /*
2 * Copyright (c) 2005 The University of Reading
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University of Reading, nor the names of the
14 * authors or contributors may be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 package uk.ac.rdg.resc.jstyx.infernogrid;
30
31 import java.util.Vector;
32
33 /***
34 * Simple representation of an S-expression. S-expressions contain <i>atoms</i>
35 * and other S-expressions. So the S-expression <code>(a (b c))</code> contains
36 * the atom <code>a</code> and the S-expression <code>(b c)</code>
37 *
38 * @author Jon Blower
39 * $Revision: 354 $
40 * $Date: 2005-08-10 19:34:28 +0100 (Wed, 10 Aug 2005) $
41 * $Log$
42 * Revision 1.2 2005/08/10 18:34:28 jonblower
43 * Implemented working S-expression parser
44 *
45 * Revision 1.1 2005/08/08 07:43:03 jonblower
46 * Initial import
47 *
48 */
49 public class Sexpression
50 {
51
52 private Vector contents; // Vector of objects contained in this S-expression
53 // They might be Strings (representing atoms) or
54 // other S-expressions
55
56 /*** Creates a new instance of Sexpression */
57 public Sexpression()
58 {
59 this.contents = new Vector();
60 }
61
62 /***
63 * Adds a token (an atom or another Sexpression) to this Sexpression
64 */
65 public void add(Object token)
66 {
67 this.contents.add(token);
68 //System.out.println("Added a " + token.getClass());
69 }
70
71 /***
72 * @return the number of tokens (atoms and Sexpressions) contained in this
73 * Sexpression
74 */
75 public int getSize()
76 {
77 return this.contents.size();
78 }
79
80 /***
81 * @return the token (atom or Sexpression) at the given index within this
82 * Sexpression. If the type of the returned object is String, the token is
83 * an atom (string literal). If the type is a Sexpression (the only other
84 * option), the token is a nested Sexpression
85 * @throws ArrayIndexOutOfBoundsException if the given index is out of
86 * range
87 */
88 public Object getToken(int i)
89 {
90 if (i >= this.contents.size() || i < 0)
91 {
92 throw new ArrayIndexOutOfBoundsException(i);
93 }
94 return this.contents.get(i);
95 }
96
97 /***
98 * Recursive method to display the contents of this Sexpression
99 */
100 public String toString()
101 {
102 StringBuffer strBuf = new StringBuffer();
103 for (int i = 0; i < this.contents.size(); i++)
104 {
105 Object token = this.contents.get(i);
106 if (token instanceof String)
107 {
108 strBuf.append(" \"" + token + "\" ");
109 }
110 else
111 {
112 strBuf.append("(" + token + ")");
113 }
114 }
115 strBuf.append(" [" + this.getSize() + " tokens]");
116 return strBuf.toString();
117 }
118
119 }