Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1d811f9b5ca68e1ca3baac6eaceb16f50febe20f (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
/*******************************************************************************
 * Copyright (c) 2005, 2013 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.taglib;

import javax.servlet.jsp.tagext.VariableInfo;

import org.eclipse.wst.sse.core.utils.StringUtils;

/**
 * Contains info about a TaglibVariable: classname, variablename.
 */
public class TaglibVariable {

	private String fVarClass = null;
	private String fVarName = null;
	private int fScope;
	private String fDescription;

	/** fixed end-of-line value */
	private final String ENDL = "\n"; //$NON-NLS-1$

	private final static String AT_END = "AT_END";
	private final static String AT_BEGIN = "AT_BEGIN";
	private final static String NESTED = "NESTED";

	public static final int M_PRIVATE = 1;
	public static final int M_NONE = 0;

	/**
	 * 
	 */
	public TaglibVariable(String varClass, String varName, int scope) {
		setVarClass(varClass);
		setVarName(varName);
		setScope(scope);
	}

	public TaglibVariable(String varClass, String varName, String scope) {
		setVarClass(varClass);
		setVarName(varName);
		setScope(scope);
	}

	public TaglibVariable(String varClass, String varName, String scope, String description) {
		setVarClass(varClass);
		setVarName(varName);
		setScope(scope);
		setDescription(description);
	}

	TaglibVariable(String varClass, String varName, int scope, String description) {
		setVarClass(varClass);
		setVarName(varName);
		setScope(scope);
		setDescription(description);
	}

	/**
	 * @return Returns the fVarClass.
	 */
	public final String getVarClass() {
		return fVarClass;
	}

	/**
	 * @param varClass
	 *            The fVarClass to set.
	 */
	public final void setVarClass(String varClass) {
		fVarClass = varClass;
	}

	/**
	 * @return Returns the fVarName.
	 */
	public final String getVarName() {
		return fVarName;
	}

	/**
	 * @param varName
	 *            The fVarName to set.
	 */
	public final void setVarName(String varName) {
		fVarName = varName;
	}

	/**
	 * Convenience method.
	 * 
	 * @return
	 */
	public final String getDeclarationString() {
		return getDeclarationString(false, M_NONE);
	}

	/**
	 * Convenience method.
	 * 
	 * @return
	 */
	public final String getDeclarationString(boolean includeDoc, int style) {
		return getDeclarationString(includeDoc, "pageContext", style); //$NON-NLS-1$
	}

	public final String getDeclarationString(String context) {
		return getDeclarationString(false, context, M_NONE);
	}

	public final String getDeclarationString(boolean includeDoc, String context, int style) {
		String declaration = null;
		/*
		 * no description for now --JDT would need to show it for local
		 * variables and ILocalVariable has no "doc range"
		 */
		if (includeDoc && getDescription() != null) {
			if (style == M_PRIVATE) {
				declaration = "/** " + ENDL + StringUtils.replace(getDescription(), "*/", "*\\/") + ENDL + " */ " + ENDL + "private " + getVarClass() + " " + getVarName() + " = (" + getVarClass() + ") "+context+".getAttribute(\"" + getVarName() + "\");" + ENDL; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$
			}
			else {
				declaration = "/** " + ENDL + StringUtils.replace(getDescription(), "*/", "*\\/") + ENDL + " */ " + ENDL + getVarClass() + " " + getVarName() + " = (" + getVarClass() + ") "+context+".getAttribute(\"" + getVarName() + "\");" + ENDL; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
			}
		}
		else {
			if (style == M_PRIVATE) {
				declaration = "private " + getVarClass() + " " + getVarName() + " = (" + getVarClass() + ") "+context+".getAttribute(\"" + getVarName() + "\");" + ENDL; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
			}
			else {
				declaration = getVarClass() + " " + getVarName() + " = (" + getVarClass() + ") "+context+".getAttribute(\"" + getVarName() + "\");" + ENDL; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
			}
		}
		return declaration;
	}

	public String getDescription() {
		return fDescription;
	}

	public int getScope() {
		return fScope;
	}

	public void setScope(int scope) {
		fScope = scope;
	}

	public void setScope(String scopeString) {
		int scope = VariableInfo.AT_BEGIN;

		String trimmedScope = scopeString.trim();
		if (NESTED.equals(trimmedScope)) {
			scope = VariableInfo.NESTED;
		}
		else if (AT_BEGIN.equals(trimmedScope)) {
			scope = VariableInfo.AT_BEGIN;
		}
		else if (AT_END.equals(trimmedScope)) {
			scope = VariableInfo.AT_END;
		}

		fScope = scope;
	}

	public void setDescription(String description) {
		fDescription = description;
	}
}

Back to the top