ICE 3.4.2
[php5-ice-freebsdport.git] / java / demo / Freeze / library / Parser.java
blob48303c358be59f26255f5b16857569d779d3fba8
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 import Demo.*;
12 class Parser
14 Parser(Ice.Communicator communicator, LibraryPrx library)
16 _library = library;
19 void
20 usage()
22 System.err.print(
23 "help Print this message.\n" +
24 "exit, quit Exit this program.\n" +
25 "add isbn title authors Create new book.\n" +
26 "isbn NUMBER Find the book with given ISBN number.\n" +
27 "authors NAME Find all books by the given authors.\n" +
28 "next Set the current book to the next one that was found.\n" +
29 "current Display the current book.\n" +
30 "rent NAME Rent the current book for customer NAME.\n" +
31 "return Return the currently rented book.\n" +
32 "remove Permanently remove the current book from the library.\n" +
33 "size SIZE Set the evictor size for books to SIZE.\n" +
34 "shutdown Shut the library server down.\n");
37 void
38 addBook(java.util.List<String> args)
40 if(args.size() != 3)
42 error("`add' requires at exactly three arguments (type `help' for more info)");
43 return;
46 try
48 BookDescription desc = new BookDescription();
49 desc.isbn = args.get(0);
50 desc.title = args.get(1);
51 desc.authors = args.get(2);
53 BookPrx book = _library.createBook(desc);
54 System.out.println("added new book with isbn " + desc.isbn);
56 catch(DatabaseException ex)
58 error(ex.message);
60 catch(BookExistsException ex)
62 error("the book already exists.");
64 catch(Ice.LocalException ex)
66 error(ex.toString());
70 void
71 findIsbn(java.util.List<String> args)
73 if(args.size() != 1)
75 error("`isbn' requires exactly one argument (type `help' for more info)");
76 return;
79 try
81 _foundBooks = null;
82 _current = 0;
84 BookPrx book = _library.findByIsbn(args.get(0));
85 if(book == null)
87 System.out.println("no book with that ISBN number exists.");
89 else
91 _foundBooks = new BookPrx[1];
92 _foundBooks[0] = book;
93 printCurrent();
96 catch(DatabaseException ex)
98 error(ex.message);
100 catch(Ice.LocalException ex)
102 error(ex.toString());
106 void
107 findAuthors(java.util.List<String> args)
109 if(args.size() != 1)
111 error("`authors' requires exactly one argument (type `help' for more info)");
112 return;
117 _foundBooks = _library.findByAuthors(args.get(0));
118 _current = 0;
119 System.out.println("number of books found: " + _foundBooks.length);
120 printCurrent();
122 catch(DatabaseException ex)
124 error(ex.message);
126 catch(Ice.LocalException ex)
128 error(ex.toString());
132 void
133 nextFoundBook()
135 if(_foundBooks != null && _current != _foundBooks.length)
137 ++_current;
139 printCurrent();
142 void
143 printCurrent()
147 if(_foundBooks != null && _current != _foundBooks.length)
149 BookDescription desc = _foundBooks[_current].getBookDescription();
150 String renter = null;
153 renter = _foundBooks[_current].getRenterName();
155 catch(BookNotRentedException ex)
159 System.out.println("current book is:" );
160 System.out.println("isbn: " + desc.isbn);
161 System.out.println("title: " + desc.title);
162 System.out.println("authors: " + desc.authors);
163 if(renter != null)
165 System.out.println("rented: " + renter);
168 else
170 System.out.println("no current book");
173 catch(Ice.ObjectNotExistException ex)
175 System.out.println("current book no longer exists");
177 catch(Ice.LocalException ex)
179 error(ex.toString());
183 void
184 rentCurrent(java.util.List<String> args)
186 if(args.size() != 1)
188 error("`rent' requires exactly one argument (type `help' for more info)");
189 return;
194 if(_foundBooks != null && _current != _foundBooks.length)
196 _foundBooks[_current].rentBook(args.get(0));
197 System.out.println("the book is now rented by `" + args.get(0) + "'");
199 else
201 System.out.println("no current book");
204 catch(BookRentedException ex)
206 System.out.println("the book has already been rented.");
208 catch(Ice.ObjectNotExistException ex)
210 System.out.println("current book no longer exists");
212 catch(Ice.LocalException ex)
214 error(ex.toString());
218 void
219 returnCurrent()
223 if(_foundBooks != null && _current != _foundBooks.length)
225 _foundBooks[_current].returnBook();
226 System.out.println( "the book has been returned.");
228 else
230 System.out.println("no current book");
233 catch(BookNotRentedException ex)
235 System.out.println("the book is not currently rented.");
237 catch(Ice.ObjectNotExistException ex)
239 System.out.println("current book no longer exists");
241 catch(Ice.LocalException ex)
243 error(ex.toString());
247 void
248 removeCurrent()
252 if(_foundBooks != null && _current != _foundBooks.length)
254 _foundBooks[_current].destroy();
255 System.out.println("removed current book" );
257 else
259 System.out.println("no current book" );
262 catch(DatabaseException ex)
264 error(ex.message);
266 catch(Ice.ObjectNotExistException ex)
268 System.out.println("current book no longer exists");
270 catch(Ice.LocalException ex)
272 error(ex.toString());
276 void
277 setEvictorSize(java.util.List<String> args)
279 if(args.size() != 1)
281 error("`size' requires exactly one argument (type `help' for more info)");
282 return;
285 String s = args.get(0);
288 _library.setEvictorSize(Integer.parseInt(s));
290 catch(NumberFormatException ex)
292 error("not a number " + s);
294 catch(DatabaseException ex)
296 error(ex.message);
298 catch(Ice.LocalException ex)
300 error(ex.toString());
304 void
305 shutdown()
309 _library.shutdown();
311 catch(Ice.LocalException ex)
313 error(ex.toString());
317 void
318 error(String s)
320 System.err.println("error: " + s);
323 void
324 warning(String s)
326 System.err.println("warning: " + s);
329 String
330 getInput()
332 System.out.print(">>> ");
333 System.out.flush();
337 return _in.readLine();
339 catch(java.io.IOException e)
341 return null;
346 parse()
348 _foundBooks = new BookPrx[0];
349 _current = 0;
351 _in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
353 Grammar g = new Grammar(this);
354 g.parse();
356 return 0;
359 private BookPrx[] _foundBooks;
360 private int _current;
362 private LibraryPrx _library;
364 private java.io.BufferedReader _in;
365 private boolean _interactive;