KickJava   Java API By Example, From Geeks To Geeks.

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


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.ISchemaElement;
18 import org.eclipse.pde.internal.core.ischema.ISchemaObject;
19 import org.eclipse.pde.internal.core.ischema.ISchemaSimpleType;
20 import org.eclipse.pde.internal.core.util.SchemaUtil;
21 import org.eclipse.pde.internal.core.util.XMLComponentRegistry;
22
23 public class SchemaAttribute extends SchemaObject implements ISchemaAttribute {
24
25     private static final long serialVersionUID = 1L;
26
27     private int kind = STRING;
28
29     private int use = OPTIONAL;
30
31     private String JavaDoc valueFilter;
32
33     private ISchemaSimpleType type;
34
35     private String JavaDoc basedOn;
36
37     private Object JavaDoc value;
38
39     public static final String JavaDoc P_USE = "useProperty"; //$NON-NLS-1$
40

41     public static final String JavaDoc P_VALUE_FILTER = "valueFilterProperty"; //$NON-NLS-1$
42

43     public static final String JavaDoc P_VALUE = "value"; //$NON-NLS-1$
44

45     public static final String JavaDoc P_KIND = "kindProperty"; //$NON-NLS-1$
46

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

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

51     private boolean fTranslatable;
52
53     private boolean fDeprecated;
54
55     public SchemaAttribute(ISchemaAttribute att, String JavaDoc newName) {
56         super(att.getParent(), newName);
57         kind = att.getKind();
58         use = att.getUse();
59         value = att.getValue();
60         type = new SchemaSimpleType(att.getType());
61         basedOn = att.getBasedOn();
62     }
63
64     public SchemaAttribute(ISchemaObject parent, String JavaDoc name) {
65         super(parent, name);
66     }
67
68     public String JavaDoc getBasedOn() {
69         return getKind() == JAVA ? basedOn : null;
70     }
71
72     public int getKind() {
73         return kind;
74     }
75
76     public ISchemaSimpleType getType() {
77         return type;
78     }
79
80     public int getUse() {
81         return use;
82     }
83
84     public Object JavaDoc getValue() {
85         return value;
86     }
87
88     public String JavaDoc getValueFilter() {
89         return valueFilter;
90     }
91
92     public void setBasedOn(String JavaDoc newBasedOn) {
93         String JavaDoc oldValue = basedOn;
94         basedOn = newBasedOn;
95         getSchema().fireModelObjectChanged(this, P_BASED_ON, oldValue, basedOn);
96     }
97
98     public void setKind(int newKind) {
99         Integer JavaDoc oldValue = new Integer JavaDoc(kind);
100         kind = newKind;
101         getSchema().fireModelObjectChanged(this, P_KIND, oldValue,
102                 new Integer JavaDoc(kind));
103     }
104     
105     public void setTranslatableProperty(boolean translatable) {
106         boolean oldValue = fTranslatable;
107         fTranslatable = translatable;
108         getSchema().fireModelObjectChanged(this, P_TRANSLATABLE,
109                 Boolean.valueOf(oldValue), Boolean.valueOf(translatable));
110     }
111     
112     public void setDeprecatedProperty(boolean deprecated) {
113         boolean oldValue = fDeprecated;
114         fDeprecated = deprecated;
115         getSchema().fireModelObjectChanged(this, P_DEPRECATED,
116                 Boolean.valueOf(oldValue), Boolean.valueOf(deprecated));
117     }
118
119     public void setType(ISchemaSimpleType newType) {
120         Object JavaDoc oldValue = type;
121         type = newType;
122         getSchema().fireModelObjectChanged(this, P_TYPE, oldValue, type);
123     }
124
125     public void setParent(ISchemaObject obj) {
126         super.setParent(obj);
127         if (type != null)
128             type.setSchema(getSchema());
129     }
130
131     public void setUse(int newUse) {
132         Integer JavaDoc oldValue = new Integer JavaDoc(use);
133         use = newUse;
134         getSchema().fireModelObjectChanged(this, P_USE, oldValue,
135                 new Integer JavaDoc(use));
136     }
137
138     public void setValue(String JavaDoc value) {
139         String JavaDoc oldValue = (String JavaDoc) this.value;
140         this.value = value;
141         getSchema().fireModelObjectChanged(this, P_VALUE, oldValue, value);
142     }
143
144     public void setValueFilter(String JavaDoc valueFilter) {
145         String JavaDoc oldValue = this.valueFilter;
146         this.valueFilter = valueFilter;
147         getSchema().fireModelObjectChanged(this, P_VALUE_FILTER, oldValue,
148                 valueFilter);
149     }
150
151     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
152         boolean annotation = false;
153         ISchemaSimpleType type = getType();
154         String JavaDoc typeName = type.getName();
155         writer.print(indent);
156         writer.print("<attribute name=\"" + getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
157
if (type.getRestriction() == null)
158             writer.print(" type=\"" + typeName + "\""); //$NON-NLS-1$ //$NON-NLS-2$
159
String JavaDoc useString = null;
160         switch (getUse()) {
161         case OPTIONAL:
162             // don't write default setting
163
// useString="optional";
164
break;
165         case DEFAULT:
166             useString = "default"; //$NON-NLS-1$
167
break;
168         case REQUIRED:
169             useString = "required"; //$NON-NLS-1$
170
break;
171         }
172         if (useString != null) {
173             writer.print(" use=\"" + useString + "\""); //$NON-NLS-1$ //$NON-NLS-2$
174
}
175         if (value != null && getUse() == DEFAULT) {
176             writer.print(" value=\"" + value + "\""); //$NON-NLS-1$ //$NON-NLS-2$
177
}
178         String JavaDoc documentation = getWritableDescription();
179         if (documentation != null || this.getBasedOn() != null
180                 || getKind() != STRING) {
181             // Add annotation
182
annotation = true;
183             writer.println(">"); //$NON-NLS-1$
184
String JavaDoc annIndent = indent + Schema.INDENT;
185             String JavaDoc indent2 = annIndent + Schema.INDENT;
186             String JavaDoc indent3 = indent2 + Schema.INDENT;
187             writer.print(annIndent);
188             writer.println("<annotation>"); //$NON-NLS-1$
189
if (documentation != null) {
190                 writer.println(indent2 + "<documentation>"); //$NON-NLS-1$
191
writer.println(indent3 + documentation);
192                 writer.println(indent2 + "</documentation>"); //$NON-NLS-1$
193
}
194             if (getBasedOn() != null || getKind() != STRING || isDeprecated() || isTranslatable()) {
195                 writer.println(indent2 + "<appInfo>"); //$NON-NLS-1$
196
writer.print(indent3 + "<meta.attribute"); //$NON-NLS-1$
197
String JavaDoc kindValue = null;
198                 switch (getKind()) {
199                 case JAVA:
200                     kindValue = "java"; //$NON-NLS-1$
201
break;
202                 case RESOURCE:
203                     kindValue = "resource"; //$NON-NLS-1$
204
break;
205                 }
206                 if (kindValue != null)
207                     writer.print(" kind=\"" + kindValue + "\""); //$NON-NLS-1$ //$NON-NLS-2$
208
if (getBasedOn() != null)
209                     writer.print(" basedOn=\"" + getBasedOn() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
210
if (isTranslatable())
211                     writer.print(" translatable=\"true\""); //$NON-NLS-1$
212
if (isDeprecated())
213                     writer.print(" deprecated=\"true\""); //$NON-NLS-1$
214
writer.println("/>"); //$NON-NLS-1$
215
writer.println(indent2 + "</appInfo>"); //$NON-NLS-1$
216
}
217             writer.println(annIndent + "</annotation>"); //$NON-NLS-1$
218
}
219         if (type.getRestriction() != null) {
220             type.write(indent + Schema.INDENT, writer);
221         }
222         if (annotation || type.getRestriction() != null) {
223             writer.println(indent + "</attribute>"); //$NON-NLS-1$
224
} else {
225             writer.println("/>"); //$NON-NLS-1$
226
}
227     }
228
229     /*
230      * (non-Javadoc)
231      *
232      * @see org.eclipse.pde.internal.core.ischema.ISchemaAttribute#isTranslatable()
233      */

234     public boolean isTranslatable() {
235         if (getKind() == STRING && fTranslatable)
236             return type == null || "string".equals(type.getName()); //$NON-NLS-1$
237
return false;
238     }
239
240     /*
241      * (non-Javadoc)
242      *
243      * @see org.eclipse.pde.internal.core.ischema.IMetaAttribute#isDeprecated()
244      */

245     public boolean isDeprecated() {
246         return fDeprecated;
247     }
248     
249     public String JavaDoc getDescription() {
250         if (super.getDescription() != null) {
251             return super.getDescription();
252         }
253         ISchema schema = getSchema();
254         if ((schema == null) ||
255                 (schema.getURL() == null)) {
256             return null;
257         }
258         String JavaDoc elementName = null;
259         if (getParent() instanceof ISchemaElement) {
260             elementName = ((ISchemaElement)getParent()).getName();
261             if (elementName == null) {
262                 return null;
263             }
264         }
265         String JavaDoc hashkey = schema.getURL().hashCode() + "_" + elementName + "_" + getName(); //$NON-NLS-1$ //$NON-NLS-2$
266
String JavaDoc description =
267             XMLComponentRegistry.Instance().getDescription(
268                 hashkey, XMLComponentRegistry.F_ATTRIBUTE_COMPONENT);
269         if (description == null) {
270             SchemaAttributeHandler handler =
271                 new SchemaAttributeHandler(elementName, getName());
272             SchemaUtil.parseURL(schema.getURL(), handler);
273             description = handler.getDescription();
274             XMLComponentRegistry.Instance().putDescription(hashkey, description,
275                     XMLComponentRegistry.F_ATTRIBUTE_COMPONENT);
276         }
277         
278         return description;
279     }
280     
281 }
282
Popular Tags