| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class SourceMapConsumerV2 implements SourceMapConsumer { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private Map<Integer, List<Integer>> characterMap = null; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private JSONArray lineMaps = null; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
private List<OriginalMapping> mappings; |
| 53 |
|
| 54 |
public SourceMapConsumerV2() {} |
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 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 |
|
| 82 |
|
| 83 |
private void parseInternal(JSONObject sourceMapRoot) |
| 84 |
throws JSONException, SourceMapParseException { |
| 85 |
|
| 86 |
|
| 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 |
|
| 105 |
|
| 106 |
characterMap = Maps.newHashMap(); |
| 107 |
|
| 108 |
JSONArray sources = sourceMapRoot.getJSONArray("sources"); |
| 109 |
JSONArray names = sourceMapRoot.has("names") |
| 110 |
? sourceMapRoot.getJSONArray("names") : null; |
| 111 |
|
| 112 |
|
| 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 |
|
| 120 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |