1 11 package org.eclipse.pde.internal.core.schema; 12 13 import java.io.PrintWriter ; 14 import java.util.Vector ; 15 16 import org.eclipse.pde.internal.core.ischema.ISchema; 17 import org.eclipse.pde.internal.core.ischema.ISchemaEnumeration; 18 import org.eclipse.pde.internal.core.ischema.ISchemaObject; 19 import org.eclipse.pde.internal.core.ischema.ISchemaRestriction; 20 import org.eclipse.pde.internal.core.ischema.ISchemaSimpleType; 21 22 public class ChoiceRestriction 23 extends SchemaObject 24 implements ISchemaRestriction { 25 26 private static final long serialVersionUID = 1L; 27 private ISchemaSimpleType baseType; 28 private Vector children; 29 public static final String P_CHOICES = "choices"; 31 public ChoiceRestriction(ISchema schema) { 32 super(schema, "__choice__"); 34 } 35 public ChoiceRestriction(ChoiceRestriction source) { 36 this(source.getSchema()); 37 children = new Vector (); 38 Object [] choices = source.getChildren(); 39 for (int i = 0; i < choices.length; i++) { 40 children.add( 41 new SchemaEnumeration( 42 this, 43 ((ISchemaEnumeration) choices[i]).getName())); 44 } 45 } 46 public ISchemaSimpleType getBaseType() { 47 return baseType; 48 } 49 public Object [] getChildren() { 50 return (children != null) ? children.toArray() : new Object [0]; 51 } 52 public String [] getChoicesAsStrings() { 53 if (children == null) 54 return new String [0]; 55 Vector result = new Vector (); 56 for (int i = 0; i < children.size(); i++) { 57 ISchemaEnumeration enumeration = (ISchemaEnumeration) children.get(i); 58 result.addElement(enumeration.getName()); 59 } 60 String [] choices = new String [result.size()]; 61 result.copyInto(choices); 62 return choices; 63 } 64 public ISchemaObject getParent() { 65 if (baseType != null) 66 return baseType.getSchema(); 67 return super.getParent(); 68 } 69 public boolean isValueValid(java.lang.Object value) { 70 if (children == null) 71 return false; 72 String svalue = value.toString(); 73 74 for (int i = 0; i < children.size(); i++) { 75 ISchemaEnumeration enumeration = (ISchemaEnumeration) children.get(i); 76 if (enumeration.getName().equals(svalue)) 77 return true; 78 } 79 return false; 80 } 81 public void setBaseType(ISchemaSimpleType baseType) { 82 this.baseType = baseType; 83 } 84 public void setChildren(Vector children) { 85 Vector oldValue = this.children; 86 this.children = children; 87 if (getParent() != null) 88 getSchema().fireModelObjectChanged( 89 this, 90 P_CHOICES, 91 oldValue, 92 children); 93 } 94 public String toString() { 95 if (children == null) 96 return ""; StringBuffer buffer = new StringBuffer (); 98 99 for (int i = 0; i < children.size(); i++) { 100 Object child = children.get(i); 101 if (child instanceof ISchemaEnumeration) { 102 ISchemaEnumeration enumeration = (ISchemaEnumeration) child; 103 if (i > 0) 104 buffer.append(", "); buffer.append(enumeration.getName()); 106 } 107 } 108 return buffer.toString(); 109 } 110 public void write(String indent, PrintWriter writer) { 111 writer.println( 112 indent + "<restriction base=\"" + baseType.getName() + "\">"); for (int i = 0; i < children.size(); i++) { 114 Object child = children.get(i); 115 if (child instanceof ISchemaEnumeration) { 116 ISchemaEnumeration enumeration = (ISchemaEnumeration) child; 117 enumeration.write(indent + Schema.INDENT, writer); 118 } 119 } 120 writer.println(indent + "</restriction>"); } 122 } 123 | Popular Tags |