KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
24 import org.objectweb.asm.Attribute;
25
26 class RulesImpl implements Rules
27 {
28     private static final String JavaDoc RESOURCE_SUFFIX = "RESOURCE";
29     
30     private Wildcard[] wildcards;
31     private HashMap cache = new HashMap();
32     private boolean verbose;
33
34     public RulesImpl(List ruleList, boolean verbose) {
35         this.verbose = verbose;
36         wildcards = PatternElement.createWildcards(ruleList);
37     }
38
39     public String JavaDoc fixPath(String JavaDoc path) {
40         int slash = path.lastIndexOf('/');
41         String JavaDoc end;
42         if (slash < 0) {
43             end = path;
44             path = RESOURCE_SUFFIX;
45         } else {
46             end = path.substring(slash + 1);
47             path = path.substring(0, slash + 1) + RESOURCE_SUFFIX;
48         }
49         boolean absolute = path.startsWith("/");
50         if (absolute)
51             path = path.substring(1);
52         path = fixName(path);
53         if (absolute)
54             path = "/" + path;
55         path = path.substring(0, path.length() - RESOURCE_SUFFIX.length()) + end;
56         return path;
57     }
58     
59     public String JavaDoc fixDesc(String JavaDoc desc) {
60         return fixDesc(desc, false);
61     }
62     
63     private String JavaDoc fixDesc(String JavaDoc desc, boolean allowGenerics) {
64         if (desc.charAt(desc.length() - 1) != ';')
65             return desc;
66         String JavaDoc value = (String JavaDoc)cache.get(desc);
67         if (value == null) {
68             if (allowGenerics && (desc.charAt(desc.length() - 2) == '>')) {
69                 // TODO: this is broken
70
int lt = desc.indexOf('<');
71                 String JavaDoc main = replaceHelper(desc.substring(0, lt) + ";", Wildcard.STYLE_DESC);
72                 String JavaDoc param = replaceHelper(desc.substring(lt + 1, desc.length() - 2), Wildcard.STYLE_DESC);
73                 value = main.substring(0, main.length() - 1) + "<" + param + ">;";
74             } else {
75                 value = replaceHelper(desc, Wildcard.STYLE_DESC);
76             }
77             cache.put(desc, value);
78         }
79         return value;
80     }
81
82     private String JavaDoc replaceHelper(String JavaDoc value, int style) {
83         for (int i = 0; i < wildcards.length; i++) {
84             String JavaDoc test = wildcards[i].replace(value, style);
85             if (test != null)
86                 return test;
87         }
88         return value;
89     }
90
91     public String JavaDoc fixName(String JavaDoc name) {
92         if (name == null)
93             return null;
94         String JavaDoc desc = fixDesc("L" + name + ";");
95         return desc.substring(1, desc.length() - 1);
96     }
97
98     public String JavaDoc fixMethodDesc(String JavaDoc desc) {
99         return fixMethodDesc(desc, false);
100     }
101
102     private String JavaDoc fixMethodDesc(String JavaDoc desc, boolean allowGenerics) {
103         String JavaDoc value = (String JavaDoc)cache.get(desc);
104         if (value == null) {
105             if (desc.indexOf('L') < 0) {
106                 value = desc;
107             } else {
108                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
109                 sb.append('(');
110                 int end = desc.lastIndexOf(')');
111                 for (int i = 1; i < end; i++) {
112                     char c = desc.charAt(i);
113                     if (c == 'L') {
114                         for (int j = i + 1; j < end; j++) {
115                             if (desc.charAt(j) == ';') {
116                                 if (allowGenerics && j + 1 < end && desc.charAt(j + 1) == '>')
117                                     j += 2;
118                                 sb.append(fixDesc(desc.substring(i, j + 1), allowGenerics));
119                                 i = j;
120                                 break;
121                             }
122                         }
123                     } else {
124                         sb.append(c);
125                     }
126                 }
127                 sb.append(')');
128                 sb.append(fixDesc(desc.substring(end + 1), allowGenerics));
129                 value = sb.toString();
130             }
131             cache.put(desc, value);
132         }
133         return value;
134     }
135
136     public String JavaDoc fixString(String JavaDoc className, String JavaDoc value) {
137         String JavaDoc newValue = fixClassForName(value);
138         if (newValue.equals(value))
139             newValue = fixPath(value);
140         if (newValue.equals(value))
141             newValue = replaceHelper(newValue, Wildcard.STYLE_IDENTIFIER);
142         if (verbose && !newValue.equals(value))
143             System.err.println("Changed " + className + " \"" + value + "\" -> \"" + newValue + "\"");
144         return newValue;
145     }
146
147     private String JavaDoc fixClassForName(String JavaDoc value)
148     {
149         if (value.indexOf('.') >= 0) {
150             String JavaDoc desc1 = value.replace('.', '/');
151             String JavaDoc desc2 = fixDesc(desc1);
152             if (!desc2.equals(desc1))
153                 return desc2.replace('/', '.');
154         }
155         return value;
156     }
157
158     public Attribute fixAttribute(Attribute attr) {
159         // TODO?
160
return attr;
161     }
162
163     public String JavaDoc fixSignature(String JavaDoc signature) {
164         if (signature == null)
165             return null;
166         if (signature.charAt(0) == '(')
167             return fixMethodDesc(signature, true);
168         return fixDesc(signature, true);
169     }
170 }
171
Popular Tags