blob: 2b4a0261cba6118d025e61e421491ddef5e137b8 (
plain) (
blame)
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
|
/*******************************************************************************
* 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.search;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.search.SearchParticipant;
import org.eclipse.jface.text.Position;
import org.eclipse.jst.jsp.core.internal.Logger;
import org.eclipse.jst.jsp.core.internal.java.IJSPTranslation;
import org.eclipse.jst.jsp.core.internal.java.JSPTranslation;
import org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter;
import org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapterFactory;
import org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension;
import org.eclipse.wst.common.encoding.exceptions.UnsupportedCharsetExceptionWithDetail;
import org.eclipse.wst.sse.core.IModelManager;
import org.eclipse.wst.sse.core.IStructuredModel;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.xml.core.document.XMLDocument;
import org.eclipse.wst.xml.core.document.XMLModel;
/**
* Created with a .jsp file, but should appear to be a .java file for indexing
* and searching purposes. There are purposely few fields in this class,
* and those fields are lightweight since it's possible for many JSP search
* documents to exist in memory at one time (eg. after importing a project
* with a large number of JSP files)
*
* @author pavery
*/
public class JSPSearchDocument {
private String UNKNOWN_PATH = "**path unknown**"; //$NON-NLS-1$
private String fJSPPathString = UNKNOWN_PATH;
private String fCUPath = UNKNOWN_PATH;
private SearchParticipant fParticipant = null;
/**
* @param file
* @param participant
* @throws CoreException
*/
public JSPSearchDocument(String filePath, SearchParticipant participant) {
this.fJSPPathString = filePath;
this.fParticipant = participant;
}
public SearchParticipant getParticipant() {
return this.fParticipant;
}
/**
* @see org.eclipse.jdt.core.search.SearchDocument#getCharContents()
*/
public char[] getCharContents() {
JSPTranslation trans = getJSPTranslation();
return trans != null ? trans.getJavaText().toCharArray() : new char[0];
}
public String getJavaText() {
return new String(getCharContents());
}
private IModelManager getModelManager() {
return StructuredModelManager.getModelManager();
}
/**
* It's not recommended for clients to hold on to this JSPTranslation
* since it's kind of large. If possible, hold on to the JSPSearchDocument,
* which is more of a lightweight proxy.
*
* @return the JSPTranslation for the jsp file, or null if it's an unsupported file.
*/
public final JSPTranslationExtension getJSPTranslation() {
JSPTranslationExtension translation = null;
IFile jspFile = getFile();
if(!JSPSearchSupport.isJsp(jspFile))
return translation;
XMLModel xmlModel = null;
try {
// get existing model for read, then get document from it
xmlModel = (XMLModel) getModelManager().getModelForRead(jspFile);
// handle unsupported
if (xmlModel != null) {
setupAdapterFactory(xmlModel);
XMLDocument doc = xmlModel.getDocument();
JSPTranslationAdapter adapter = (JSPTranslationAdapter) doc.getAdapterFor(IJSPTranslation.class);
translation = adapter.getJSPTranslation();
}
}
catch (IOException e) {
Logger.logException(e);
}
catch (CoreException e) {
Logger.logException(e);
}
catch (UnsupportedCharsetExceptionWithDetail e) {
// no need to log this. Just consider it an invalid file for our
// purposes.
//Logger.logException(e);
}
finally {
if (xmlModel != null)
xmlModel.releaseFromRead();
}
return translation;
}
/**
* add the factory for JSPTranslationAdapter here
*
* @param sm
*/
private void setupAdapterFactory(IStructuredModel sm) {
JSPTranslationAdapterFactory factory = new JSPTranslationAdapterFactory();
sm.getFactoryRegistry().addFactory(factory);
}
/**
* the path to the Java compilation unit
*
* @see org.eclipse.jdt.core.search.SearchDocument#getPath()
*/
public String getPath() {
JSPTranslation trans = getJSPTranslation();
// caching the path since it's expensive to get translation
if(this.fCUPath == null || this.fCUPath == UNKNOWN_PATH) {
if(trans != null)
this.fCUPath = trans.getJavaPath();
}
return fCUPath != null ? fCUPath : UNKNOWN_PATH;
}
public int getJspOffset(int javaOffset) {
// copied from JSPTranslation
int result = -1;
int offsetInRange = 0;
Position jspPos, javaPos = null;
JSPTranslation trans = getJSPTranslation();
if(trans != null) {
HashMap java2jspMap = trans.getJava2JspMap();
// iterate all mapped java ranges
Iterator it = java2jspMap.keySet().iterator();
while (it.hasNext()) {
javaPos = (Position) it.next();
// need to count the last position as included
if (!javaPos.includes(javaOffset) && !(javaPos.offset+javaPos.length == javaOffset))
continue;
offsetInRange = javaOffset - javaPos.offset;
jspPos = (Position) java2jspMap.get(javaPos);
if(jspPos != null)
result = jspPos.offset + offsetInRange;
else {
Logger.log(Logger.ERROR, "jspPosition was null!" + javaOffset); //$NON-NLS-1$
}
break;
}
}
return result;
}
public IFile getFile() {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IPath jspPath = new Path(this.fJSPPathString);
IFile jspFile = root.getFile(jspPath);
if(!jspFile.exists()) {
// possibly outside workspace
jspFile = root.getFileForLocation(jspPath);
}
return jspFile;
}
public void release() {
// nothing to do now since JSPTranslation is created on the fly
}
/**
* for debugging
*/
public String toString() {
return "[JSPSearchDocument:" + this.fJSPPathString + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jdt.core.search.SearchDocument#getEncoding()
*/
public String getEncoding() {
// TODO Auto-generated method stub
return null;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jdt.core.search.SearchDocument#getByteContents()
*/
public byte[] getByteContents() {
// TODO Auto-generated method stub
return null;
}
}
|