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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
|
/*******************************************************************************
* Copyright (c) 2009, 2017 Andrew Gvozdev and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Andrew Gvozdev - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.language.settings.providers;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.CommandLauncherManager;
import org.eclipse.cdt.core.language.settings.providers.ICListenerAgent;
import org.eclipse.cdt.core.language.settings.providers.ILanguageSettingsBroadcastingProvider;
import org.eclipse.cdt.core.language.settings.providers.ILanguageSettingsChangeEvent;
import org.eclipse.cdt.core.language.settings.providers.ILanguageSettingsChangeListener;
import org.eclipse.cdt.core.language.settings.providers.ILanguageSettingsEditableProvider;
import org.eclipse.cdt.core.language.settings.providers.ILanguageSettingsProvider;
import org.eclipse.cdt.core.language.settings.providers.ILanguageSettingsProvidersKeeper;
import org.eclipse.cdt.core.language.settings.providers.LanguageSettingsManager;
import org.eclipse.cdt.core.language.settings.providers.LanguageSettingsSerializableProvider;
import org.eclipse.cdt.core.language.settings.providers.LanguageSettingsStorage;
import org.eclipse.cdt.core.language.settings.providers.ScannerDiscoveryLegacySupport;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.ICSettingEntry;
import org.eclipse.cdt.internal.core.XmlUtil;
import org.eclipse.cdt.internal.core.model.Util;
import org.eclipse.cdt.internal.core.settings.model.CConfigurationSpecSettings;
import org.eclipse.cdt.internal.core.settings.model.IInternalCCfgInfo;
import org.eclipse.cdt.internal.core.settings.model.SettingsModelMessages;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ILock;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.osgi.util.NLS;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Helper class handling serialization and notifications for language settings entries {@link ICLanguageSettingEntry}.
*/
public class LanguageSettingsProvidersSerializer {
public static final String PROVIDER_EXTENSION_POINT_ID = LanguageSettingsExtensionManager.PROVIDER_EXTENSION_POINT_ID;
public static final String ATTR_ID = LanguageSettingsExtensionManager.ATTR_ID;
public static final String ATTR_NAME = LanguageSettingsExtensionManager.ATTR_NAME;
public static final String ATTR_CLASS = LanguageSettingsExtensionManager.ATTR_CLASS;
public static final String ELEM_PROVIDER = LanguageSettingsExtensionManager.ELEM_PROVIDER;
public static final String ELEM_LANGUAGE_SCOPE = LanguageSettingsExtensionManager.ELEM_LANGUAGE_SCOPE;
public static final String JOB_FAMILY_SERIALIZE_LANGUAGE_SETTINGS_PROJECT = "CDT_JOB_FAMILY_SERIALIZE_LANGUAGE_SETTINGS_PROJECT"; //$NON-NLS-1$
public static final String JOB_FAMILY_SERIALIZE_LANGUAGE_SETTINGS_WORKSPACE = "CDT_JOB_FAMILY_SERIALIZE_LANGUAGE_SETTINGS_WORKSPACE"; //$NON-NLS-1$
private static final String PREFERENCE_WORSPACE_PROVIDERS_SET = "language.settings.providers.workspace.prefs.toggle"; //$NON-NLS-1$
private static final String CPROJECT_STORAGE_MODULE_LANGUAGE_SETTINGS_PROVIDERS = "org.eclipse.cdt.core.LanguageSettingsProviders"; //$NON-NLS-1$
private static final String STORAGE_WORKSPACE_LANGUAGE_SETTINGS = "language.settings.xml"; //$NON-NLS-1$
private static final String STORAGE_PROJECT_PATH = ".settings/language.settings.xml"; //$NON-NLS-1$
private static final int PROGRESS_MONITOR_SCALE = 100;
private static final int TICKS_SERIALIZING = 1 * PROGRESS_MONITOR_SCALE;
private static final String ELEM_PLUGIN = "plugin"; //$NON-NLS-1$
private static final String ELEM_EXTENSION = "extension"; //$NON-NLS-1$
private static final String ATTR_EXTENSION_POINT = "point"; //$NON-NLS-1$
private static final String ELEM_PROJECT = "project"; //$NON-NLS-1$
private static final String ELEM_CONFIGURATION = "configuration"; //$NON-NLS-1$
private static final String ELEM_PROVIDER_REFERENCE = "provider-reference"; //$NON-NLS-1$
private static final String ATTR_STORE_ENTRIES_WITH_PROJECT = "store-entries-with-project"; //$NON-NLS-1$
// those are for readability of xml only
private static final String ATTR_REF = "ref"; //$NON-NLS-1$
private static final String VALUE_REF_SHARED_PROVIDER = "shared-provider"; //$NON-NLS-1$
private static final String ATTR_COPY_OF = "copy-of"; //$NON-NLS-1$
private static final String VALUE_COPY_OF_EXTENSION = "extension"; //$NON-NLS-1$
/** Cache of true (raw) workspace providers */
private static Map<String, ILanguageSettingsProvider> rawGlobalWorkspaceProviders = new HashMap<String, ILanguageSettingsProvider>();
/** Cache of workspace providers wrappers */
private static Map<String, ILanguageSettingsProvider> globalWorkspaceProviders = new HashMap<String, ILanguageSettingsProvider>();
private static ListenerList<ILanguageSettingsChangeListener> fLanguageSettingsChangeListeners = new ListenerList<>(
ListenerList.IDENTITY);
private static ILock serializingLock = Job.getJobManager().newLock();
private static ILock serializingLockWsp = Job.getJobManager().newLock();
/**
* Dummy class to represent ill-defined provider.
*/
private static class NotAccessibleProvider implements ILanguageSettingsProvider {
private final String id;
private NotAccessibleProvider(String providerId) {
this.id = providerId;
}
@Override
public List<ICLanguageSettingEntry> getSettingEntries(ICConfigurationDescription cfgDescription, IResource rc,
String languageId) {
return null;
}
@Override
public String getName() {
return null;
}
@Override
public String getId() {
return id;
}
}
/**
* language settings provider listener-cfgDescription association
*/
private static class ListenerAssociation {
private ICListenerAgent listener;
private ICConfigurationDescription cfgDescription;
public ListenerAssociation(ICListenerAgent la, ICConfigurationDescription cfgd) {
listener = la;
cfgDescription = cfgd;
}
}
/**
* Wrapper for workspace providers to ensure level of indirection. That way workspace providers
* can be changed/replaced without notifying/changing the configurations which keep the providers
* in their lists.
*/
private static class LanguageSettingsWorkspaceProvider implements ILanguageSettingsProvider, ICListenerAgent {
private String providerId;
private int projectCount = 0;
private LanguageSettingsWorkspaceProvider(String id) {
Assert.isNotNull(id);
providerId = id;
}
@Override
public String getId() {
return providerId;
}
@Override
public String getName() {
ILanguageSettingsProvider rawProvider = getRawProvider();
String name = rawProvider != null ? rawProvider.getName() : null;
return name;
}
@Override
public List<ICLanguageSettingEntry> getSettingEntries(ICConfigurationDescription cfgDescription, IResource rc,
String languageId) {
ILanguageSettingsProvider rawProvider = getRawProvider();
List<ICLanguageSettingEntry> entries = rawProvider != null
? rawProvider.getSettingEntries(cfgDescription, rc, languageId)
: null;
if (cfgDescription != null) {
IProject project = cfgDescription.getProjectDescription().getProject();
entries = CommandLauncherManager.getInstance().getLanguageSettingEntries(project, entries);
}
return entries;
}
/**
* Do not cache the "raw" provider as workspace provider can be changed at any time.
*/
private ILanguageSettingsProvider getRawProvider() {
return LanguageSettingsProvidersSerializer.getRawWorkspaceProvider(providerId);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof LanguageSettingsWorkspaceProvider) {
LanguageSettingsWorkspaceProvider that = (LanguageSettingsWorkspaceProvider) obj;
return providerId.equals(that.providerId);
}
return false;
}
/**
* Method toString() for debugging purposes.
*/
@SuppressWarnings("nls")
@Override
public String toString() {
return "id=" + getId() + ", name=" + getName();
}
/**
* We count number of times <b>workspace</b> provider (not the raw one!) associated
* with a <b>project</b>. If a project includes it multiple times via different configurations
* it still counts as 1.
*/
private int getProjectCount() {
return projectCount;
}
private synchronized int incrementProjectCount() {
projectCount++;
return projectCount;
}
private synchronized int decrementProjectCount() {
projectCount--;
return projectCount;
}
@Override
public void registerListener(ICConfigurationDescription cfgDescription) {
// keep in mind that rawProvider can change externally
ILanguageSettingsProvider rawProvider = getRawProvider();
if (rawProvider instanceof ICListenerAgent) {
((ICListenerAgent) rawProvider).registerListener(null);
}
}
@Override
public void unregisterListener() {
// keep in mind that rawProvider can change externally
ILanguageSettingsProvider rawProvider = getRawProvider();
if (rawProvider instanceof ICListenerAgent) {
((ICListenerAgent) rawProvider).unregisterListener();
}
}
}
/**
* Language Settings Change Event implementation.
*/
private static class LanguageSettingsChangeEvent implements ILanguageSettingsChangeEvent {
private String projectName = null;
private Map<String/*cfg*/, LanguageSettingsDelta> deltaMap = new HashMap<String, LanguageSettingsDelta>();
/**
* The act of creating event resets internal delta count in configuration state.
* That implies that when the event is retrieved it must be fired or delta will go missing.
* That side effect is here to ensure atomic processing of firing & resetting the delta.
*/
public LanguageSettingsChangeEvent(ICProjectDescription prjDescription) {
if (!prjDescription.isReadOnly()) {
// The logic goes that we send notifications only for acting description but not for currently being prepared to set
String msg = "Project description " + prjDescription.getName() + " is expected to be read-only"; //$NON-NLS-1$ //$NON-NLS-2$
CCorePlugin.log(new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, msg, new Exception(msg)));
}
projectName = prjDescription.getName();
ICConfigurationDescription[] cfgDescriptions = prjDescription.getConfigurations();
for (ICConfigurationDescription cfgDescription : cfgDescriptions) {
if (cfgDescription instanceof IInternalCCfgInfo) {
CConfigurationSpecSettings specSettings = null;
try {
specSettings = ((IInternalCCfgInfo) cfgDescription).getSpecSettings();
} catch (CoreException e) {
CCorePlugin.log(e);
}
if (specSettings != null) {
String cfgId = cfgDescription.getId();
if (ScannerDiscoveryLegacySupport
.isLanguageSettingsProvidersFunctionalityEnabled(prjDescription.getProject())) {
LanguageSettingsDelta delta = specSettings.dropDelta();
if (delta != null) {
deltaMap.put(cfgId, delta);
}
} else {
deltaMap.remove(cfgId);
}
} else {
IStatus ss = new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID,
"Internal error: Missing specSettings for " //$NON-NLS-1$
+ cfgDescription.getClass().getSimpleName());
CCorePlugin.log(new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, ss.getMessage(),
new CoreException(ss)));
}
}
}
}
@Override
public String getProjectName() {
return projectName;
}
@Override
public String[] getConfigurationDescriptionIds() {
return deltaMap.keySet().toArray(new String[deltaMap.size()]);
}
@SuppressWarnings("nls")
@Override
public String toString() {
return "LanguageSettingsChangeEvent for project=[" + getProjectName() + "]" + ", configurations="
+ deltaMap.keySet();
}
@Override
public Set<IResource> getAffectedResources(String cfgId) {
LanguageSettingsDelta delta = deltaMap.get(cfgId);
if (delta != null) {
Set<String> paths = delta.getAffectedResourcePaths();
if (!paths.isEmpty()) {
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
Set<IResource> resources = new HashSet<IResource>();
for (String path : paths) {
IResource rc = project.findMember(path);
if (rc != null) {
resources.add(rc);
}
}
return resources;
}
}
return null;
}
}
/** static initializer */
static {
try {
loadLanguageSettingsWorkspace();
} catch (Throwable e) {
// log but swallow any exception
CCorePlugin.log("Error loading workspace language settings providers", e); //$NON-NLS-1$
}
}
/**
* Tells if language settings entries of the provider are persisted with the project
* (under .settings/ folder) or in workspace area. Persistence in the project area lets
* the entries migrate with the project.
*
* @param provider - provider to check the persistence mode.
* @return {@code true} if LSE persisted with the project or {@code false} if in the workspace.
*/
public static boolean isStoringEntriesInProjectArea(LanguageSettingsSerializableProvider provider) {
return provider.getPropertyBool(ATTR_STORE_ENTRIES_WITH_PROJECT);
}
/**
* Define where language settings are persisted for the provider.
*
* @param provider - provider to set the persistence mode.
* @param storeEntriesWithProject - {@code true} if with the project,
* {@code false} if in workspace area.
*/
public static void setStoringEntriesInProjectArea(LanguageSettingsSerializableProvider provider,
boolean storeEntriesWithProject) {
provider.setPropertyBool(ATTR_STORE_ENTRIES_WITH_PROJECT, storeEntriesWithProject);
}
/**
* Determine location of the project store of language settings providers in the plug-in state area.
*
* @param store - name of the store.
* @return location of the store in the plug-in state area.
*/
private static IFile getStoreInProjectArea(IProject project) {
return project.getFile(STORAGE_PROJECT_PATH);
}
/**
* Determine location of the store in the plug-in state area.
*
* @param store - name of the store.
* @return location of the store in the plug-in state area.
*/
private static URI getStoreInWorkspaceArea(String store) {
IPath location = CCorePlugin.getDefault().getStateLocation().append(store);
return URIUtil.toURI(location);
}
/**
* Set and store user defined providers in workspace area.
*
* @param providers - array of user defined providers
* @throws CoreException in case of problems
*/
public static void setWorkspaceProviders(List<ILanguageSettingsProvider> providers) throws CoreException {
setWorkspaceProvidersInternal(providers);
serializeLanguageSettingsWorkspaceInBackground();
// generate preference change event for preference change listeners (value is not intended to be used)
IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID);
prefs.putBoolean(PREFERENCE_WORSPACE_PROVIDERS_SET,
!prefs.getBoolean(PREFERENCE_WORSPACE_PROVIDERS_SET, false));
}
/**
* Internal method to set user defined providers in memory.
*
* @param providers - list of user defined providers. If {@code null}
* is passed user defined providers are cleared.
*/
private static void setWorkspaceProvidersInternal(List<ILanguageSettingsProvider> providers) {
Map<String, ILanguageSettingsProvider> rawNewProviders = new HashMap<String, ILanguageSettingsProvider>();
// add given providers
if (providers != null) {
for (ILanguageSettingsProvider provider : providers) {
if (isWorkspaceProvider(provider)) {
provider = rawGlobalWorkspaceProviders.get(provider.getId());
}
if (provider != null) {
rawNewProviders.put(provider.getId(), provider);
}
}
}
// fill the rest from extension registry
// this list is independent from the internal list of extensions in LanguageSettingsExtensionManager
for (String id : LanguageSettingsExtensionManager.getExtensionProviderIds()) {
if (!rawNewProviders.containsKey(id)) {
ILanguageSettingsProvider provider = LanguageSettingsExtensionManager.getExtensionProviderCopy(id,
true);
if (provider == null) {
provider = LanguageSettingsExtensionManager.loadProvider(id);
}
if (provider != null) {
rawNewProviders.put(provider.getId(), provider);
}
}
}
// register listeners
List<ICListenerAgent> oldListeners = selectListeners(rawGlobalWorkspaceProviders.values());
List<ICListenerAgent> newListeners = selectListeners(rawNewProviders.values());
for (ICListenerAgent oldListener : oldListeners) {
if (!isInList(newListeners, oldListener)) {
LanguageSettingsWorkspaceProvider wspProvider = (LanguageSettingsWorkspaceProvider) globalWorkspaceProviders
.get(((ILanguageSettingsProvider) oldListener).getId());
if (wspProvider != null && wspProvider.getProjectCount() > 0) {
oldListener.unregisterListener();
}
}
}
for (ICListenerAgent newListener : newListeners) {
if (!isInList(oldListeners, newListener)) {
LanguageSettingsWorkspaceProvider wspProvider = (LanguageSettingsWorkspaceProvider) globalWorkspaceProviders
.get(((ILanguageSettingsProvider) newListener).getId());
if (wspProvider != null && wspProvider.getProjectCount() > 0) {
newListener.registerListener(null);
}
}
}
rawGlobalWorkspaceProviders = rawNewProviders;
}
/**
* Create event for language settings changes of workspace providers in a project.
*/
private static LanguageSettingsChangeEvent createEvent(ICProjectDescription prjDescription,
List<String> providerIds) {
ICConfigurationDescription[] cfgDescriptions = prjDescription.getConfigurations();
for (ICConfigurationDescription cfgDescription : cfgDescriptions) {
if (cfgDescription instanceof ILanguageSettingsProvidersKeeper) {
for (ILanguageSettingsProvider provider : ((ILanguageSettingsProvidersKeeper) cfgDescription)
.getLanguageSettingProviders()) {
if (isWorkspaceProvider(provider) && providerIds.contains(provider.getId())) {
LanguageSettingsChangeEvent event = new LanguageSettingsChangeEvent(prjDescription);
if (event.getConfigurationDescriptionIds().length > 0) {
return event;
}
return null;
}
}
}
}
return null;
}
/**
* Compute events for language settings changes in workspace.
*/
private static List<LanguageSettingsChangeEvent> createLanguageSettingsChangeEvents(
List<ILanguageSettingsBroadcastingProvider> providers) {
List<LanguageSettingsChangeEvent> events = new ArrayList<LanguageSettingsChangeEvent>();
List<String> providerIds = new ArrayList<String>();
for (ILanguageSettingsBroadcastingProvider provider : providers) {
providerIds.add(provider.getId());
}
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject[] projects = root.getProjects();
for (IProject project : projects) {
try {
if (project.isAccessible()) {
ICProjectDescription prjDescription = CCorePlugin.getDefault().getProjectDescription(project,
false);
if (prjDescription != null) {
LanguageSettingsChangeEvent event = createEvent(prjDescription, providerIds);
if (event != null) {
events.add(event);
}
}
}
} catch (Throwable e) {
// log and swallow any exception
CCorePlugin.log("Error creating event about changes in workspace language settings providers, " //$NON-NLS-1$
+ "project=" + project.getName(), e); //$NON-NLS-1$
}
}
return events;
}
/**
* Save language settings providers of the workspace (global providers) to persistent storage.
* @throws CoreException
*/
public static void serializeLanguageSettingsWorkspace() throws CoreException {
URI uriStoreWsp = getStoreInWorkspaceArea(STORAGE_WORKSPACE_LANGUAGE_SETTINGS);
List<ILanguageSettingsBroadcastingProvider> broadcastingWorkspaceProviders = new ArrayList<ILanguageSettingsBroadcastingProvider>();
List<LanguageSettingsSerializableProvider> serializingWorkspaceProviders = new ArrayList<LanguageSettingsSerializableProvider>();
for (ILanguageSettingsProvider provider : rawGlobalWorkspaceProviders.values()) {
if (provider instanceof ILanguageSettingsBroadcastingProvider) {
broadcastingWorkspaceProviders.add((ILanguageSettingsBroadcastingProvider) provider);
}
if (provider instanceof LanguageSettingsSerializableProvider) {
if (!LanguageSettingsManager.isEqualExtensionProvider(provider, true)) {
serializingWorkspaceProviders.add((LanguageSettingsSerializableProvider) provider);
}
}
}
try {
List<LanguageSettingsChangeEvent> events = null;
if (serializingWorkspaceProviders.isEmpty()) {
java.io.File fileStoreWsp = new java.io.File(uriStoreWsp);
try {
serializingLockWsp.acquire();
fileStoreWsp.delete();
// manufacture events while inside the lock
events = createLanguageSettingsChangeEvents(broadcastingWorkspaceProviders);
} finally {
serializingLockWsp.release();
}
} else {
Document doc = XmlUtil.newDocument();
Element rootElement = XmlUtil.appendElement(doc, ELEM_PLUGIN);
Element elementExtension = XmlUtil.appendElement(rootElement, ELEM_EXTENSION,
new String[] { ATTR_EXTENSION_POINT, PROVIDER_EXTENSION_POINT_ID });
for (LanguageSettingsSerializableProvider provider : serializingWorkspaceProviders) {
provider.serialize(elementExtension);
}
try {
serializingLockWsp.acquire();
String eol = Util.getLineSeparator(uriStoreWsp);
if (eol == null) {
eol = Util.getDefaultLineSeparator();
}
XmlUtil.serializeXml(doc, uriStoreWsp, eol);
// manufacture events while inside the lock
events = createLanguageSettingsChangeEvents(broadcastingWorkspaceProviders);
} finally {
serializingLockWsp.release();
}
}
// notify the listeners outside the lock
for (LanguageSettingsChangeEvent event : events) {
notifyLanguageSettingsChangeListeners(event);
}
} catch (Exception e) {
String msg = "Internal error while trying to serialize language settings"; //$NON-NLS-1$
CCorePlugin.log(msg, e);
throw new CoreException(new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, msg, e));
}
}
/**
* Save language settings providers of the workspace (global providers) to persistent storage
* in background.
*/
public static void serializeLanguageSettingsWorkspaceInBackground() {
Job[] jobs = Job.getJobManager().find(JOB_FAMILY_SERIALIZE_LANGUAGE_SETTINGS_WORKSPACE);
for (Job job : jobs) {
if (job.getState() == Job.WAITING) {
// do not schedule if there is serializing job in queue already
return;
}
}
Job job = new WorkspaceJob(
SettingsModelMessages.getString("LanguageSettingsProvidersSerializer.SerializeJobName")) { //$NON-NLS-1$
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
try {
monitor.beginTask(
SettingsModelMessages
.getString("LanguageSettingsProvidersSerializer.SerializingForWorkspace"), //$NON-NLS-1$
TICKS_SERIALIZING);
serializeLanguageSettingsWorkspace();
monitor.worked(TICKS_SERIALIZING);
return Status.OK_STATUS;
} catch (Throwable e) {
String msg = "Internal error running job of serializing language settings in workspace"; //$NON-NLS-1$
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, msg, e);
} finally {
monitor.done();
}
}
@Override
public boolean belongsTo(Object family) {
return family == JOB_FAMILY_SERIALIZE_LANGUAGE_SETTINGS_WORKSPACE;
}
};
job.setRule(ResourcesPlugin.getWorkspace().getRoot());
job.schedule();
}
/**
* Load language settings for workspace.
*/
public static void loadLanguageSettingsWorkspace() {
// ensure completion of any scheduled background serialization
try {
Job.getJobManager().join(JOB_FAMILY_SERIALIZE_LANGUAGE_SETTINGS_WORKSPACE, null);
} catch (OperationCanceledException e) {
return;
} catch (InterruptedException e) {
CCorePlugin.log(e);
// restore interrupted status
Thread.currentThread().interrupt();
}
List<ILanguageSettingsProvider> providers = null;
URI uriStoreWsp = getStoreInWorkspaceArea(STORAGE_WORKSPACE_LANGUAGE_SETTINGS);
Document doc = null;
try {
serializingLockWsp.acquire();
doc = XmlUtil.loadXml(uriStoreWsp);
} catch (Exception e) {
CCorePlugin.log("Can't load preferences from file " + uriStoreWsp, e); //$NON-NLS-1$
} finally {
serializingLockWsp.release();
}
if (doc != null) {
Element rootElement = doc.getDocumentElement();
NodeList providerNodes = rootElement.getElementsByTagName(ELEM_PROVIDER);
List<String> userDefinedProvidersIds = new ArrayList<String>(providerNodes.getLength());
providers = new ArrayList<ILanguageSettingsProvider>(providerNodes.getLength());
for (int i = 0; i < providerNodes.getLength(); i++) {
Node providerNode = providerNodes.item(i);
final String providerId = XmlUtil.determineAttributeValue(providerNode, ATTR_ID);
if (userDefinedProvidersIds.contains(providerId)) {
String msg = "Ignored an attempt to persist duplicate language settings provider, id=" + providerId; //$NON-NLS-1$
CCorePlugin.log(new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, msg, new Exception()));
continue;
}
userDefinedProvidersIds.add(providerId);
ILanguageSettingsProvider provider = null;
try {
provider = loadProvider(providerNode);
} catch (Exception e) {
CCorePlugin.log("Error initializing workspace language settings providers", e); //$NON-NLS-1$
}
if (provider == null) {
provider = new NotAccessibleProvider(providerId);
}
providers.add(provider);
}
}
setWorkspaceProvidersInternal(providers);
}
/**
* @noreference This method is not intended to be referenced by clients.
* It is public solely for benefit of JUnit testing.
*/
public static void serializeLanguageSettingsInternal(Element projectElementPrjStore, Element projectElementWspStore,
ICProjectDescription prjDescription) {
ICConfigurationDescription[] cfgDescriptions = prjDescription.getConfigurations();
for (ICConfigurationDescription cfgDescription : cfgDescriptions) {
if (!(cfgDescription instanceof ILanguageSettingsProvidersKeeper))
continue;
// no lazy initialization as we may need to save 0 providers when it is different from default
Element elementConfiguration = XmlUtil.appendElement(projectElementPrjStore, ELEM_CONFIGURATION,
new String[] { ATTR_ID, cfgDescription.getId(), ATTR_NAME, cfgDescription.getName(), });
Element elementConfigurationWsp = null;
List<ILanguageSettingsProvider> providers = ((ILanguageSettingsProvidersKeeper) cfgDescription)
.getLanguageSettingProviders();
if (providers.size() > 0) {
Element elementExtension = null;
Element elementExtensionWsp = null;
for (ILanguageSettingsProvider provider : providers) {
if (isWorkspaceProvider(provider)) {
if (elementExtension == null) {
elementExtension = XmlUtil.appendElement(elementConfiguration, ELEM_EXTENSION,
new String[] { ATTR_EXTENSION_POINT, PROVIDER_EXTENSION_POINT_ID });
}
// Element elementProviderReference =
XmlUtil.appendElement(elementExtension, ELEM_PROVIDER_REFERENCE,
new String[] { ATTR_ID, provider.getId(), ATTR_REF, VALUE_REF_SHARED_PROVIDER, });
continue;
}
if (!(provider instanceof LanguageSettingsSerializableProvider)) {
if (elementExtension == null) {
elementExtension = XmlUtil.appendElement(elementConfiguration, ELEM_EXTENSION,
new String[] { ATTR_EXTENSION_POINT, PROVIDER_EXTENSION_POINT_ID });
}
// Element elementProvider =
XmlUtil.appendElement(elementExtension, ELEM_PROVIDER, new String[] { ATTR_ID, provider.getId(),
ATTR_NAME, provider.getName(), ATTR_CLASS, provider.getClass().getCanonicalName(), });
} else if (LanguageSettingsManager.isEqualExtensionProvider(provider, true)) {
if (elementExtension == null) {
elementExtension = XmlUtil.appendElement(elementConfiguration, ELEM_EXTENSION,
new String[] { ATTR_EXTENSION_POINT, PROVIDER_EXTENSION_POINT_ID });
}
// Element elementProvider =
XmlUtil.appendElement(elementExtension, ELEM_PROVIDER,
new String[] { ATTR_ID, provider.getId(), ATTR_COPY_OF, VALUE_COPY_OF_EXTENSION, });
} else {
try {
LanguageSettingsSerializableProvider lss = (LanguageSettingsSerializableProvider) provider;
boolean isWspStorageAvailable = (projectElementWspStore != null)
&& (projectElementPrjStore != projectElementWspStore);
if (isStoringEntriesInProjectArea(lss) || !isWspStorageAvailable) {
if (elementExtension == null) {
elementExtension = XmlUtil.appendElement(elementConfiguration, ELEM_EXTENSION,
new String[] { ATTR_EXTENSION_POINT, PROVIDER_EXTENSION_POINT_ID });
}
lss.serialize(elementExtension);
} else {
if (elementExtension == null) {
elementExtension = XmlUtil.appendElement(elementConfiguration, ELEM_EXTENSION,
new String[] { ATTR_EXTENSION_POINT, PROVIDER_EXTENSION_POINT_ID });
}
if (elementExtensionWsp == null) {
if (elementConfigurationWsp == null) {
elementConfigurationWsp = XmlUtil.appendElement(projectElementWspStore,
ELEM_CONFIGURATION, new String[] { ATTR_ID, cfgDescription.getId(),
ATTR_NAME, cfgDescription.getName(), });
}
elementExtensionWsp = XmlUtil.appendElement(elementConfigurationWsp, ELEM_EXTENSION,
new String[] { ATTR_EXTENSION_POINT, PROVIDER_EXTENSION_POINT_ID });
}
Element elementProviderWsp = XmlUtil.appendElement(elementExtensionWsp, ELEM_PROVIDER,
new String[] { ATTR_ID, provider.getId() }); // no attributes kept in workspace storage
// split storage
lss.serializeAttributes(elementExtension);
lss.serializeEntries(elementProviderWsp);
}
} catch (Throwable e) {
// protect from any exceptions from implementers
CCorePlugin.log("Exception trying serialize provider " + provider.getId(), e); //$NON-NLS-1$
}
}
}
}
}
}
/**
* Check if all the language settings providers in the project match defaults.
*/
private static boolean isEqualToDefaultProviders(ICProjectDescription prjDescription) {
ICConfigurationDescription[] cfgDescriptions = prjDescription.getConfigurations();
for (ICConfigurationDescription cfgDescription : cfgDescriptions) {
if (!(cfgDescription instanceof ILanguageSettingsProvidersKeeper)) {
continue;
}
String[] defaultIds = ((ILanguageSettingsProvidersKeeper) cfgDescription)
.getDefaultLanguageSettingsProvidersIds();
if (defaultIds == null) {
defaultIds = ScannerDiscoveryLegacySupport.getDefaultProviderIdsLegacy(cfgDescription);
}
// check size
List<ILanguageSettingsProvider> providers = ((ILanguageSettingsProvidersKeeper) cfgDescription)
.getLanguageSettingProviders();
if (providers.size() != defaultIds.length) {
return false;
}
// check ids
for (int i = 0; i < defaultIds.length; i++) {
ILanguageSettingsProvider provider = providers.get(i);
if (!provider.getId().equals(defaultIds[i])) {
return false;
}
}
// check equality (most expensive, so check last)
for (ILanguageSettingsProvider provider : providers) {
if (LanguageSettingsManager.isPreferShared(provider.getId())) {
if (!isWorkspaceProvider(provider)) {
return false;
}
} else {
if (!LanguageSettingsManager.isEqualExtensionProvider(provider, true)) {
return false;
}
}
}
}
return true;
}
/**
* Save language settings providers of a project to persistent storage.
*
* @param prjDescription - project description of the project.
* @throws CoreException if something goes wrong.
*/
public static void serializeLanguageSettings(ICProjectDescription prjDescription) throws CoreException {
IProject project = prjDescription.getProject();
try {
// Add the storage module to .cpoject and persist on disk as a side effect of adding
prjDescription.getStorage(CPROJECT_STORAGE_MODULE_LANGUAGE_SETTINGS_PROVIDERS, true);
} catch (CoreException e) {
CCorePlugin.log("Internal error while trying to serialize language settings", e); //$NON-NLS-1$
}
try {
// The storage could be split in two, one for provider properties, another one for entries,
// depending on provider flag
// Document to store in project area
Document docStorePrj = XmlUtil.newDocument();
Element projectElementStorePrj = XmlUtil.appendElement(docStorePrj, ELEM_PROJECT);
// Document to store in workspace area
Document docStoreWsp = XmlUtil.newDocument();
Element projectElementStoreWsp = XmlUtil.appendElement(docStoreWsp, ELEM_PROJECT);
URI uriStoreWsp = getStoreInWorkspaceArea(project.getName() + '.' + STORAGE_WORKSPACE_LANGUAGE_SETTINGS);
LanguageSettingsChangeEvent event = null;
try {
// Note that need for serialization may exist even if LS *entries* event delta is empty,
// as set of providers or their properties may differ
serializingLock.acquire();
if (!isEqualToDefaultProviders(prjDescription)) {
serializeLanguageSettingsInternal(projectElementStorePrj, projectElementStoreWsp, prjDescription);
}
// Absent store means default providers as specified in the toolchain
IFile fileStorePrj = getStoreInProjectArea(project);
boolean isProjectStoreEmpty = projectElementStorePrj.getChildNodes().getLength() == 0;
if (isProjectStoreEmpty) {
if (fileStorePrj.exists()) {
fileStorePrj.delete(true, null);
}
} else {
project.getWorkspace().validateEdit(new IFile[] { fileStorePrj }, IWorkspace.VALIDATE_PROMPT);
IContainer folder = fileStorePrj.getParent();
if (folder instanceof IFolder && !folder.exists()) {
((IFolder) folder).create(true, true, null);
}
XmlUtil.serializeXml(docStorePrj, fileStorePrj);
}
// project-specific location in workspace area
boolean isWorkspaceStoreEmpty = projectElementStoreWsp.getChildNodes().getLength() == 0;
if (isWorkspaceStoreEmpty) {
new java.io.File(uriStoreWsp).delete();
} else {
String eol = Util.getLineSeparator(uriStoreWsp);
if (eol == null) {
eol = Util.getDefaultLineSeparator(project);
}
XmlUtil.serializeXml(docStoreWsp, uriStoreWsp, eol);
}
// manufacture the event only if serialization was successful
event = new LanguageSettingsChangeEvent(prjDescription);
} finally {
serializingLock.release();
}
// notify the listeners outside the lock
if (event.getConfigurationDescriptionIds().length > 0) {
notifyLanguageSettingsChangeListeners(event);
}
} catch (Exception e) {
String msg = "Internal error while trying to serialize language settings"; //$NON-NLS-1$
CCorePlugin.log(msg, e);
throw new CoreException(new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, msg, e));
}
}
/**
* Save language settings providers of a project to persistent storage in background.
*
* @param prjDescription - project description of the project.
*/
public static void serializeLanguageSettingsInBackground(final ICProjectDescription prjDescription) {
Job job = new Job(SettingsModelMessages.getString("LanguageSettingsProvidersSerializer.SerializeJobName")) { //$NON-NLS-1$
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
monitor.beginTask(NLS.bind(
SettingsModelMessages
.getString("LanguageSettingsProvidersSerializer.SerializingForProject"), //$NON-NLS-1$
prjDescription.getName()), TICKS_SERIALIZING);
serializeLanguageSettings(prjDescription);
monitor.worked(TICKS_SERIALIZING);
return Status.OK_STATUS;
} catch (Throwable e) {
String msg = "Internal error running job of serializing language settings for project " //$NON-NLS-1$
+ prjDescription.getName();
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, msg, e);
} finally {
monitor.done();
}
}
@Override
public boolean belongsTo(Object family) {
return family == JOB_FAMILY_SERIALIZE_LANGUAGE_SETTINGS_PROJECT;
}
};
ISchedulingRule rule = null;
IProject project = prjDescription.getProject();
if (project != null) {
IFile fileStorePrj = getStoreInProjectArea(project);
IContainer folder = fileStorePrj.getParent();
if (folder instanceof IFolder && !folder.exists()) {
try {
((IFolder) folder).create(true, true, null);
} catch (CoreException e) {
CCorePlugin.log(e);
}
}
if (folder.isAccessible()) {
rule = folder;
}
}
if (rule == null) {
rule = ResourcesPlugin.getWorkspace().getRoot();
}
job.setRule(rule);
job.schedule();
}
/**
* Load language settings to the project description from XML.
*
* @noreference This method is not intended to be referenced by clients.
* It is public solely for benefit of JUnit testing.
*/
public static void loadLanguageSettingsInternal(Element projectElementPrj, Element projectElementWsp,
ICProjectDescription prjDescription) {
/*
<project>
<configuration id="cfg.id">
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
<provider .../>
<provider-reference id="..."/>
</extension>
</configuration>
</project>
*/
NodeList configurationNodes = projectElementPrj.getChildNodes();
for (int ic = 0; ic < configurationNodes.getLength(); ic++) {
Node cfgNode = configurationNodes.item(ic);
if (!isElementWithName(cfgNode, ELEM_CONFIGURATION)) {
continue;
}
List<ILanguageSettingsProvider> providers = new ArrayList<ILanguageSettingsProvider>();
String cfgId = XmlUtil.determineAttributeValue(cfgNode, ATTR_ID);
NodeList extensionNodes = cfgNode.getChildNodes();
for (int ie = 0; ie < extensionNodes.getLength(); ie++) {
Node extNode = extensionNodes.item(ie);
if (!isElementWithName(extNode, ELEM_EXTENSION)) {
continue;
}
NodeList providerNodes = extNode.getChildNodes();
for (int ip = 0; ip < providerNodes.getLength(); ip++) {
ILanguageSettingsProvider provider = null;
Node providerNode = providerNodes.item(ip);
if (isElementWithName(providerNode, ELEM_PROVIDER_REFERENCE)) {
String providerId = XmlUtil.determineAttributeValue(providerNode, ATTR_ID);
provider = getWorkspaceProvider(providerId);
} else if (isElementWithName(providerNode, ELEM_PROVIDER)) {
String providerClass = XmlUtil.determineAttributeValue(providerNode, ATTR_CLASS);
if (providerClass == null || providerClass.isEmpty()) {
// provider is copied from extension if "class" is not supplied
String providerId = XmlUtil.determineAttributeValue(providerNode, ATTR_ID);
provider = LanguageSettingsManager.getExtensionProviderCopy(providerId, true);
if (provider == null) {
String msg = "Internal Error trying to retrieve copy of extension provider id=" //$NON-NLS-1$
+ providerId;
CCorePlugin
.log(new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, msg, new Exception(msg)));
provider = LanguageSettingsManager.getWorkspaceProvider(providerId);
}
} else {
try {
provider = loadProvider(providerNode);
} catch (CoreException e) {
@SuppressWarnings("nls")
String msg = "Error loading provider class=[" + providerClass + "] " + "in project="
+ prjDescription.getProject().getName() + ", cfg=[" + cfgId + "]";
CCorePlugin.log(msg, e);
}
if (provider instanceof LanguageSettingsSerializableProvider) {
LanguageSettingsSerializableProvider lss = (LanguageSettingsSerializableProvider) provider;
if (!isStoringEntriesInProjectArea(lss) && projectElementWsp != null) {
loadProviderEntries(lss, cfgId, projectElementWsp);
}
}
}
}
if (provider != null) {
providers.add(provider);
}
}
}
ICConfigurationDescription cfgDescription = prjDescription.getConfigurationById(cfgId);
setProvidersWithoutNotification(cfgDescription, providers);
}
}
/**
* Set providers into configuration description avoiding triggering an event.
*/
private static void setProvidersWithoutNotification(ICConfigurationDescription cfgDescription,
List<ILanguageSettingsProvider> providers) {
if (cfgDescription instanceof ILanguageSettingsProvidersKeeper) {
((ILanguageSettingsProvidersKeeper) cfgDescription).setLanguageSettingProviders(providers);
if (cfgDescription instanceof IInternalCCfgInfo) {
try {
// swallow delta
((IInternalCCfgInfo) cfgDescription).getSpecSettings().dropDelta();
} catch (CoreException e) {
CCorePlugin.log(e);
}
}
}
}
/**
* Check if the node is {@code Element} and that the name matches.
*/
private static boolean isElementWithName(Node cfgNode, String name) {
return cfgNode instanceof Element && cfgNode.getNodeName().equals(name);
}
/**
* Load provider entries for the given configuration from XML Element.
*/
private static void loadProviderEntries(LanguageSettingsSerializableProvider provider, String cfgId,
Element projectElement) {
/*
<project>
<configuration id="cfg.id">
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
<provider .../>
<provider-reference id="..."/>
</extension>
</configuration>
</project>
*/
NodeList configurationNodes = projectElement.getChildNodes();
for (int ic = 0; ic < configurationNodes.getLength(); ic++) {
Node cfgNode = configurationNodes.item(ic);
if (!isElementWithName(cfgNode, ELEM_CONFIGURATION)) {
continue;
}
String cfgIdXml = XmlUtil.determineAttributeValue(cfgNode, ATTR_ID);
if (!cfgId.equals(cfgIdXml)) {
continue;
}
NodeList extensionNodes = cfgNode.getChildNodes();
for (int ie = 0; ie < extensionNodes.getLength(); ie++) {
Node extNode = extensionNodes.item(ie);
if (!isElementWithName(extNode, ELEM_EXTENSION)) {
continue;
}
NodeList providerNodes = extNode.getChildNodes();
for (int ip = 0; ip < providerNodes.getLength(); ip++) {
Node providerNode = providerNodes.item(ip);
if (!isElementWithName(providerNode, ELEM_PROVIDER)) {
continue;
}
String id = XmlUtil.determineAttributeValue(providerNode, ATTR_ID);
if (provider.getId().equals(id)) {
provider.loadEntries((Element) providerNode);
return;
}
}
}
}
}
/**
* Load provider from provider node.
*/
private static ILanguageSettingsProvider loadProvider(Node providerNode) throws CoreException {
String attrClass = XmlUtil.determineAttributeValue(providerNode, ATTR_CLASS);
ILanguageSettingsProvider provider = LanguageSettingsExtensionManager.instantiateProviderClass(attrClass);
if (provider instanceof LanguageSettingsSerializableProvider) {
((LanguageSettingsSerializableProvider) provider).load((Element) providerNode);
}
return provider;
}
/**
* Load language settings from workspace and project storages for the given project description.
* @param prjDescription - project description to load language settings.
*/
public static void loadLanguageSettings(ICProjectDescription prjDescription) {
IProject project = prjDescription.getProject();
IFile storeInPrjArea = getStoreInProjectArea(project);
boolean isStoreInProjectAreaExist = storeInPrjArea.exists();
if (isStoreInProjectAreaExist) {
Document doc = null;
try {
doc = XmlUtil.loadXml(storeInPrjArea);
Element rootElementPrj = doc.getDocumentElement(); // <project>
URI uriStoreWsp = getStoreInWorkspaceArea(
project.getName() + '.' + STORAGE_WORKSPACE_LANGUAGE_SETTINGS);
Document docWsp = null;
try {
serializingLock.acquire();
docWsp = XmlUtil.loadXml(uriStoreWsp);
} finally {
serializingLock.release();
}
Element rootElementWsp = null; // <project>
if (docWsp != null) {
rootElementWsp = docWsp.getDocumentElement();
}
loadLanguageSettingsInternal(rootElementPrj, rootElementWsp, prjDescription);
} catch (Exception e) {
CCorePlugin.log("Can't load preferences from file " + storeInPrjArea.getLocation(), e); //$NON-NLS-1$
}
} else {
// If storage in project area does not exist set default providers defined in the tool-chain
for (ICConfigurationDescription cfgDescription : prjDescription.getConfigurations()) {
if (cfgDescription instanceof ILanguageSettingsProvidersKeeper) {
String[] ids = ((ILanguageSettingsProvidersKeeper) cfgDescription)
.getDefaultLanguageSettingsProvidersIds();
if (ids != null) {
List<ILanguageSettingsProvider> providers = new ArrayList<ILanguageSettingsProvider>(
ids.length);
for (String id : ids) {
if (LanguageSettingsExtensionManager.isPreferShared(id)) {
providers.add(LanguageSettingsManager.getWorkspaceProvider(id));
} else {
providers.add(LanguageSettingsManager.getExtensionProviderCopy(id, true));
}
}
((ILanguageSettingsProvidersKeeper) cfgDescription).setLanguageSettingProviders(providers);
}
}
}
}
// propagate the preference to project properties
ScannerDiscoveryLegacySupport.defineLanguageSettingsEnablement(project);
}
/**
* Get Language Settings Provider from the list of workspace providers,
* see {@link #getWorkspaceProviders()}.
*
* @param id - ID of provider to find.
* @return the workspace provider. If provider is not defined - still workspace
* provider wrapper is created and returned.
*/
public static ILanguageSettingsProvider getWorkspaceProvider(String id) {
ILanguageSettingsProvider provider = globalWorkspaceProviders.get(id);
if (provider == null) {
provider = new LanguageSettingsWorkspaceProvider(id);
globalWorkspaceProviders.put(id, provider);
}
return provider;
}
/**
* Helper method to get to real underlying provider collecting entries as opposed
* to wrapper which is normally used for workspace provider.
* @see #isWorkspaceProvider(ILanguageSettingsProvider)
*
* @param id - ID of the provider.
* @return raw underlying provider.
*/
public static ILanguageSettingsProvider getRawWorkspaceProvider(String id) {
return rawGlobalWorkspaceProviders.get(id);
}
/**
* Get Language Settings Providers defined in the workspace. That includes
* user-defined providers and after that providers defined as extensions via
* {@code org.eclipse.cdt.core.LanguageSettingsProvider} extension point.
* Note that this returns wrappers around workspace provider so underlying
* provider could be replaced internally without need to change configuration.
* See also {@link #getRawWorkspaceProvider(String)}.
*
* @return list of workspace providers.
*/
public static List<ILanguageSettingsProvider> getWorkspaceProviders() {
ArrayList<ILanguageSettingsProvider> workspaceProviders = new ArrayList<ILanguageSettingsProvider>();
for (ILanguageSettingsProvider rawProvider : rawGlobalWorkspaceProviders.values()) {
workspaceProviders.add(getWorkspaceProvider(rawProvider.getId()));
}
return workspaceProviders;
}
/**
* Checks if the provider is a workspace level provider.
* This method is intended to check providers retrieved from a configuration.
* Raw providers from {@link #getRawWorkspaceProvider(String)}
* are not considered as workspace providers.
*
* @param provider - provider to check.
* @return {@code true} if the given provider is workspace provider, {@code false} otherwise.
*/
public static boolean isWorkspaceProvider(ILanguageSettingsProvider provider) {
return provider instanceof LanguageSettingsWorkspaceProvider;
}
/**
* Check that this particular element is in the list.
*/
private static <T> boolean isInList(Collection<T> list, T element) {
// list.contains(element) won't do it as we are interested in exact object, not in equal object
for (T elem : list) {
if (elem == element)
return true;
}
return false;
}
/**
* Check that this particular element is in the association list.
*/
private static boolean isListenerInTheListOfAssociations(Collection<ListenerAssociation> list,
ICListenerAgent element) {
for (ListenerAssociation la : list) {
// we are interested in exact object, not in equal object
if (la.listener == element)
return true;
}
return false;
}
/**
* Get a providers list including only providers of type {@link ICListenerAgent}
* for a given project description - collecting from all configurations.
*/
private static List<ICListenerAgent> getListeners(ICProjectDescription prjDescription) {
List<ICListenerAgent> listeners = new ArrayList<ICListenerAgent>();
if (prjDescription != null) {
for (ICConfigurationDescription cfgDescription : prjDescription.getConfigurations()) {
if (cfgDescription instanceof ILanguageSettingsProvidersKeeper) {
List<ILanguageSettingsProvider> providers = ((ILanguageSettingsProvidersKeeper) cfgDescription)
.getLanguageSettingProviders();
for (ILanguageSettingsProvider provider : providers) {
if (provider instanceof ICListenerAgent) {
ICListenerAgent listener = (ICListenerAgent) provider;
if (!isInList(listeners, listener)) {
listeners.add(listener);
}
}
}
}
}
}
return listeners;
}
/**
* Pick from the list providers which are listeners, i.e. instances of type {@link ICListenerAgent}.
*/
private static List<ICListenerAgent> selectListeners(Collection<ILanguageSettingsProvider> values) {
List<ICListenerAgent> listeners = new ArrayList<ICListenerAgent>();
for (ILanguageSettingsProvider provider : values) {
if (provider instanceof ICListenerAgent)
listeners.add((ICListenerAgent) provider);
}
return listeners;
}
/**
* Get a providers list including only providers of type {@link ICListenerAgent}
* for a given project description - collecting from all configurations.
*/
private static List<ListenerAssociation> getListenersAssociations(ICProjectDescription prjDescription) {
List<ListenerAssociation> associations = new ArrayList<ListenerAssociation>();
if (prjDescription != null) {
for (ICConfigurationDescription cfgDescription : prjDescription.getConfigurations()) {
if (cfgDescription instanceof ILanguageSettingsProvidersKeeper) {
List<ILanguageSettingsProvider> providers = ((ILanguageSettingsProvidersKeeper) cfgDescription)
.getLanguageSettingProviders();
List<ICListenerAgent> listeners = selectListeners(providers);
for (ICListenerAgent listener : listeners) {
if (!isListenerInTheListOfAssociations(associations, listener)) {
associations.add(new ListenerAssociation(listener, cfgDescription));
}
}
}
}
}
return associations;
}
/**
* Unregister listeners which are not used anymore and register new listeners.
* The method is called when project description is applied to workspace.
*
* @param oldPrjDescription - old project descriptions being replaced in the workspace.
* @param newPrjDescription - new project description being applied to the workspace.
*/
public static void reRegisterListeners(ICProjectDescription oldPrjDescription,
ICProjectDescription newPrjDescription) {
if (oldPrjDescription == newPrjDescription) {
return;
}
List<ICListenerAgent> oldListeners = getListeners(oldPrjDescription);
List<ListenerAssociation> newAssociations = getListenersAssociations(newPrjDescription);
// unregister old listeners
for (ICListenerAgent oldListener : oldListeners) {
if (!isListenerInTheListOfAssociations(newAssociations, oldListener)) {
int count = 0;
if (oldListener instanceof LanguageSettingsWorkspaceProvider) {
count = ((LanguageSettingsWorkspaceProvider) oldListener).decrementProjectCount();
}
if (count <= 0) {
try {
oldListener.unregisterListener();
} catch (Throwable e) {
// protect from any exceptions from implementers
CCorePlugin.log("Exception trying unregister listener " //$NON-NLS-1$
+ ((ILanguageSettingsProvider) oldListener).getId(), e);
}
}
}
}
// register new listeners
for (ListenerAssociation newListenerAssociation : newAssociations) {
ICListenerAgent newListener = newListenerAssociation.listener;
if (!isInList(oldListeners, newListener)) {
int count = 1;
if (newListener instanceof LanguageSettingsWorkspaceProvider) {
count = ((LanguageSettingsWorkspaceProvider) newListener).incrementProjectCount();
}
if (count == 1) {
try {
newListener.registerListener(newListenerAssociation.cfgDescription);
} catch (Throwable e) {
// protect from any exceptions from implementers
CCorePlugin.log("Exception trying register listener " //$NON-NLS-1$
+ ((ILanguageSettingsProvider) newListener).getId(), e);
}
}
}
}
}
/**
* Adds a listener that will be notified of changes in language settings.
*
* @param listener - the listener to add
*/
public static void registerLanguageSettingsChangeListener(ILanguageSettingsChangeListener listener) {
fLanguageSettingsChangeListeners.add(listener);
}
/**
* Removes a language settings change listener.
*
* @param listener - the listener to remove.
*/
public static void unregisterLanguageSettingsChangeListener(ILanguageSettingsChangeListener listener) {
fLanguageSettingsChangeListeners.remove(listener);
}
/**
* Notifies all language settings change listeners of a change in language settings entries.
*
* @param event - the {@link ILanguageSettingsChangeEvent} event to be broadcast.
*/
private static void notifyLanguageSettingsChangeListeners(ILanguageSettingsChangeEvent event) {
for (ILanguageSettingsChangeListener listener : fLanguageSettingsChangeListeners) {
listener.handleEvent(event);
}
}
/**
* Get list of setting entries from the pool in {@link LanguageSettingsStorage}.
*/
private static List<ICLanguageSettingEntry> getSettingEntriesPooled(ILanguageSettingsProvider provider,
ICConfigurationDescription cfgDescription, IResource rc, String languageId) {
try {
return LanguageSettingsStorage.getPooledList(provider.getSettingEntries(cfgDescription, rc, languageId));
} catch (Throwable e) {
String cfgId = cfgDescription != null ? cfgDescription.getId() : null;
@SuppressWarnings("nls")
String msg = "Exception in provider " + provider.getId() + ": getSettingEntries(" + cfgId + ", " + rc + ", "
+ languageId + ")";
CCorePlugin.log(msg, e);
// return empty list to prevent getting potentially non-empty list from up the resource tree
return LanguageSettingsStorage.getPooledEmptyList();
}
}
/**
* Returns the list of setting entries of the given provider
* for the given configuration description, resource and language.
* This method reaches to the parent folder of the resource recursively
* if the resource does not define the entries for the given provider.
*
* @param provider - language settings provider.
* @param cfgDescription - configuration description.
* @param rc - resource such as file or folder.
* @param languageId - language id.
*
* @return the list of setting entries which is unmodifiable. Never returns {@code null}
* although individual providers mandated to return {@code null} if no settings defined.
*/
public static List<ICLanguageSettingEntry> getSettingEntriesUpResourceTree(ILanguageSettingsProvider provider,
ICConfigurationDescription cfgDescription, IResource rc, String languageId) {
Assert.isTrue(!(rc instanceof IWorkspaceRoot));
if (provider != null) {
List<ICLanguageSettingEntry> entries = getSettingEntriesPooled(provider, cfgDescription, rc, languageId);
if (entries != null) {
return entries;
}
if (rc != null) {
IResource parentFolder = (rc instanceof IProject) ? null : rc.getParent();
if (parentFolder != null) {
return getSettingEntriesUpResourceTree(provider, cfgDescription, parentFolder, languageId);
}
// if out of parent resources - get default entries
entries = getSettingEntriesPooled(provider, cfgDescription, null, languageId);
if (entries != null) {
return entries;
}
entries = getSettingEntriesPooled(provider, null, null, languageId);
if (entries != null) {
return entries;
}
}
}
return LanguageSettingsStorage.getPooledEmptyList();
}
/**
* Test if the binary flag contains a particular bit.
*/
private static boolean checkBit(int flags, int bit) {
return (flags & bit) == bit;
}
/**
* Returns the list of setting entries of a certain kind (such as include paths)
* for the given configuration description, resource and language. This is a
* combined list for all providers taking into account settings of parent folder
* if settings for the given resource are not defined.
*
* @param cfgDescription - configuration description.
* @param rc - resource such as file or folder.
* @param languageId - language id.
* @param kind - kind of language settings entries, such as
* {@link ICSettingEntry#INCLUDE_PATH} etc. This is a binary flag
* and it is possible to specify composite kind.
* Use {@link ICSettingEntry#ALL} to get all kinds.
* @param checkLocality - specifies if parameter {@code isLocal} should be considered.
* @param isLocal - {@code true} if "local" entries should be provided and
* {@code false} for "system" entries. This makes sense for include paths where
* [#include "..."] is "local" and [#include <...>] is system.
*
* @return the list of setting entries found.
*/
private static List<ICLanguageSettingEntry> getSettingEntriesByKind(ICConfigurationDescription cfgDescription,
IResource rc, String languageId, int kind, boolean checkLocality, boolean isLocal) {
if (!(cfgDescription instanceof ILanguageSettingsProvidersKeeper)) {
return null;
}
List<ICLanguageSettingEntry> entries = new ArrayList<ICLanguageSettingEntry>();
List<String> alreadyAdded = new ArrayList<String>();
List<ILanguageSettingsProvider> providers = ((ILanguageSettingsProvidersKeeper) cfgDescription)
.getLanguageSettingProviders();
for (ILanguageSettingsProvider provider : providers) {
List<ICLanguageSettingEntry> providerEntries = getSettingEntriesUpResourceTree(provider, cfgDescription, rc,
languageId);
for (ICLanguageSettingEntry entry : providerEntries) {
if (entry != null) {
String entryName = entry.getName();
boolean isRightKind = checkBit(kind, entry.getKind());
// Only first entry is considered
// Entry flagged as "UNDEFINED" prevents adding entry with the same name down the line
if (isRightKind && !alreadyAdded.contains(entryName)) {
int flags = entry.getFlags();
boolean isRightLocal = !checkLocality || (checkBit(flags, ICSettingEntry.LOCAL) == isLocal);
if (isRightLocal) {
if (!checkBit(flags, ICSettingEntry.UNDEFINED)) {
entries.add(entry);
}
alreadyAdded.add(entryName);
}
}
}
}
}
return entries;
}
/**
* Returns the list of setting entries of a certain kind (such as include paths)
* for the given configuration description, resource and language. This is a
* combined list for all providers taking into account settings of parent folder
* if settings for the given resource are not defined. For include paths both
* local (#include "...") and system (#include <...>) entries are returned.
*
* @param cfgDescription - configuration description.
* @param rc - resource such as file or folder.
* @param languageId - language id.
* @param kind - kind of language settings entries, such as
* {@link ICSettingEntry#INCLUDE_PATH} etc. This is a binary flag
* and it is possible to specify composite kind.
* Use {@link ICSettingEntry#ALL} to get all kinds.
*
* @return the list of setting entries.
*/
public static List<ICLanguageSettingEntry> getSettingEntriesByKind(ICConfigurationDescription cfgDescription,
IResource rc, String languageId, int kind) {
return getSettingEntriesByKind(cfgDescription, rc, languageId, kind, /* checkLocality */ false,
/* isLocal */ false);
}
/**
* Returns the list of "system" (such as [#include <...>]) setting entries of a certain kind
* for the given configuration description, resource and language. This is a
* combined list for all providers taking into account settings of parent folder
* if settings for the given resource are not defined.
*
* @param cfgDescription - configuration description.
* @param rc - resource such as file or folder.
* @param languageId - language id.
* @param kind - kind of language settings entries, such as
* {@link ICSettingEntry#INCLUDE_PATH} etc. This is a binary flag
* and it is possible to specify composite kind.
* Use {@link ICSettingEntry#ALL} to get all kinds.
*
* @return the list of setting entries.
*/
public static List<ICLanguageSettingEntry> getSystemSettingEntriesByKind(ICConfigurationDescription cfgDescription,
IResource rc, String languageId, int kind) {
return getSettingEntriesByKind(cfgDescription, rc, languageId, kind, /* checkLocality */ true,
/* isLocal */ false);
}
/**
* Returns the list of "local" (such as [#include "..."]) setting entries of a certain kind
* for the given configuration description, resource and language. This is a
* combined list for all providers taking into account settings of parent folder
* if settings for the given resource are not defined.
*
* @param cfgDescription - configuration description.
* @param rc - resource such as file or folder.
* @param languageId - language id.
* @param kind - kind of language settings entries, such as
* {@link ICSettingEntry#INCLUDE_PATH} etc. This is a binary flag
* and it is possible to specify composite kind.
* Use {@link ICSettingEntry#ALL} to get all kinds.
*
* @return the list of setting entries.
*/
public static List<ICLanguageSettingEntry> getLocalSettingEntriesByKind(ICConfigurationDescription cfgDescription,
IResource rc, String languageId, int kind) {
return getSettingEntriesByKind(cfgDescription, rc, languageId, kind, /* checkLocality */ true,
/* isLocal */ true);
}
/**
* Deep clone of a list of language settings providers.
*
* @param baseProviders - list of providers to clone.
* @return newly cloned list.
*/
public static List<ILanguageSettingsProvider> cloneProviders(List<ILanguageSettingsProvider> baseProviders) {
List<ILanguageSettingsProvider> newProviders = new ArrayList<ILanguageSettingsProvider>();
for (ILanguageSettingsProvider provider : baseProviders) {
if (provider instanceof ILanguageSettingsEditableProvider) {
ILanguageSettingsEditableProvider newProvider = LanguageSettingsManager
.getProviderCopy((ILanguageSettingsEditableProvider) provider, true);
if (newProvider != null) {
provider = newProvider;
}
}
newProviders.add(provider);
}
return new ArrayList<ILanguageSettingsProvider>(newProviders);
}
}
|