View Javadoc

1   /*
2    * Version: MPL 1.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is Rhino code, released
15   * May 6, 1999.
16   *
17   * The Initial Developer of the Original Code is
18   * Netscape Communications Corporation.
19   * Portions created by the Initial Developer are Copyright (C) 1997-1999
20   * the Initial Developer. All Rights Reserved.
21   *
22   * Contributor(s):
23   *   Richard Backhouse
24   */
25  package org.dojotoolkit.shrinksafe;
26  
27  import java.util.Iterator;
28  import java.util.Map;
29  
30  
31  /*
32   * This Class provides a container for the replaced tokens applied for a given scope
33   * It provides a method to traverse down through the hierarchy to seach for a 
34   * replacement match. It also provides a method that generates debug information.
35   */
36  
37  public class ReplacedTokens {
38  	private int[] parents = null;
39  	private Map replacements = null;
40  	private Map lookup = null;
41  	private DebugData debugData = null;
42  	
43  	public ReplacedTokens(Map replacements, int[] parents, Map lookup, DebugData debugData) {
44  		this.replacements = replacements; 
45  		this.parents = parents;
46  		this.lookup = lookup;
47  		this.debugData = debugData;
48  	}
49  	
50  	public String find(String token) {
51  		String replacedToken = null;
52  		if (replacements != null) {
53  			replacedToken = (String)replacements.get(token);
54  		}
55  		if (replacedToken == null) {
56  			for (int i = parents.length; i > 0; i--) {
57  				int parentPos = parents[i-1];
58  				ReplacedTokens parent = (ReplacedTokens)lookup.get(new Integer(parentPos));
59  				if (parent.replacements != null) {
60  					replacedToken = (String)parent.replacements.get(token);
61  					if (replacedToken != null) {
62  						break;
63  					}
64  				}
65  			}
66  		}
67  		if (replacedToken == null) {
68  			replacedToken = token;
69  		}
70  		return replacedToken;
71  	}
72  	
73  	public String printDebugData() {
74  		StringBuffer sb = new StringBuffer();
75  		if (debugData != null) {
76  			sb.append("Start:"+debugData.start);
77  			sb.append(' ');
78  			sb.append("End:"+debugData.end);
79  			sb.append(' ');
80  			sb.append("Compressed Start:"+debugData.compressedStart);
81  			sb.append(' ');
82  			sb.append("Compressed End:"+debugData.compressedEnd);
83  			sb.append(' ');
84  			if (debugData.paramAndVarNames != null) {
85  				sb.append("Params and Vars: [");
86  				for (String paramVar: debugData.paramAndVarNames) {
87  					sb.append(paramVar);
88  					sb.append(' ');
89  				}
90  				sb.append("]\n");
91  			}
92  			if (replacements != null && replacements.size() > 0) {
93  				sb.append("\t");
94  				sb.append("Replacements:\n");
95  				for (Iterator itr = replacements.keySet().iterator(); itr.hasNext();) {
96  					String token = (String)itr.next();
97  					String replacement = (String)replacements.get(token);
98  					if (!token.equals(replacement)) {
99  						sb.append("\t\t");
100 						sb.append('[');
101 						sb.append(token);
102 						sb.append(']');
103 						sb.append(" replaced with ");
104 						sb.append('[');
105 						sb.append(replacement);
106 						sb.append(']');
107 						sb.append('\n');
108 					}
109 				}
110 				sb.append("\n");
111 			}
112 			for (int i = parents.length; i > 0; i--) {
113 				int parentPos = parents[i-1];
114 				ReplacedTokens parent = (ReplacedTokens)lookup.get(new Integer(parentPos));
115 				if (parent.replacements != null && parent.replacements.size() > 0) {
116 					sb.append("\t");
117 					sb.append("Parent Replacements level ["+i+"]:\n");
118 					for (Iterator itr = parent.replacements.keySet().iterator(); itr.hasNext();) {
119 						String token = (String)itr.next();
120 						String replacement = (String)parent.replacements.get(token);
121 						if (!token.equals(replacement)) {
122 							sb.append("\t\t");
123 							sb.append('[');
124 							sb.append(token);
125 							sb.append(']');
126 							sb.append(" replaced with ");
127 							sb.append('[');
128 							sb.append(replacement);
129 							sb.append(']');
130 							sb.append('\n');
131 						}
132 					}
133 					sb.append("\n");
134 				}
135 			}
136 		}
137 		return sb.toString();
138 	}
139 	
140 	public String toJson() {
141 		StringBuffer json = new StringBuffer();
142 		json.append('{');
143 		if (debugData != null) {
144 			json.append("start: "+debugData.start);
145 			json.append(", ");
146 			json.append("end: "+debugData.end);
147 			json.append(", ");
148 			json.append("compressedStart: "+debugData.compressedStart);
149 			json.append(", ");
150 			json.append("compressedEnd: "+debugData.compressedEnd);
151 			json.append(", ");
152 			json.append("replacements: {");
153 			if (replacements != null && replacements.size() > 0) {
154 				json.append(replacementsToJson(replacements));
155 			}
156 			json.append('}');
157 			if (parents.length > 0) {
158 				json.append(", ");
159 				json.append("parentReplacements: [");
160 			}
161 			int count = 1;
162 			for (int i = 0; i < parents.length; i++) {
163 				json.append('{');
164 				int parentPos = parents[i];
165 				ReplacedTokens parent = (ReplacedTokens)lookup.get(new Integer(parentPos));
166 				if (parent.replacements != null && parent.replacements.size() > 0) {
167 					json.append(replacementsToJson(parent.replacements));
168 				}
169 				json.append('}');
170 				if (count++ < parents.length) {
171 					json.append(", ");
172 				}
173 			}
174 			if (parents.length > 0) {
175 				json.append("]");
176 			}
177 		}
178 		json.append("}");
179 		return json.toString();
180 	}
181 	
182 	private static String replacementsToJson(Map replacements) {
183 		StringBuffer sb = new StringBuffer();
184 		int count = 1;
185 		for (Iterator itr = replacements.keySet().iterator(); itr.hasNext();) {
186 			String token = (String)itr.next();
187 			String replacement = (String)replacements.get(token);
188 			sb.append("\""+replacement+'\"');
189 			sb.append(" : ");
190 			sb.append("\""+token+"\"");
191 			if (count++ < replacements.size()) {
192 				sb.append(", ");
193 			}
194 		}
195 		
196 		return sb.toString();
197 	}
198 }