001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.schema;
011:
012: import java.io.PrintWriter;
013:
014: import org.eclipse.pde.internal.core.ischema.ISchema;
015: import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
016: import org.eclipse.pde.internal.core.ischema.ISchemaElement;
017: import org.eclipse.pde.internal.core.ischema.ISchemaObject;
018: import org.eclipse.pde.internal.core.ischema.ISchemaSimpleType;
019: import org.eclipse.pde.internal.core.util.SchemaUtil;
020: import org.eclipse.pde.internal.core.util.XMLComponentRegistry;
021:
022: public class SchemaAttribute extends SchemaObject implements
023: ISchemaAttribute {
024:
025: private static final long serialVersionUID = 1L;
026:
027: private int kind = STRING;
028:
029: private int use = OPTIONAL;
030:
031: private String valueFilter;
032:
033: private ISchemaSimpleType type;
034:
035: private String basedOn;
036:
037: private Object value;
038:
039: public static final String P_USE = "useProperty"; //$NON-NLS-1$
040:
041: public static final String P_VALUE_FILTER = "valueFilterProperty"; //$NON-NLS-1$
042:
043: public static final String P_VALUE = "value"; //$NON-NLS-1$
044:
045: public static final String P_KIND = "kindProperty"; //$NON-NLS-1$
046:
047: public static final String P_TYPE = "typeProperty"; //$NON-NLS-1$
048:
049: public static final String P_BASED_ON = "basedOnProperty"; //$NON-NLS-1$
050:
051: private boolean fTranslatable;
052:
053: private boolean fDeprecated;
054:
055: public SchemaAttribute(ISchemaAttribute att, String newName) {
056: super (att.getParent(), newName);
057: kind = att.getKind();
058: use = att.getUse();
059: value = att.getValue();
060: type = new SchemaSimpleType(att.getType());
061: basedOn = att.getBasedOn();
062: }
063:
064: public SchemaAttribute(ISchemaObject parent, String name) {
065: super (parent, name);
066: }
067:
068: public String getBasedOn() {
069: return getKind() == JAVA ? basedOn : null;
070: }
071:
072: public int getKind() {
073: return kind;
074: }
075:
076: public ISchemaSimpleType getType() {
077: return type;
078: }
079:
080: public int getUse() {
081: return use;
082: }
083:
084: public Object getValue() {
085: return value;
086: }
087:
088: public String getValueFilter() {
089: return valueFilter;
090: }
091:
092: public void setBasedOn(String newBasedOn) {
093: String oldValue = basedOn;
094: basedOn = newBasedOn;
095: getSchema().fireModelObjectChanged(this , P_BASED_ON, oldValue,
096: basedOn);
097: }
098:
099: public void setKind(int newKind) {
100: Integer oldValue = new Integer(kind);
101: kind = newKind;
102: getSchema().fireModelObjectChanged(this , P_KIND, oldValue,
103: new Integer(kind));
104: }
105:
106: public void setTranslatableProperty(boolean translatable) {
107: boolean oldValue = fTranslatable;
108: fTranslatable = translatable;
109: getSchema().fireModelObjectChanged(this , P_TRANSLATABLE,
110: Boolean.valueOf(oldValue),
111: Boolean.valueOf(translatable));
112: }
113:
114: public void setDeprecatedProperty(boolean deprecated) {
115: boolean oldValue = fDeprecated;
116: fDeprecated = deprecated;
117: getSchema().fireModelObjectChanged(this , P_DEPRECATED,
118: Boolean.valueOf(oldValue), Boolean.valueOf(deprecated));
119: }
120:
121: public void setType(ISchemaSimpleType newType) {
122: Object oldValue = type;
123: type = newType;
124: getSchema()
125: .fireModelObjectChanged(this , P_TYPE, oldValue, type);
126: }
127:
128: public void setParent(ISchemaObject obj) {
129: super .setParent(obj);
130: if (type != null)
131: type.setSchema(getSchema());
132: }
133:
134: public void setUse(int newUse) {
135: Integer oldValue = new Integer(use);
136: use = newUse;
137: getSchema().fireModelObjectChanged(this , P_USE, oldValue,
138: new Integer(use));
139: }
140:
141: public void setValue(String value) {
142: String oldValue = (String) this .value;
143: this .value = value;
144: getSchema().fireModelObjectChanged(this , P_VALUE, oldValue,
145: value);
146: }
147:
148: public void setValueFilter(String valueFilter) {
149: String oldValue = this .valueFilter;
150: this .valueFilter = valueFilter;
151: getSchema().fireModelObjectChanged(this , P_VALUE_FILTER,
152: oldValue, valueFilter);
153: }
154:
155: public void write(String indent, PrintWriter writer) {
156: boolean annotation = false;
157: ISchemaSimpleType type = getType();
158: String typeName = type.getName();
159: writer.print(indent);
160: writer.print("<attribute name=\"" + getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
161: if (type.getRestriction() == null)
162: writer.print(" type=\"" + typeName + "\""); //$NON-NLS-1$ //$NON-NLS-2$
163: String useString = null;
164: switch (getUse()) {
165: case OPTIONAL:
166: // don't write default setting
167: // useString="optional";
168: break;
169: case DEFAULT:
170: useString = "default"; //$NON-NLS-1$
171: break;
172: case REQUIRED:
173: useString = "required"; //$NON-NLS-1$
174: break;
175: }
176: if (useString != null) {
177: writer.print(" use=\"" + useString + "\""); //$NON-NLS-1$ //$NON-NLS-2$
178: }
179: if (value != null && getUse() == DEFAULT) {
180: writer.print(" value=\"" + value + "\""); //$NON-NLS-1$ //$NON-NLS-2$
181: }
182: String documentation = getWritableDescription();
183: if (documentation != null || this .getBasedOn() != null
184: || getKind() != STRING) {
185: // Add annotation
186: annotation = true;
187: writer.println(">"); //$NON-NLS-1$
188: String annIndent = indent + Schema.INDENT;
189: String indent2 = annIndent + Schema.INDENT;
190: String indent3 = indent2 + Schema.INDENT;
191: writer.print(annIndent);
192: writer.println("<annotation>"); //$NON-NLS-1$
193: if (documentation != null) {
194: writer.println(indent2 + "<documentation>"); //$NON-NLS-1$
195: writer.println(indent3 + documentation);
196: writer.println(indent2 + "</documentation>"); //$NON-NLS-1$
197: }
198: if (getBasedOn() != null || getKind() != STRING
199: || isDeprecated() || isTranslatable()) {
200: writer.println(indent2 + "<appInfo>"); //$NON-NLS-1$
201: writer.print(indent3 + "<meta.attribute"); //$NON-NLS-1$
202: String kindValue = null;
203: switch (getKind()) {
204: case JAVA:
205: kindValue = "java"; //$NON-NLS-1$
206: break;
207: case RESOURCE:
208: kindValue = "resource"; //$NON-NLS-1$
209: break;
210: }
211: if (kindValue != null)
212: writer.print(" kind=\"" + kindValue + "\""); //$NON-NLS-1$ //$NON-NLS-2$
213: if (getBasedOn() != null)
214: writer.print(" basedOn=\"" + getBasedOn() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
215: if (isTranslatable())
216: writer.print(" translatable=\"true\""); //$NON-NLS-1$
217: if (isDeprecated())
218: writer.print(" deprecated=\"true\""); //$NON-NLS-1$
219: writer.println("/>"); //$NON-NLS-1$
220: writer.println(indent2 + "</appInfo>"); //$NON-NLS-1$
221: }
222: writer.println(annIndent + "</annotation>"); //$NON-NLS-1$
223: }
224: if (type.getRestriction() != null) {
225: type.write(indent + Schema.INDENT, writer);
226: }
227: if (annotation || type.getRestriction() != null) {
228: writer.println(indent + "</attribute>"); //$NON-NLS-1$
229: } else {
230: writer.println("/>"); //$NON-NLS-1$
231: }
232: }
233:
234: /*
235: * (non-Javadoc)
236: *
237: * @see org.eclipse.pde.internal.core.ischema.ISchemaAttribute#isTranslatable()
238: */
239: public boolean isTranslatable() {
240: if (getKind() == STRING && fTranslatable)
241: return type == null || "string".equals(type.getName()); //$NON-NLS-1$
242: return false;
243: }
244:
245: /*
246: * (non-Javadoc)
247: *
248: * @see org.eclipse.pde.internal.core.ischema.IMetaAttribute#isDeprecated()
249: */
250: public boolean isDeprecated() {
251: return fDeprecated;
252: }
253:
254: public String getDescription() {
255: if (super .getDescription() != null) {
256: return super .getDescription();
257: }
258: ISchema schema = getSchema();
259: if ((schema == null) || (schema.getURL() == null)) {
260: return null;
261: }
262: String elementName = null;
263: if (getParent() instanceof ISchemaElement) {
264: elementName = ((ISchemaElement) getParent()).getName();
265: if (elementName == null) {
266: return null;
267: }
268: }
269: String hashkey = schema.getURL().hashCode()
270: + "_" + elementName + "_" + getName(); //$NON-NLS-1$ //$NON-NLS-2$
271: String description = XMLComponentRegistry.Instance()
272: .getDescription(hashkey,
273: XMLComponentRegistry.F_ATTRIBUTE_COMPONENT);
274: if (description == null) {
275: SchemaAttributeHandler handler = new SchemaAttributeHandler(
276: elementName, getName());
277: SchemaUtil.parseURL(schema.getURL(), handler);
278: description = handler.getDescription();
279: XMLComponentRegistry.Instance().putDescription(hashkey,
280: description,
281: XMLComponentRegistry.F_ATTRIBUTE_COMPONENT);
282: }
283:
284: return description;
285: }
286:
287: }
|