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.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 P_ICON_NAME = "iconName"; 33 public static final String P_LABEL_PROPERTY = "labelProperty"; 35 public static final String P_TYPE = "type"; 37 private String labelProperty; 38 39 private ISchemaType type; 40 41 private String iconName; 42 43 private boolean fTranslatable; 44 45 private boolean fDeprecated; 46 47 public SchemaElement(ISchemaObject parent, String name) { 48 super(parent, name); 49 } 50 51 private String calculateChildRepresentation(ISchemaObject object, 52 boolean addLinks) { 53 String child = ""; if (object instanceof ISchemaCompositor) { 55 child = calculateCompositorRepresentation( 56 (ISchemaCompositor) object, addLinks); 57 if (!child.equals("EMPTY") && child.length() > 0) { child = "(" + child + ")"; } 60 } else { 61 child = object.getName(); 62 if (addLinks) { 63 child = "<a HREF=\"#e." + child + "\">" + child + "</a>"; } 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 += "?"; else 76 child += "*"; } else if (minOccurs == 1) { 78 if (maxOccurs > 1) 79 child += "+"; } 81 return child; 82 } 83 84 private String calculateCompositorRepresentation( 85 ISchemaCompositor compositor, boolean addLinks) { 86 int kind = compositor.getKind(); 87 ISchemaObject[] children = compositor.getChildren(); 88 if (children.length == 0) 89 return "EMPTY"; String text = kind == ISchemaCompositor.GROUP ? "(" : ""; for (int i = 0; i < children.length; i++) { 92 ISchemaObject object = children[i]; 93 String child = calculateChildRepresentation(object, addLinks); 94 95 text += child; 96 if (i < children.length - 1) { 97 if (kind == ISchemaCompositor.SEQUENCE) 98 text += " , "; else if (kind == ISchemaCompositor.CHOICE) 100 text += " | "; } 102 } 103 if (kind == ISchemaCompositor.GROUP) 104 text += ")"; return text; 106 } 107 108 public ISchemaAttribute getAttribute(String 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 getDTDRepresentation(boolean addLinks) { 130 String text = ""; if (type == null) 132 text += "EMPTY"; 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"; 142 } else 143 text += "(#PCDATA)"; } 145 if (text.length() > 0) { 146 if (!text.equals("EMPTY") && text.charAt(0) != '(') text = "(" + text + ")"; } 149 return text; 150 } 151 152 public String getIconProperty() { 153 return iconName; 154 } 155 156 public String 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 newIconName) { 185 String 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 labelProperty) { 206 String 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 oldValue = type; 214 type = newType; 215 getSchema().fireModelObjectChanged(this, P_TYPE, oldValue, type); 216 } 217 218 public void write(String indent, PrintWriter writer) { 219 writer.print(indent + "<element name=\"" + getName() + "\""); ISchemaType type = getType(); 221 if (type instanceof SchemaSimpleType) { 222 writer.print(" type=\"" + type.getName() + "\""); } 224 225 writer.println(">"); String indent2 = indent + Schema.INDENT; 227 String realDescription = getWritableDescription(); 228 if (realDescription.length() == 0) 229 realDescription = null; 230 if (realDescription != null || iconName != null 231 || labelProperty != null || isDeprecated() || hasTranslatableContent()) { 232 String indent3 = indent2 + Schema.INDENT; 233 String indent4 = indent3 + Schema.INDENT; 234 writer.println(indent2 + "<annotation>"); if (iconName != null || labelProperty != null || isDeprecated() || hasTranslatableContent()) { 236 writer.println(indent3 + "<appInfo>"); writer.print(indent4 + "<meta.element"); if (labelProperty != null) 239 writer.print(" labelAttribute=\"" + labelProperty + "\""); if (iconName != null) 241 writer.print(" icon=\"" + iconName + "\""); if (hasTranslatableContent()) 243 writer.print(" translatable=\"true\""); if (isDeprecated()) 245 writer.print(" deprecated=\"true\""); String extendedProperties = getExtendedAttributes(); 247 if (extendedProperties != null) 248 writer.print(extendedProperties); 249 writer.println("/>"); writer.println(indent3 + "</appInfo>"); } 252 if (realDescription != null) { 253 writer.println(indent3 + "<documentation>"); if (getDescription() != null) 255 writer.println(indent4 + realDescription); 256 writer.println(indent3 + "</documentation>"); } 258 writer.println(indent2 + "</annotation>"); } 260 261 if (type instanceof SchemaComplexType) { 262 SchemaComplexType complexType = (SchemaComplexType) type; 263 complexType.write(indent2, writer); 264 } 265 writer.println(indent + "</element>"); } 267 268 273 public boolean hasTranslatableContent() { 274 return fTranslatable; 275 } 276 277 280 public boolean isDeprecated() { 281 return fDeprecated; 282 } 283 284 public String getExtendedAttributes() { 285 return null; 286 } 287 288 public String getDescription() { 289 if (super.getDescription() != null) { 290 return super.getDescription(); 291 } 292 ISchema schema = getSchema(); 293 if ((schema == null) || 294 (schema.getURL() == null)) { 295 return null; 297 } 298 String hashkey = schema.getURL().hashCode() + "_" + getName(); String 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 |