Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 527e7c3f390ae7008e60c7a2e129bce3379e9b33 (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
/*******************************************************************************
 * Copyright (c) 2004, 2005 Sybase, Inc. 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.facesconfig.ui.pageflow.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

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.jst.jsp.core.internal.contentmodel.TaglibController;
import org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager;
import org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker;
import org.eclipse.jst.jsp.core.internal.contentmodel.tld.provisional.TLDDocument;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;

/**
 * utility class for JSP related information.
 * 
 * @author Yang Liu
 */
public class JSPUtil {
	/**
	 * find out whether the specified taglib has been defined in the IDOMModel.
	 * If found, then return the prefix. If can't find, then will try to add a
	 * taglib declaration into the model, and try to use the specified default
	 * prefix
	 * 
	 * @param model
	 * @param uri
	 * @return
	 */
	public static String getOrCreatePrefix(IDOMModel model, String uri,
			String defaultPrefix) {
		String prefix = getPrefix(model, uri);
		if (prefix != null)
			return prefix;
		String s = findUnusedPrefix(model, defaultPrefix);

		// TODO: should create the taglib inside the IDOMModel
		return s;
	}

	/**
	 * 
	 * @param model
	 * @param uri
	 * @return null means this is tld is not declared in the jsp file
	 */
	public static String getPrefix(IDOMModel model, String uri) {
		TLDCMDocumentManager m = TaglibController.getTLDCMDocumentManager(model
				.getStructuredDocument());
		if (m == null)
			return null;
		List trackers = m.getTaglibTrackers();
		for (Iterator iter = trackers.iterator(); iter.hasNext();) {
			TaglibTracker tracker = (TaglibTracker) iter.next();
			if (uri.equals(tracker.getURI())) {
				return tracker.getPrefix();
			} else {
				CMDocument cmdoc = tracker.getDocument();
				if (cmdoc instanceof TLDDocument
						&& uri.equals(((TLDDocument) cmdoc).getUri())) {
					return tracker.getPrefix();
				}
			}
		}
		return null;
	}

	public static String findUnusedPrefix(IDOMModel model, String suggestion) {
		if (suggestion == null)
			suggestion = "p";
		TLDCMDocumentManager m = TaglibController.getTLDCMDocumentManager(model
				.getStructuredDocument());
		if (m == null)
			return suggestion;
		List trackers = m.getTaglibTrackers();
		Set map = new HashSet();
		for (Iterator iter = trackers.iterator(); iter.hasNext();) {
			TaglibTracker tracker = (TaglibTracker) iter.next();
			map.add(tracker.getPrefix());
		}
		if (!map.contains(suggestion))
			return suggestion;
		for (int i = 1;; i++) {
			if (!map.contains(suggestion + i))
				return suggestion + i;
		}
	}

	/**
	 * given the prefix, find the corresponding jsp tld URI.
	 * 
	 * @param model
	 * @param prefix
	 * @return
	 */
	public static String findURIForPrefix(IDOMModel model, String prefix) {
		if (prefix == null || model == null)
			return null;
		TLDCMDocumentManager m = TaglibController.getTLDCMDocumentManager(model
				.getStructuredDocument());
		if (m == null)
			return null;
		List trackers = m.getTaglibTrackers();
		for (Iterator iter = trackers.iterator(); iter.hasNext();) {
			TaglibTracker tracker = (TaglibTracker) iter.next();
			if (prefix.equals(tracker.getPrefix())) {
				CMDocument cmdoc = tracker.getDocument();
				if (cmdoc instanceof TLDDocument) {
					return ((TLDDocument) cmdoc).getUri();
				} else
					return null;
			}
		}
		return null;
	}
	
	/**
	 * get the action list in the jsp file
	 * 
	 * @return - action list
	 */
	public static List getActionListInJSPFile(String jspFileName) {
		/** jsp dom adapter */
		JSPDomAdapter jspAdapter;

		List actions = new ArrayList();
		jspAdapter = new JSPDomAdapter();
		// convert the relative directory to project directory, e.g., /a.jsp to
		// /testproject/webroot/a.sjp
		String physicalJspPath = jspFileName;
		if (physicalJspPath != null && physicalJspPath.length() > 0) {
			IPath jspPath = new Path(physicalJspPath);
			IFile jspFile = ResourcesPlugin.getWorkspace().getRoot().getFile(
					jspPath);

			if (jspFile != null && jspFile.exists()) {
				// initialize the adapter to initialize the model of jsp
				if (jspAdapter.initialize(jspFile)) {
					// the prefix of JSF HTML TagLib
					String prefix = jspAdapter
							.getTagLibPrefix(JSPDomAdapter.JSF_HTML_TAGLIB);

					// get the command butonns
					List buttonActions = jspAdapter.getElementsByTagNameNS(
							prefix, "commandButton");//$NON-NLS-1$
					if (buttonActions != null) {
						actions.addAll(buttonActions);
					}

					// get the command links
					List linkActions = jspAdapter.getElementsByTagNameNS(
							prefix, "commandLink");//$NON-NLS-1$
					if (linkActions != null) {
						actions.addAll(linkActions);
					}
				}
			}
		}
		jspAdapter.releaseModel();
		return actions;
	}
}

Back to the top