Skip to content

Commit 6939b6a

Browse files
author
huangbin
committedSep 20, 2016
当类名不是namespace时,增加对namespace的推断
1 parent 27af1fa commit 6939b6a

11 files changed

+392
-128
lines changed
 

‎META-INF/MANIFEST.MF

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: P3
44
Bundle-SymbolicName: codeplus;singleton:=true
5-
Bundle-Version: 1.0.0.qualifier
5+
Bundle-Version: 1.1.0.qualifier
66
Bundle-Activator: codeplus.Activator
77
Require-Bundle: org.eclipse.ui,
88
org.eclipse.core.runtime,

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ mybatis是一个流行的ORM工具,其有一个特点:将sql抽取出来单
1212
## 安装方法
1313
把jar包放到eclipse所在位置的dropins子文件夹(若没有可以新建一个)中,重启eclipse即可。
1414
## 如何使用
15-
1. 在以dao命名结尾的类中,当鼠标在sqlId的文本上悬停时,会弹出弹层,显示出实际的sql,如图所示:
15+
1. 在以dao或daoimpl命名结尾的类中,当鼠标在sqlId的文本上悬停时,会弹出弹层,显示出实际的sql,如图所示:
1616
![鼠标悬停](https://raw.githubusercontent.com/huangice/images/master/screenshots/codeplus-hover.png)
1717
(由于第一次需要扫描工程中所有的xml文件,所以第一次弹出会稍慢一些,之后就会很快了。)
1818
这个弹层下有两个按钮,第一个按钮点击会打开对应的xml文件并定位到对应的行;第二个按钮点击则会复制sql到剪切板。
19-
2. 在以dao或mapper命名结尾的类中,在任意位置右键点击,可以看到弹出的右键菜单中有一个选项:Open mybatis sql xml,如图:
19+
2. 在以dao、daoimpl或mapper命名结尾的类中,在任意位置右键点击,可以看到弹出的右键菜单中有一个选项:Open mybatis sql xml,如图:
2020
![右键](https://raw.githubusercontent.com/huangice/images/master/screenshots/codeplus-menu.png)
2121
点击会打开对应的xml文件。如果在sqlId的文本上右键时,会同时定位到对应的sql行。
2222

‎src/codeplus/hover/JavaDaoHover.java

+98-113
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import java.awt.Toolkit;
44
import java.awt.datatransfer.StringSelection;
5+
import java.util.Arrays;
56
import java.util.List;
67

78
import javax.xml.xpath.XPathExpressionException;
89

910
import org.eclipse.core.resources.IFile;
1011
import org.eclipse.core.resources.IProject;
11-
import org.eclipse.core.runtime.CoreException;
1212
import org.eclipse.jdt.core.IJavaProject;
1313
import org.eclipse.jdt.core.JavaCore;
1414
import org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover;
@@ -17,7 +17,6 @@
1717
import org.eclipse.jface.action.ToolBarManager;
1818
import org.eclipse.jface.text.BadLocationException;
1919
import org.eclipse.jface.text.DefaultInformationControl;
20-
import org.eclipse.jface.text.IDocument;
2120
import org.eclipse.jface.text.IInformationControl;
2221
import org.eclipse.jface.text.IInformationControlCreator;
2322
import org.eclipse.jface.text.IRegion;
@@ -27,98 +26,91 @@
2726
import org.eclipse.swt.SWT;
2827
import org.eclipse.swt.widgets.Shell;
2928
import org.eclipse.ui.IEditorPart;
30-
import org.eclipse.ui.IWorkbenchPage;
31-
import org.eclipse.ui.PartInitException;
32-
import org.eclipse.ui.PlatformUI;
33-
import org.eclipse.ui.ide.IDE;
34-
import org.eclipse.ui.texteditor.IDocumentProvider;
35-
import org.eclipse.ui.texteditor.ITextEditor;
36-
import org.eclipse.wst.xml.core.internal.document.ElementImpl;
37-
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
38-
import org.w3c.dom.Node;
3929

4030
import codeplus.Activator;
4131
import codeplus.util.EditorUtil;
42-
import codeplus.util.MapperNamespaceCache;
4332
import codeplus.util.TextUtil;
4433
import codeplus.util.XmlUtil;
4534
import codeplus.util.XmlUtil.MatchFile;
46-
import codeplus.util.XpathUtil;
35+
import codeplus.util.mapper.FindMapperXml;
4736

4837
public class JavaDaoHover implements IJavaEditorTextHover, ITextHoverExtension, ITextHoverExtension2 {
4938
private IEditorPart currentEditor;
5039
private ITextViewer currentViewer;
5140
private IRegion currentRegion;
5241

53-
@Override
54-
public String getHoverInfo(ITextViewer viewer, IRegion region) {
55-
this.currentViewer = viewer;
56-
this.currentRegion = region;
42+
private String[] getNamespaceAndSqlid() throws BadLocationException {
43+
String hoverStr = currentViewer.getDocument().get(currentRegion.getOffset(), currentRegion.getLength());
5744

58-
try {
59-
String hoverStr = viewer.getDocument().get(region.getOffset(), region.getLength());
60-
61-
String content = EditorUtil.getText(currentEditor);
62-
String selected = TextUtil.getSelectedWord(content, region.getOffset());
63-
String sqlId = null;
64-
String wholeSqlId = null;
65-
if (selected != null && selected.contains(hoverStr) && selected.contains(".")) {
66-
wholeSqlId = selected;
67-
}
45+
String content = EditorUtil.getText(currentEditor);
46+
String selected = TextUtil.getSelectedWord(content, currentRegion.getOffset());
47+
String sqlId = null;
48+
String wholeSqlId = null;
49+
if (selected != null && selected.contains(hoverStr) && selected.contains(".")) {
50+
wholeSqlId = selected;
51+
}
6852

69-
IFile file = currentEditor.getEditorInput().getAdapter(IFile.class);
70-
String fileName = file.getName();
71-
if (fileName.matches(".*(?i)(Dao|Mapper)\\.java")) {
72-
IProject project = EditorUtil.getCurrentProject(currentEditor);
73-
IJavaProject javaProj = JavaCore.create(project);
74-
75-
String namespace = null;
76-
if (wholeSqlId != null) {
77-
int index = wholeSqlId.lastIndexOf(".");
78-
namespace = wholeSqlId.substring(0, index);
79-
sqlId = wholeSqlId.substring(index + 1);
80-
} else {
53+
IFile file = currentEditor.getEditorInput().getAdapter(IFile.class);
54+
String fileName = file.getName();
55+
if (fileName.matches(FindMapperXml.MATCH_REG)) {
56+
String namespace = null;
57+
if (wholeSqlId != null) {
58+
int index = wholeSqlId.lastIndexOf(".");
59+
namespace = wholeSqlId.substring(0, index);
60+
if (namespace.trim().length() == 0) {
8161
String packName = TextUtil.getJavaPackage(file);
8262
namespace = new StringBuilder(packName).append(".").append(fileName.substring(0, fileName.lastIndexOf("."))).toString();
83-
sqlId = hoverStr;
8463
}
64+
sqlId = wholeSqlId.substring(index + 1);
65+
return new String[] { namespace, sqlId };
66+
} else {
67+
String packName = TextUtil.getJavaPackage(file);
68+
namespace = new StringBuilder(packName).append(".").append(fileName.substring(0, fileName.lastIndexOf("."))).toString();
69+
sqlId = hoverStr;
70+
return new String[] { namespace, sqlId };
71+
}
72+
}
73+
return null;
74+
}
8575

86-
List<IFile> lst = MapperNamespaceCache.getInstance().get(javaProj, namespace, null);
87-
if (lst != null) {
88-
for (IFile mapperFile : lst) {
89-
IDOMDocument mapperDocument = XmlUtil.getMapperDocument(mapperFile);
90-
Node node = XpathUtil.xpathNode(mapperDocument, "//*[@id='" + sqlId + "']");
91-
if (node != null) {
92-
StringBuilder buffer = new StringBuilder();
93-
XmlUtil.computeStatementText((ElementImpl) node, buffer, javaProj);
94-
95-
String s = buffer.toString();
96-
buffer.delete(0, buffer.length());
97-
98-
buffer.append("|&#x3000;&#x3000;Tips: button1 open sql xml file; button2 copy sql to clipboard. <br>");
99-
buffer.append(
100-
"|-----------------------------------------------------------------------------------------------------------------------<br>");
101-
102-
s = TextUtil.htmlEncode(s);
103-
s = s.replaceAll("\t", "&#x3000;&#x3000;");
104-
105-
// s = s.replaceAll("\n", "<br>");
106-
for (String str : s.split("\n")) {
107-
buffer.append("|&#x3000;&#x3000;").append(str).append("<br>");
108-
}
109-
s = buffer.toString();
76+
@Override
77+
public String getHoverInfo(ITextViewer viewer, IRegion region) {
78+
this.currentViewer = viewer;
79+
this.currentRegion = region;
11080

111-
return s;
112-
}
81+
try {
82+
IProject project = EditorUtil.getCurrentProject(currentEditor);
83+
IJavaProject javaProj = JavaCore.create(project);
84+
85+
String[] arr = getNamespaceAndSqlid();
86+
if (arr != null) {
87+
String namespace = arr[0];
88+
String sqlId = arr[1];
89+
String s = FindMapperXml.findSql(javaProj, namespace, sqlId);
90+
if (s != null) {
91+
StringBuilder buffer = new StringBuilder();
92+
buffer.append("|&#x3000;&#x3000;Tips: button1 open sql xml file; button2 copy sql to clipboard. <br>");
93+
buffer.append(
94+
"|-----------------------------------------------------------------------------------------------------------------------<br>");
95+
96+
s = TextUtil.htmlEncode(s);
97+
s = s.replaceAll("\t", "&#x3000;&#x3000;");
98+
99+
// s = s.replaceAll("\n", "<br>");
100+
for (String str : s.split("\n")) {
101+
buffer.append("|&#x3000;&#x3000;").append(str).append("<br>");
113102
}
103+
s = buffer.toString();
104+
return s;
114105
}
115-
} else {
116-
return null;
117106
}
107+
return null;
118108
} catch (BadLocationException e) {
119109
Activator.log(e);
120110
} catch (XPathExpressionException e) {
121111
Activator.log(e);
112+
} catch (InterruptedException e) {
113+
Activator.log(e);
122114
}
123115
return null;
124116
}
@@ -143,26 +135,32 @@ public IInformationControlCreator getInformationPresenterControlCreator() {
143135
IAction action1 = new Action() {
144136
public void run() {
145137
try {
146-
String sqlId = currentViewer.getDocument().get(currentRegion.getOffset(), currentRegion.getLength());
147138
IFile file = currentEditor.getEditorInput().getAdapter(IFile.class);
148139
String fileName = file.getName();
149-
if (fileName.matches(".*(?i)(Dao|Mapper)\\.java")) {
150-
String packName = TextUtil.getJavaPackage(file);
151-
String namespace = new StringBuilder(packName).append(".").append(fileName.substring(0, fileName.lastIndexOf(".")))
152-
.toString();
153-
154-
IProject project = EditorUtil.getCurrentProject(currentEditor);
155-
IJavaProject javaProj = JavaCore.create(project);
156-
List<IFile> lst = MapperNamespaceCache.getInstance().get(javaProj, namespace, null);
157-
158-
if (lst != null) {
159-
MatchFile mf = XmlUtil.getMatchFileById(lst, sqlId);
160-
IFile fileToOpen = mf.getFile();
161-
EditorUtil.openFile(fileToOpen, mf.getMatchLineNo());
140+
if (fileName.matches(FindMapperXml.MATCH_REG)) {
141+
String[] arr = getNamespaceAndSqlid();
142+
if (arr != null) {
143+
String namespace = arr[0];
144+
String sqlId = arr[1];
145+
146+
IProject project = EditorUtil.getCurrentProject(currentEditor);
147+
IJavaProject javaProj = JavaCore.create(project);
148+
IFile f = FindMapperXml.findSqlXml(javaProj, namespace, sqlId);
149+
if (f != null) {
150+
List<IFile> lst = Arrays.asList(f);
151+
152+
MatchFile mf = XmlUtil.getMatchFileById(lst, sqlId);
153+
IFile fileToOpen = mf.getFile();
154+
EditorUtil.openFile(fileToOpen, mf.getMatchLineNo());
155+
}
162156
}
163157
}
164158
} catch (BadLocationException e) {
165159
Activator.log(e);
160+
} catch (XPathExpressionException e) {
161+
Activator.log(e);
162+
} catch (InterruptedException e) {
163+
Activator.log(e);
166164
}
167165
}
168166
};
@@ -174,45 +172,32 @@ public void run() {
174172
IAction action2 = new Action() {
175173
public void run() {
176174
try {
177-
String sqlId = currentViewer.getDocument().get(currentRegion.getOffset(), currentRegion.getLength());
178175
IFile file = currentEditor.getEditorInput().getAdapter(IFile.class);
179176
String fileName = file.getName();
180-
if (fileName.matches(".*(?i)(Dao|Mapper)\\.java")) {
181-
String packName = TextUtil.getJavaPackage(file);
182-
String namespace = new StringBuilder(packName).append(".").append(fileName.substring(0, fileName.lastIndexOf(".")))
183-
.toString();
184-
185-
IProject project = EditorUtil.getCurrentProject(currentEditor);
186-
IJavaProject javaProj = JavaCore.create(project);
187-
List<IFile> lst = MapperNamespaceCache.getInstance().get(javaProj, namespace, null);
188-
189-
if (lst != null)
190-
for (IFile mapperFile : lst) {
191-
String charset = mapperFile.getCharset();
192-
IDOMDocument mapperDocument = XmlUtil.getMapperDocument(mapperFile);
193-
Node node = XpathUtil.xpathNode(mapperDocument, "//*[@id='" + sqlId + "']");
194-
if (node != null) {
195-
StringBuilder buffer = new StringBuilder();
196-
XmlUtil.computeStatementText((ElementImpl) node, buffer, javaProj);
197-
198-
String s = buffer.toString();
199-
200-
Toolkit toolkit = Toolkit.getDefaultToolkit();
201-
java.awt.datatransfer.Clipboard clipboard = toolkit.getSystemClipboard();
202-
StringSelection stringSel = new StringSelection(s);
203-
clipboard.setContents(stringSel, null);
204-
return;
205-
}
177+
if (fileName.matches(FindMapperXml.MATCH_REG)) {
178+
String[] arr = getNamespaceAndSqlid();
179+
if (arr != null) {
180+
String namespace = arr[0];
181+
String sqlId = arr[1];
182+
183+
IProject project = EditorUtil.getCurrentProject(currentEditor);
184+
IJavaProject javaProj = JavaCore.create(project);
185+
String s = FindMapperXml.findSql(javaProj, namespace, sqlId);
186+
if (s != null) {
187+
Toolkit toolkit = Toolkit.getDefaultToolkit();
188+
java.awt.datatransfer.Clipboard clipboard = toolkit.getSystemClipboard();
189+
StringSelection stringSel = new StringSelection(s);
190+
clipboard.setContents(stringSel, null);
191+
return;
206192
}
193+
}
207194
}
208-
} catch (PartInitException e) {
209-
Activator.log(e);
210195
} catch (BadLocationException e) {
211196
Activator.log(e);
212-
} catch (CoreException e) {
213-
Activator.log(e);
214197
} catch (XPathExpressionException e) {
215198
Activator.log(e);
199+
} catch (InterruptedException e) {
200+
Activator.log(e);
216201
}
217202
}
218203
};

‎src/codeplus/popup/GoDeclarationInterfaceDelegate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323

2424
import codeplus.Activator;
2525
import codeplus.util.EditorUtil;
26-
import codeplus.util.MapperNamespaceCache;
2726
import codeplus.util.TextUtil;
2827
import codeplus.util.XmlUtil;
2928
import codeplus.util.XmlUtil.MatchFile;
29+
import codeplus.util.mapper.MapperNamespaceCache;
3030

3131
public class GoDeclarationInterfaceDelegate extends ActionDelegate implements IEditorActionDelegate {
3232

‎src/codeplus/popup/GoSqlXmlActionDelegate.java

+28-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package codeplus.popup;
22

3+
import java.util.Arrays;
34
import java.util.List;
45

6+
import javax.xml.xpath.XPathExpressionException;
7+
58
import org.eclipse.core.resources.IFile;
69
import org.eclipse.core.resources.IProject;
710
import org.eclipse.jdt.core.IJavaProject;
@@ -23,10 +26,11 @@
2326

2427
import codeplus.Activator;
2528
import codeplus.util.EditorUtil;
26-
import codeplus.util.MapperNamespaceCache;
2729
import codeplus.util.TextUtil;
2830
import codeplus.util.XmlUtil;
2931
import codeplus.util.XmlUtil.MatchFile;
32+
import codeplus.util.mapper.FindMapperXml;
33+
import codeplus.util.mapper.MapperNamespaceCache;
3034

3135
public class GoSqlXmlActionDelegate extends ActionDelegate implements IEditorActionDelegate {
3236

@@ -36,28 +40,42 @@ public class GoSqlXmlActionDelegate extends ActionDelegate implements IEditorAct
3640
* @see ActionDelegate#run(IAction)
3741
*/
3842
public void run(IAction action) {
39-
// MessageBox box = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
40-
// box.setMessage("Executing: " + getClass());
41-
// box.open();
42-
4343
ISelection i = currentEditor.getEditorSite().getSelectionProvider().getSelection();
4444
if (i instanceof ITextSelection) {
4545
ITextSelection ts = (ITextSelection) i;
4646
int off = ts.getOffset();
4747

4848
IFile file = currentEditor.getEditorInput().getAdapter(IFile.class);
4949
String fileName = file.getName();
50-
if (fileName.matches(".*(?i)(Dao|Mapper)\\.java")) {
50+
if (fileName.matches(FindMapperXml.MATCH_REG)) {
5151
String content = EditorUtil.getText(currentEditor);
5252
String sqlId = TextUtil.getSelectedWord(content, off);
53-
String packName = TextUtil.getJavaPackage(file);
54-
String namespace = new StringBuilder(packName).append(".").append(fileName.substring(0, fileName.lastIndexOf("."))).toString();
53+
54+
String namespace = null;
55+
if (sqlId.contains(".")) {
56+
int index = sqlId.lastIndexOf(".");
57+
namespace = sqlId.substring(0, index);
58+
if (namespace.trim().length() == 0) {
59+
String packName = TextUtil.getJavaPackage(file);
60+
namespace = new StringBuilder(packName).append(".").append(fileName.substring(0, fileName.lastIndexOf("."))).toString();
61+
}
62+
sqlId = sqlId.substring(index + 1);
63+
} else {
64+
String packName = TextUtil.getJavaPackage(file);
65+
namespace = new StringBuilder(packName).append(".").append(fileName.substring(0, fileName.lastIndexOf("."))).toString();
66+
}
5567

5668
IProject project = EditorUtil.getCurrentProject(currentEditor);
5769
IJavaProject javaProj = JavaCore.create(project);
58-
List<IFile> lst = MapperNamespaceCache.getInstance().get(javaProj, namespace, null);
70+
IFile f = null;
71+
try {
72+
f = FindMapperXml.findSqlXml(javaProj, namespace, sqlId);
73+
} catch (XPathExpressionException | InterruptedException e) {
74+
Activator.log(e);
75+
}
76+
if (f != null) {
77+
List<IFile> lst = Arrays.asList(f);
5978

60-
if (lst != null) {
6179
MatchFile mf = XmlUtil.getMatchFileById(lst, sqlId);
6280
IFile fileToOpen = mf.getFile();
6381
EditorUtil.openFile(fileToOpen, mf.getMatchLineNo());

‎src/codeplus/util/SimilarityUtil.java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package codeplus.util;
2+
3+
public class SimilarityUtil {
4+
5+
private static int min(int one, int two, int three) {
6+
int min = one;
7+
if (two < min) {
8+
min = two;
9+
}
10+
if (three < min) {
11+
min = three;
12+
}
13+
return min;
14+
}
15+
16+
public static int ld(String str1, String str2) {
17+
int d[][]; // 矩阵
18+
int n = str1.length();
19+
int m = str2.length();
20+
int i; // 遍历str1的
21+
int j; // 遍历str2的
22+
char ch1; // str1的
23+
char ch2; // str2的
24+
int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
25+
if (n == 0) {
26+
return m;
27+
}
28+
if (m == 0) {
29+
return n;
30+
}
31+
d = new int[n + 1][m + 1];
32+
for (i = 0; i <= n; i++) { // 初始化第一列
33+
d[i][0] = i;
34+
}
35+
for (j = 0; j <= m; j++) { // 初始化第一行
36+
d[0][j] = j;
37+
}
38+
for (i = 1; i <= n; i++) { // 遍历str1
39+
ch1 = str1.charAt(i - 1);
40+
// 去匹配str2
41+
for (j = 1; j <= m; j++) {
42+
ch2 = str2.charAt(j - 1);
43+
if (ch1 == ch2) {
44+
temp = 0;
45+
} else {
46+
temp = 1;
47+
}
48+
// 左边+1,上边+1, 左上角+temp取最小
49+
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + temp);
50+
}
51+
}
52+
return d[n][m];
53+
}
54+
55+
public static double sim(String str1, String str2) {
56+
int ld = ld(str1, str2);
57+
return 1 - (double) ld / Math.max(str1.length(), str2.length());
58+
}
59+
60+
public static void main(String[] args) {
61+
62+
// String str1 = "chenlb.1.net";
63+
// String str2 = "chenlb.1.nt";
64+
// System.out.println("ld="+ld(str1, str2));
65+
// System.out.println("sim="+sim(str1, str2));
66+
String s = "safmapper.java";
67+
boolean b = s.matches(".*(?i)(dao(impl)?|mapper)\\.java");
68+
System.out.println(b);
69+
}
70+
}

‎src/codeplus/util/XmlUtil.java

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.w3c.dom.NodeList;
2727

2828
import codeplus.Activator;
29+
import codeplus.util.mapper.MapperNamespaceCache;
2930

3031
public class XmlUtil {
3132
public static class MatchFile {

‎src/codeplus/util/cache/Cache.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package codeplus.util.cache;
2+
3+
import java.util.concurrent.Callable;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
import java.util.concurrent.ConcurrentMap;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.Future;
8+
import java.util.concurrent.FutureTask;
9+
10+
11+
public class Cache<K, V> implements Computable<K, V> {
12+
private final ConcurrentMap<K, Future<V>> cache = new ConcurrentHashMap<K, Future<V>>();
13+
private final Computable<K, V> c;
14+
15+
public Cache(Computable<K, V> c) {
16+
this.c = c;
17+
}
18+
19+
@Override
20+
public synchronized V compute(final K key) throws InterruptedException {
21+
Future<V> f = cache.get(key);
22+
if (f == null) {
23+
Callable<V> eval = new Callable<V>() {
24+
@Override
25+
public V call() throws Exception {
26+
return c.compute(key);
27+
}
28+
};
29+
FutureTask<V> ft = new FutureTask<V>(eval);
30+
f = cache.putIfAbsent(key, ft);
31+
if (f == null) {
32+
f = ft;
33+
ft.run();// 实际运算发生在这
34+
}
35+
}
36+
try {
37+
return f.get();
38+
} catch (ExecutionException e) {
39+
throw new RuntimeException(e);
40+
}
41+
}
42+
43+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package codeplus.util.cache;
2+
3+
public interface Computable<K, V> {
4+
V compute(K key) throws InterruptedException;
5+
}
+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package codeplus.util.mapper;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.Set;
7+
8+
import javax.xml.xpath.XPathExpressionException;
9+
10+
import org.eclipse.core.resources.IFile;
11+
import org.eclipse.jdt.core.IJavaProject;
12+
import org.eclipse.wst.xml.core.internal.document.ElementImpl;
13+
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
14+
import org.w3c.dom.Node;
15+
16+
import codeplus.util.SimilarityUtil;
17+
import codeplus.util.XmlUtil;
18+
import codeplus.util.XpathUtil;
19+
import codeplus.util.cache.Cache;
20+
import codeplus.util.cache.Computable;
21+
22+
public class FindMapperXml {
23+
24+
public static final String MATCH_REG = ".*(?i)(dao(impl)?|mapper)\\.java";
25+
26+
private static final Map<IJavaProject, Cache<String, String>> simCache = new HashMap<>();
27+
28+
private static synchronized Cache<String, String> getSimCache(final IJavaProject javaProj) {
29+
Cache<String, String> map = simCache.get(javaProj);
30+
if (map == null) {
31+
map = new Cache<String, String>(new Computable<String, String>() {
32+
@Override
33+
public String compute(String key) throws InterruptedException {
34+
Map<String, List<IFile>> map = MapperNamespaceCache.getInstance().getCacheMap(javaProj, null);
35+
Set<String> ns = map.keySet();
36+
37+
// 包含大于相似
38+
int len = 0;
39+
String maxContains = null;
40+
for (String s : ns) {
41+
String simpleName = s.substring(s.lastIndexOf(".") + 1);
42+
if (key.contains(simpleName)) {
43+
if (simpleName.length() > len) {
44+
len = simpleName.length();
45+
maxContains = s;
46+
}
47+
}
48+
}
49+
50+
// 没有包含再按相似度匹配
51+
if (maxContains == null) {
52+
double max = 0;
53+
String simNamespace = null;
54+
for (String s : ns) {
55+
double sim = SimilarityUtil.sim(key, s);
56+
if (sim > max) {
57+
max = sim;
58+
simNamespace = s;
59+
}
60+
}
61+
return simNamespace;
62+
}
63+
return maxContains;
64+
}
65+
});
66+
simCache.put(javaProj, map);
67+
}
68+
return map;
69+
}
70+
71+
private static String getSimNamespace(IJavaProject javaProj, String namespace) throws InterruptedException {
72+
String simNamespace = getSimCache(javaProj).compute(namespace);
73+
return simNamespace;
74+
}
75+
76+
public static String findSql(IJavaProject javaProj, String namespace, String sqlId)
77+
throws XPathExpressionException, InterruptedException {
78+
List<IFile> lst = MapperNamespaceCache.getInstance().get(javaProj, namespace, null);
79+
if (lst != null) {
80+
for (IFile mapperFile : lst) {
81+
IDOMDocument mapperDocument = XmlUtil.getMapperDocument(mapperFile);
82+
Node node = XpathUtil.xpathNode(mapperDocument, "//*[@id='" + sqlId + "']");
83+
if (node != null) {
84+
StringBuilder buffer = new StringBuilder();
85+
XmlUtil.computeStatementText((ElementImpl) node, buffer, javaProj);
86+
87+
String s = buffer.toString();
88+
return s;
89+
}
90+
}
91+
}
92+
93+
// 采用相似度查找
94+
String simNamespace = getSimNamespace(javaProj, namespace);
95+
lst = MapperNamespaceCache.getInstance().get(javaProj, simNamespace, null);
96+
if (lst != null) {
97+
for (IFile mapperFile : lst) {
98+
IDOMDocument mapperDocument = XmlUtil.getMapperDocument(mapperFile);
99+
Node node = XpathUtil.xpathNode(mapperDocument, "//*[@id='" + sqlId + "']");
100+
if (node != null) {
101+
StringBuilder buffer = new StringBuilder();
102+
XmlUtil.computeStatementText((ElementImpl) node, buffer, javaProj);
103+
104+
String s = buffer.toString();
105+
return s;
106+
}
107+
}
108+
}
109+
110+
return null;
111+
}
112+
113+
public static IFile findSqlXml(IJavaProject javaProj, String namespace, String sqlId)
114+
throws XPathExpressionException, InterruptedException {
115+
List<IFile> lst = MapperNamespaceCache.getInstance().get(javaProj, namespace, null);
116+
if (lst != null) {
117+
for (IFile mapperFile : lst) {
118+
IDOMDocument mapperDocument = XmlUtil.getMapperDocument(mapperFile);
119+
Node node = XpathUtil.xpathNode(mapperDocument, "//*[@id='" + sqlId + "']");
120+
if (node != null) {
121+
return mapperFile;
122+
}
123+
}
124+
}
125+
126+
// 采用相似度查找
127+
String simNamespace = getSimNamespace(javaProj, namespace);
128+
lst = MapperNamespaceCache.getInstance().get(javaProj, simNamespace, null);
129+
if (lst != null) {
130+
for (IFile mapperFile : lst) {
131+
IDOMDocument mapperDocument = XmlUtil.getMapperDocument(mapperFile);
132+
Node node = XpathUtil.xpathNode(mapperDocument, "//*[@id='" + sqlId + "']");
133+
if (node != null) {
134+
return mapperFile;
135+
}
136+
}
137+
}
138+
return null;
139+
}
140+
141+
}

‎src/codeplus/util/MapperNamespaceCache.java ‎src/codeplus/util/mapper/MapperNamespaceCache.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package codeplus.util;
1+
package codeplus.util.mapper;
22

33
import java.util.ArrayList;
44
import java.util.Iterator;
@@ -26,6 +26,7 @@
2626
import org.w3c.dom.Node;
2727

2828
import codeplus.Activator;
29+
import codeplus.util.XpathUtil;
2930

3031
//import net.harawata.mybatipse.Activator;
3132
//import net.harawata.mybatipse.util.XpathUtil;

0 commit comments

Comments
 (0)
Please sign in to comment.