1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
/*******************************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jst.jsp.core.internal.java;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.jst.jsp.core.internal.Logger;
import org.eclipse.jst.jsp.core.internal.regions.DOMJSPRegionContexts;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.text.edits.UndoEdit;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.FileBufferModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
/**
* Adds the notion of IDocuments (jsp Document and java Document)
* Used for TextEdit translation
* @author pavery
*/
public class JSPTranslationExtension extends JSPTranslation {
// for debugging
private static final boolean DEBUG;
static {
String value= Platform.getDebugOption("org.eclipse.jst.jsp.core/debug/jsptranslation"); //$NON-NLS-1$
DEBUG= value != null && value.equalsIgnoreCase("true"); //$NON-NLS-1$
}
private static final String CT_ID_JSP_FRAGMENT = "org.eclipse.jst.jsp.core.jspfragmentsource"; //$NON-NLS-1$
// just a convenience data structure
// to keep track of java position deltas
private class PositionDelta {
public boolean isDeleted = false;
public int preOffset = 0;
public int preLength = 0;
public int postOffset = 0;
public int postLength = 0;
public PositionDelta(int preOffset, int preLength) {
this.preOffset = preOffset;
this.preLength = preLength;
}
public void setPostEditData(int postOffset, int postLength, boolean isDeleted) {
this.postOffset = postOffset;
this.postLength = postLength;
this.isDeleted = isDeleted;
}
}
private IDocument fJspDocument = null;
private IDocument fJavaDocument = null;
public JSPTranslationExtension(IDocument jspDocument, IDocument javaDocument, IJavaProject javaProj, JSPTranslator translator) {
super(javaProj, translator);
fJspDocument = jspDocument;
fJavaDocument = javaDocument;
// make sure positions are added to Java and JSP documents
// this is necessary for text edits
addPositionsToDocuments();
}
public IDocument getJspDocument() {
return fJspDocument;
}
public IDocument getJavaDocument() {
return fJavaDocument;
}
public String getJavaText() {
return getJavaDocument() != null ? getJavaDocument().get() : ""; //$NON-NLS-1$
}
/**
* Returns a corresponding TextEdit for the JSP file given a TextEdit for a Java file.
*
* @param javaEdit
* @return the corresponding JSP edits (not applied to the document yet)
*/
public TextEdit getJspEdit(TextEdit javaEdit) {
if(javaEdit == null)
return null;
List jspEdits = new ArrayList();
int offset = javaEdit.getOffset();
int length = javaEdit.getLength();
if(javaEdit instanceof MultiTextEdit && javaEdit.getChildren().length > 0) {
IRegion r = TextEdit.getCoverage(getAllEdits(javaEdit));
offset = r.getOffset();
length = r.getLength();
}
// get java ranges that will be affected by the edit
Position[] javaPositions = getJavaRanges(offset, length);
// record position data before the change
Position[] jspPositions = new Position[javaPositions.length];
PositionDelta[] deltas = new PositionDelta[javaPositions.length];
for(int i=0; i<javaPositions.length; i++) {
deltas[i] = new PositionDelta(javaPositions[i].offset, javaPositions[i].length);
// isIndirect means the position doesn't actually exist as exact text
// mapping from java <-> jsp (eg. an import statement)
if(!isIndirect(javaPositions[i].offset))
jspPositions[i] = (Position)getJava2JspMap().get(javaPositions[i]);
}
if(DEBUG) {
System.out.println("================================================"); //$NON-NLS-1$
System.out.println("deltas:"); //$NON-NLS-1$
String javaText = getJavaText();
for(int i=0; i<deltas.length; i++)
System.out.println("pos[" + deltas[i].preOffset + ":" + deltas[i].preLength + "]" + javaText.substring(deltas[i].preOffset, deltas[i].preOffset + deltas[i].preLength) ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.out.println("==============================================="); //$NON-NLS-1$
}
UndoEdit undo = null;
// apply the edit to the java document
try {
undo = javaEdit.apply(getJavaDocument());
} catch (MalformedTreeException e) {
Logger.logException(e);
} catch (BadLocationException e) {
Logger.logException(e);
}
// now at this point Java positions are unreliable since they were updated after applying java edit.
String newJavaText = getJavaDocument().get();
if(DEBUG)
System.out.println("java post format text:\n" + newJavaText); //$NON-NLS-1$
// record post edit data
for(int i=0; i<javaPositions.length; i++)
deltas[i].setPostEditData(javaPositions[i].offset, javaPositions[i].length, javaPositions[i].isDeleted);
// create appropriate text edits for deltas
Position jspPos = null;
String replaceText = ""; //$NON-NLS-1$
for(int i=0; i<deltas.length; i++) {
jspPos = jspPositions[i];
// can be null if it's an indirect mapping position
// or if something was added into java that was not originally in JSP (like a new import...)
if(jspPos != null) {
if(deltas[i].isDeleted) {
jspEdits.add(new DeleteEdit(jspPos.offset, jspPos.length));
}
else {
replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
// get rid of pre and post white space or fine tuned adjustment later.
// fix text here...
replaceText = fixJspReplaceText(replaceText, jspPos.offset);
jspEdits.add(new ReplaceEdit(jspPos.offset, jspPos.length, replaceText));
}
if(DEBUG)
debugReplace(deltas, jspPos, replaceText, i);
}
else {
// the new Java text has no corresponding JSP position
// possible new import?
if(isImport(javaPositions[i].getOffset()) && replaceText.lastIndexOf("import ") != -1) { //$NON-NLS-1$
replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
String importText = replaceText.substring(replaceText.lastIndexOf("import "), replaceText.indexOf(";")); //$NON-NLS-1$ //$NON-NLS-2$
// evenutally need to check if it's XML-JSP
importText = "<%@page import=\"" + importText + "\" %>\n"; //$NON-NLS-1$ //$NON-NLS-2$
jspEdits.add(new InsertEdit(0, importText));
}
}
}
TextEdit allJspEdits = createMultiTextEdit((TextEdit[])jspEdits.toArray(new TextEdit[jspEdits.size()]));
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=105632
// undo the java edit
// (so the underlying Java document still represents what's in the editor)
if(undo != null) {
try {
undo.apply(getJavaDocument());
}
catch (MalformedTreeException e) {
Logger.logException(e);
}
catch (BadLocationException e) {
Logger.logException(e);
}
}
return allJspEdits;
}
private String fixJspReplaceText(String replaceText, int jspOffset) {
// result is the text inbetween the delimiters
// eg.
//
// <% result
// %>
String result = replaceText.trim();
String preDelimiterWhitespace = ""; //$NON-NLS-1$
IDocument jspDoc = getJspDocument();
if(jspDoc instanceof IStructuredDocument) {
IStructuredDocument sDoc = (IStructuredDocument)jspDoc;
IStructuredDocumentRegion[] regions = sDoc.getStructuredDocumentRegions(0, jspOffset);
IStructuredDocumentRegion lastRegion = regions[regions.length-1];
// only specifically modify scriptlets
if(lastRegion != null && lastRegion.getType() == DOMJSPRegionContexts.JSP_SCRIPTLET_OPEN) {
for (int i = regions.length-1; i >= 0; i--) {
IStructuredDocumentRegion region = regions[i];
// is there a better way to check whitespace?
if(region.getType() == DOMRegionContext.XML_CONTENT && region.getFullText().trim().equals("")) { //$NON-NLS-1$
preDelimiterWhitespace = region.getFullText();
preDelimiterWhitespace = preDelimiterWhitespace.replaceAll("\r", ""); //$NON-NLS-1$ //$NON-NLS-2$
preDelimiterWhitespace = preDelimiterWhitespace.replaceAll("\n", ""); //$NON-NLS-1$ //$NON-NLS-2$
// need to determine indent for that first line...
String initialIndent = getInitialIndent(result);
// fix the first line of java code
result = TextUtilities.getDefaultLineDelimiter(sDoc)
+ initialIndent
+ result;
result = adjustIndent(result, preDelimiterWhitespace, TextUtilities.getDefaultLineDelimiter(sDoc));
// add whitespace before last delimiter to match
// it w/ the opening delimiter
result = result + TextUtilities.getDefaultLineDelimiter(sDoc) + preDelimiterWhitespace;
break;
}
}
}
}
return result;
}
private String adjustIndent(String textBefore, String indent, String delim) {
// first replace multiple indent with single indent
// the triple indent occurs because the scriptlet code
// actually occurs under:
//
// class
// method
// code
//
// in the translated java document
textBefore = textBefore.replaceAll("\t\t\t", "\t"); //$NON-NLS-1$ //$NON-NLS-2$
// get indent after 2nd line break
StringBuffer textAfter = new StringBuffer();
// will this work on mac?
textBefore = textBefore.replaceAll("\r", ""); //$NON-NLS-1$ //$NON-NLS-2$
StringTokenizer st = new StringTokenizer(textBefore, "\n", true); //$NON-NLS-1$
while(st.hasMoreTokens()) {
String tok = st.nextToken();
if(tok.equals("\n")) { //$NON-NLS-1$
textAfter.append(delim);
}
else {
// prepend each line w/ specified indent
textAfter.append(indent);
textAfter.append(tok);
}
}
return textAfter.toString();
}
private String getInitialIndent(String result) {
// get indent after 2nd line break
String indent = ""; //$NON-NLS-1$
StringTokenizer st = new StringTokenizer(result, "\r\n", false); //$NON-NLS-1$
if(st.countTokens() > 1) {
String tok = st.nextToken();
tok = st.nextToken();
int index =0;
if(tok != null) {
while(tok.charAt(index) == ' ' || tok.charAt(index) == '\t') {
indent += tok.charAt(index);
index++;
}
}
}
return indent;
}
/**
* Combines an array of edits into one MultiTextEdit (with the appropriate coverage region)
* @param edits
* @return
*/
private TextEdit createMultiTextEdit(TextEdit[] edits) {
if(edits.length == 0)
return new MultiTextEdit();
IRegion region = TextEdit.getCoverage(edits);
MultiTextEdit multiEdit = new MultiTextEdit(region.getOffset(), region.getLength());
for (int i = 0; i < edits.length; i++) {
addToMultiEdit(edits[i], multiEdit);
}
return multiEdit;
}
private void addToMultiEdit(TextEdit edit, MultiTextEdit multiEdit) {
// check for overlap here
// discard overlapping edits..
// possible exponential performance hit... need a better way...
TextEdit[] children = multiEdit.getChildren();
for (int i = 0; i < children.length; i++) {
if(children[i].covers(edit))
// don't add
return;
}
multiEdit.addChild(edit);
}
/**
* @param translation
*/
private void addPositionsToDocuments() {
// can be null if it's a NullJSPTranslation
if(getJavaDocument() != null && getJspDocument() != null) {
HashMap java2jsp = getJava2JspMap();
Iterator it = java2jsp.keySet().iterator();
Position javaPos = null;
while(it.hasNext()) {
javaPos =(Position)it.next();
try {
fJavaDocument.addPosition(javaPos);
} catch (BadLocationException e) {
if(DEBUG) {
System.out.println("tyring to add Java Position:[" + javaPos.offset + ":" + javaPos.length + "] to " + getJavaPath()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
//System.out.println("substring :[" + fJavaDocument.get().substring(javaPos.offset) + "]"); //$NON-NLS-1$ //$NON-NLS-2$
Logger.logException(e);
}
}
try {
fJspDocument.addPosition((Position)java2jsp.get(javaPos));
} catch (BadLocationException e) {
if(DEBUG) {
System.out.println("tyring to add JSP Position:[" + ((Position)java2jsp.get(javaPos)).offset + ":" +((Position)java2jsp.get(javaPos)).length + "] to " + getJavaPath()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
Logger.logException(e);
}
}
}
}
}
/**
* Recursively gets all child edits
* @param javaEdit
* @return all child edits
*/
private TextEdit[] getAllEdits(TextEdit javaEdit) {
List result = new ArrayList();
if(javaEdit instanceof MultiTextEdit) {
TextEdit[] children = javaEdit.getChildren();
for (int i = 0; i < children.length; i++)
result.addAll(Arrays.asList(getAllEdits(children[i])));
}
else
result.add(javaEdit);
return (TextEdit[])result.toArray(new TextEdit[result.size()]);
}
public void reconcileCompilationUnit() {
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=105109
// don't want errors for JSP fragments
// since it's likely we don't know their context
if(!isJspFragment())
super.reconcileCompilationUnit();
}
private boolean isJspFragment() {
boolean isFrag = false;
// pa_TODO
// need a way to get underlying IResource or IFile
// from IDocument
// then check content type to see if it's JSP fragment
ITextFileBuffer buf = FileBufferModelManager.getInstance().getBuffer(getJspDocument());
if(buf != null) {
isFrag = isJspFragment(buf);
}
else {
isFrag = isJspFragment(getJspDocument());
}
return isFrag;
}
private boolean isJspFragment(IDocument sDoc) {
boolean isFrag = false;
// buffer is null (no live models around)
IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(sDoc);
try {
if(sModel != null) {
IPath p = new Path(sModel.getBaseLocation());
IFile f = ResourcesPlugin.getWorkspace().getRoot().getFile(p);
if(f != null && f.exists()) {
IContentType jspFragType = Platform.getContentTypeManager().getContentType(CT_ID_JSP_FRAGMENT);
if(jspFragType != null)
isFrag = jspFragType.isAssociatedWith(f.getName());
}
}
}
finally {
if(sModel != null)
sModel.releaseFromRead();
}
return isFrag;
}
private boolean isJspFragment(ITextFileBuffer buf) {
boolean isFrag = false;
IPath loc = buf.getLocation();
if(loc != null) {
IFile f = ResourcesPlugin.getWorkspace().getRoot().getFile(loc);
if(f != null && f.exists()) {
IContentType jspFragType = Platform.getContentTypeManager().getContentType(CT_ID_JSP_FRAGMENT);
if(jspFragType != null)
isFrag = jspFragType.isAssociatedWith(f.getName());
}
}
return isFrag;
}
/**
* @param deltas
* @param jspPos
* @param replaceText
* @param jspText
* @param i
*/
private void debugReplace(PositionDelta[] deltas, Position jspPos, String replaceText, int i) {
String jspChunk;
jspChunk = getJspDocument().get().substring(jspPos.offset, jspPos.offset + jspPos.length);
if(!deltas[i].isDeleted) {
System.out.println("replacing:"); //$NON-NLS-1$
System.out.println("jsp:[" + jspChunk + "]"); //$NON-NLS-1$ //$NON-NLS-2$
System.out.println("w/ :[" + replaceText + "]"); //$NON-NLS-1$ //$NON-NLS-2$
System.out.println("--------------------------------"); //$NON-NLS-1$
}
}
}
|