Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 48140a2ca62b2cb6bf7ea23dcb98b1907439cea4 (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
/*******************************************************************************
 * 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.ui.breakpointproviders;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.debug.core.JDIDebugModel;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jst.jsp.ui.internal.JSPUIPlugin;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.wst.sse.ui.extensions.breakpoint.SourceEditingTextTools;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
 * A BreakpointProvider supporting server-side Java as a JSP language
 */
public class JavaBreakpointProvider extends AbstractBreakpointProvider {

	/*
	 * @param res
	 * @return String
	 */
	private static final String getTypeName(IResource res) {
		IPath path = res.getFullPath();
		// Assume under Web Content folder if more than 2 segments
		if (path.segmentCount() > 2) {
			path = path.removeFirstSegments(2);
		}
		else {
			path = path.removeFirstSegments(1);
		}
		String typeName = path.toString().replace(IPath.SEPARATOR, '.');
		if (res.getFileExtension() != null) {
			typeName = typeName.substring(0, typeName.lastIndexOf('.'));
		}
		return typeName;
	}


	/* (non-Javadoc)
	 * @see com.ibm.sse.editor.extensions.BreakpointProvider#canAddBreakpoint(org.w3c.dom.Document, org.eclipse.jface.text.IDocument, org.eclipse.ui.IEditorInput, org.w3c.dom.Node, int, int)
	 */
	public boolean canAddBreakpoint(Document doc, IDocument idoc, IEditorInput input, Node node, int lineNumber, int offset) {
		IResource res = input instanceof IFileEditorInput ? ((IFileEditorInput) input).getFile() : null;

		return res != null && !isBreakpointExist(res, lineNumber) && isValidPosition(doc, idoc, lineNumber) && (getPageLanguage(doc) == JAVA);
	}

	/* (non-Javadoc)
	 * @see com.ibm.sse.editor.extensions.breakpoint.IBreakpointProvider#addBreakpoint(org.w3c.dom.Document, org.eclipse.jface.text.IDocument, org.eclipse.ui.IEditorInput, org.w3c.dom.Node, int, int)
	 */
	public IStatus addBreakpoint(Document doc, IDocument idoc, IEditorInput input, Node node, int lineNumber, int offset) throws CoreException {
		int pos = getValidPosition(doc, idoc, lineNumber);
		if (pos != NO_VALID_CONTENT) {
			IResource res = getEditorInputResource(input);
			if (res != null) {
				String typeName = getTypeName(res);
				try {
					JDIDebugModel.createLineBreakpoint(res, typeName, lineNumber, pos, pos, 0, true, null);
				}
				catch(CoreException e) {
					return e.getStatus();
				}
			}
		}
		return new Status(IStatus.OK, JSPUIPlugin.ID, IStatus.OK, JSPUIPlugin.getResourceString("%OK"), null); //$NON-NLS-1$
	}

	/*
	 * @param res
	 * @param lineNumber
	 * @return boolean
	 */
	private boolean isBreakpointExist(IResource res, int lineNumber) {
		try {
			return JDIDebugModel.lineBreakpointExists(getTypeName(res), lineNumber) != null;
		}
		catch (CoreException e) {
			return false;
		}
	}

	/*
	 * @param doc
	 * @param idoc
	 * @param lineNumber
	 * @return boolean
	 */
	private boolean isValidPosition(Document doc, IDocument idoc, int lineNumber) {
		return getValidPosition(doc, idoc, lineNumber) != NO_VALID_CONTENT;
	}

	/* (non-Javadoc)
	 * @see com.ibm.sse.editor.extensions.BreakpointProvider#setSourceEditingTextTools(com.ibm.sse.editor.extensions.SourceEditingTextTools)
	 */
	public void setSourceEditingTextTools(SourceEditingTextTools util) {
	}

	/* (non-Javadoc)
	 * @see com.ibm.sse.editor.extensions.BreakpointProvider#getResource(org.eclipse.ui.IStorageEditorInput)
	 */
	public IResource getResource(IEditorInput input) {
		return getEditorInputResource(input);
	}
}

Back to the top