ICE 3.4.2
[php5-ice-freebsdport.git] / java / demo / book / evictor_filesystem / Parser.java
blobb033d685f61f0107f2bba33377126098e8f1f637
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 Filesystem.*;
12 class Parser
14 Parser(DirectoryPrx root)
16 _dirs = new java.util.LinkedList<DirectoryPrx>();
17 _dirs.addFirst(root);
20 void
21 usage()
23 System.err.print(
24 "help Print this message.\n" +
25 "pwd Print current directory (/ = root).\n" +
26 "cd [DIR] Change directory (/ or empty = root).\n" +
27 "ls List current directory.\n" +
28 "lr Recursively list current directory.\n" +
29 "mkdir DIR [DIR...] Create directories DIR in current directory.\n" +
30 "mkfile FILE [FILE...] Create files FILE in current directory.\n" +
31 "rm NAME [NAME...] Delete directory or file NAME (rm * to delete all).\n" +
32 "cat FILE List the contents of FILE.\n" +
33 "write FILE [STRING...] Write STRING to FILE.\n" +
34 "exit, quit Exit this program.\n");
37 void
38 list(boolean recursive)
40 try
42 list(_dirs.get(0), recursive, 0);
44 catch(Ice.LocalException ex)
46 error(ex.toString());
50 void
51 list(Filesystem.DirectoryPrx dir, boolean recursive, int depth)
53 StringBuilder b = new StringBuilder();
54 for(int i = 0; i < depth; ++i)
56 b.append('\t');
58 String indent = b.toString();
60 NodeDesc[] contents = dir.list();
62 for(int i = 0; i < contents.length; ++i)
64 DirectoryPrx d
65 = contents[i].type == NodeType.DirType
66 ? DirectoryPrxHelper.uncheckedCast(contents[i].proxy)
67 : null;
68 System.out.print(indent + contents[i].name + (d != null ? " (directory)" : " (file)"));
69 if(d != null && recursive)
71 System.out.println(":");
72 list(d, true, ++depth);
74 else
76 System.out.println();
81 void
82 createFile(java.util.List<String> names)
84 DirectoryPrx dir = _dirs.getFirst();
86 for(String name : names)
88 if(name.equals(".."))
90 System.out.println("Cannot create a file named `..'");
91 continue;
94 try
96 dir.createFile(name);
98 catch(NameInUse ex)
100 System.out.println("`" + name + "' exists already");
105 void
106 createDir(java.util.List<String> names)
108 DirectoryPrx dir = _dirs.getFirst();
110 for(String name : names)
112 if(name.equals(".."))
114 System.out.println("Cannot create a directory named `..'");
115 continue;
120 dir.createDirectory(name);
122 catch(NameInUse ex)
124 System.out.println("`" + name + "' exists already");
129 void
130 pwd()
132 if(_dirs.size() == 1)
134 System.out.print("/");
136 else
138 java.util.ListIterator<DirectoryPrx> i = _dirs.listIterator(_dirs.size());
139 i.previous();
140 while(i.hasPrevious())
142 System.out.print("/" + i.previous().name());
145 System.out.println();
148 void
149 cd(String name)
151 if(name.equals("/"))
153 while(_dirs.size() > 1)
155 _dirs.removeFirst();
157 return;
160 if(name.equals(".."))
162 if(_dirs.size() > 1)
164 _dirs.removeFirst();
166 return;
169 DirectoryPrx dir = _dirs.getFirst();
170 NodeDesc d;
173 d = dir.find(name);
175 catch(NoSuchName ex)
177 System.out.println("`" + name + "': no such directory");
178 return;
180 if(d.type == NodeType.FileType)
182 System.out.println("`" + name + "': not a directory");
183 return;
185 _dirs.addFirst(DirectoryPrxHelper.uncheckedCast(d.proxy));
188 void
189 cat(String name)
191 DirectoryPrx dir = _dirs.getFirst();
192 NodeDesc d;
195 d = dir.find(name);
197 catch(NoSuchName ex)
199 System.out.println("`" + name + "': no such file");
200 return;
202 if(d.type == NodeType.DirType)
204 System.out.println("`" + name + "': not a file");
205 return;
207 FilePrx f = FilePrxHelper.uncheckedCast(d.proxy);
208 String[] l = f.read();
209 for(int i = 0; i < l.length; ++i)
211 System.out.println(l[i]);
215 void
216 write(java.util.LinkedList<String> args)
218 DirectoryPrx dir = _dirs.getFirst();
219 String name = args.getFirst();
220 args.removeFirst();
221 NodeDesc d;
224 d = dir.find(name);
226 catch(NoSuchName ex)
228 System.out.println("`" + name + "': no such file");
229 return;
231 if(d.type == NodeType.DirType)
233 System.out.println("`" + name + "': not a file");
234 return;
236 FilePrx f = FilePrxHelper.uncheckedCast(d.proxy);
238 String[] l = args.toArray(new String[0]);
241 f.write(l);
243 catch(GenericError ex)
245 System.out.println("`" + name + "': cannot write to file: " + ex.reason);
249 void
250 destroy(java.util.List<String> names)
252 DirectoryPrx dir = _dirs.getFirst();
254 for(String name : names)
256 if(name.equals("*"))
258 NodeDesc[] nodes = dir.list();
259 for(NodeDesc node : nodes)
263 node.proxy.destroy();
265 catch(PermissionDenied ex)
267 System.out.println("cannot remove `" + node.name + "': " + ex.reason);
270 return;
272 else
274 NodeDesc d;
277 d = dir.find(name);
279 catch(NoSuchName ex)
281 System.out.println("`" + name + "': no such file or directory");
282 return;
286 d.proxy.destroy();
288 catch(PermissionDenied ex)
290 System.out.println("cannot remove `" + name + "': " + ex.reason);
296 void
297 error(String s)
299 System.err.println("error: " + s);
302 void
303 warning(String s)
305 System.err.println("warning: " + s);
308 String
309 getInput()
311 System.out.print("> ");
312 System.out.flush();
316 return _in.readLine();
318 catch(java.io.IOException e)
320 return null;
325 parse()
327 _in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
329 Grammar g = new Grammar(this);
330 g.parse();
332 return 0;
336 parse(java.io.BufferedReader in)
338 _in = in;
340 Grammar g = new Grammar(this);
341 g.parse();
343 return 0;
346 private java.util.LinkedList<DirectoryPrx> _dirs;
348 private java.io.BufferedReader _in;