1
1
/*******************************************************************************
2
- * Copyright (c) 2009, 2019 Xored Software Inc and others.
2
+ * Copyright (c) 2009 Xored Software Inc and others.
3
3
* All rights reserved. This program and the accompanying materials
4
4
* are made available under the terms of the Eclipse Public License v2.0
5
5
* which accompanies this distribution, and is available at
14
14
15
15
import java .net .URI ;
16
16
import java .net .URISyntaxException ;
17
+ import java .net .URL ;
17
18
import java .util .regex .Matcher ;
18
19
import java .util .regex .Pattern ;
19
20
21
+ import org .eclipse .core .runtime .Adapters ;
20
22
import org .eclipse .core .runtime .Platform ;
21
23
import org .eclipse .jface .resource .ImageDescriptor ;
22
24
import org .eclipse .swt .program .Program ;
23
25
import org .eclipse .ui .internal .misc .ExternalProgramImageDescriptor ;
24
26
import org .osgi .framework .Bundle ;
25
27
26
28
public enum DescriptorInfo {
27
- BUNDLE_URL () {
28
- public final Pattern pattern = Pattern .compile ("URLImageDescriptor\\ (((bundleentry|bundleresource).*)\\ )" );
29
-
29
+ URL_ADAPTER () {
30
30
@ Override
31
- public String extract (String descriptorStr ) {
32
- Matcher matcher = pattern .matcher (descriptorStr );
33
- if (matcher .matches ()) {
34
- String uriStr = matcher .group (1 );
35
- URI bundleUri = null ;
31
+ String extract (ImageDescriptor descriptor ) {
32
+ URL url = Adapters .adapt (descriptor , URL .class , false );
33
+ if (url != null ) {
36
34
try {
37
- bundleUri = new URI ( uriStr );
35
+ return extractFromUri ( url . toURI () );
38
36
} catch (URISyntaxException e ) {
39
- return "InvalidUri(" + uriStr + ")" ;
37
+ JFaceAspectsActivator .log (e );
38
+ return null ;
40
39
}
41
-
42
- String host = bundleUri .getHost ();
43
- int bundleIdEndIndex = host .indexOf (".fwk" );
44
- if (bundleIdEndIndex == -1 ) {
45
- return "UnknownBundleId(" + uriStr + ")" ;
46
- }
47
-
48
- int bundleId = -1 ;
49
- try {
50
- bundleId = Integer .parseInt (host .substring (0 , bundleIdEndIndex ));
51
- } catch (NumberFormatException e ) {
52
- return "UnknownBundleId(" + uriStr + ")" ;
53
- }
54
-
55
- Bundle imageBundle = JFaceAspectsActivator .getDefault ().getBundle ().getBundleContext ()
56
- .getBundle (bundleId );
57
- String bundleName = imageBundle == null ? "unknownBundle" : imageBundle .getSymbolicName ();
58
- return String .format ("%s%s" , bundleName , bundleUri .getPath ());
59
- } else {
60
- return null ;
61
40
}
41
+ return null ;
42
+ }
43
+
44
+ },
45
+ EXTERNAL_PROGRAM_ADAPTER () {
46
+ @ Override
47
+ String extract (ImageDescriptor descriptor ) {
48
+ Program p = Adapters .adapt (descriptor , Program .class , false );
49
+ return extractFromProgram (p );
62
50
}
51
+
52
+ },
53
+ BUNDLE_URL () {
54
+ private final Pattern pattern = Pattern .compile ("URLImageDescriptor\\ (((bundleentry|bundleresource).*)\\ )" );
63
55
64
56
@ Override
65
57
String extract (ImageDescriptor descriptor ) {
66
- return extract (descriptor .toString ());
58
+ Matcher matcher = pattern .matcher (descriptor .toString ());
59
+ if (!matcher .matches ()) {
60
+ return null ;
61
+ }
62
+ String uriStr = matcher .group (1 );
63
+ URI bundleUri = null ;
64
+ try {
65
+ bundleUri = new URI (uriStr );
66
+ } catch (URISyntaxException e ) {
67
+ return "InvalidUri(" + uriStr + ")" ;
68
+ }
69
+ return extractFromUri (bundleUri );
67
70
}
68
71
},
69
72
70
73
ABSOLUTE_URL () {
71
- public final Pattern pattern = Pattern .compile ("URLImageDescriptor\\ ((file:/|platform:/plugin/)(.*)\\ )" );
72
-
74
+ private final Pattern pattern = Pattern .compile ("URLImageDescriptor\\ ((file:/|platform:/plugin/)(.*)\\ )" );
73
75
@ Override
74
- public String extract (String descriptorStr ) {
75
- Matcher matcher = pattern .matcher (descriptorStr );
76
+ String extract (ImageDescriptor descriptor ) {
77
+ Matcher matcher = pattern .matcher (descriptor . toString () );
76
78
if (matcher .matches ()) {
77
79
return matcher .group (2 );
78
80
} else {
79
81
return null ;
80
82
}
81
83
}
82
-
83
- @ Override
84
- String extract (ImageDescriptor descriptor ) {
85
- return extract (descriptor .toString ());
86
- }
87
84
},
88
85
89
86
FILE_CLASS () {
90
- public final Pattern pattern = Pattern .compile ("FileImageDescriptor\\ (location=class (.*), name=(.*)\\ )" );
91
-
87
+ private final Pattern pattern = Pattern .compile ("FileImageDescriptor\\ (location=class (.*), name=(.*)\\ )" );
92
88
@ Override
93
- public String extract (String descriptorStr ) {
94
- Matcher matcher = pattern .matcher (descriptorStr );
89
+ String extract (ImageDescriptor descriptor ) {
90
+ Matcher matcher = pattern .matcher (descriptor . toString () );
95
91
if (matcher .matches ()) {
96
92
return String .format ("%s%s" , matcher .group (1 ), matcher .group (2 ));
97
93
} else {
98
94
return null ;
99
95
}
100
96
}
101
-
102
- @ Override
103
- String extract (ImageDescriptor descriptor ) {
104
- return extract (descriptor .toString ());
105
- }
106
97
},
107
98
108
99
/**
109
100
* Gets info from program because ExternalProgramImageDescriptor.toString() has no useful information
110
101
*/
111
102
EXTERNAL_PROGRAM () {
112
- private final boolean isWindows = Platform .getOS ().equals (Platform .OS_WIN32 );
113
-
114
103
@ Override
115
104
String extract (ImageDescriptor descriptor ) {
116
105
if (descriptor instanceof ExternalProgramImageDescriptor ) {
117
- if (isWindows ) {
118
- try {
119
- Program p = (Program ) getField (descriptor , "program" , true );
120
-
121
- String extension = (String ) getField (p , "extension" , true );
122
- if (extension != null && !extension .isEmpty ()) {
123
- return extension ;
124
- }
125
-
126
- String iconName = (String ) getField (p , "iconName" , true );
127
- if (iconName != null && !iconName .isEmpty ()) {
128
- return iconName ;
129
- }
130
- } catch (IllegalArgumentException e ) {
131
- JFaceAspectsActivator .log (e );
132
- } catch (IllegalAccessException e ) {
133
- JFaceAspectsActivator .log (e );
134
- } catch (NoSuchFieldException e ) {
135
- JFaceAspectsActivator .log (e );
136
- } catch (SecurityException e ) {
137
- JFaceAspectsActivator .log (e );
138
- }
106
+ try {
107
+ return extractFromProgram ( (Program ) getField (descriptor , "program" , true ) );
108
+ } catch (NoSuchFieldException | IllegalAccessException e ) {
109
+ JFaceAspectsActivator .log (e );
110
+ return null ;
139
111
}
140
112
}
141
113
return null ;
142
114
}
143
115
};
116
+
117
+
144
118
145
119
public static String getInfo (ImageDescriptor descriptor ) {
146
120
for (DescriptorInfo i : DescriptorInfo .values ()) {
@@ -152,9 +126,57 @@ public static String getInfo(ImageDescriptor descriptor) {
152
126
return null ;
153
127
}
154
128
155
- public String extract (String descriptorStr ) {
156
- return null ;
129
+ abstract String extract (ImageDescriptor descriptor );
130
+
131
+ private static final boolean isWindows = Platform .getOS ().equals (Platform .OS_WIN32 );
132
+ private static String extractFromProgram (Program p ) {
133
+ if (p == null ) {
134
+ return null ;
135
+ }
136
+ try {
137
+ if (isWindows ) {
138
+ String extension = (String ) getField (p , "extension" , true );
139
+ if (extension != null && !extension .isEmpty ()) {
140
+ return extension ;
141
+ }
142
+
143
+ String iconName = (String ) getField (p , "iconName" , true );
144
+ if (iconName != null && !iconName .isEmpty ()) {
145
+ return iconName ;
146
+ }
147
+ }
148
+ } catch (IllegalArgumentException e ) {
149
+ JFaceAspectsActivator .log (e );
150
+ } catch (IllegalAccessException e ) {
151
+ JFaceAspectsActivator .log (e );
152
+ } catch (NoSuchFieldException e ) {
153
+ JFaceAspectsActivator .log (e );
154
+ } catch (SecurityException e ) {
155
+ JFaceAspectsActivator .log (e );
156
+ }
157
+ return p .getName ().isEmpty () ? null : p .getName ();
158
+ }
159
+ private static String extractFromUri (URI bundleUri ) {
160
+ String host = bundleUri .getHost ();
161
+ if (host == null ) {
162
+ return null ;
163
+ }
164
+ int bundleIdEndIndex = host .indexOf (".fwk" );
165
+ if (bundleIdEndIndex == -1 ) {
166
+ return "UnknownBundleId(" + bundleUri + ")" ;
167
+ }
168
+
169
+ int bundleId = -1 ;
170
+ try {
171
+ bundleId = Integer .parseInt (host .substring (0 , bundleIdEndIndex ));
172
+ } catch (NumberFormatException e ) {
173
+ return "UnknownBundleId(" + bundleUri + ")" ;
174
+ }
175
+
176
+ Bundle imageBundle = JFaceAspectsActivator .getDefault ().getBundle ().getBundleContext ()
177
+ .getBundle (bundleId );
178
+ String bundleName = imageBundle == null ? "unknownBundle" : imageBundle .getSymbolicName ();
179
+ return String .format ("%s%s" , bundleName , bundleUri .getPath ());
157
180
}
158
181
159
- abstract String extract (ImageDescriptor descriptor );
160
182
}
0 commit comments