Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e35539a39a0378b32ec815d531ce1a5181bcd45e (plain) (blame)
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
/*******************************************************************************
 *  Copyright (c) 2000, 2013 IBM Corporation and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 * 
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *     David Carver - STAR - bug 213255
 *******************************************************************************/
package org.eclipse.pde.internal.core.schema;

import java.io.*;
import java.net.*;
import java.util.*;
import org.eclipse.core.runtime.*;
import org.eclipse.pde.core.*;
import org.eclipse.pde.core.plugin.IPluginBase;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.internal.core.*;
import org.eclipse.pde.internal.core.ischema.*;
import org.eclipse.pde.internal.core.util.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class Schema extends PlatformObject implements ISchema {

	private URL fURL;

	private ListenerList fListeners = new ListenerList();

	private Vector<ISchemaElement> fElements = new Vector<ISchemaElement>();

	private Vector<DocumentSection> fDocSections = new Vector<DocumentSection>();

	private Vector<ISchemaInclude> fIncludes;

	private String fPointID;

	private String fPluginID;

	private ISchemaDescriptor fSchemaDescriptor;

	private boolean fLoaded;

	private Vector<SchemaElementReference> fReferences;

	private String fDescription;

	private double fTargetVersion;

	private String fName = ""; //$NON-NLS-1$

	private boolean fNotificationEnabled;

	public final static String INDENT = "   "; //$NON-NLS-1$

	private boolean fDisposed;

	private boolean fValid;

	private boolean fAbbreviated;

	private List<IPath> fSearchPath;

	public Schema(String pluginId, String pointId, String name, boolean abbreviated) {
		fPluginID = pluginId;
		fPointID = pointId;
		fName = name;
		fAbbreviated = abbreviated;
	}

	public Schema(ISchemaDescriptor schemaDescriptor, URL url, boolean abbreviated) {
		fSchemaDescriptor = schemaDescriptor;
		fURL = url;
		fAbbreviated = abbreviated;
	}

	public void addDocumentSection(DocumentSection docSection) {
		fDocSections.add(docSection);
		fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.INSERT, new Object[] {docSection}, null));

	}

	public void addElement(ISchemaElement element) {
		addElement(element, null);
	}

	public void addElement(ISchemaElement element, ISchemaElement afterElement) {
		int index = -1;
		if (afterElement != null) {
			index = fElements.indexOf(afterElement);
		}
		if (index != -1)
			fElements.add(index + 1, element);
		else
			fElements.add(element);
		fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.INSERT, new Object[] {element}, null));
	}

	public void addInclude(ISchemaInclude include) {
		if (fIncludes == null)
			fIncludes = new Vector<ISchemaInclude>();
		fIncludes.add(include);
		fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.INSERT, new Object[] {include}, null));
	}

	public void removeInclude(ISchemaInclude include) {
		if (fIncludes == null)
			return;
		fIncludes.remove(include);
		fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.REMOVE, new Object[] {include}, null));
	}

	public void addModelChangedListener(IModelChangedListener listener) {
		fListeners.add(listener);
	}

	private void collectElements(ISchemaCompositor compositor, Vector<Object> result) {
		Object[] children = compositor.getChildren();
		for (int i = 0; i < children.length; i++) {
			Object child = children[i];
			if (child instanceof ISchemaCompositor)
				collectElements((ISchemaCompositor) child, result);
			else if (child instanceof ISchemaObjectReference) {
				ISchemaObjectReference ref = (ISchemaObjectReference) child;
				Object referenced = ref.getReferencedObject();
				if (referenced instanceof ISchemaElement)
					result.addElement(referenced);
			}
		}
	}

	public void dispose() {
		if (fIncludes != null) {
			for (int i = 0; i < fIncludes.size(); i++) {
				ISchemaInclude include = fIncludes.get(i);
				include.dispose();
			}
		}
		reset();
		fDisposed = true;
	}

	public ISchemaElement findElement(String name) {
		if (!isLoaded())
			load();
		for (int i = 0; i < fElements.size(); i++) {
			ISchemaElement element = fElements.get(i);
			if (element.getName().equals(name))
				return element;
		}
		if (fIncludes != null) {
			for (int i = 0; i < fIncludes.size(); i++) {
				ISchemaInclude include = fIncludes.get(i);
				ISchema ischema = include.getIncludedSchema();
				if (ischema == null)
					continue;
				ISchemaElement element = ischema.findElement(name);
				if (element != null)
					return element;
			}
		}
		return null;
	}

	public void fireModelChanged(IModelChangedEvent event) {
		if (!fNotificationEnabled)
			return;
		Object[] listeners = fListeners.getListeners();
		for (int i = 0; i < listeners.length; i++) {
			((IModelChangedListener) listeners[i]).modelChanged(event);
		}
	}

	public void fireModelObjectChanged(Object object, String property, Object oldValue, Object newValue) {
		fireModelChanged(new ModelChangedEvent(this, object, property, oldValue, newValue));
	}

	private String getAttribute(Node node, String name) {
		NamedNodeMap map = node.getAttributes();
		Node attNode = map.getNamedItem(name);
		if (attNode != null) {
			String value = attNode.getNodeValue();
			if (value.length() > 0)
				return value;
		}
		return null;
	}

	public ISchemaElement[] getCandidateChildren(ISchemaElement element) {
		Vector<Object> candidates = new Vector<Object>();
		ISchemaType type = element.getType();
		if (type instanceof ISchemaComplexType) {
			ISchemaCompositor compositor = ((ISchemaComplexType) type).getCompositor();
			if (compositor != null)
				collectElements(compositor, candidates);
		}
		ISchemaElement[] result = new ISchemaElement[candidates.size()];
		candidates.copyInto(result);
		return result;
	}

	public String getDescription() {
		return fDescription;
	}

	public boolean isValid() {
		return fValid;
	}

	public IDocumentSection[] getDocumentSections() {
		return fDocSections.toArray(new IDocumentSection[fDocSections.size()]);
	}

	public int getElementCount() {
		return fElements.size();
	}

	public int getResolvedElementCount() {
		int localCount = getElementCount();
		if (fIncludes == null)
			return localCount;
		int totalCount = localCount;
		for (int i = 0; i < fIncludes.size(); i++) {
			ISchemaInclude include = fIncludes.get(i);
			ISchema schema = include.getIncludedSchema();
			if (schema == null)
				continue;
			totalCount += schema.getResolvedElementCount();
		}
		return totalCount;
	}

	public ISchemaElement[] getElements() {
		if (!isLoaded())
			load();
		return fElements.toArray(new ISchemaElement[fElements.size()]);
	}

	public String[] getElementNames() {
		ISchemaElement[] elements = getElements();
		String[] names = new String[elements.length];
		for (int i = 0; i < elements.length; i++)
			names[i] = elements[i].getName();
		return names;
	}

	@SuppressWarnings("unchecked")
	public ISchemaElement[] getResolvedElements() {
		if (fIncludes == null)
			return getElements();
		if (!isLoaded())
			load();
		@SuppressWarnings("rawtypes")
		Vector result = (Vector) fElements.clone();
		for (int i = 0; i < fIncludes.size(); i++) {
			ISchemaInclude include = fIncludes.get(i);
			ISchema schema = include.getIncludedSchema();
			if (schema == null)
				continue;
			ISchemaElement[] ielements = schema.getElements();
			for (int j = 0; j < ielements.length; j++)
				result.add(ielements[j]);
		}
		return (ISchemaElement[]) result.toArray(new ISchemaElement[result.size()]);
	}

	public ISchemaInclude[] getIncludes() {
		if (fIncludes == null)
			return new ISchemaInclude[0];
		return fIncludes.toArray(new ISchemaInclude[fIncludes.size()]);
	}

	public String getName() {
		return fName;
	}

	private String getNormalizedText(String source) {
		if (source == null)
			return ""; //$NON-NLS-1$

		String result = source.replace('\t', ' ');
		result = result.trim();
		return result;
	}

	public ISchemaObject getParent() {
		return null;
	}

	public void setParent(ISchemaObject obj) {
	}

	public ISchemaElement getElementAt(int index) {
		return fElements.get(index);
	}

	public String getQualifiedPointId() {
		return fPluginID + "." + fPointID; //$NON-NLS-1$
	}

	public String getPluginId() {
		return fPluginID;
	}

	public String getPointId() {
		return fPointID;
	}

	public ISchema getSchema() {
		return this;
	}

	public ISchemaDescriptor getSchemaDescriptor() {
		return fSchemaDescriptor;
	}

	public URL getURL() {
		return fURL;
	}

	public int indexOf(Object obj) {
		return fElements.indexOf(obj);
	}

	public boolean isDisposed() {
		return fDisposed;
	}

	public boolean isEditable() {
		return false;
	}

	public boolean isLoaded() {
		return fLoaded;
	}

	public boolean isNotificationEnabled() {
		return fNotificationEnabled;
	}

	public void load() {
		URLConnection connection = null;
		InputStream input = null;
		try {
			connection = SchemaUtil.getURLConnection(fURL);
			input = connection.getInputStream();
			load(input);
		} catch (FileNotFoundException e) {
			fLoaded = false;
		} catch (IOException e) {
			PDECore.logException(e);
		} finally {
			try {
				if (input != null) {
					input.close();
				}
				if (connection instanceof JarURLConnection) {
					((JarURLConnection) connection).getJarFile().close();
				}
			} catch (IOException e1) {
			}
		}
	}

	public void load(InputStream stream) {
		try {
			SAXParserWrapper parser = new SAXParserWrapper();
			XMLDefaultHandler handler = new XMLDefaultHandler(fAbbreviated);
			parser.parse(stream, handler);
			traverseDocumentTree(handler.getDocumentElement());
		} catch (SAXException e) {
			// ignore parse errors - 'loaded' will be false anyway
		} catch (IOException e) {
			PDECore.logException(e, "IOException reading following URL: " + fURL); //$NON-NLS-1$
		} catch (Exception e) {
			PDECore.logException(e);
		}
	}

	private ISchemaAttribute processAttribute(ISchemaElement element, Node elementNode) {
		String aname = getAttribute(elementNode, "name"); //$NON-NLS-1$
		if (aname == null)
			return null;
		String atype = getAttribute(elementNode, "type"); //$NON-NLS-1$
		String ause = getAttribute(elementNode, "use"); //$NON-NLS-1$
		String avalue = getAttribute(elementNode, "value"); //$NON-NLS-1$
		ISchemaSimpleType type = null;
		if (atype != null) {
			type = (ISchemaSimpleType) resolveTypeReference(atype);
		}
		SchemaAttribute attribute = new SchemaAttribute(element, aname);
		//attribute.bindSourceLocation(elementNode, lineTable);
		if (ause != null) {
			int use = ISchemaAttribute.OPTIONAL;
			if (ause.equals("required")) //$NON-NLS-1$
				use = ISchemaAttribute.REQUIRED;
			else if (ause.equals("optional")) //$NON-NLS-1$
				use = ISchemaAttribute.OPTIONAL;
			else if (ause.equals("default")) //$NON-NLS-1$
				use = ISchemaAttribute.DEFAULT;
			attribute.setUse(use);
		}
		if (avalue != null)
			attribute.setValue(avalue);
		NodeList children = elementNode.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				String tag = child.getNodeName();
				if (tag.equals("annotation")) { //$NON-NLS-1$
					processAttributeAnnotation(attribute, child);
				} else if (tag.equals("simpleType")) { //$NON-NLS-1$
					processAttributeSimpleType(attribute, child);
				}
			}
		}
		if (type != null && attribute.getType() == null)
			attribute.setType(type);
		return attribute;
	}

	private void processAttributeAnnotation(SchemaAttribute element, Node node) {
		NodeList children = node.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				if (child.getNodeName().equals("documentation")) { //$NON-NLS-1$
					Node doc = child.getFirstChild();
					if (doc != null)
						element.setDescription(getNormalizedText(doc.getNodeValue()));
				} else if (child.getNodeName().equals("appInfo") || child.getNodeName().equals("appinfo")) { //$NON-NLS-1$ //$NON-NLS-2$
					NodeList infos = child.getChildNodes();
					for (int j = 0; j < infos.getLength(); j++) {
						Node meta = infos.item(j);
						if (meta.getNodeType() == Node.ELEMENT_NODE) {
							if (meta.getNodeName().equals("meta.attribute")) { //$NON-NLS-1$
								element.setKind(processKind(getAttribute(meta, "kind"))); //$NON-NLS-1$
								element.setBasedOn(getAttribute(meta, "basedOn")); //$NON-NLS-1$
								element.setTranslatableProperty(processTranslatable(getAttribute(meta, "translatable"))); //$NON-NLS-1$
								element.setDeprecatedProperty(processDeprecated(getAttribute(meta, "deprecated"))); //$NON-NLS-1$
							}
						}
					}
				}
			}
		}
	}

	private boolean processTranslatable(String value) {
		return (value != null && "true".equals(value)); //$NON-NLS-1$
	}

	private boolean processDeprecated(String value) {
		return value != null && "true".equals(value); //$NON-NLS-1$
	}

	private SchemaSimpleType processAttributeRestriction(SchemaAttribute attribute, Node node) {
		NodeList children = node.getChildNodes();
		if (children.getLength() == 0)
			return null;
		String baseName = getAttribute(node, "base"); //$NON-NLS-1$
		if (baseName.equals("string") == false) { //$NON-NLS-1$
			return new SchemaSimpleType(attribute.getSchema(), "string"); //$NON-NLS-1$
		}
		SchemaSimpleType type = new SchemaSimpleType(attribute.getSchema(), baseName);
		Vector<ISchemaEnumeration> items = new Vector<ISchemaEnumeration>();
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				if (child.getNodeName().equals("enumeration")) { //$NON-NLS-1$
					ISchemaEnumeration enumeration = processEnumeration(attribute.getSchema(), child);
					if (enumeration != null)
						items.add(enumeration);
				}
			}
		}
		ChoiceRestriction restriction = new ChoiceRestriction(attribute.getSchema());
		restriction.setChildren(items);
		type.setRestriction(restriction);
		return type;
	}

	private void processAttributeSimpleType(SchemaAttribute attribute, Node node) {
		NodeList children = node.getChildNodes();
		if (children.getLength() == 0)
			return;
		SchemaSimpleType type = null;
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				if (child.getNodeName().equals("restriction")) { //$NON-NLS-1$
					type = processAttributeRestriction(attribute, child);
				}
			}
		}
		if (type != null)
			attribute.setType(type);
	}

	private SchemaComplexType processComplexType(ISchemaElement owner, Node typeNode) {
		String aname = getAttribute(typeNode, "name"); //$NON-NLS-1$
		String amixed = getAttribute(typeNode, "mixed"); //$NON-NLS-1$
		SchemaComplexType complexType = new SchemaComplexType(this, aname);
		if (amixed != null && amixed.equals("true")) //$NON-NLS-1$
			complexType.setMixed(true);
		NodeList children = typeNode.getChildNodes();
		ISchemaCompositor compositor = null;
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				if (child.getNodeName().equals("attribute")) { //$NON-NLS-1$
					complexType.addAttribute(processAttribute(owner, child));
				} else {
					ISchemaObject object = processCompositorChild(owner, child, ISchemaCompositor.ROOT);
					if (object instanceof ISchemaCompositor && compositor == null) {
						compositor = (ISchemaCompositor) object;
					}
				}
			}
		}
		complexType.setCompositor(compositor);
		return complexType;
	}

	private ISchemaCompositor processCompositor(ISchemaObject parent, Node node, int type) {
		SchemaCompositor compositor = new SchemaCompositor(parent, type);
		NodeList children = node.getChildNodes();
		int minOccurs = 1;
		int maxOccurs = 1;
		String aminOccurs = getAttribute(node, "minOccurs"); //$NON-NLS-1$
		String amaxOccurs = getAttribute(node, "maxOccurs"); //$NON-NLS-1$
		if (aminOccurs != null)
			minOccurs = Integer.valueOf(aminOccurs).intValue();
		if (amaxOccurs != null) {
			if (amaxOccurs.equals("unbounded")) //$NON-NLS-1$
				maxOccurs = Integer.MAX_VALUE;
			else {
				maxOccurs = Integer.valueOf(amaxOccurs).intValue();
			}
		}
		compositor.setMinOccurs(minOccurs);
		compositor.setMaxOccurs(maxOccurs);
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			ISchemaObject object = processCompositorChild(compositor, child, type);
			if (object != null)
				compositor.addChild(object);
		}
		return compositor;
	}

	private ISchemaObject processCompositorChild(ISchemaObject parent, Node child, int parentKind) {
		String tag = child.getNodeName();
		if (tag.equals("element") && parentKind != ISchemaCompositor.ROOT) { //$NON-NLS-1$
			return processElement(parent, child);
		}
		// sequence: element | group | choice | sequence
		if (tag.equals("sequence") && parentKind != ISchemaCompositor.ALL) { //$NON-NLS-1$
			return processCompositor(parent, child, ISchemaCompositor.SEQUENCE);
		}
		// choice: element | group | choice | sequence
		if (tag.equals("choice") && parentKind != ISchemaCompositor.ALL) { //$NON-NLS-1$
			return processCompositor(parent, child, ISchemaCompositor.CHOICE);
		}
		// all: element
		if (tag.equals("all") //$NON-NLS-1$
				&& (parentKind == ISchemaCompositor.ROOT || parentKind == ISchemaCompositor.GROUP)) {
			return processCompositor(parent, child, ISchemaCompositor.SEQUENCE);
		}
		// group: all | choice | sequence
		if (tag.equals("group") //$NON-NLS-1$
				&& (parentKind == ISchemaCompositor.CHOICE || parentKind == ISchemaCompositor.SEQUENCE)) {
			return processCompositor(parent, child, ISchemaCompositor.SEQUENCE);
		}
		return null;
	}

	private ISchemaElement processElement(ISchemaObject parent, Node elementNode) {
		if (parent instanceof ISchemaCompositor)
			return processElementReference((ISchemaCompositor) parent, elementNode);
		return processElementDeclaration(parent, elementNode);
	}

	private ISchemaElement processElementDeclaration(ISchemaObject parent, Node elementNode) {
		String aname = getAttribute(elementNode, "name"); //$NON-NLS-1$
		if (aname == null)
			return null;
		String atype = getAttribute(elementNode, "type"); //$NON-NLS-1$	
		int minOccurs = getMinOccurs(elementNode);
		int maxOccurs = getMaxOccurs(elementNode);

		ISchemaType type = null;
		if (atype != null) {
			type = resolveTypeReference(atype);
		}
		SchemaElement element;
		if (aname.equals("extension")) //$NON-NLS-1$
			element = new SchemaRootElement(parent, aname);
		else
			element = new SchemaElement(parent, aname);
		//element.bindSourceLocation(elementNode, lineTable);
		element.setMinOccurs(minOccurs);
		element.setMaxOccurs(maxOccurs);
		NodeList children = elementNode.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				String tag = child.getNodeName();
				if (type == null && tag.equals("complexType")) { //$NON-NLS-1$
					type = processComplexType(element, child);
				}
				if (tag.equals("annotation")) { //$NON-NLS-1$
					processElementAnnotation(element, child);
				}
			}
		}
		element.setType(type);
		return element;
	}

	private ISchemaElement processElementReference(ISchemaCompositor compositor, Node elementNode) {
		String aref = getAttribute(elementNode, "ref"); //$NON-NLS-1$
		if (aref == null) {
			return null;
		}
		int minOccurs = getMinOccurs(elementNode);
		int maxOccurs = getMaxOccurs(elementNode);

		SchemaElementReference reference = new SchemaElementReference(compositor, aref);
		reference.addComments(elementNode);
		reference.setMinOccurs(minOccurs);
		reference.setMaxOccurs(maxOccurs);
		fReferences.addElement(reference);
		//reference.bindSourceLocation(elementNode, lineTable);
		return reference;
	}

	private int getMinOccurs(Node elementNode) {
		String aminOccurs = getAttribute(elementNode, "minOccurs"); //$NON-NLS-1$
		if (aminOccurs != null)
			return Integer.valueOf(aminOccurs).intValue();
		return 1;

	}

	private int getMaxOccurs(Node elementNode) {
		String amaxOccurs = getAttribute(elementNode, "maxOccurs"); //$NON-NLS-1$
		if (amaxOccurs != null) {
			if (amaxOccurs.equals("unbounded")) //$NON-NLS-1$
				return Integer.MAX_VALUE;
			return Integer.valueOf(amaxOccurs).intValue();
		}
		return 1;
	}

	private void processElementAnnotation(SchemaElement element, Node node) {
		NodeList children = node.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				if (child.getNodeName().equals("documentation") && !fAbbreviated) { //$NON-NLS-1$
					element.setDescription(getNormalizedText(child.getFirstChild().getNodeValue()));
				} else if (child.getNodeName().equals("appInfo") || child.getNodeName().equals("appinfo")) { //$NON-NLS-1$ //$NON-NLS-2$
					NodeList infos = child.getChildNodes();
					for (int j = 0; j < infos.getLength(); j++) {
						Node meta = infos.item(j);
						if (meta.getNodeType() == Node.ELEMENT_NODE) {
							if (meta.getNodeName().equals("meta.element")) { //$NON-NLS-1$
								element.setLabelProperty(getAttribute(meta, "labelAttribute")); //$NON-NLS-1$
								element.setIconProperty(getAttribute(meta, "icon")); //$NON-NLS-1$
								if (element.getIconProperty() == null)
									element.setIconProperty(getAttribute(meta, "iconName")); //$NON-NLS-1$
								element.setTranslatableProperty(processTranslatable(getAttribute(meta, "translatable"))); //$NON-NLS-1$
								element.setDeprecatedProperty(processDeprecated(getAttribute(meta, "deprecated"))); //$NON-NLS-1$
								if (element instanceof ISchemaRootElement) {
									// set deprecated suggestion
									String depSug = getAttribute(meta, ISchemaRootElement.P_DEP_REPLACEMENT);
									((ISchemaRootElement) element).setDeprecatedSuggestion(depSug);

									// set internal
									String internal = getAttribute(meta, ISchemaRootElement.P_INTERNAL);
									((ISchemaRootElement) element).setInternal(Boolean.valueOf(internal).booleanValue());
								}
							}
						}
					}
				}
			}
		}
	}

	private ISchemaEnumeration processEnumeration(ISchema schema, Node node) {
		String name = getAttribute(node, "value"); //$NON-NLS-1$
		return new SchemaEnumeration(schema, name);
	}

	private int processKind(String name) {
		if (name != null) {
			if (name.equals("java")) //$NON-NLS-1$
				return IMetaAttribute.JAVA;
			if (name.equals("resource")) //$NON-NLS-1$
				return IMetaAttribute.RESOURCE;
			if (name.equals("identifier")) //$NON-NLS-1$
				return IMetaAttribute.IDENTIFIER;
		}
		return IMetaAttribute.STRING;
	}

	private void processSchemaAnnotation(Node node) {
		NodeList children = node.getChildNodes();
		String section = "overview"; //$NON-NLS-1$
		String sectionName = "Overview"; //$NON-NLS-1$
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				if (child.getNodeName().equals("documentation") && !fAbbreviated) { //$NON-NLS-1$
					String text = getNormalizedText(child.getFirstChild().getNodeValue());
					if (section != null) {
						if (section.equals("overview")) { //$NON-NLS-1$
							setDescription(text);
						} else {
							DocumentSection sec = new DocumentSection(this, section, sectionName);
							sec.setDescription(text);
							fDocSections.add(sec);
						}
					}
				} else if (child.getNodeName().equals("appInfo") || child.getNodeName().equals("appinfo")) { //$NON-NLS-1$ //$NON-NLS-2$
					NodeList infos = child.getChildNodes();
					for (int j = 0; j < infos.getLength(); j++) {
						Node meta = infos.item(j);
						if (meta.getNodeType() == Node.ELEMENT_NODE) {
							if (meta.getNodeName().equals("meta.schema")) { //$NON-NLS-1$
								section = "overview"; //$NON-NLS-1$
								setName(getAttribute(meta, "name")); //$NON-NLS-1$
								fPluginID = getAttribute(meta, "plugin"); //$NON-NLS-1$
								fPointID = getAttribute(meta, "id"); //$NON-NLS-1$
								fValid = true;
							} else if (meta.getNodeName().equals("meta.section")) { //$NON-NLS-1$
								section = getAttribute(meta, "type"); //$NON-NLS-1$
								sectionName = getAttribute(meta, "name"); //$NON-NLS-1$
								if (sectionName == null)
									sectionName = section;
							}
						}
					}
				}
			}
		}
	}

	private void processInclude(Node node) {
		String location = getAttribute(node, "schemaLocation"); //$NON-NLS-1$
		SchemaInclude include = new SchemaInclude(this, location, fAbbreviated, fSearchPath);
		if (fIncludes == null)
			fIncludes = new Vector<ISchemaInclude>();
		fIncludes.add(include);
	}

	public void reload() {
		reload(null);
	}

	public void reload(InputStream is) {
		setNotificationEnabled(false);
		reset();
		if (is != null)
			load(is);
		else
			load();
		setNotificationEnabled(true);
		if (isLoaded())
			fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.WORLD_CHANGED, new Object[0], null));
	}

	public void removeDocumentSection(IDocumentSection docSection) {
		fDocSections.remove(docSection);
		fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.REMOVE, new Object[] {docSection}, null));
	}

	public void moveElementToSibling(ISchemaElement element, ISchemaObject sibling) {
		if (!isLoaded())
			load();
		int index = fElements.indexOf(element);
		int newIndex;
		if (sibling != null && fElements.contains(sibling))
			newIndex = fElements.indexOf(sibling);
		else
			newIndex = fElements.size() - 1;

		if (index > newIndex) {
			for (int i = index; i > newIndex; i--) {
				fElements.set(i, fElements.get(i - 1));
			}
		} else if (index < newIndex) {
			for (int i = index; i < newIndex; i++) {
				fElements.set(i, fElements.get(i + 1));
			}
		} else
			// don't need to move
			return;
		fElements.set(newIndex, element);
		fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.CHANGE, new Object[] {this}, null));
	}

	public void removeElement(ISchemaElement element) {
		fElements.remove(element);
		fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.REMOVE, new Object[] {element}, null));
	}

	public void removeModelChangedListener(IModelChangedListener listener) {
		fListeners.remove(listener);
	}

	private void reset() {
		fElements = new Vector<ISchemaElement>();
		fDocSections = new Vector<DocumentSection>();
		fIncludes = null;
		fPointID = null;
		fPluginID = null;
		fReferences = null;
		fDescription = null;
		fName = null;
		fValid = false;
		fLoaded = false;
	}

	private void resolveElementReference(ISchemaObjectReference reference) {
		ISchemaElement[] elementList = getResolvedElements();
		for (int i = 0; i < elementList.length; i++) {
			ISchemaElement element = elementList[i];
			if (!(element instanceof ISchemaObjectReference) && element.getName().equals(reference.getName())) {
				// Link
				reference.setReferencedObject(element);
				break;
			}
		}
	}

	private void resolveReference(ISchemaObjectReference reference) {
		Class<?> clazz = reference.getReferencedObjectClass();
		if (clazz.equals(ISchemaElement.class)) {
			resolveElementReference(reference);
		}
	}

	private void resolveReferences(Vector<SchemaElementReference> references) {
		for (int i = 0; i < references.size(); i++) {
			ISchemaObjectReference reference = references.elementAt(i);
			resolveReference(reference);
		}
	}

	private SchemaType resolveTypeReference(String typeName) {
		// for now, create a simple type
		return new SchemaSimpleType(this, typeName);
	}

	public void setDescription(String newDescription) {
		String oldValue = fDescription;
		fDescription = newDescription;
		fireModelObjectChanged(this, P_DESCRIPTION, oldValue, fDescription);
	}

	public void setName(String newName) {
		if (newName == null)
			newName = ""; //$NON-NLS-1$
		String oldValue = fName;
		fName = newName;
		fireModelObjectChanged(this, P_NAME, oldValue, fName);
	}

	public void setPluginId(String newId) {
		String oldValue = fPluginID;
		fPluginID = newId;
		fireModelObjectChanged(this, P_PLUGIN, oldValue, newId);
	}

	public void setPointId(String newId) {
		String oldValue = fPointID;
		fPointID = newId;
		fireModelObjectChanged(this, P_POINT, oldValue, newId);
	}

	public void setNotificationEnabled(boolean newNotificationEnabled) {
		fNotificationEnabled = newNotificationEnabled;
	}

	/**
	 * Sets a list of additional schema relative or absolute paths to search when
	 * trying to find an included schema.  Must be set before {@link #load()} is
	 * called.
	 * 
	 * @param searchPath the list of paths to search for included schema or <code>null</code> for no additional paths
	 */
	public void setSearchPath(List<IPath> searchPath) {
		fSearchPath = searchPath;
	}

	@Override
	public String toString() {
		return fName;
	}

	public void traverseDocumentTree(Node root) {
		if (root == null)
			return;
		NodeList children = root.getChildNodes();
		fReferences = new Vector<SchemaElementReference>();
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				String nodeName = child.getNodeName().toLowerCase(Locale.ENGLISH);
				if (nodeName.equals("element")) { //$NON-NLS-1$
					ISchemaElement element = processElement(this, child);
					if (element == null) {
						fValid = false;
						return;
					}
					ISchemaAttribute[] attributes = element.getAttributes();
					for (int j = 0; j < attributes.length; j++)
						if (attributes[j] == null) {
							fValid = false;
							return;
						}
					fElements.add(element);
				} else if (nodeName.equals("annotation")) { //$NON-NLS-1$
					processSchemaAnnotation(child);
				} else if (nodeName.equals("include")) { //$NON-NLS-1$
					processInclude(child);
				}
			}
		}
		addOmittedDocumentSections();
		fLoaded = true;
		if (fReferences.size() > 0)
			resolveReferences(fReferences);
		fReferences = null;
	}

	private void addOmittedDocumentSections() {
		for (int i = 0; i < DocumentSection.DOC_SECTIONS.length; i++) {
			DocumentSection section = new DocumentSection(this, DocumentSection.DOC_SECTIONS[i], null);
			if (!fDocSections.contains(section)) {
				addDocumentSection(section);
			}
		}
		Collections.sort(fDocSections);
	}

	public void updateReferencesFor(ISchemaElement element) {
		updateReferencesFor(element, ISchema.REFRESH_RENAME);
	}

	public void updateReferencesFor(ISchemaElement element, int kind) {
		for (int i = 0; i < fElements.size(); i++) {
			ISchemaElement el = fElements.get(i);
			if (el.equals(element))
				continue;
			ISchemaType type = el.getType();
			if (type instanceof ISchemaComplexType) {
				SchemaCompositor compositor = (SchemaCompositor) ((ISchemaComplexType) type).getCompositor();
				if (compositor != null)
					compositor.updateReferencesFor(element, kind);
			}
		}
	}

	public void write(String indent, PrintWriter writer) {
		writer.println("<?xml version='1.0' encoding='UTF-8'?>"); //$NON-NLS-1$
		writer.println("<!-- Schema file written by PDE -->"); //$NON-NLS-1$
		writer.println("<schema targetNamespace=\"" + fPluginID + "\" xmlns=\"http://www.w3.org/2001/XMLSchema\">"); //$NON-NLS-1$ //$NON-NLS-2$
		String indent2 = INDENT + INDENT;
		String indent3 = indent2 + INDENT;
		writer.println(indent + "<annotation>"); //$NON-NLS-1$

		writer.println(indent2 + (getSchemaVersion() >= 3.4 ? "<appinfo>" : "<appInfo>")); //$NON-NLS-1$ //$NON-NLS-2$
		writer.print(indent3 + "<meta.schema plugin=\"" + fPluginID + "\""); //$NON-NLS-1$ //$NON-NLS-2$
		writer.print(" id=\"" + fPointID + "\""); //$NON-NLS-1$ //$NON-NLS-2$
		writer.println(" name=\"" + getName() + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
		writer.println(indent2 + (getSchemaVersion() >= 3.4 ? "</appinfo>" : "</appInfo>")); //$NON-NLS-1$ //$NON-NLS-2$
		writer.println(indent2 + "<documentation>"); //$NON-NLS-1$
		writer.println(indent3 + getWritableDescription());
		writer.println(indent2 + "</documentation>"); //$NON-NLS-1$
		writer.println(INDENT + "</annotation>"); //$NON-NLS-1$
		writer.println();
		// add includes, if defined
		if (fIncludes != null) {
			for (int i = 0; i < fIncludes.size(); i++) {
				ISchemaInclude include = fIncludes.get(i);
				include.write(INDENT, writer);
				writer.println();
			}
		}
		// add elements
		for (int i = 0; i < fElements.size(); i++) {
			ISchemaElement element = fElements.get(i);
			element.write(INDENT, writer);
			writer.println();
		}
		// add document sections
		for (int i = 0; i < fDocSections.size(); i++) {
			IDocumentSection section = fDocSections.get(i);
			section.write(INDENT, writer);
			writer.println();
		}
		writer.println("</schema>"); //$NON-NLS-1$
	}

	private String getWritableDescription() {
		String lineDelimiter = System.getProperty("line.separator"); //$NON-NLS-1$
		String description = PDEXMLHelper.getWritableString(getDescription());
		String platformDescription = description.replaceAll("\\r\\n|\\r|\\n", lineDelimiter); //$NON-NLS-1$

		return platformDescription;
	}

	public boolean isDeperecated() {
		Iterator<ISchemaElement> it = fElements.iterator();
		while (it.hasNext()) {
			Object next = it.next();
			if (next instanceof SchemaRootElement)
				return ((SchemaRootElement) next).isDeprecated();
		}
		return false;
	}

	public String getDeprecatedSuggestion() {
		Iterator<ISchemaElement> it = fElements.iterator();
		while (it.hasNext()) {
			Object next = it.next();
			if (next instanceof SchemaRootElement)
				return ((SchemaRootElement) next).getDeprecatedSuggestion();
		}
		return null;
	}

	public boolean isInternal() {
		Iterator<ISchemaElement> it = fElements.iterator();
		while (it.hasNext()) {
			Object next = it.next();
			if (next instanceof SchemaRootElement)
				return ((SchemaRootElement) next).isInternal();
		}
		return false;
	}

	public double getSchemaVersion() {
		if (fTargetVersion == 0) {
			IPluginModelBase model = PDECore.getDefault().getModelManager().findModel(fPluginID);
			if (model != null) {
				IPluginBase base = model.getPluginBase();
				if (base != null) {
					if (base.getSchemaVersion() != null) {
						fTargetVersion = Double.parseDouble(base.getSchemaVersion());
					}
				}
			}
			if (fTargetVersion == 0) {
				// Use default for target platform
				fTargetVersion = Double.parseDouble(TargetPlatformHelper.getSchemaVersion());
			}
		}
		return fTargetVersion;
	}
}

Back to the top