1
2 package jsdsi.sexp;
3
4 import java.io.IOException;
5 import java.io.StringWriter;
6 import java.net.MalformedURLException;
7 import java.net.URI;
8 import java.net.URISyntaxException;
9 import java.net.URL;
10 import java.util.Date;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.TimeZone;
14
15 /***
16 * Utilities for converting SDSI objects to S-expression objects.
17 *
18 * @author Sameer Ajmani
19 * @author Sean Radford
20 * @version $Revision: 1.7.2.1 $ $Date: 2005/11/08 03:12:52 $
21 */
22 public class SexpUtil {
23 // Utilities to support toSexp()
24
25 public static SexpList toSexp(String type, Sexp[] ss) {
26 return new SexpList(new SexpString(type), ss);
27 }
28
29 public static SexpList toSexpList(String type) {
30 return new SexpList(new SexpString(type), new Sexp[0]);
31 }
32
33 public static SexpList toSexp(String type, List l) {
34 Sexp[] ss = new Sexp[l.size()];
35 l.toArray(ss);
36 return toSexp(type, ss);
37 }
38
39 public static SexpString toSexp(String s) {
40 return new SexpString(s);
41 }
42
43 public static SexpString toSexp(byte[] b) {
44 return new SexpString(b);
45 }
46
47 /***
48 * @param u
49 * @return @deprecated use {@link #toSexp(URI[])}
50 */
51 public static SexpList toSexp(URL[] u) {
52 Sexp[] ss = new Sexp[u.length];
53 for (int i = 0; i < u.length; i++) {
54 ss[i] = toSexp(u[i].toString());
55 }
56 return toSexp("uri", ss);
57 }
58
59 public static SexpList toSexp(URI[] u) {
60 Sexp[] ss = new Sexp[u.length];
61 for (int i = 0; i < u.length; i++) {
62 ss[i] = toSexp(u[i].toString());
63 }
64 return toSexp("uri", ss);
65 }
66
67 public static Sexp toSexpComment(String c) {
68 return toSexp("comment", new Sexp[] { toSexp(c) });
69 }
70
71 public static Sexp toSexpDisplayHint(String d) {
72 return toSexp("display", new Sexp[] { toSexp(d) });
73 }
74
75 static private java.text.SimpleDateFormat dateFormat;
76 static {
77 dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
78 TimeZone utc = TimeZone.getTimeZone("GMT");
79 dateFormat.setTimeZone(utc);
80 }
81
82 public static SexpString toSexp(Date d) {
83 return toSexp(dateFormat.format(d));
84 }
85
86 // Utilities to support fromSexp()
87
88 public static Date parseDate(String s) throws SexpParseException {
89 try {
90 // need to synchronise parsing a Format objects are not thread safe
91 synchronized (dateFormat) {
92 return dateFormat.parse(s);
93 }
94 } catch (java.text.ParseException e) {
95 throw new SexpParseException(e);
96 }
97 }
98
99 public static void check(boolean test,
100 String message) throws SexpParseException {
101 if (!test) {
102 throw new SexpParseException(message);
103 }
104 }
105
106 public static SexpString getSexpString(Sexp s) throws SexpParseException {
107 check(s instanceof SexpString, "expected string");
108 return (SexpString) s;
109 }
110
111 public static byte[] getByteArray(Sexp s) throws SexpParseException {
112 return getSexpString(s).toByteArray();
113 }
114
115 public static String getString(Sexp s) throws SexpParseException {
116 return getSexpString(s).toString();
117 }
118
119 public static void checkType(SexpList l,
120 String type) throws SexpParseException {
121 check(type.equals(l.getType()), "expected type " + type);
122 }
123
124 public static SexpList getList(Sexp s,
125 String type) throws SexpParseException {
126 check(s instanceof SexpList, "expected list");
127 SexpList l = (SexpList) s;
128 if (type != null) {
129 checkType(l, type);
130 }
131 return l;
132 }
133
134 public static SexpList getList(Sexp s) throws SexpParseException {
135 return getList(s, null);
136 }
137
138 public static Iterator getBody(SexpList l) {
139 // assumes list type has been checked
140 // get iterator for list after type
141 Iterator it = l.iterator();
142 it.next(); // skip type
143 return it;
144 }
145
146 public static Sexp getNext(Iterator i,
147 String message) throws SexpParseException {
148 check(i.hasNext(), message);
149 return (Sexp) i.next();
150 }
151
152 public static byte[] getNextByteArray(Iterator i,
153 String message) throws SexpParseException {
154 return getByteArray(getNext(i, message));
155 }
156
157 public static String getNextString(Iterator i,
158 String message) throws SexpParseException {
159 return getString(getNext(i, message));
160 }
161
162 public static SexpList getNextList(Iterator i,
163 String message) throws SexpParseException {
164 return getList(getNext(i, message));
165 }
166
167 public static SexpList getNextList(Iterator i, String type, String message)
168 throws SexpParseException {
169 return getList(getNext(i, message), type);
170 }
171
172 public static void checkDone(Iterator i,
173 String message) throws SexpParseException {
174 if (i.hasNext()) {
175 try {
176 StringWriter sw = new StringWriter();
177 sw.write("unexpected extra fields in " + message + ":\n");
178 while (i.hasNext()) {
179 ((Sexp) i.next()).writeReadable(sw, 0, 72, 0);
180 }
181 check(false, sw.toString());
182 } catch (IOException e) {
183 throw new Error(e);
184 }
185 }
186 }
187
188 /***
189 * @param l
190 * @return @throws SexpParseException
191 * @deprecated use {@link #parseURIs(SexpList)}
192 */
193 public static URL[] parseURLs(SexpList l) throws SexpParseException {
194 Iterator ubody = getBody(l);
195 URL[] urls = new URL[l.size() - 1];
196 try {
197 for (int i = 0; i < urls.length; i++) {
198 urls[i] = new URL(getString(getNext(ubody, "uri")).toString());
199 }
200 } catch (MalformedURLException e) {
201 throw new SexpParseException(e);
202 }
203 checkDone(ubody, "uris"); // sanity check
204 return urls;
205 }
206
207 public static URI[] parseURIs(SexpList l) throws SexpParseException {
208 Iterator ubody = getBody(l);
209 URI[] uris = new URI[l.size() - 1];
210 try {
211 for (int i = 0; i < uris.length; i++) {
212 uris[i] = new URI(getString(getNext(ubody, "uri")).toString());
213 }
214 } catch (URISyntaxException e) {
215 throw new SexpParseException(e);
216 }
217 checkDone(ubody, "uris"); // sanity check
218 return uris;
219 }
220 }
This page was automatically generated by Maven