Skip to main content
summaryrefslogtreecommitdiffstats
blob: f7d14406c8f64a6168f5a1fc1805cd83a694723a (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
/*******************************************************************************
 * Copyright (c) 2008 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.j2ee.internal.ui.refactoring;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.jst.j2ee.internal.plugin.J2EEUIMessages;
import org.eclipse.jst.j2ee.internal.plugin.J2EEUIPlugin;
import org.eclipse.jst.j2ee.internal.project.ProjectSupportResourceHandler;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.wst.common.componentcore.internal.util.ComponentUtilities;

import com.ibm.icu.text.UTF16;
import com.ibm.icu.util.StringTokenizer;

public class RenameContextRootChange extends Change {

	private final IProject project;
	private final String newContextRoot;
	private final String oldContextRoot;

	public RenameContextRootChange(IProject project, String newContextRoot, String oldContextRoot) {
		this.project = project;
		this.oldContextRoot = oldContextRoot;
		this.newContextRoot = newContextRoot;
	}

	@Override
	public Object getModifiedElement() {
		return project;
	}

	@Override
	public String getName() {
		return J2EEUIMessages.getResourceString("RenameContextRootFromXtoY", new String[]{oldContextRoot, //$NON-NLS-1$
				newContextRoot});
	}

	@Override
	public void initializeValidationData(IProgressMonitor pm) {

	}

	public static IStatus validateContextRoot(String name) {
		IStatus status = Status.OK_STATUS;
		if (name == null || name.length() == 0) {
			// this was added because the error message shouldnt be shown
			// initially. It should be shown only if context
			// root field is edited to
			status = new Status(IStatus.ERROR, J2EEUIPlugin.PLUGIN_ID,
					ProjectSupportResourceHandler.getString(
							ProjectSupportResourceHandler.Context_Root_cannot_be_empty_2,
							new Object[0]));

		} else if (!(name.indexOf(' ') > -1)) {
			StringTokenizer stok = new StringTokenizer(name, "."); //$NON-NLS-1$
			while (stok.hasMoreTokens()) {
				String token = stok.nextToken();
				int cp;
		        for (int i = 0; i < token.length(); i += UTF16.getCharCount(cp)) {
		            cp = UTF16.charAt(token, i);
					if (!(token.charAt(i) == '_') && !(token.charAt(i) == '-')
							&& !(token.charAt(i) == '/')
							&& Character.isLetterOrDigit(token.charAt(i)) == false) {
						if (Character.isWhitespace(token.charAt(i)) == false) {
							String invalidCharString = null;
							if (UTF16.getCharCount(cp)>1)
							{
								invalidCharString = UTF16.valueOf(cp); 
							}
							else
							{
								invalidCharString = (new Character(token.charAt(i))).toString();
							}
							Object[] invalidChar = new Object[]{invalidCharString};
							status = new Status(
									IStatus.ERROR,
									J2EEUIPlugin.PLUGIN_ID,
									ProjectSupportResourceHandler
											.getString(
													ProjectSupportResourceHandler.The_character_is_invalid_in_a_context_root,
													invalidChar));

						}
					}
				}
			}
		} // en/ end of if(name.trim
		else {
			status = new Status(IStatus.ERROR, J2EEUIPlugin.PLUGIN_ID,
					ProjectSupportResourceHandler.getString(
							ProjectSupportResourceHandler.Names_cannot_contain_whitespace_,
							new Object[0]));

		}
		return status;

	}

	@Override
	public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException,
			OperationCanceledException {
		IStatus phase1 = validateContextRoot(newContextRoot);
		if (phase1.isOK())
			return new RefactoringStatus();
		return RefactoringStatus.create(phase1);
	}

	@Override
	public Change perform(IProgressMonitor pm) throws CoreException {

		ComponentUtilities.setServerContextRoot(project, newContextRoot);
		
		return new RenameContextRootChange(project, oldContextRoot, newContextRoot);
	}

}

Back to the top