|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 package org.chromium.debug.core.model; |
|
6 |
|
7 import java.util.ArrayList; |
|
8 import java.util.Collections; |
|
9 import java.util.Comparator; |
|
10 import java.util.List; |
|
11 |
|
12 import org.chromium.sdk.Script; |
|
13 import org.eclipse.osgi.util.NLS; |
|
14 |
|
15 /** |
|
16 * Creates from a set of scripts a mock-up of full resource (scripts are positioned according |
|
17 * to their line numbers and the whitespace is filled with text pattern). |
|
18 */ |
|
19 class MockUpResourceWriter { |
|
20 static String writeScriptSource(List<Script> scripts) { |
|
21 ArrayList<Script> sortedScriptsArrayList = new ArrayList<Script>(scripts); |
|
22 Collections.sort(sortedScriptsArrayList, scriptPositionComparator); |
|
23 MockUpResourceWriter writer = new MockUpResourceWriter(); |
|
24 for (Script script : sortedScriptsArrayList) { |
|
25 writer.writeSript(script); |
|
26 } |
|
27 return writer.getResult(); |
|
28 } |
|
29 |
|
30 |
|
31 private int line = 0; |
|
32 private int col = 0; |
|
33 private final StringBuilder builder = new StringBuilder(); |
|
34 |
|
35 private void writeSript(Script script) { |
|
36 int scriptLine = script.getStartLine(); |
|
37 if (scriptLine > line) { |
|
38 fillLines(scriptLine - line); |
|
39 line = scriptLine; |
|
40 } else if (scriptLine < line) { |
|
41 writeLineMissMessage(scriptLine); |
|
42 } else { |
|
43 int scriptCol = script.getStartColumn(); |
|
44 if (col < scriptCol) { |
|
45 fillColumns(scriptCol - col); |
|
46 } else if (col > scriptCol) { |
|
47 final boolean expectCorrectStartColumn = false; |
|
48 if (expectCorrectStartColumn) { |
|
49 writeln(""); //$NON-NLS-1$ |
|
50 writeLineMissMessage(scriptLine); |
|
51 } else { |
|
52 // Ignore. |
|
53 } |
|
54 } |
|
55 } |
|
56 |
|
57 if (script.hasSource()) { |
|
58 writeText(script.getSource()); |
|
59 } else { |
|
60 writeln(Messages.MockUpResourceWriter_SCRIPT_WITHOUT_TEXT); |
|
61 } |
|
62 } |
|
63 |
|
64 private void writeLineMissMessage(int scriptLine) { |
|
65 writeln(NLS.bind(Messages.MockUpResourceWriter_SCRIPTS_OVERLAPPED, |
|
66 line + 1 - scriptLine, scriptLine + 1)); |
|
67 } |
|
68 |
|
69 private void writeText(String text) { |
|
70 int pos = 0; |
|
71 while (true) { |
|
72 int nlPos = text.indexOf('\n', pos); |
|
73 if (nlPos == -1) { |
|
74 String rest = text.substring(pos); |
|
75 builder.append(rest); |
|
76 col += rest.length(); |
|
77 break; |
|
78 } |
|
79 writeln(text.substring(pos, nlPos)); |
|
80 pos = nlPos + 1; |
|
81 } |
|
82 } |
|
83 |
|
84 private void writeln(String str) { |
|
85 builder.append(str).append('\n'); |
|
86 line++; |
|
87 col = 0; |
|
88 } |
|
89 |
|
90 private void fillLines(int lines) { |
|
91 if (col != 0) { |
|
92 builder.append('\n'); |
|
93 line++; |
|
94 } |
|
95 for (int i = 0; i < lines; i++) { |
|
96 builder.append(NOT_A_JAVASCRIPT_FILLER).append('\n'); |
|
97 } |
|
98 line += lines; |
|
99 col = 0; |
|
100 } |
|
101 |
|
102 private void fillColumns(int number) { |
|
103 if (number < NOT_A_JAVASCRIPT_FILLER.length()) { |
|
104 if (number < 1) { |
|
105 // Nothing. |
|
106 } else if (number == 1) { |
|
107 builder.append('*'); |
|
108 col += 1; |
|
109 } else { |
|
110 builder.append('{'); |
|
111 for (int i = 2; i < number; i++) { |
|
112 builder.append('*'); |
|
113 } |
|
114 builder.append('}'); |
|
115 col += number; |
|
116 } |
|
117 } |
|
118 } |
|
119 |
|
120 private String getResult() { |
|
121 return builder.toString(); |
|
122 } |
|
123 |
|
124 private static final String NOT_A_JAVASCRIPT_FILLER = |
|
125 Messages.MockUpResourceWriter_NOT_A_JAVASCRIPT; |
|
126 |
|
127 private static final Comparator<Script> scriptPositionComparator = new Comparator<Script>() { |
|
128 public int compare(Script o1, Script o2) { |
|
129 int line1 = o1.getStartLine(); |
|
130 int line2 = o2.getStartLine(); |
|
131 if (line1 < line2) { |
|
132 return -1; |
|
133 } else if (line1 == line2) { |
|
134 return 0; |
|
135 } else { |
|
136 return 1; |
|
137 } |
|
138 } |
|
139 }; |
|
140 } |