:: EGOTHOR |
All the hard steps are done, and what is left is to announce the elementary filter to the world. This can be done in either of two ways.
Adding Filters by Hand.
Finder f = new Finder(); f.add(DefineReader.IN(), new DefineReader());
Adding Filters Automatically.
Finder f = new Finder(); f.scanPackages();
As you have probably already guessed, the second method has distinct advantages over the first. The first is, it's just easier. You won't have to write f.add( ) for all of your filters. Secondly, and more importantly, scanPackages( ) does its work at run-time, looking for filters that are available, relieving the programmer of having to know what filters are available.
The usage is then straighforward:
String[] from = {"FILENAME"}; Path p = f.find("java.lang.String", from, "java.io.Reader", null); if (p == null) { System.out.println("Mission impossible :-)"); } else { System.out.println(p.toString()); Mill m = p.compile(); Hashtable h = new Hashtable(); m.sow(h); m.initialize("autoexec.bat"); try { java.io.Reader d = (java.io.Reader) m.process(); /* read from d, if you like */ } catch (Exception x) { x.printStackTrace(); } finally { m.initialize(null); } }
If you want to reuse the Path, you can. There are two ways - you will always compile a new Mill:
m = p.compile(); h = new Hashtable(); m.sow(h); m.initialize("autoexec.bat"); .... m.process(); m.initialize(null); m = p.compile(); h = new Hashtable(); m.sow(h); m.initialize("autoexec.bat"); .... m.process(); m.initialize(null); m = p.compile(); h = new Hashtable(); m.sow(h); m.initialize("autoexec.bat"); .... m.process(); m.initialize(null); etc.
or you will use the algorithm that was shown above:
m = p.compile(); h = new Hashtable(); m.sow(h); m.initialize("autoexec.bat"); .... m.process(); m.initialize(null); h = new Hashtable(); m.sow(h); m.initialize("autoexec.bat"); .... m.process(); m.initialize(null); h = new Hashtable(); m.sow(h); m.initialize("autoexec.bat"); .... m.process(); m.initialize(null); etc.
Which is better? Obviously, the second approach, because if the filter is complicated, e.g., it contains HTML parsers, the new instance must first be constructed and it takes a lot of time. On the other hand, you may say, the first approach need not call m.initialize(null), because it always uses a fresh Mill. Yes, it could, but the files which were opened by the elementary filters, may stay in an open state. It is never a good idea to leave a file open, so this approach must be taken with care.
Tip | |
---|---|
If you use N threads, you can construct one Path and then let the threads compile it if they need the respective Mill. |
Prev | Up | Next |
Writing Your Own Filter | Home | Predefined Filters |
© 2003-2004 Egothor Developers |