Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1acb7befe0b1d6c68e77f87193b685976654a499 (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
/*******************************************************************************
 * Copyright (c) 2004, 2005 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.ui.internal.java.search;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.search.SearchDocument;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchParticipant;
import org.eclipse.jdt.core.search.SearchRequestor;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jst.jsp.core.internal.java.JSPTranslation;
import org.eclipse.jst.jsp.core.internal.java.search.JSPSearchSupport;
import org.eclipse.jst.jsp.core.internal.java.search.JavaSearchDocumentDelegate;

/**
 * Accepts matches from JSPSearchSupport.search(...) request.
 * Adapts the results from java to JSP and displays in the SearchResultView.
 * 
 * @author pavery
 */
public class BasicJSPSearchRequestor extends SearchRequestor {

	// for debugging
	private static final boolean DEBUG;
	static {
		String value= Platform.getDebugOption("org.eclipse.jst.jsp.core/debug/jspsearch"); //$NON-NLS-1$
		DEBUG= value != null && value.equalsIgnoreCase("true"); //$NON-NLS-1$
	}
	
	/**
	 * Maps java search coordinates to corresponding JSP coordinates.
	 * Adds the matches to the Search Results view.
	 * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch)
	 */
	public void acceptSearchMatch(SearchMatch match) throws CoreException {

		if(JSPSearchSupport.getInstance().isCanceled())
			return;
				
		String matchDocumentPath = match.getResource().getFullPath().toString();
		SearchDocument searchDoc = JSPSearchSupport.getInstance().getSearchDocument(matchDocumentPath);
		
		if (searchDoc != null && searchDoc instanceof JavaSearchDocumentDelegate) {
			JavaSearchDocumentDelegate javaSearchDoc = (JavaSearchDocumentDelegate)searchDoc;
			int jspStart = javaSearchDoc.getJspOffset(match.getOffset());
			int jspEnd = javaSearchDoc.getJspOffset(match.getOffset() + match.getLength());

			JSPTranslation trans = javaSearchDoc.getJspTranslation();
			String jspText = trans.getJspText();
			String javaText = javaSearchDoc.getJavaText();

			if (DEBUG) 
				displayDebugInfo(match, jspStart, jspEnd, jspText, javaText);
		
			if (jspStart > -1 && jspEnd > -1)
				addSearchMatch(new Document(trans.getJspText()), javaSearchDoc.getFile(), jspStart, jspEnd, jspText);
		}
	}

	/**
	 * @param searchDoc
	 * @param jspStart
	 * @param jspEnd
	 * @param jspTranslation
	 * @param jspText
	 * @throws CoreException
	 */
	protected void addSearchMatch(IDocument jspDocument, IFile jspFile, int jspStart, int jspEnd, String jspText) {
		// implement in subclass
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.core.search.SearchRequestor#beginReporting()
	 */
	public void beginReporting() {

		if (DEBUG)
			System.out.println("JSP Search requestor: beginReporting()"); //$NON-NLS-1$
	}

	/**
	 * @see org.eclipse.jdt.core.search.SearchRequestor#endReporting()
	 */
	public void endReporting() {

		if (DEBUG)
			System.out.println("JSP Search requestor: endReporting()"); //$NON-NLS-1$
	}

	/**
	 * @see org.eclipse.jdt.core.search.SearchRequestor#enterParticipant(org.eclipse.jdt.core.search.SearchParticipant)
	 */
	public void enterParticipant(SearchParticipant participant) {

		if (DEBUG)
			System.out.println("JSP Search requestor: enterParticipant()"); //$NON-NLS-1$
	}

	/**
	 * @see org.eclipse.jdt.core.search.SearchRequestor#exitParticipant(org.eclipse.jdt.core.search.SearchParticipant)
	 */
	public void exitParticipant(SearchParticipant participant) {

		if (DEBUG)
			System.out.println("JSP Search requestor: exitParticipant()"); //$NON-NLS-1$
	}

	/**
	 * For debug.
	 * @param origMatch
	 * @param jspStart
	 * @param jspEnd
	 * @param jspText
	 * @param javaText
	 */
	private void displayDebugInfo(SearchMatch origMatch, int jspStart, int jspEnd, String jspText, String javaText) {

		if (origMatch == null || jspStart == -1 || jspEnd == -1 || jspEnd < jspStart || jspText == null || javaText == null)
			return;

		System.out.println("+-----------------------------------------+"); //$NON-NLS-1$
		System.out.println("accept possible match [jspDoc: " + origMatch.getResource().getFullPath().toOSString() + " " + origMatch.getOffset() + ":" + origMatch.getOffset() + origMatch.getLength() + "]?"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		System.out.println("match info:"); //$NON-NLS-1$
		System.out.println("the java text is:" + javaText.substring(origMatch.getOffset(), origMatch.getOffset() + origMatch.getLength())); //$NON-NLS-1$
		System.out.println("java search match translates to jsp coords [start: " + jspStart + " end:" + jspEnd + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		System.out.println(" the jsp text is:" + jspText.substring(jspStart, jspEnd)); //$NON-NLS-1$
	}
}

Back to the top