2
* The contents of this file are subject to the Netscape Public
3
* License Version 1.1 (the "License"); you may not use this file
4
* except in compliance with the License. You may obtain a copy of
5
* the License at http://www.mozilla.org/NPL/
7
* Software distributed under the License is distributed on an "AS
8
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9
* implied. See the License for the specific language governing
10
* rights and limitations under the License.
12
* The Original Code is Rhino code, released
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 1997-1999 Netscape Communications Corporation. All
25
* Alternatively, the contents of this file may be used under the
26
* terms of the GNU Public License (the "GPL"), in which case the
27
* provisions of the GPL are applicable instead of those above.
28
* If you wish to allow use of your version of this file only
29
* under the terms of the GPL and not to allow others to use your
30
* version of this file under the NPL, indicate your decision by
31
* deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL. If you do not delete
33
* the provisions above, a recipient may use your version of this
34
* file under either the NPL or the GPL.
38
package org.mozilla.javascript.optimizer;
40
import org.mozilla.javascript.*;
42
import java.util.Hashtable;
44
public class OptClassNameHelper implements ClassNameHelper {
46
public OptClassNameHelper() {
47
setTargetClassFileName(null);
50
public String getGeneratingDirectory() {
51
return generatingDirectory;
58
public synchronized String getJavaScriptClassName(String functionName,
61
StringBuffer s = new StringBuffer();
62
if (packageName != null && packageName.length() > 0) {
63
s.append(packageName);
66
s.append(initialName);
67
if (generatingDirectory != null) {
68
if (functionName != null) {
70
s.append(functionName);
71
} else if (!primary) {
75
s.append(globalSerial++);
78
// We wish to produce unique class names between calls to reset()
79
// we disregard case since we may write the class names to file
80
// systems that are case insensitive
81
String result = s.toString();
82
String lowerResult = result.toLowerCase();
83
String base = lowerResult;
85
if (classNames == null)
86
classNames = new Hashtable();
87
while (classNames.get(lowerResult) != null) {
88
lowerResult = base + ++count;
90
classNames.put(lowerResult, Boolean.TRUE);
91
return count == 0 ? result : (result + count);
94
public String getTargetClassFileName() {
95
return getTargetClassFileName(getInitialClassName());
98
public void setTargetClassFileName(String classFileName) {
99
if (classFileName == null) {
100
packageName = "org.mozilla.javascript.gen";
104
int lastSeparator = classFileName.lastIndexOf(File.separatorChar);
106
if (lastSeparator == -1) {
107
generatingDirectory = "";
108
initialName = classFileName;
110
generatingDirectory = classFileName.substring(0, lastSeparator);
111
initialName = classFileName.substring(lastSeparator+1);
113
if (initialName.endsWith(".class"))
114
initialName = initialName.substring(0, initialName.length() - 6);
115
setInitialClassName(initialName);
118
public String getTargetPackage() {
122
public void setTargetPackage(String targetPackage) {
123
this.packageName = targetPackage;
126
public String getTargetClassFileName(String className) {
127
if (generatingDirectory == null)
129
StringBuffer sb = new StringBuffer();
130
if (generatingDirectory.length() > 0) {
131
sb.append(generatingDirectory);
132
sb.append(File.separator);
134
sb.append(className);
136
return sb.toString();
139
public Class getTargetExtends() {
140
return targetExtends;
143
public void setTargetExtends(Class extendsClass) {
144
targetExtends = extendsClass;
147
public Class[] getTargetImplements() {
148
return targetImplements;
151
public void setTargetImplements(Class[] implementsClasses) {
152
targetImplements = implementsClasses;
155
String getInitialClassName() {
159
void setInitialClassName(String initialName) {
160
this.initialName = initialName;
164
public ClassOutput getClassOutput() {
168
public void setClassOutput(ClassOutput classOutput) {
169
this.classOutput = classOutput;
172
private String generatingDirectory;
173
private String packageName;
174
private String initialName;
175
private static int globalSerial=1;
176
private int serial=1;
177
private Class targetExtends;
178
private Class[] targetImplements;
179
private ClassOutput classOutput;
180
private Hashtable classNames;