KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > schema > Schema


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.schema;
12
13 import java.io.FileNotFoundException JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.eclipse.core.runtime.PlatformObject;
23 import org.eclipse.pde.core.IModelChangedEvent;
24 import org.eclipse.pde.core.IModelChangedListener;
25 import org.eclipse.pde.core.ModelChangedEvent;
26 import org.eclipse.pde.internal.core.PDECore;
27 import org.eclipse.pde.internal.core.XMLDefaultHandler;
28 import org.eclipse.pde.internal.core.ischema.IDocumentSection;
29 import org.eclipse.pde.internal.core.ischema.IMetaAttribute;
30 import org.eclipse.pde.internal.core.ischema.ISchema;
31 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
32 import org.eclipse.pde.internal.core.ischema.ISchemaComplexType;
33 import org.eclipse.pde.internal.core.ischema.ISchemaCompositor;
34 import org.eclipse.pde.internal.core.ischema.ISchemaDescriptor;
35 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
36 import org.eclipse.pde.internal.core.ischema.ISchemaEnumeration;
37 import org.eclipse.pde.internal.core.ischema.ISchemaInclude;
38 import org.eclipse.pde.internal.core.ischema.ISchemaObject;
39 import org.eclipse.pde.internal.core.ischema.ISchemaObjectReference;
40 import org.eclipse.pde.internal.core.ischema.ISchemaRootElement;
41 import org.eclipse.pde.internal.core.ischema.ISchemaSimpleType;
42 import org.eclipse.pde.internal.core.ischema.ISchemaType;
43 import org.eclipse.pde.internal.core.util.PDEXMLHelper;
44 import org.eclipse.pde.internal.core.util.SAXParserWrapper;
45 import org.eclipse.pde.internal.core.util.SchemaUtil;
46 import org.w3c.dom.NamedNodeMap JavaDoc;
47 import org.w3c.dom.Node JavaDoc;
48 import org.w3c.dom.NodeList JavaDoc;
49 import org.xml.sax.SAXException JavaDoc;
50
51 public class Schema extends PlatformObject implements ISchema {
52
53     private URL JavaDoc fURL;
54
55     private Vector JavaDoc fListeners = new Vector JavaDoc();
56
57     private Vector JavaDoc fElements = new Vector JavaDoc();
58
59     private Vector JavaDoc fDocSections = new Vector JavaDoc();
60
61     private Vector JavaDoc fIncludes;
62
63     private String JavaDoc fPointID;
64
65     private String JavaDoc fPluginID;
66     
67     private ISchemaDescriptor fSchemaDescriptor;
68
69     private boolean fLoaded;
70
71     private Vector JavaDoc fReferences;
72
73     private String JavaDoc fDescription;
74
75     private String JavaDoc fName = ""; //$NON-NLS-1$
76

77     private boolean fNotificationEnabled;
78
79     public final static String JavaDoc INDENT = " "; //$NON-NLS-1$
80

81     private boolean fDisposed;
82
83     private boolean fValid;
84
85     private boolean fAbbreviated;
86
87     public Schema(String JavaDoc pluginId, String JavaDoc pointId, String JavaDoc name, boolean abbreviated) {
88         fPluginID = pluginId;
89         fPointID = pointId;
90         fName = name;
91         fAbbreviated = abbreviated;
92     }
93
94     public Schema(ISchemaDescriptor schemaDescriptor, URL JavaDoc url, boolean abbreviated) {
95         fSchemaDescriptor = schemaDescriptor;
96         fURL = url;
97         fAbbreviated = abbreviated;
98     }
99
100     public void addDocumentSection(IDocumentSection docSection) {
101         fDocSections.addElement(docSection);
102         fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.INSERT,
103                 new Object JavaDoc[] { docSection }, null));
104     }
105
106     public void addElement(ISchemaElement element) {
107         addElement(element, null);
108     }
109
110     public void addElement(ISchemaElement element, ISchemaElement afterElement) {
111         int index = -1;
112         if (afterElement != null) {
113             index = fElements.indexOf(afterElement);
114         }
115         if (index != -1)
116             fElements.add(index + 1, element);
117         else
118             fElements.add(element);
119         fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.INSERT,
120                 new Object JavaDoc[] { element }, null));
121     }
122
123     public void addInclude(ISchemaInclude include) {
124         if (fIncludes == null)
125             fIncludes = new Vector JavaDoc();
126         fIncludes.add(include);
127         fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.INSERT,
128                 new Object JavaDoc[] { include }, null));
129     }
130
131     public void removeInclude(ISchemaInclude include) {
132         if (fIncludes == null)
133             return;
134         fIncludes.remove(include);
135         fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.REMOVE,
136                 new Object JavaDoc[] { include }, null));
137     }
138
139     public void addModelChangedListener(IModelChangedListener listener) {
140         fListeners.addElement(listener);
141     }
142
143     private void collectElements(ISchemaCompositor compositor, Vector JavaDoc result) {
144         Object JavaDoc[] children = compositor.getChildren();
145         for (int i = 0; i < children.length; i++) {
146             Object JavaDoc child = children[i];
147             if (child instanceof ISchemaCompositor)
148                 collectElements((ISchemaCompositor) child, result);
149             else if (child instanceof ISchemaObjectReference) {
150                 ISchemaObjectReference ref = (ISchemaObjectReference) child;
151                 Object JavaDoc referenced = ref.getReferencedObject();
152                 if (referenced instanceof ISchemaElement)
153                     result.addElement(referenced);
154             }
155         }
156     }
157
158     public void dispose() {
159         if (fIncludes != null) {
160             for (int i = 0; i < fIncludes.size(); i++) {
161                 ISchemaInclude include = (ISchemaInclude) fIncludes.get(i);
162                 include.dispose();
163             }
164         }
165         reset();
166         fDisposed = true;
167     }
168
169     public ISchemaElement findElement(String JavaDoc name) {
170         if (!isLoaded())
171             load();
172         for (int i = 0; i < fElements.size(); i++) {
173             ISchemaElement element = (ISchemaElement) fElements.elementAt(i);
174             if (element.getName().equals(name))
175                 return element;
176         }
177         if (fIncludes != null) {
178             for (int i = 0; i < fIncludes.size(); i++) {
179                 ISchemaInclude include = (ISchemaInclude) fIncludes.get(i);
180                 ISchema ischema = include.getIncludedSchema();
181                 if (ischema == null)
182                     continue;
183                 ISchemaElement element = ischema.findElement(name);
184                 if (element != null)
185                     return element;
186             }
187         }
188         return null;
189     }
190
191     public void fireModelChanged(IModelChangedEvent event) {
192         if (!fNotificationEnabled)
193             return;
194         for (Iterator JavaDoc iter = fListeners.iterator(); iter.hasNext();) {
195             IModelChangedListener listener = (IModelChangedListener) iter
196                     .next();
197             listener.modelChanged(event);
198         }
199     }
200
201     public void fireModelObjectChanged(Object JavaDoc object, String JavaDoc property,
202             Object JavaDoc oldValue, Object JavaDoc newValue) {
203         fireModelChanged(new ModelChangedEvent(this, object, property,
204                 oldValue, newValue));
205     }
206
207     private String JavaDoc getAttribute(Node JavaDoc node, String JavaDoc name) {
208         NamedNodeMap JavaDoc map = node.getAttributes();
209         Node JavaDoc attNode = map.getNamedItem(name);
210         if (attNode != null) {
211             String JavaDoc value = attNode.getNodeValue();
212             if (value.length() > 0)
213                 return value;
214         }
215         return null;
216     }
217
218     public ISchemaElement[] getCandidateChildren(ISchemaElement element) {
219         Vector JavaDoc candidates = new Vector JavaDoc();
220         ISchemaType type = element.getType();
221         if (type instanceof ISchemaComplexType) {
222             ISchemaCompositor compositor = ((ISchemaComplexType)type).getCompositor();
223             if (compositor != null)
224                 collectElements(compositor, candidates);
225         }
226         ISchemaElement[] result = new ISchemaElement[candidates.size()];
227         candidates.copyInto(result);
228         return result;
229     }
230
231     public String JavaDoc getDescription() {
232         return fDescription;
233     }
234
235     public boolean isValid() {
236         return fValid;
237     }
238
239     public IDocumentSection[] getDocumentSections() {
240         IDocumentSection[] result = new IDocumentSection[fDocSections.size()];
241         fDocSections.copyInto(result);
242         return result;
243     }
244
245     public int getElementCount() {
246         return fElements.size();
247     }
248
249     public int getResolvedElementCount() {
250         int localCount = getElementCount();
251         if (fIncludes == null)
252             return localCount;
253         int totalCount = localCount;
254         for (int i = 0; i < fIncludes.size(); i++) {
255             ISchemaInclude include = (ISchemaInclude) fIncludes.get(i);
256             ISchema schema = include.getIncludedSchema();
257             if (schema == null)
258                 continue;
259             totalCount += schema.getResolvedElementCount();
260         }
261         return totalCount;
262     }
263
264     public ISchemaElement[] getElements() {
265         if (!isLoaded())
266             load();
267         ISchemaElement[] result = new ISchemaElement[fElements.size()];
268         fElements.copyInto(result);
269         return result;
270     }
271
272     public ISchemaElement[] getResolvedElements() {
273         if (fIncludes == null)
274             return getElements();
275         if (!isLoaded())
276             load();
277         Vector JavaDoc result = (Vector JavaDoc) fElements.clone();
278         for (int i = 0; i < fIncludes.size(); i++) {
279             ISchemaInclude include = (ISchemaInclude) fIncludes.get(i);
280             ISchema schema = include.getIncludedSchema();
281             if (schema == null)
282                 continue;
283             ISchemaElement[] ielements = schema.getElements();
284             for (int j = 0; j < ielements.length; j++)
285                 result.add(ielements[j]);
286         }
287         return (ISchemaElement[]) result.toArray(new ISchemaElement[result
288                 .size()]);
289     }
290
291     public ISchemaInclude[] getIncludes() {
292         if (fIncludes == null)
293             return new ISchemaInclude[0];
294         return (ISchemaInclude[]) fIncludes.toArray(new ISchemaInclude[fIncludes
295                 .size()]);
296     }
297
298     public String JavaDoc getName() {
299         return fName;
300     }
301
302     private String JavaDoc getNormalizedText(String JavaDoc source) {
303         if (source == null)
304             return ""; //$NON-NLS-1$
305

306         String JavaDoc result = source.replace('\t', ' ');
307         result = result.trim();
308         return result;
309     }
310
311     public ISchemaObject getParent() {
312         return null;
313     }
314
315     public void setParent(ISchemaObject obj) {
316     }
317
318     public ISchemaElement getElementAt(int index)
319     { return (ISchemaElement)fElements.get(index);
320     }
321     
322     public String JavaDoc getQualifiedPointId() {
323         return fPluginID + "." + fPointID; //$NON-NLS-1$
324
}
325
326     public String JavaDoc getPluginId() {
327         return fPluginID;
328     }
329
330     public String JavaDoc getPointId() {
331         return fPointID;
332     }
333
334     public ISchema getSchema() {
335         return this;
336     }
337
338     public ISchemaDescriptor getSchemaDescriptor() {
339         return fSchemaDescriptor;
340     }
341
342     public URL JavaDoc getURL() {
343         return fURL;
344     }
345
346     public int indexOf(Object JavaDoc obj) {
347         return fElements.indexOf(obj);
348     }
349     
350     public boolean isDisposed() {
351         return fDisposed;
352     }
353
354     public boolean isEditable() {
355         return false;
356     }
357
358     public boolean isLoaded() {
359         return fLoaded;
360     }
361
362     public boolean isNotificationEnabled() {
363         return fNotificationEnabled;
364     }
365
366     public void load() {
367         InputStream JavaDoc input = null;
368         try {
369             input = SchemaUtil.getInputStream(fURL);
370             load(input);
371         } catch (FileNotFoundException JavaDoc e) {
372             fLoaded = false;
373         } catch (IOException JavaDoc e) {
374             PDECore.logException(e);
375         } finally {
376             try {
377                 if (input != null)
378                     input.close();
379             } catch (IOException JavaDoc e1) {
380             }
381         }
382     }
383
384     public void load(InputStream JavaDoc stream) {
385         try {
386             SAXParserWrapper parser = new SAXParserWrapper();
387             XMLDefaultHandler handler = new XMLDefaultHandler(fAbbreviated);
388             parser.parse(stream, handler);
389             traverseDocumentTree(handler.getDocumentElement());
390         } catch (SAXException JavaDoc e) {
391             // ignore parse errors - 'loaded' will be false anyway
392
} catch (Exception JavaDoc e) {
393             PDECore.logException(e);
394         }
395     }
396
397     private ISchemaAttribute processAttribute(ISchemaElement element,
398             Node JavaDoc elementNode) {
399         String JavaDoc aname = getAttribute(elementNode, "name"); //$NON-NLS-1$
400
String JavaDoc atype = getAttribute(elementNode, "type"); //$NON-NLS-1$
401
String JavaDoc ause = getAttribute(elementNode, "use"); //$NON-NLS-1$
402
String JavaDoc avalue = getAttribute(elementNode, "value"); //$NON-NLS-1$
403
ISchemaSimpleType type = null;
404         if (atype != null) {
405             type = (ISchemaSimpleType) resolveTypeReference(atype);
406         }
407         SchemaAttribute attribute = new SchemaAttribute(element, aname);
408         //attribute.bindSourceLocation(elementNode, lineTable);
409
if (ause != null) {
410             int use = ISchemaAttribute.OPTIONAL;
411             if (ause.equals("required")) //$NON-NLS-1$
412
use = ISchemaAttribute.REQUIRED;
413             else if (ause.equals("optional")) //$NON-NLS-1$
414
use = ISchemaAttribute.OPTIONAL;
415             else if (ause.equals("default")) //$NON-NLS-1$
416
use = ISchemaAttribute.DEFAULT;
417             attribute.setUse(use);
418         }
419         if (avalue != null)
420             attribute.setValue(avalue);
421         NodeList JavaDoc children = elementNode.getChildNodes();
422         for (int i = 0; i < children.getLength(); i++) {
423             Node JavaDoc child = children.item(i);
424             if (child.getNodeType() == Node.ELEMENT_NODE) {
425                 String JavaDoc tag = child.getNodeName();
426                 if (tag.equals("annotation")) { //$NON-NLS-1$
427
processAttributeAnnotation(attribute, child);
428                 } else if (tag.equals("simpleType")) { //$NON-NLS-1$
429
processAttributeSimpleType(attribute, child);
430                 }
431             }
432         }
433         if (type != null && attribute.getType() == null)
434             attribute.setType(type);
435         return attribute;
436     }
437
438     private void processAttributeAnnotation(SchemaAttribute element, Node JavaDoc node) {
439         NodeList JavaDoc children = node.getChildNodes();
440         for (int i = 0; i < children.getLength(); i++) {
441             Node JavaDoc child = children.item(i);
442             if (child.getNodeType() == Node.ELEMENT_NODE) {
443                 if (child.getNodeName().equals("documentation")) { //$NON-NLS-1$
444
Node JavaDoc doc = child.getFirstChild();
445                     if (doc != null)
446                         element.setDescription(getNormalizedText(doc.getNodeValue()));
447                 } else if (child.getNodeName().equals("appInfo")) { //$NON-NLS-1$
448
NodeList JavaDoc infos = child.getChildNodes();
449                     for (int j = 0; j < infos.getLength(); j++) {
450                         Node JavaDoc meta = infos.item(j);
451                         if (meta.getNodeType() == Node.ELEMENT_NODE) {
452                             if (meta.getNodeName().equals("meta.attribute")) { //$NON-NLS-1$
453
element.setKind(processKind(getAttribute(meta,
454                                         "kind"))); //$NON-NLS-1$
455
element.setBasedOn(
456                                         getAttribute(meta,"basedOn")); //$NON-NLS-1$
457
element.setTranslatableProperty(
458                                         processTranslatable(getAttribute(
459                                                 meta, "translatable"))); //$NON-NLS-1$
460
element.setDeprecatedProperty(
461                                         processDeprecated(getAttribute(
462                                                 meta, "deprecated"))); //$NON-NLS-1$
463
}
464                         }
465                     }
466                 }
467             }
468         }
469     }
470
471     private boolean processTranslatable(String JavaDoc value) {
472         return (value != null && "true".equals(value)); //$NON-NLS-1$
473
}
474
475     private boolean processDeprecated(String JavaDoc value) {
476         return value != null && "true".equals(value); //$NON-NLS-1$
477
}
478
479     private SchemaSimpleType processAttributeRestriction(
480             SchemaAttribute attribute, Node JavaDoc node) {
481         NodeList JavaDoc children = node.getChildNodes();
482         if (children.getLength() == 0)
483             return null;
484         String JavaDoc baseName = getAttribute(node, "base"); //$NON-NLS-1$
485
if (baseName.equals("string") == false) { //$NON-NLS-1$
486
return new SchemaSimpleType(attribute.getSchema(), "string"); //$NON-NLS-1$
487
}
488         SchemaSimpleType type = new SchemaSimpleType(attribute.getSchema(),
489                 baseName);
490         Vector JavaDoc items = new Vector JavaDoc();
491         for (int i = 0; i < children.getLength(); i++) {
492             Node JavaDoc child = children.item(i);
493             if (child.getNodeType() == Node.ELEMENT_NODE) {
494                 if (child.getNodeName().equals("enumeration")) { //$NON-NLS-1$
495
ISchemaEnumeration enumeration = processEnumeration(
496                             attribute.getSchema(), child);
497                     if (enumeration != null)
498                         items.addElement(enumeration);
499                 }
500             }
501         }
502         ChoiceRestriction restriction = new ChoiceRestriction(attribute
503                 .getSchema());
504         restriction.setChildren(items);
505         type.setRestriction(restriction);
506         return type;
507     }
508
509     private void processAttributeSimpleType(SchemaAttribute attribute, Node JavaDoc node) {
510         NodeList JavaDoc children = node.getChildNodes();
511         if (children.getLength() == 0)
512             return;
513         SchemaSimpleType type = null;
514         for (int i = 0; i < children.getLength(); i++) {
515             Node JavaDoc child = children.item(i);
516             if (child.getNodeType() == Node.ELEMENT_NODE) {
517                 if (child.getNodeName().equals("restriction")) { //$NON-NLS-1$
518
type = processAttributeRestriction(attribute, child);
519                 }
520             }
521         }
522         if (type != null)
523             attribute.setType(type);
524     }
525
526     private SchemaComplexType processComplexType(ISchemaElement owner,
527             Node JavaDoc typeNode) {
528         String JavaDoc aname = getAttribute(typeNode, "name"); //$NON-NLS-1$
529
String JavaDoc amixed = getAttribute(typeNode, "mixed"); //$NON-NLS-1$
530
SchemaComplexType complexType = new SchemaComplexType(this, aname);
531         if (amixed != null && amixed.equals("true")) //$NON-NLS-1$
532
complexType.setMixed(true);
533         NodeList JavaDoc children = typeNode.getChildNodes();
534         ISchemaCompositor compositor = null;
535         for (int i = 0; i < children.getLength(); i++) {
536             Node JavaDoc child = children.item(i);
537             if (child.getNodeType() == Node.ELEMENT_NODE) {
538                 if (child.getNodeName().equals("attribute")) { //$NON-NLS-1$
539
complexType.addAttribute(processAttribute(owner, child));
540                 } else {
541                     ISchemaObject object = processCompositorChild(owner, child,
542                             ISchemaCompositor.ROOT);
543                     if (object instanceof ISchemaCompositor
544                             && compositor == null) {
545                         compositor = (ISchemaCompositor) object;
546                     }
547                 }
548             }
549         }
550         complexType.setCompositor(compositor);
551         return complexType;
552     }
553
554     private ISchemaCompositor processCompositor(ISchemaObject parent,
555             Node JavaDoc node, int type) {
556         SchemaCompositor compositor = new SchemaCompositor(parent, type);
557         NodeList JavaDoc children = node.getChildNodes();
558         int minOccurs = 1;
559         int maxOccurs = 1;
560         String JavaDoc aminOccurs = getAttribute(node, "minOccurs"); //$NON-NLS-1$
561
String JavaDoc amaxOccurs = getAttribute(node, "maxOccurs"); //$NON-NLS-1$
562
if (aminOccurs != null)
563             minOccurs = Integer.valueOf(aminOccurs).intValue();
564         if (amaxOccurs != null) {
565             if (amaxOccurs.equals("unbounded")) //$NON-NLS-1$
566
maxOccurs = Integer.MAX_VALUE;
567             else {
568                 maxOccurs = Integer.valueOf(amaxOccurs).intValue();
569             }
570         }
571         compositor.setMinOccurs(minOccurs);
572         compositor.setMaxOccurs(maxOccurs);
573         for (int i = 0; i < children.getLength(); i++) {
574             Node JavaDoc child = children.item(i);
575             ISchemaObject object = processCompositorChild(compositor, child,
576                     type);
577             if (object != null)
578                 compositor.addChild(object);
579         }
580         return compositor;
581     }
582
583     private ISchemaObject processCompositorChild(ISchemaObject parent,
584             Node JavaDoc child, int parentKind) {
585         String JavaDoc tag = child.getNodeName();
586         if (tag.equals("element") && parentKind != ISchemaCompositor.ROOT) { //$NON-NLS-1$
587
return processElement(parent, child);
588         }
589         // sequence: element | group | choice | sequence
590
if (tag.equals("sequence") && parentKind != ISchemaCompositor.ALL) { //$NON-NLS-1$
591
return processCompositor(parent, child, ISchemaCompositor.SEQUENCE);
592         }
593         // choice: element | group | choice | sequence
594
if (tag.equals("choice") && parentKind != ISchemaCompositor.ALL) { //$NON-NLS-1$
595
return processCompositor(parent, child, ISchemaCompositor.CHOICE);
596         }
597         // all: element
598
if (tag.equals("all") //$NON-NLS-1$
599
&& (parentKind == ISchemaCompositor.ROOT || parentKind == ISchemaCompositor.GROUP)) {
600             return processCompositor(parent, child, ISchemaCompositor.SEQUENCE);
601         }
602         // group: all | choice | sequence
603
if (tag.equals("group") //$NON-NLS-1$
604
&& (parentKind == ISchemaCompositor.CHOICE || parentKind == ISchemaCompositor.SEQUENCE)) {
605             return processCompositor(parent, child, ISchemaCompositor.SEQUENCE);
606         }
607         return null;
608     }
609
610     private ISchemaElement processElement(ISchemaObject parent, Node JavaDoc elementNode) {
611         String JavaDoc aname = getAttribute(elementNode, "name"); //$NON-NLS-1$
612
String JavaDoc atype = getAttribute(elementNode, "type"); //$NON-NLS-1$
613
String JavaDoc aref = getAttribute(elementNode, "ref"); //$NON-NLS-1$
614

615         int minOccurs = 1;
616         int maxOccurs = 1;
617         String JavaDoc aminOccurs = getAttribute(elementNode, "minOccurs"); //$NON-NLS-1$
618
String JavaDoc amaxOccurs = getAttribute(elementNode, "maxOccurs"); //$NON-NLS-1$
619
if (aminOccurs != null)
620             minOccurs = Integer.valueOf(aminOccurs).intValue();
621         if (amaxOccurs != null) {
622             if (amaxOccurs.equals("unbounded")) //$NON-NLS-1$
623
maxOccurs = Integer.MAX_VALUE;
624             else {
625                 maxOccurs = Integer.valueOf(amaxOccurs).intValue();
626             }
627         }
628         if (aref != null) {
629             // Reference!!
630
SchemaElementReference reference = new SchemaElementReference(
631                     (ISchemaCompositor) parent, aref);
632             reference.addComments(elementNode);
633             reference.setMinOccurs(minOccurs);
634             reference.setMaxOccurs(maxOccurs);
635             fReferences.addElement(reference);
636             //reference.bindSourceLocation(elementNode, lineTable);
637
return reference;
638         }
639         ISchemaType type = null;
640         if (atype != null) {
641             type = resolveTypeReference(atype);
642         }
643         SchemaElement element;
644         if (aname.equals("extension")) //$NON-NLS-1$
645
element = new SchemaRootElement(parent, aname);
646         else
647             element = new SchemaElement(parent, aname);
648         //element.bindSourceLocation(elementNode, lineTable);
649
element.setMinOccurs(minOccurs);
650         element.setMaxOccurs(maxOccurs);
651         NodeList JavaDoc children = elementNode.getChildNodes();
652         for (int i = 0; i < children.getLength(); i++) {
653             Node JavaDoc child = children.item(i);
654             if (child.getNodeType() == Node.ELEMENT_NODE) {
655                 String JavaDoc tag = child.getNodeName();
656                 if (type == null && tag.equals("complexType")) { //$NON-NLS-1$
657
type = processComplexType(element, child);
658                 }
659                 if (tag.equals("annotation")) { //$NON-NLS-1$
660
processElementAnnotation(element, child);
661                 }
662             }
663         }
664         element.setType(type);
665         return element;
666     }
667
668     private void processElementAnnotation(SchemaElement element, Node JavaDoc node) {
669         NodeList JavaDoc children = node.getChildNodes();
670         for (int i = 0; i < children.getLength(); i++) {
671             Node JavaDoc child = children.item(i);
672             if (child.getNodeType() == Node.ELEMENT_NODE) {
673                 if (child.getNodeName().equals("documentation") && !fAbbreviated) { //$NON-NLS-1$
674
element.setDescription(getNormalizedText(child
675                             .getFirstChild().getNodeValue()));
676                 } else if (child.getNodeName().equals("appInfo")) { //$NON-NLS-1$
677
NodeList JavaDoc infos = child.getChildNodes();
678                     for (int j = 0; j < infos.getLength(); j++) {
679                         Node JavaDoc meta = infos.item(j);
680                         if (meta.getNodeType() == Node.ELEMENT_NODE) {
681                             if (meta.getNodeName().equals("meta.element")) { //$NON-NLS-1$
682
element.setLabelProperty(getAttribute(meta,
683                                         "labelAttribute")); //$NON-NLS-1$
684
element.setIconProperty(getAttribute(meta,
685                                         "icon")); //$NON-NLS-1$
686
if (element.getIconProperty() == null)
687                                     element.setIconProperty(getAttribute(meta,
688                                             "iconName")); //$NON-NLS-1$
689
element.setTranslatableProperty(processTranslatable(getAttribute(
690                                                 meta, "translatable"))); //$NON-NLS-1$
691
element.setDeprecatedProperty(processDeprecated(getAttribute(
692                                                 meta, "deprecated"))); //$NON-NLS-1$
693
if (element instanceof ISchemaRootElement) {
694                                     String JavaDoc depSug = getAttribute(meta, SchemaRootElement.P_DEP_REPLACEMENT);
695                                     ((ISchemaRootElement)element).setDeprecatedSuggestion(depSug);
696                                 }
697                             }
698                         }
699                     }
700                 }
701             }
702         }
703     }
704
705     private ISchemaEnumeration processEnumeration(ISchema schema, Node JavaDoc node) {
706         String JavaDoc name = getAttribute(node, "value"); //$NON-NLS-1$
707
return new SchemaEnumeration(schema, name);
708     }
709
710     private int processKind(String JavaDoc name) {
711         if (name != null) {
712             if (name.equals("java")) //$NON-NLS-1$
713
return IMetaAttribute.JAVA;
714             if (name.equals("resource")) //$NON-NLS-1$
715
return IMetaAttribute.RESOURCE;
716         }
717         return IMetaAttribute.STRING;
718     }
719
720     private void processSchemaAnnotation(Node JavaDoc node) {
721         NodeList JavaDoc children = node.getChildNodes();
722         String JavaDoc section = "overview"; //$NON-NLS-1$
723
String JavaDoc sectionName = "Overview"; //$NON-NLS-1$
724
for (int i = 0; i < children.getLength(); i++) {
725             Node JavaDoc child = children.item(i);
726             if (child.getNodeType() == Node.ELEMENT_NODE) {
727                 if (child.getNodeName().equals("documentation") && !fAbbreviated) { //$NON-NLS-1$
728
String JavaDoc text = getNormalizedText(child.getFirstChild()
729                             .getNodeValue());
730                     if (section != null) {
731                         if (section.equals("overview")) { //$NON-NLS-1$
732
setDescription(text);
733                         } else {
734                             DocumentSection sec = new DocumentSection(this,
735                                     section, sectionName);
736                             sec.setDescription(text);
737                             fDocSections.addElement(sec);
738                         }
739                     }
740                 } else if (child.getNodeName().equals("appInfo")) { //$NON-NLS-1$
741
NodeList JavaDoc infos = child.getChildNodes();
742                     for (int j = 0; j < infos.getLength(); j++) {
743                         Node JavaDoc meta = infos.item(j);
744                         if (meta.getNodeType() == Node.ELEMENT_NODE) {
745                             if (meta.getNodeName().equals("meta.schema")) { //$NON-NLS-1$
746
section = "overview"; //$NON-NLS-1$
747
setName(getAttribute(meta, "name")); //$NON-NLS-1$
748
fPluginID = getAttribute(meta, "plugin"); //$NON-NLS-1$
749
fPointID = getAttribute(meta, "id"); //$NON-NLS-1$
750
fValid = true;
751                             } else if (meta.getNodeName()
752                                     .equals("meta.section")) { //$NON-NLS-1$
753
section = getAttribute(meta, "type"); //$NON-NLS-1$
754
sectionName = getAttribute(meta, "name"); //$NON-NLS-1$
755
if (sectionName == null)
756                                     sectionName = section;
757                             }
758                         }
759                     }
760                 }
761             }
762         }
763     }
764
765     private void processInclude(Node JavaDoc node) {
766         String JavaDoc location = getAttribute(node, "schemaLocation"); //$NON-NLS-1$
767
SchemaInclude include = new SchemaInclude(this, location, fAbbreviated);
768         if (fIncludes == null)
769             fIncludes = new Vector JavaDoc();
770         fIncludes.add(include);
771     }
772
773     public void reload() {
774         reload(null);
775     }
776
777     public void reload(InputStream JavaDoc is) {
778         setNotificationEnabled(false);
779         reset();
780         if (is != null)
781             load(is);
782         else
783             load();
784         setNotificationEnabled(true);
785         if (isLoaded())
786             fireModelChanged(new ModelChangedEvent(this,
787                     IModelChangedEvent.WORLD_CHANGED, new Object JavaDoc[0], null));
788     }
789
790     public void removeDocumentSection(IDocumentSection docSection) {
791         fDocSections.removeElement(docSection);
792         fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.REMOVE,
793                 new Object JavaDoc[] { docSection }, null));
794     }
795
796     public void moveElementToSibling(ISchemaElement element, ISchemaObject sibling) {
797         if (!isLoaded())
798             load();
799         int index = fElements.indexOf(element);
800         int newIndex;
801         if (sibling != null && fElements.contains(sibling))
802             newIndex = fElements.indexOf(sibling);
803         else
804             newIndex = fElements.size() - 1;
805         
806         if (index > newIndex) {
807             for (int i = index; i > newIndex; i--) {
808                 fElements.set(i, fElements.elementAt(i - 1));
809             }
810         } else if (index < newIndex) {
811             for (int i = index; i < newIndex; i++) {
812                 fElements.set(i, fElements.elementAt(i + 1));
813             }
814         } else // don't need to move
815
return;
816         fElements.set(newIndex, element);
817         fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.CHANGE,
818                 new Object JavaDoc[] { this }, null));
819     }
820     
821     public void removeElement(ISchemaElement element) {
822         fElements.removeElement(element);
823         fireModelChanged(new ModelChangedEvent(this, IModelChangedEvent.REMOVE,
824                 new Object JavaDoc[] { element }, null));
825     }
826
827     public void removeModelChangedListener(IModelChangedListener listener) {
828         fListeners.removeElement(listener);
829     }
830
831     private void reset() {
832         fElements = new Vector JavaDoc();
833         fDocSections = new Vector JavaDoc();
834         fIncludes = null;
835         fPointID = null;
836         fPluginID = null;
837         fReferences = null;
838         fDescription = null;
839         fName = null;
840         fValid = false;
841         fLoaded = false;
842     }
843
844     private void resolveElementReference(ISchemaObjectReference reference) {
845         ISchemaElement[] elementList = getResolvedElements();
846         for (int i = 0; i < elementList.length; i++) {
847             ISchemaElement element = elementList[i];
848             if (!(element instanceof ISchemaObjectReference)
849                     && element.getName().equals(reference.getName())) {
850                 // Link
851
reference.setReferencedObject(element);
852                 break;
853             }
854         }
855     }
856
857     private void resolveReference(ISchemaObjectReference reference) {
858         Class JavaDoc clazz = reference.getReferencedObjectClass();
859         if (clazz.equals(ISchemaElement.class)) {
860             resolveElementReference(reference);
861         }
862     }
863
864     private void resolveReferences(Vector JavaDoc references) {
865         for (int i = 0; i < references.size(); i++) {
866             ISchemaObjectReference reference = (ISchemaObjectReference) references
867                     .elementAt(i);
868             resolveReference(reference);
869         }
870     }
871
872     private SchemaType resolveTypeReference(String JavaDoc typeName) {
873         // for now, create a simple type
874
return new SchemaSimpleType(this, typeName);
875     }
876
877     public void setDescription(String JavaDoc newDescription) {
878         String JavaDoc oldValue = fDescription;
879         fDescription = newDescription;
880         fireModelObjectChanged(this, P_DESCRIPTION, oldValue, fDescription);
881     }
882
883     public void setName(String JavaDoc newName) {
884         if (newName == null)
885             newName = ""; //$NON-NLS-1$
886
String JavaDoc oldValue = fName;
887         fName = newName;
888         fireModelObjectChanged(this, P_NAME, oldValue, fName);
889     }
890
891     public void setPluginId(String JavaDoc newId) {
892         String JavaDoc oldValue = fPluginID;
893         fPluginID = newId;
894         fireModelObjectChanged(this, P_PLUGIN, oldValue, newId);
895     }
896
897     public void setPointId(String JavaDoc newId) {
898         String JavaDoc oldValue = fPointID;
899         fPointID = newId;
900         fireModelObjectChanged(this, P_POINT, oldValue, newId);
901     }
902
903     public void setNotificationEnabled(boolean newNotificationEnabled) {
904         fNotificationEnabled = newNotificationEnabled;
905     }
906
907     public String JavaDoc toString() {
908         return fName;
909     }
910
911     public void traverseDocumentTree(Node JavaDoc root) {
912         if (root == null)
913             return;
914         NodeList JavaDoc children = root.getChildNodes();
915         fReferences = new Vector JavaDoc();
916         for (int i = 0; i < children.getLength(); i++) {
917             Node JavaDoc child = children.item(i);
918             if (child.getNodeType() == Node.ELEMENT_NODE) {
919                 String JavaDoc nodeName = child.getNodeName().toLowerCase(Locale.ENGLISH);
920                 if (nodeName.equals("element")) { //$NON-NLS-1$
921
ISchemaElement element = processElement(this, child);
922                     fElements.addElement(element);
923                 } else if (nodeName.equals("annotation")) { //$NON-NLS-1$
924
processSchemaAnnotation(child);
925                 } else if (nodeName.equals("include")) { //$NON-NLS-1$
926
processInclude(child);
927                 }
928             }
929         }
930         fLoaded = true;
931         if (fReferences.size() > 0)
932             resolveReferences(fReferences);
933         fReferences = null;
934     }
935
936     public void updateReferencesFor(ISchemaElement element) {
937         updateReferencesFor(element, ISchema.REFRESH_RENAME);
938     }
939
940     public void updateReferencesFor(ISchemaElement element, int kind) {
941         for (int i = 0; i < fElements.size(); i++) {
942             ISchemaElement el = (ISchemaElement) fElements.elementAt(i);
943             if (el.equals(element))
944                 continue;
945             ISchemaType type = el.getType();
946             if (type instanceof ISchemaComplexType) {
947                 SchemaCompositor compositor = (SchemaCompositor) ((ISchemaComplexType) type)
948                         .getCompositor();
949                 if (compositor != null)
950                     compositor.updateReferencesFor(element, kind);
951             }
952         }
953     }
954
955     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
956         writer.println("<?xml version='1.0' encoding='UTF-8'?>"); //$NON-NLS-1$
957
writer.println("<!-- Schema file written by PDE -->"); //$NON-NLS-1$
958
writer.println("<schema targetNamespace=\"" + fPluginID + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
959
String JavaDoc indent2 = INDENT + INDENT;
960         String JavaDoc indent3 = indent2 + INDENT;
961         writer.println(indent + "<annotation>"); //$NON-NLS-1$
962
writer.println(indent2 + "<appInfo>"); //$NON-NLS-1$
963
writer.print(indent3 + "<meta.schema plugin=\"" + fPluginID + "\""); //$NON-NLS-1$ //$NON-NLS-2$
964
writer.print(" id=\"" + fPointID + "\""); //$NON-NLS-1$ //$NON-NLS-2$
965
writer.println(" name=\"" + getName() + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
966
writer.println(indent2 + "</appInfo>"); //$NON-NLS-1$
967
writer.println(indent2 + "<documentation>"); //$NON-NLS-1$
968
writer.println(indent3
969                 + getWritableDescription());
970         writer.println(indent2 + "</documentation>"); //$NON-NLS-1$
971
writer.println(INDENT + "</annotation>"); //$NON-NLS-1$
972
writer.println();
973         // add includes, if defined
974
if (fIncludes != null) {
975             for (int i = 0; i < fIncludes.size(); i++) {
976                 ISchemaInclude include = (ISchemaInclude) fIncludes.get(i);
977                 include.write(INDENT, writer);
978                 writer.println();
979             }
980         }
981         // add elements
982
for (int i = 0; i < fElements.size(); i++) {
983             ISchemaElement element = (ISchemaElement) fElements.elementAt(i);
984             element.write(INDENT, writer);
985             writer.println();
986         }
987         // add document sections
988
for (int i = 0; i < fDocSections.size(); i++) {
989             IDocumentSection section = (IDocumentSection) fDocSections.elementAt(i);
990             section.write(INDENT, writer);
991             writer.println();
992         }
993         writer.println("</schema>"); //$NON-NLS-1$
994
}
995
996     private String JavaDoc getWritableDescription() {
997         String JavaDoc lineDelimiter = System.getProperty("line.separator"); //$NON-NLS-1$
998
String JavaDoc description = PDEXMLHelper.getWritableString(getDescription());
999         String JavaDoc platformDescription = description.replaceAll(
1000                "\\r\\n|\\r|\\n", lineDelimiter); //$NON-NLS-1$
1001

1002        return platformDescription;
1003    }
1004
1005    public boolean isDeperecated() {
1006        Iterator JavaDoc it = fElements.iterator();
1007        while (it.hasNext()) {
1008            Object JavaDoc next = it.next();
1009            if (next instanceof SchemaRootElement)
1010                return ((SchemaRootElement)next).isDeprecated();
1011        }
1012        return false;
1013    }
1014
1015    public String JavaDoc getDeprecatedSuggestion() {
1016        Iterator JavaDoc it = fElements.iterator();
1017        while (it.hasNext()) {
1018            Object JavaDoc next = it.next();
1019            if (next instanceof SchemaRootElement)
1020                return ((SchemaRootElement)next).getDeprecatedSuggestion();
1021        }
1022        return null;
1023    }
1024
1025}
1026
Popular Tags