Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2011 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 org.json.JSONException;
20 import org.json.JSONObject;
21
22 /**
23 * Detect and parse the provided source map.
24 * @author [email protected] (John Lenz)
25 */
26 public class SourceMapConsumerFactory {
27
28 /** not constructible */
29 private SourceMapConsumerFactory() {}
30
31 /**
32 * @param contents The string representing the source map file contents.
33 * @return The parsed source map.
34 * @throws SourceMapParseException
35 */
36 public static SourceMapping parse(String contents)
37 throws SourceMapParseException {
38 return parse(contents, null);
39 }
40
41 /**
42 * @param contents The string representing the source map file contents.
43 * @param supplier A supplier for any referenced maps.
44 * @return The parsed source map.
45 * @throws SourceMapParseException
46 */
47 public static SourceMapping parse(String contents, SourceMapSupplier supplier)
48 throws SourceMapParseException {
49 // Version 1, starts with a magic string
50 if (contents.startsWith("/** Begin line maps. **/")) {
51 SourceMapConsumerV1 consumer = new SourceMapConsumerV1();
52 consumer.parse(contents);
53 return consumer;
54 } else if (contents.startsWith("{")){
55 try {
56 // Revision 2 and 3, are JSON Objects
57 JSONObject sourceMapRoot = new JSONObject(contents);
58 // Check basic assertions about the format.
59 int version = sourceMapRoot.getInt("version");
60 switch (version) {
61 case 2: {
62 SourceMapConsumerV2 consumer = new SourceMapConsumerV2();
63 consumer.parse(sourceMapRoot);
64 return consumer;
65 }
66 case 3: {
67 SourceMapConsumerV3 consumer = new SourceMapConsumerV3();
68 consumer.parse(sourceMapRoot, supplier);
69 return consumer;
70 }
71 default:
72 throw new SourceMapParseException(
73 "Unknown source map version:" + version);
74 }
75 } catch (JSONException ex) {
76 throw new SourceMapParseException("JSON parse exception: " + ex);
77 }
78 }
79
80 throw new SourceMapParseException("unable to detect source map format");
81 }
82 }