KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonicsystems > jarjar > RulesFileParser


1 /*
2   Jar Jar Links - A utility to repackage and embed Java libraries
3   Copyright (C) 2004 Tonic Systems, Inc.
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; see the file COPYING. if not, write to
17   the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18   Boston, MA 02111-1307 USA
19 */

20
21 package com.tonicsystems.jarjar;
22
23 import java.io.*;
24 import java.util.*;
25
26 class RulesFileParser
27 {
28     private RulesFileParser() {
29     }
30
31     public static List parse(File file) throws IOException {
32         return parse(new FileReader(file));
33     }
34
35     public static List parse(String JavaDoc value) throws IOException {
36         return parse(new java.io.StringReader JavaDoc(value));
37     }
38
39     private static List parse(Reader r) throws IOException {
40         List patterns = new ArrayList();
41         BufferedReader br = new BufferedReader(r);
42         int c = 1;
43         String JavaDoc line;
44         while ((line = br.readLine()) != null) {
45             List parts = splitOnWhitespace(line);
46             if (parts.size() < 2)
47                 error(c, parts);
48             String JavaDoc type = (String JavaDoc)parts.get(0);
49             PatternElement element = null;
50             if (type.equals("rule")) {
51                 if (parts.size() < 3)
52                     error(c, parts);
53                 element = new Rule();
54                 ((Rule)element).setResult((String JavaDoc)parts.get(2));
55             } else if (type.equals("zap")) {
56                 element = new Zap();
57             } else if (type.equals("depkill")) {
58                 element = new DepKill();
59             } else {
60                 error(c, parts);
61             }
62             element.setPattern((String JavaDoc)parts.get(1));
63             patterns.add(element);
64             c++;
65         }
66         r.close();
67         return patterns;
68     }
69
70     private static void error(int line, List parts) {
71         throw new IllegalArgumentException JavaDoc("Error on line " + line + ": " + parts);
72     }
73
74     private static List splitOnWhitespace(String JavaDoc line) {
75         List list = new ArrayList();
76         Enumeration e = new StringTokenizer(line);
77         while (e.hasMoreElements())
78             list.add(e.nextElement());
79         return list;
80     }
81 }
82
Popular Tags