~ubuntu-branches/ubuntu/karmic/rhino/karmic

« back to all changes in this revision

Viewing changes to src/org/mozilla/javascript/optimizer/OptClassNameHelper.java

  • Committer: Bazaar Package Importer
  • Author(s): Jerry Haltom
  • Date: 2005-03-19 16:56:07 UTC
  • mto: (11.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050319165607-geu3j3fnqlkpqkh1
Tags: upstream-1.6.R1
ImportĀ upstreamĀ versionĀ 1.6.R1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
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/
6
 
 * 
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.
11
 
 * 
12
 
 * The Original Code is Rhino code, released
13
 
 * May 6, 1999.
14
 
 *
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
18
 
 * Rights Reserved.
19
 
 * 
20
 
 * Contributor(s):
21
 
 * Norris Boyd
22
 
 * Roger Lawrence
23
 
 * Andi Vajda
24
 
 * 
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.
35
 
 */
36
 
 
37
 
 
38
 
package org.mozilla.javascript.optimizer;
39
 
 
40
 
import org.mozilla.javascript.*;
41
 
import java.io.*;
42
 
import java.util.Hashtable;
43
 
 
44
 
public class OptClassNameHelper implements ClassNameHelper {
45
 
  
46
 
    public OptClassNameHelper() {
47
 
        setTargetClassFileName(null);
48
 
    }
49
 
 
50
 
    public String getGeneratingDirectory() {
51
 
        return generatingDirectory;
52
 
    }
53
 
    
54
 
    public void reset() {
55
 
        classNames = null;
56
 
    }
57
 
 
58
 
    public synchronized String getJavaScriptClassName(String functionName, 
59
 
                                                      boolean primary) 
60
 
    {
61
 
        StringBuffer s = new StringBuffer();
62
 
        if (packageName != null && packageName.length() > 0) {
63
 
            s.append(packageName);
64
 
            s.append('.');
65
 
        }
66
 
        s.append(initialName);
67
 
        if (generatingDirectory != null) {
68
 
            if (functionName != null) {
69
 
                s.append('$');
70
 
                s.append(functionName);
71
 
            } else if (!primary) {
72
 
                s.append(++serial);
73
 
            }
74
 
        } else {
75
 
            s.append(globalSerial++);
76
 
        }
77
 
        
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;
84
 
        int count = 0;
85
 
        if (classNames == null)
86
 
            classNames = new Hashtable();
87
 
        while (classNames.get(lowerResult) != null) {
88
 
            lowerResult = base + ++count;
89
 
        }
90
 
        classNames.put(lowerResult, Boolean.TRUE);
91
 
        return count == 0 ? result : (result + count);
92
 
    }
93
 
 
94
 
    public String getTargetClassFileName() {
95
 
        return getTargetClassFileName(getInitialClassName());
96
 
    }
97
 
 
98
 
    public void setTargetClassFileName(String classFileName) {
99
 
        if (classFileName == null) {
100
 
            packageName = "org.mozilla.javascript.gen";
101
 
            initialName = "c";
102
 
            return;
103
 
        }
104
 
        int lastSeparator = classFileName.lastIndexOf(File.separatorChar);
105
 
        String initialName;
106
 
        if (lastSeparator == -1) {
107
 
            generatingDirectory = "";
108
 
            initialName = classFileName;
109
 
        } else {
110
 
            generatingDirectory = classFileName.substring(0, lastSeparator);
111
 
            initialName = classFileName.substring(lastSeparator+1);
112
 
        }
113
 
        if (initialName.endsWith(".class"))
114
 
            initialName = initialName.substring(0, initialName.length() - 6);
115
 
        setInitialClassName(initialName);
116
 
    }
117
 
 
118
 
    public String getTargetPackage() {
119
 
        return packageName;
120
 
    }
121
 
 
122
 
    public void setTargetPackage(String targetPackage) {
123
 
        this.packageName = targetPackage;
124
 
    }
125
 
 
126
 
    public String getTargetClassFileName(String className) {
127
 
        if (generatingDirectory == null)
128
 
            return null;
129
 
        StringBuffer sb = new StringBuffer();
130
 
        if (generatingDirectory.length() > 0) {
131
 
            sb.append(generatingDirectory);
132
 
            sb.append(File.separator);
133
 
        }
134
 
        sb.append(className);
135
 
        sb.append(".class");
136
 
        return sb.toString();
137
 
    }
138
 
 
139
 
    public Class getTargetExtends() {
140
 
        return targetExtends;
141
 
    }
142
 
    
143
 
    public void setTargetExtends(Class extendsClass) {
144
 
        targetExtends = extendsClass;
145
 
    }
146
 
    
147
 
    public Class[] getTargetImplements() {
148
 
        return targetImplements;
149
 
    }
150
 
    
151
 
    public void setTargetImplements(Class[] implementsClasses) {
152
 
        targetImplements = implementsClasses;
153
 
    }
154
 
 
155
 
    String getInitialClassName() {
156
 
        return initialName;
157
 
    }
158
 
 
159
 
    void setInitialClassName(String initialName) {
160
 
        this.initialName = initialName;
161
 
        serial = 0;
162
 
    }
163
 
 
164
 
    public ClassOutput getClassOutput() {
165
 
        return classOutput;
166
 
    }
167
 
 
168
 
    public void setClassOutput(ClassOutput classOutput) {
169
 
        this.classOutput = classOutput;
170
 
    }
171
 
 
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;
181
 
}