Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c38b44f5a5967d79e3467fb6b7eb0f9f01324ff7 (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) 2013 CEA LIST.
 *
 * 
 * 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:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.search.ui.actions;

import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.infra.refactoring.refactoringOnElement.ITransformationOnElement;
import org.eclipse.papyrus.uml.search.ui.query.AbstractPapyrusQuery;
import org.eclipse.papyrus.views.search.regex.PatternHelper;
import org.eclipse.papyrus.views.search.results.AbstractResultEntry;
import org.eclipse.papyrus.views.search.results.AttributeMatch;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;


public class ReplaceTransformationOnElement implements ITransformationOnElement {

	/**
	 * The {@link AbstractResultEntry} list representing textual matches with the String pattern to be replaced.
	 */
	private Set<AbstractResultEntry> fAttributeMatchList;

	/**
	 * The {@link AbstractPapyrusQuery} representing the search of matches.
	 */
	private AbstractPapyrusQuery fQuery;

	/**
	 * The string that should replace the String pattern to be replaced.
	 */
	private String fNewString;


	public ReplaceTransformationOnElement(Set<AbstractResultEntry> toProcess, AbstractPapyrusQuery query, String newString) {
		fAttributeMatchList = toProcess;
		fQuery = query;
		fNewString = newString;
	}


	public void transformationToExecute(EObject element) {
		for(AbstractResultEntry match : fAttributeMatchList) {
			if(match.getSource() instanceof EObject && match instanceof AttributeMatch){
				AttributeMatch attributeMatch=(AttributeMatch) match;
				if(EcoreUtil.equals(element, (EObject)match.getSource())) {

					if(attributeMatch.getMetaAttribute() instanceof EAttribute) {
						Object value = element.eGet((EAttribute)attributeMatch.getMetaAttribute());
						if(value != null) {
							if(value instanceof String) {
								String originalvalue = (String)value;
								Pattern pattern = PatternHelper.getInstance().createPattern(fQuery.getSearchQueryText(), fQuery.isCaseSensitive(), fQuery.isRegularExpression());

								String newValue = computeReplacementString(pattern, originalvalue, fNewString);

								element.eSet((EAttribute)attributeMatch.getMetaAttribute(), newValue);

							}
						}
					}

					else if(attributeMatch.getMetaAttribute() instanceof Property) {
						Property source = (Property)attributeMatch.getMetaAttribute();
						Class containingClass = source.getClass_();
						if(containingClass instanceof Stereotype) {
							if(element instanceof Element) {
								Stereotype stereotype = ((Element)element).getAppliedStereotype(containingClass.getQualifiedName());
								Object tagValue = ((Element)element).getValue(stereotype, source.getName());
								if(tagValue instanceof String) {
									Object value = tagValue;

									if(value != null) {
										if(value instanceof String) {
											String originalvalue = (String)value;
											Pattern pattern = PatternHelper.getInstance().createPattern(fQuery.getSearchQueryText(), fQuery.isCaseSensitive(), fQuery.isRegularExpression());

											String newValue = computeReplacementString(pattern, originalvalue, fNewString);

											((Element)element).setValue(stereotype, source.getName(), newValue);

										}
									}

								}
							}
						}
					}
				}
		}
		}
	}


	/**
	 * Compute the replacement.
	 * @param pattern The pattern that we will us to compute the replacement. 
	 * @param originalText The String to be modified.
	 * @param replacementText The String to append.
	 * @return The new String.
	 */
	private String computeReplacementString(Pattern pattern, String originalText, String replacementText) throws PatternSyntaxException {
		if(pattern != null) {
			try {

				Matcher matcher = pattern.matcher(originalText);
				StringBuffer sb = new StringBuffer();
				matcher.reset();
				if(matcher.find()) {
					matcher.appendReplacement(sb, replacementText);
				} else {
					return null;
				}
				matcher.appendTail(sb);
				return sb.toString();
			} catch (IndexOutOfBoundsException ex) {
				throw new PatternSyntaxException(ex.getLocalizedMessage(), replacementText, -1);
			}
		}
		return replacementText;
	}

}

Back to the top