KickJava   Java API By Example, From Geeks To Geeks.

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


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
15 import org.eclipse.pde.internal.core.ischema.ISchema;
16 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
17 import org.eclipse.pde.internal.core.ischema.ISchemaComplexType;
18 import org.eclipse.pde.internal.core.ischema.ISchemaCompositor;
19 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
20 import org.eclipse.pde.internal.core.ischema.ISchemaObject;
21 import org.eclipse.pde.internal.core.ischema.ISchemaRepeatable;
22 import org.eclipse.pde.internal.core.ischema.ISchemaType;
23 import org.eclipse.pde.internal.core.util.SchemaUtil;
24 import org.eclipse.pde.internal.core.util.XMLComponentRegistry;
25
26 public class SchemaElement extends RepeatableSchemaObject implements
27         ISchemaElement {
28
29     private static final long serialVersionUID = 1L;
30
31     public static final String JavaDoc P_ICON_NAME = "iconName"; //$NON-NLS-1$
32

33     public static final String JavaDoc P_LABEL_PROPERTY = "labelProperty"; //$NON-NLS-1$
34

35     public static final String JavaDoc P_TYPE = "type"; //$NON-NLS-1$
36

37     private String JavaDoc labelProperty;
38
39     private ISchemaType type;
40
41     private String JavaDoc iconName;
42
43     private boolean fTranslatable;
44
45     private boolean fDeprecated;
46
47     public SchemaElement(ISchemaObject parent, String JavaDoc name) {
48         super(parent, name);
49     }
50
51     private String JavaDoc calculateChildRepresentation(ISchemaObject object,
52             boolean addLinks) {
53         String JavaDoc child = ""; //$NON-NLS-1$
54
if (object instanceof ISchemaCompositor) {
55             child = calculateCompositorRepresentation(
56                     (ISchemaCompositor) object, addLinks);
57             if (!child.equals("EMPTY") && child.length() > 0) { //$NON-NLS-1$
58
child = "(" + child + ")"; //$NON-NLS-1$ //$NON-NLS-2$
59
}
60         } else {
61             child = object.getName();
62             if (addLinks) {
63                 child = "<a HREF=\"#e." + child + "\">" + child + "</a>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
64
}
65         }
66         int minOccurs = 1;
67         int maxOccurs = 1;
68         if (object instanceof ISchemaRepeatable) {
69             minOccurs = ((ISchemaRepeatable) object).getMinOccurs();
70             maxOccurs = ((ISchemaRepeatable) object).getMaxOccurs();
71         }
72         if (minOccurs == 0) {
73             if (maxOccurs == 1)
74                 child += "?"; //$NON-NLS-1$
75
else
76                 child += "*"; //$NON-NLS-1$
77
} else if (minOccurs == 1) {
78             if (maxOccurs > 1)
79                 child += "+"; //$NON-NLS-1$
80
}
81         return child;
82     }
83
84     private String JavaDoc calculateCompositorRepresentation(
85             ISchemaCompositor compositor, boolean addLinks) {
86         int kind = compositor.getKind();
87         ISchemaObject[] children = compositor.getChildren();
88         if (children.length == 0)
89             return "EMPTY"; //$NON-NLS-1$
90
String JavaDoc text = kind == ISchemaCompositor.GROUP ? "(" : ""; //$NON-NLS-1$ //$NON-NLS-2$
91
for (int i = 0; i < children.length; i++) {
92             ISchemaObject object = children[i];
93             String JavaDoc child = calculateChildRepresentation(object, addLinks);
94
95             text += child;
96             if (i < children.length - 1) {
97                 if (kind == ISchemaCompositor.SEQUENCE)
98                     text += " , "; //$NON-NLS-1$
99
else if (kind == ISchemaCompositor.CHOICE)
100                     text += " | "; //$NON-NLS-1$
101
}
102         }
103         if (kind == ISchemaCompositor.GROUP)
104             text += ")"; //$NON-NLS-1$
105
return text;
106     }
107
108     public ISchemaAttribute getAttribute(String JavaDoc name) {
109         if (type != null && type instanceof ISchemaComplexType) {
110             return ((ISchemaComplexType) type).getAttribute(name);
111         }
112         return null;
113     }
114
115     public int getAttributeCount() {
116         if (type != null && type instanceof ISchemaComplexType) {
117             return ((ISchemaComplexType) type).getAttributeCount();
118         }
119         return 0;
120     }
121
122     public ISchemaAttribute[] getAttributes() {
123         if (type != null && type instanceof ISchemaComplexType) {
124             return ((ISchemaComplexType) type).getAttributes();
125         }
126         return new ISchemaAttribute[0];
127     }
128
129     public String JavaDoc getDTDRepresentation(boolean addLinks) {
130         String JavaDoc text = ""; //$NON-NLS-1$
131
if (type == null)
132             text += "EMPTY"; //$NON-NLS-1$
133
else {
134             if (type instanceof ISchemaComplexType) {
135                 ISchemaComplexType complexType = (ISchemaComplexType) type;
136                 ISchemaCompositor compositor = complexType.getCompositor();
137                 if (compositor != null)
138                     text += calculateChildRepresentation(compositor, addLinks);
139                 else
140                     text += "EMPTY"; //$NON-NLS-1$
141

142             } else
143                 text += "(#PCDATA)"; //$NON-NLS-1$
144
}
145         if (text.length() > 0) {
146             if (!text.equals("EMPTY") && text.charAt(0) != '(') //$NON-NLS-1$
147
text = "(" + text + ")"; //$NON-NLS-1$ //$NON-NLS-2$
148
}
149         return text;
150     }
151
152     public String JavaDoc getIconProperty() {
153         return iconName;
154     }
155
156     public String JavaDoc getLabelProperty() {
157         return labelProperty;
158     }
159
160     public ISchemaType getType() {
161         return type;
162     }
163
164     public void setParent(ISchemaObject parent) {
165         super.setParent(parent);
166         if (type != null) {
167             type.setSchema(getSchema());
168             if (type instanceof ISchemaComplexType) {
169                 ISchemaComplexType ctype = (ISchemaComplexType) type;
170                 ISchemaCompositor comp = ctype.getCompositor();
171                 if (comp != null)
172                     comp.setParent(this);
173             }
174         }
175         if (getAttributeCount() > 0) {
176             ISchemaAttribute[] atts = getAttributes();
177             for (int i = 0; i < atts.length; i++) {
178                 ISchemaAttribute att = atts[i];
179                 att.setParent(this);
180             }
181         }
182     }
183
184     public void setIconProperty(String JavaDoc newIconName) {
185         String JavaDoc oldValue = iconName;
186         iconName = newIconName;
187         getSchema().fireModelObjectChanged(this, P_ICON_NAME, oldValue,
188                 iconName);
189     }
190     
191     public void setTranslatableProperty(boolean translatable) {
192         boolean oldValue = fTranslatable;
193         fTranslatable = translatable;
194         getSchema().fireModelObjectChanged(this, P_TRANSLATABLE,
195                 Boolean.valueOf(oldValue), Boolean.valueOf(translatable));
196     }
197     
198     public void setDeprecatedProperty(boolean deprecated) {
199         boolean oldValue = fDeprecated;
200         fDeprecated = deprecated;
201         getSchema().fireModelObjectChanged(this, P_DEPRECATED,
202                 Boolean.valueOf(oldValue), Boolean.valueOf(deprecated));
203     }
204
205     public void setLabelProperty(String JavaDoc labelProperty) {
206         String JavaDoc oldValue = this.labelProperty;
207         this.labelProperty = labelProperty;
208         getSchema().fireModelObjectChanged(this, P_LABEL_PROPERTY, oldValue,
209                 labelProperty);
210     }
211
212     public void setType(ISchemaType newType) {
213         Object JavaDoc oldValue = type;
214         type = newType;
215         getSchema().fireModelObjectChanged(this, P_TYPE, oldValue, type);
216     }
217
218     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
219         writer.print(indent + "<element name=\"" + getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
220
ISchemaType type = getType();
221         if (type instanceof SchemaSimpleType) {
222             writer.print(" type=\"" + type.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
223
}
224         
225         writer.println(">"); //$NON-NLS-1$
226
String JavaDoc indent2 = indent + Schema.INDENT;
227         String JavaDoc realDescription = getWritableDescription();
228         if (realDescription.length() == 0)
229             realDescription = null;
230         if (realDescription != null || iconName != null
231                 || labelProperty != null || isDeprecated() || hasTranslatableContent()) {
232             String JavaDoc indent3 = indent2 + Schema.INDENT;
233             String JavaDoc indent4 = indent3 + Schema.INDENT;
234             writer.println(indent2 + "<annotation>"); //$NON-NLS-1$
235
if (iconName != null || labelProperty != null || isDeprecated() || hasTranslatableContent()) {
236                 writer.println(indent3 + "<appInfo>"); //$NON-NLS-1$
237
writer.print(indent4 + "<meta.element"); //$NON-NLS-1$
238
if (labelProperty != null)
239                     writer.print(" labelAttribute=\"" + labelProperty + "\""); //$NON-NLS-1$ //$NON-NLS-2$
240
if (iconName != null)
241                     writer.print(" icon=\"" + iconName + "\""); //$NON-NLS-1$ //$NON-NLS-2$
242
if (hasTranslatableContent())
243                     writer.print(" translatable=\"true\""); //$NON-NLS-1$
244
if (isDeprecated())
245                     writer.print(" deprecated=\"true\""); //$NON-NLS-1$
246
String JavaDoc extendedProperties = getExtendedAttributes();
247                 if (extendedProperties != null)
248                     writer.print(extendedProperties);
249                 writer.println("/>"); //$NON-NLS-1$
250
writer.println(indent3 + "</appInfo>"); //$NON-NLS-1$
251
}
252             if (realDescription != null) {
253                 writer.println(indent3 + "<documentation>"); //$NON-NLS-1$
254
if (getDescription() != null)
255                     writer.println(indent4 + realDescription);
256                 writer.println(indent3 + "</documentation>"); //$NON-NLS-1$
257
}
258             writer.println(indent2 + "</annotation>"); //$NON-NLS-1$
259
}
260
261         if (type instanceof SchemaComplexType) {
262             SchemaComplexType complexType = (SchemaComplexType) type;
263             complexType.write(indent2, writer);
264         }
265         writer.println(indent + "</element>"); //$NON-NLS-1$
266
}
267
268     /*
269      * (non-Javadoc)
270      *
271      * @see org.eclipse.pde.internal.core.ischema.IMetaElement#isTranslatable()
272      */

273     public boolean hasTranslatableContent() {
274         return fTranslatable;
275     }
276
277     /* (non-Javadoc)
278      * @see org.eclipse.pde.internal.core.ischema.IMetaElement#isDeprecated()
279      */

280     public boolean isDeprecated() {
281         return fDeprecated;
282     }
283     
284     public String JavaDoc getExtendedAttributes() {
285         return null;
286     }
287     
288     public String JavaDoc getDescription() {
289         if (super.getDescription() != null) {
290             return super.getDescription();
291         }
292         ISchema schema = getSchema();
293         if ((schema == null) ||
294                 (schema.getURL() == null)) {
295             // This can happen when creating a new extension point schema
296
return null;
297         }
298         String JavaDoc hashkey = schema.getURL().hashCode() + "_" + getName(); //$NON-NLS-1$
299
String JavaDoc description =
300             XMLComponentRegistry.Instance().getDescription(
301                 hashkey, XMLComponentRegistry.F_ELEMENT_COMPONENT);
302         if (description == null) {
303             SchemaElementHandler handler =
304                 new SchemaElementHandler(getName());
305             SchemaUtil.parseURL(schema.getURL(), handler);
306             description = handler.getDescription();
307             XMLComponentRegistry.Instance().putDescription(hashkey, description,
308                     XMLComponentRegistry.F_ELEMENT_COMPONENT);
309         }
310         
311         return description;
312     }
313 }
314
Popular Tags