1
1
package fr .adrienbrault .idea .symfony2plugin .profiler ;
2
2
3
+ import com .intellij .lang .html .HTMLLanguage ;
3
4
import com .intellij .openapi .project .Project ;
5
+ import com .intellij .psi .PsiElement ;
6
+ import com .intellij .psi .PsiFileFactory ;
7
+ import com .intellij .psi .impl .source .html .HtmlFileImpl ;
8
+ import com .intellij .psi .util .PsiTreeUtil ;
9
+ import com .intellij .psi .xml .XmlTag ;
4
10
import fr .adrienbrault .idea .symfony2plugin .Symfony2ProjectComponent ;
11
+ import fr .adrienbrault .idea .symfony2plugin .profiler .dict .ProfilerRequest ;
12
+ import org .apache .commons .lang .StringUtils ;
13
+ import org .jetbrains .annotations .NotNull ;
5
14
import org .jetbrains .annotations .Nullable ;
6
-
15
+ import java . io . BufferedReader ;
7
16
import java .io .File ;
17
+ import java .io .IOException ;
18
+ import java .io .InputStreamReader ;
19
+ import java .net .URL ;
20
+ import java .net .URLConnection ;
21
+ import java .nio .charset .StandardCharsets ;
22
+ import java .util .ArrayList ;
23
+ import java .util .Collection ;
24
+ import java .util .Collections ;
25
+ import java .util .List ;
26
+ import java .util .stream .Collectors ;
8
27
9
28
public class ProfilerUtil {
10
29
@@ -24,4 +43,61 @@ public static File findProfilerCsv(Project project) {
24
43
return null ;
25
44
}
26
45
46
+ @ Nullable
47
+ public static String getProfilerIndexViaHttp (@ NotNull String url ) {
48
+ URLConnection conn ;
49
+ try {
50
+ conn = new URL ("http://127.0.0.1:8000/_profiler/empty/search/results?ip=&limit=10" ).openConnection ();
51
+ } catch (IOException e ) {
52
+ return null ;
53
+ }
54
+
55
+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (conn .getInputStream (), StandardCharsets .UTF_8 ))) {
56
+ return reader .lines ().collect (Collectors .joining ("\n " ));
57
+ } catch (IOException e ) {
58
+ return null ;
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Extract "table.search-results tbody tr td"
64
+ * We dont have complete xpath inside with html support to use internal html parser
65
+ */
66
+ @ NotNull
67
+ public static Collection <ProfilerRequest > createRequestsFromIndexHtml (@ NotNull Project project , @ NotNull String html ) {
68
+ HtmlFileImpl htmlFile = (HtmlFileImpl ) PsiFileFactory .getInstance (project ).createFileFromText (HTMLLanguage .INSTANCE , html );
69
+
70
+ PsiElement [] results = PsiTreeUtil .collectElements (htmlFile , psiElement ->
71
+ psiElement instanceof XmlTag && "table" .equals (((XmlTag ) psiElement ).getName ()) && "search-results" .equals (((XmlTag ) psiElement ).getAttributeValue ("id" ))
72
+ );
73
+
74
+ if (results .length == 0 ) {
75
+ return Collections .emptyList ();
76
+ }
77
+
78
+ XmlTag tbody = ((XmlTag ) results [0 ]).findFirstSubTag ("tbody" );
79
+ if (tbody == null ) {
80
+ return Collections .emptyList ();
81
+ }
82
+
83
+ Collection <ProfilerRequest > requests = new ArrayList <>();
84
+ for (XmlTag tr : tbody .findSubTags ("tr" )) {
85
+ List <String > values = new ArrayList <>();
86
+ for (XmlTag td : tr .findSubTags ("td" )) {
87
+ values .add (StringUtils .trim (stripHtmlTags (td .getText ().replaceAll ("\\ n" , " " ))).replace ("\\ s+" , " " ));
88
+ }
89
+
90
+ requests .add (
91
+ new ProfilerRequest (new String [] {values .get (5 ), "" , values .get (2 ), values .get (3 ), values .get (4 )}, new ProfilerIndex (new File ("aa" )))
92
+ );
93
+ }
94
+
95
+ return requests ;
96
+ }
97
+
98
+ @ NotNull
99
+ private static String stripHtmlTags (@ NotNull String text )
100
+ {
101
+ return text .replaceAll ("<[^>]*>" , "" );
102
+ }
27
103
}
0 commit comments