KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.PrintWriter JavaDoc;
14 import java.io.Serializable JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 import org.eclipse.core.runtime.PlatformObject;
19 import org.eclipse.pde.core.ISourceObject;
20 import org.eclipse.pde.internal.core.PDECoreMessages;
21 import org.eclipse.pde.internal.core.ischema.IMetaElement;
22 import org.eclipse.pde.internal.core.ischema.ISchema;
23 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
24 import org.eclipse.pde.internal.core.ischema.ISchemaCompositor;
25 import org.eclipse.pde.internal.core.ischema.ISchemaDescriptor;
26 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
27 import org.eclipse.pde.internal.core.ischema.ISchemaObject;
28 import org.eclipse.pde.internal.core.ischema.ISchemaObjectReference;
29 import org.eclipse.pde.internal.core.ischema.ISchemaType;
30 import org.w3c.dom.Comment JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
33 public class SchemaElementReference extends PlatformObject implements
34         ISchemaElement, IMetaElement, ISchemaObjectReference, ISourceObject,
35         Serializable JavaDoc {
36
37     private static final long serialVersionUID = 1L;
38
39     private ISchemaElement element;
40
41     private ISchemaCompositor compositor;
42
43     private String JavaDoc referenceName;
44
45     public static final String JavaDoc P_MAX_OCCURS = "max_occurs"; //$NON-NLS-1$
46

47     public static final String JavaDoc P_MIN_OCCURS = "min_occurs"; //$NON-NLS-1$
48

49     public static final String JavaDoc P_REFERENCE_NAME = "reference_name"; //$NON-NLS-1$
50

51     private int minOccurs = 1;
52
53     private int maxOccurs = 1;
54
55     private Vector JavaDoc comments;
56
57     private int[] range;
58
59     public SchemaElementReference(ISchemaCompositor compositor, String JavaDoc ref) {
60         referenceName = ref;
61         this.compositor = compositor;
62     }
63
64     public ISchemaAttribute getAttribute(String JavaDoc name) {
65         if (element == null)
66             return null;
67         return element.getAttribute(name);
68     }
69
70     public int getAttributeCount() {
71         if (element == null)
72             return 0;
73         return element.getAttributeCount();
74     }
75
76     public ISchemaAttribute[] getAttributes() {
77         if (element == null)
78             return new ISchemaAttribute[0];
79         return element.getAttributes();
80     }
81
82     public ISchemaCompositor getCompositor() {
83         return compositor;
84     }
85
86     public String JavaDoc getDescription() {
87         if (element == null)
88             return ""; //$NON-NLS-1$
89
return element.getDescription();
90     }
91
92     public String JavaDoc getDTDRepresentation(boolean addLinks) {
93         if (element == null)
94             return PDECoreMessages.SchemaElementReference_refElementMissing;
95         return element.getDTDRepresentation(addLinks);
96     }
97
98     public String JavaDoc getIconProperty() {
99         if (element == null)
100             return ""; //$NON-NLS-1$
101
return element.getIconProperty();
102     }
103
104     public String JavaDoc getLabelProperty() {
105         if (element == null)
106             return ""; //$NON-NLS-1$
107
return element.getLabelProperty();
108     }
109
110     public int getMaxOccurs() {
111         return maxOccurs;
112     }
113
114     public int getMinOccurs() {
115         return minOccurs;
116     }
117
118     public String JavaDoc getName() {
119         return referenceName;
120     }
121
122     public ISchemaObject getParent() {
123         return compositor;
124     }
125
126     public void setParent(ISchemaObject parent) {
127     }
128
129     public ISchemaElement getReferencedElement() {
130         return element;
131     }
132
133     public ISchemaObject getReferencedObject() {
134         return element;
135     }
136
137     public Class JavaDoc getReferencedObjectClass() {
138         return ISchemaElement.class;
139     }
140
141     public String JavaDoc getReferenceName() {
142         return referenceName;
143     }
144
145     public ISchema getSchema() {
146         if (element != null) {
147             ISchema schema = element.getSchema();
148             if (schema != null) {
149                 ISchemaDescriptor desc = schema.getSchemaDescriptor();
150                 if (!(desc instanceof IncludedSchemaDescriptor))
151                     return schema;
152             }
153         }
154         return getCompositorsSchema();
155     }
156
157     public ISchema getCompositorsSchema() {
158         if (compositor != null)
159             return compositor.getSchema();
160         return null;
161     }
162     
163     public ISchemaType getType() {
164         if (element == null)
165             return null;
166         return element.getType();
167     }
168
169     public boolean isLinked() {
170         return getReferencedObject() != null;
171     }
172
173     public void setCompositor(ISchemaCompositor newCompositor) {
174         compositor = newCompositor;
175     }
176
177     public void setMaxOccurs(int newMaxOccurs) {
178         Integer JavaDoc oldValue = new Integer JavaDoc(maxOccurs);
179         maxOccurs = newMaxOccurs;
180         ISchema schema = getCompositorsSchema();
181         if (schema != null)
182             schema.fireModelObjectChanged(this, P_MAX_OCCURS, oldValue,
183                     new Integer JavaDoc(maxOccurs));
184     }
185
186     public void setMinOccurs(int newMinOccurs) {
187         Integer JavaDoc oldValue = new Integer JavaDoc(minOccurs);
188         minOccurs = newMinOccurs;
189         ISchema schema = getCompositorsSchema();
190         if (schema != null)
191             schema.fireModelObjectChanged(this, P_MIN_OCCURS, oldValue,
192                     new Integer JavaDoc(minOccurs));
193     }
194
195     public void setReferencedObject(ISchemaObject referencedObject) {
196         if (referencedObject instanceof ISchemaElement)
197             element = (ISchemaElement) referencedObject;
198         else
199             element = null;
200     }
201
202     public void setReferenceName(String JavaDoc name) {
203         String JavaDoc oldValue = this.referenceName;
204         this.referenceName = name;
205         ISchema schema = getCompositorsSchema();
206         if (schema != null)
207             schema.fireModelObjectChanged(this, P_REFERENCE_NAME, oldValue,
208                     name);
209     }
210
211     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
212         writeComments(writer);
213         writer.print(indent + "<element"); //$NON-NLS-1$
214
writer.print(" ref=\"" + getReferenceName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
215
if (getMinOccurs() != 1 || getMaxOccurs() != 1) {
216             String JavaDoc min = "" + getMinOccurs(); //$NON-NLS-1$
217
String JavaDoc max = getMaxOccurs() == Integer.MAX_VALUE ? "unbounded" //$NON-NLS-1$
218
: ("" + getMaxOccurs()); //$NON-NLS-1$
219
writer
220                     .print(" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
221
}
222         writer.println("/>"); //$NON-NLS-1$
223
}
224
225     public void addComments(Node JavaDoc node) {
226         comments = addComments(node, comments);
227     }
228
229     public Vector JavaDoc addComments(Node JavaDoc node, Vector JavaDoc result) {
230         for (Node JavaDoc prev = node.getPreviousSibling(); prev != null; prev = prev
231                 .getPreviousSibling()) {
232             if (prev.getNodeType() == Node.TEXT_NODE)
233                 continue;
234             if (prev instanceof Comment JavaDoc) {
235                 String JavaDoc comment = prev.getNodeValue();
236                 if (result == null)
237                     result = new Vector JavaDoc();
238                 result.add(comment);
239             } else
240                 break;
241         }
242         return result;
243     }
244
245     void writeComments(PrintWriter JavaDoc writer) {
246         writeComments(writer, comments);
247     }
248
249     void writeComments(PrintWriter JavaDoc writer, Vector JavaDoc source) {
250         if (source == null)
251             return;
252         for (int i = 0; i < source.size(); i++) {
253             String JavaDoc comment = (String JavaDoc) source.elementAt(i);
254             writer.println("<!--" + comment + "-->"); //$NON-NLS-1$ //$NON-NLS-2$
255
}
256     }
257
258     public int getStartLine() {
259         return range == null ? -1 : range[0];
260     }
261
262     public int getStopLine() {
263         return range == null ? -1 : range[1];
264     }
265
266     void bindSourceLocation(Node JavaDoc node, Hashtable JavaDoc lineTable) {
267         if (lineTable == null)
268             return;
269         Integer JavaDoc[] data = (Integer JavaDoc[]) lineTable.get(node);
270         if (data != null) {
271             range = new int[] { data[0].intValue(), data[1].intValue() };
272         }
273     }
274
275     /*
276      * (non-Javadoc)
277      *
278      * @see org.eclipse.pde.internal.core.ischema.IMetaElement#isTranslatable()
279      */

280     public boolean hasTranslatableContent() {
281         return false;
282     }
283
284     /* (non-Javadoc)
285      * @see org.eclipse.pde.internal.core.ischema.IMetaElement#isDeprecated()
286      */

287     public boolean isDeprecated() {
288         return false;
289     }
290 }
291
Popular Tags