1 11 package org.eclipse.pde.internal.core.schema; 12 13 import java.io.PrintWriter ; 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 valueFilter; 32 33 private ISchemaSimpleType type; 34 35 private String basedOn; 36 37 private Object value; 38 39 public static final String P_USE = "useProperty"; 41 public static final String P_VALUE_FILTER = "valueFilterProperty"; 43 public static final String P_VALUE = "value"; 45 public static final String P_KIND = "kindProperty"; 47 public static final String P_TYPE = "typeProperty"; 49 public static final String P_BASED_ON = "basedOnProperty"; 51 private boolean fTranslatable; 52 53 private boolean fDeprecated; 54 55 public SchemaAttribute(ISchemaAttribute att, String 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 name) { 65 super(parent, name); 66 } 67 68 public String 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 getValue() { 85 return value; 86 } 87 88 public String getValueFilter() { 89 return valueFilter; 90 } 91 92 public void setBasedOn(String newBasedOn) { 93 String oldValue = basedOn; 94 basedOn = newBasedOn; 95 getSchema().fireModelObjectChanged(this, P_BASED_ON, oldValue, basedOn); 96 } 97 98 public void setKind(int newKind) { 99 Integer oldValue = new Integer (kind); 100 kind = newKind; 101 getSchema().fireModelObjectChanged(this, P_KIND, oldValue, 102 new Integer (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 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 oldValue = new Integer (use); 133 use = newUse; 134 getSchema().fireModelObjectChanged(this, P_USE, oldValue, 135 new Integer (use)); 136 } 137 138 public void setValue(String value) { 139 String oldValue = (String ) this.value; 140 this.value = value; 141 getSchema().fireModelObjectChanged(this, P_VALUE, oldValue, value); 142 } 143 144 public void setValueFilter(String valueFilter) { 145 String oldValue = this.valueFilter; 146 this.valueFilter = valueFilter; 147 getSchema().fireModelObjectChanged(this, P_VALUE_FILTER, oldValue, 148 valueFilter); 149 } 150 151 public void write(String indent, PrintWriter writer) { 152 boolean annotation = false; 153 ISchemaSimpleType type = getType(); 154 String typeName = type.getName(); 155 writer.print(indent); 156 writer.print("<attribute name=\"" + getName() + "\""); if (type.getRestriction() == null) 158 writer.print(" type=\"" + typeName + "\""); String useString = null; 160 switch (getUse()) { 161 case OPTIONAL: 162 break; 165 case DEFAULT: 166 useString = "default"; break; 168 case REQUIRED: 169 useString = "required"; break; 171 } 172 if (useString != null) { 173 writer.print(" use=\"" + useString + "\""); } 175 if (value != null && getUse() == DEFAULT) { 176 writer.print(" value=\"" + value + "\""); } 178 String documentation = getWritableDescription(); 179 if (documentation != null || this.getBasedOn() != null 180 || getKind() != STRING) { 181 annotation = true; 183 writer.println(">"); String annIndent = indent + Schema.INDENT; 185 String indent2 = annIndent + Schema.INDENT; 186 String indent3 = indent2 + Schema.INDENT; 187 writer.print(annIndent); 188 writer.println("<annotation>"); if (documentation != null) { 190 writer.println(indent2 + "<documentation>"); writer.println(indent3 + documentation); 192 writer.println(indent2 + "</documentation>"); } 194 if (getBasedOn() != null || getKind() != STRING || isDeprecated() || isTranslatable()) { 195 writer.println(indent2 + "<appInfo>"); writer.print(indent3 + "<meta.attribute"); String kindValue = null; 198 switch (getKind()) { 199 case JAVA: 200 kindValue = "java"; break; 202 case RESOURCE: 203 kindValue = "resource"; break; 205 } 206 if (kindValue != null) 207 writer.print(" kind=\"" + kindValue + "\""); if (getBasedOn() != null) 209 writer.print(" basedOn=\"" + getBasedOn() + "\""); if (isTranslatable()) 211 writer.print(" translatable=\"true\""); if (isDeprecated()) 213 writer.print(" deprecated=\"true\""); writer.println("/>"); writer.println(indent2 + "</appInfo>"); } 217 writer.println(annIndent + "</annotation>"); } 219 if (type.getRestriction() != null) { 220 type.write(indent + Schema.INDENT, writer); 221 } 222 if (annotation || type.getRestriction() != null) { 223 writer.println(indent + "</attribute>"); } else { 225 writer.println("/>"); } 227 } 228 229 234 public boolean isTranslatable() { 235 if (getKind() == STRING && fTranslatable) 236 return type == null || "string".equals(type.getName()); return false; 238 } 239 240 245 public boolean isDeprecated() { 246 return fDeprecated; 247 } 248 249 public String 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 elementName = null; 259 if (getParent() instanceof ISchemaElement) { 260 elementName = ((ISchemaElement)getParent()).getName(); 261 if (elementName == null) { 262 return null; 263 } 264 } 265 String hashkey = schema.getURL().hashCode() + "_" + elementName + "_" + getName(); String 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 |