]> git.uio.no Git - ifi-stolz-refaktor.git/blame - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/extractors/LongestCommonPrefixExtractor.java
Adding test for PropertyExtractorExecutor.
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / extractors / LongestCommonPrefixExtractor.java
CommitLineData
9a55edb7
EK
1package no.uio.ifi.refaktor.extractors;
2
9a55edb7
EK
3import no.uio.ifi.refaktor.utils.ParseUtils;
4import no.uio.ifi.refaktor.utils.SmartTextSelection;
5
6import org.eclipse.jdt.core.dom.QualifiedName;
7
74a08602 8public class LongestCommonPrefixExtractor extends PropertyExtractor {
9a55edb7 9
e08cc2c6
EK
10 protected String property;
11
9a55edb7
EK
12 public LongestCommonPrefixExtractor(SmartTextSelection selection) {
13 super(selection);
9a55edb7
EK
14 }
15
16 //TODO: what about simple names?
17
18 @Override
19 public boolean visit(QualifiedName node) {
20 if (!nodeInSelection(node))
21 return false;
22
23 String nodeText = ParseUtils.getNodeText(node, getSelection().getDocument()).trim();
24
74a08602
EK
25 if (property == null) {
26 property = nodeText;
9a55edb7 27 } else {
74a08602 28 property = longestCommonPrefix(property, nodeText);
9a55edb7
EK
29 }
30 return false;
31 }
32
33 private String longestCommonPrefix(String attribute, String nodeText) {
34 String commonPrefix = attribute.substring(0, numberOfCommonCharacters(attribute, nodeText));
35 if (commonPrefix.endsWith(".")) {
36 return stripLastChar(commonPrefix);
37 }
38 return commonPrefix;
39 }
40
41 private String stripLastChar(String commonPrefix) {
42 return commonPrefix.substring(0, commonPrefix.length() - 1);
43 }
44
45 private int numberOfCommonCharacters(String attribute, String nodeText) {
46 int charCount = 0;
47 while (belowStringLengths(charCount, attribute, nodeText) && charsMatchingAt(attribute, nodeText, charCount))
48 charCount++;
49 return charCount;
50 }
51
52 private boolean belowStringLengths(int index, String attribute, String nodeText) {
53 return index < attribute.length() && index < nodeText.length();
54 }
55
56 private boolean charsMatchingAt(String attribute, String nodeText, int index) {
57 return attribute.charAt(index) == nodeText.charAt(index);
58 }
e08cc2c6
EK
59
60 public String stringProperty() {
61 return property;
62 }
9a55edb7 63}