Debian Codegraph @

src/com/google/debugging/sourcemap/SourceMapConsumerV2.java

raw symbols SCIP
1 /*
2 * Copyright 2010 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.debugging.sourcemap;
18
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.Lists;
21 import com.google.common.collect.Maps;
22 import com.google.debugging.sourcemap.proto.Mapping.OriginalMapping;
23
24 import org.json.JSONArray;
25 import org.json.JSONException;
26 import org.json.JSONObject;
27
28 import java.util.List;
29 import java.util.Map;
30
31 /**
32 * Class for parsing version 2 of the SourceMap format, as produced by the
33 * Closure Compiler, etc.
34 * @author [email protected] (John Lenz)
35 * @author [email protected] (Joseph Schorr)
36 */
37 public class SourceMapConsumerV2 implements SourceMapConsumer {
38 /**
39 * The character map for each line. If a line does not have an entry,
40 * then it has not yet been decoded.
41 */
42 private Map<Integer, List<Integer>> characterMap = null;
43
44 /**
45 * The undecoded line maps. Will be accessed to decode lines as needed.
46 */
47 private JSONArray lineMaps = null;
48
49 /**
50 * Map of Mapping IDs to the actual mapping object.
51 */
52 private List<OriginalMapping> mappings;
53
54 public SourceMapConsumerV2() {}
55
56 /**
57 * Parses the given contents containing a source map.
58 */
59 @Override
60 public void parse(String contents) throws SourceMapParseException {
61 try {
62 JSONObject sourceMapRoot = new JSONObject(contents);
63 parse(sourceMapRoot);
64 } catch (JSONException ex) {
65 throw new SourceMapParseException("JSON parse exception: " + ex);
66 }
67 }
68
69 /**
70 * Parses the given contents containing a source map.
71 */
72 public void parse(JSONObject sourceMapRoot) throws SourceMapParseException {
73 try {
74 parseInternal(sourceMapRoot);
75 } catch (JSONException ex) {
76 throw new SourceMapParseException("JSON parse exception: " + ex);
77 }
78 }
79
80 /**
81 * Parses the given contents as version 2 of a SourceMap.
82 */
83 private void parseInternal(JSONObject sourceMapRoot)
84 throws JSONException, SourceMapParseException {
85
86 // Check basic assertions about the format.
87 int version = sourceMapRoot.getInt("version");
88 if (version != 2) {
89 throw new SourceMapParseException("Unknown version: " + version);
90 }
91
92 String file = sourceMapRoot.getString("file");
93 if (file.isEmpty()) {
94 throw new SourceMapParseException("File entry is missing or empty");
95 }
96
97 int lineCount = sourceMapRoot.getInt("lineCount");
98 lineMaps = sourceMapRoot.getJSONArray("lineMaps");
99 if (lineCount != lineMaps.length()) {
100 throw new SourceMapParseException(
101 "lineMaps length does not match lineCount");
102 }
103
104 // Build an empty character map. The character map will be filled in as
105 // lines are requested.
106 characterMap = Maps.newHashMap();
107
108 JSONArray sources = sourceMapRoot.getJSONArray("sources");
109 JSONArray names = sourceMapRoot.has("names")
110 ? sourceMapRoot.getJSONArray("names") : null;
111
112 // Create each of the OriginalMappings.
113 JSONArray jsonMappings = sourceMapRoot.getJSONArray("mappings");
114 mappings = Lists.newArrayListWithCapacity(lineCount);
115
116 for (int i = 0; i < jsonMappings.length(); i++) {
117 JSONArray entry = jsonMappings.getJSONArray(i);
118
119 // The name can be accessed in two ways: Directly (i.e. a string) or
120 // indirectly (i.e. an index into the name map).
121 String name = entry.optString(3, "");
122 if (names != null) {
123 try {
124 int nameIndex = entry.getInt(3);
125 name = names.getString(nameIndex);
126 } catch (JSONException e) {
127 }
128 }
129
130 // Build the new OriginalMapping entry.
131 String sourceFile = sources.getString(entry.getInt(0));
132 int lineNumber = entry.getInt(1);
133 int column = entry.getInt(2);
134
135 OriginalMapping.Builder builder = OriginalMapping.newBuilder()
136 .setOriginalFile(sourceFile)
137 .setLineNumber(lineNumber)
138 .setColumnPosition(column)
139 .setIdentifier(name);
140 mappings.add(builder.build());
141 }
142 }
143
144 @Override
145 public OriginalMapping getMappingForLine(int lineNumber, int columnIndex) {
146 // Normalize the line and column numbers to 0.
147 lineNumber--;
148 columnIndex--;
149
150 if (lineNumber >= lineMaps.length()) {
151 return null;
152 }
153
154 Preconditions.checkState(lineNumber >= 0, "Line number must be >= 0");
155 Preconditions.checkState(columnIndex >= 0, "Column index must be >= 0");
156
157 if (!characterMap.containsKey(lineNumber)) {
158 // Parse the line map entry and place it into the character map.
159 try {
160 characterMap.put(lineNumber,
161 SourceMapLineDecoder.decodeLine(lineMaps.getString(lineNumber)));
162 } catch (JSONException jse) {
163 throw new IllegalStateException(
164 "JSON exception when retrieving line map", jse);
165 }
166 }
167
168 List<Integer> map = characterMap.get(lineNumber);
169 if (map == null || map.size() <= columnIndex) {
170 return null;
171 }
172
173 int index = map.get(columnIndex);
174 if (index == -1) {
175 return null;
176 }
177 Preconditions.checkState(index < mappings.size(),
178 "Invalid mapping reference");
179 return mappings.get(index);
180 }
181 }