1 11 package org.eclipse.pde.internal.core.schema; 12 13 import java.io.PrintWriter ; 14 import java.io.Serializable ; 15 import java.util.Hashtable ; 16 import java.util.Vector ; 17 18 import org.eclipse.core.runtime.PlatformObject; 19 import org.eclipse.pde.core.ISourceObject; 20 import org.eclipse.pde.internal.core.PDECoreMessages; 21 import org.eclipse.pde.internal.core.ischema.IMetaElement; 22 import org.eclipse.pde.internal.core.ischema.ISchema; 23 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute; 24 import org.eclipse.pde.internal.core.ischema.ISchemaCompositor; 25 import org.eclipse.pde.internal.core.ischema.ISchemaDescriptor; 26 import org.eclipse.pde.internal.core.ischema.ISchemaElement; 27 import org.eclipse.pde.internal.core.ischema.ISchemaObject; 28 import org.eclipse.pde.internal.core.ischema.ISchemaObjectReference; 29 import org.eclipse.pde.internal.core.ischema.ISchemaType; 30 import org.w3c.dom.Comment ; 31 import org.w3c.dom.Node ; 32 33 public class SchemaElementReference extends PlatformObject implements 34 ISchemaElement, IMetaElement, ISchemaObjectReference, ISourceObject, 35 Serializable { 36 37 private static final long serialVersionUID = 1L; 38 39 private ISchemaElement element; 40 41 private ISchemaCompositor compositor; 42 43 private String referenceName; 44 45 public static final String P_MAX_OCCURS = "max_occurs"; 47 public static final String P_MIN_OCCURS = "min_occurs"; 49 public static final String P_REFERENCE_NAME = "reference_name"; 51 private int minOccurs = 1; 52 53 private int maxOccurs = 1; 54 55 private Vector comments; 56 57 private int[] range; 58 59 public SchemaElementReference(ISchemaCompositor compositor, String ref) { 60 referenceName = ref; 61 this.compositor = compositor; 62 } 63 64 public ISchemaAttribute getAttribute(String name) { 65 if (element == null) 66 return null; 67 return element.getAttribute(name); 68 } 69 70 public int getAttributeCount() { 71 if (element == null) 72 return 0; 73 return element.getAttributeCount(); 74 } 75 76 public ISchemaAttribute[] getAttributes() { 77 if (element == null) 78 return new ISchemaAttribute[0]; 79 return element.getAttributes(); 80 } 81 82 public ISchemaCompositor getCompositor() { 83 return compositor; 84 } 85 86 public String getDescription() { 87 if (element == null) 88 return ""; return element.getDescription(); 90 } 91 92 public String getDTDRepresentation(boolean addLinks) { 93 if (element == null) 94 return PDECoreMessages.SchemaElementReference_refElementMissing; 95 return element.getDTDRepresentation(addLinks); 96 } 97 98 public String getIconProperty() { 99 if (element == null) 100 return ""; return element.getIconProperty(); 102 } 103 104 public String getLabelProperty() { 105 if (element == null) 106 return ""; return element.getLabelProperty(); 108 } 109 110 public int getMaxOccurs() { 111 return maxOccurs; 112 } 113 114 public int getMinOccurs() { 115 return minOccurs; 116 } 117 118 public String getName() { 119 return referenceName; 120 } 121 122 public ISchemaObject getParent() { 123 return compositor; 124 } 125 126 public void setParent(ISchemaObject parent) { 127 } 128 129 public ISchemaElement getReferencedElement() { 130 return element; 131 } 132 133 public ISchemaObject getReferencedObject() { 134 return element; 135 } 136 137 public Class getReferencedObjectClass() { 138 return ISchemaElement.class; 139 } 140 141 public String getReferenceName() { 142 return referenceName; 143 } 144 145 public ISchema getSchema() { 146 if (element != null) { 147 ISchema schema = element.getSchema(); 148 if (schema != null) { 149 ISchemaDescriptor desc = schema.getSchemaDescriptor(); 150 if (!(desc instanceof IncludedSchemaDescriptor)) 151 return schema; 152 } 153 } 154 return getCompositorsSchema(); 155 } 156 157 public ISchema getCompositorsSchema() { 158 if (compositor != null) 159 return compositor.getSchema(); 160 return null; 161 } 162 163 public ISchemaType getType() { 164 if (element == null) 165 return null; 166 return element.getType(); 167 } 168 169 public boolean isLinked() { 170 return getReferencedObject() != null; 171 } 172 173 public void setCompositor(ISchemaCompositor newCompositor) { 174 compositor = newCompositor; 175 } 176 177 public void setMaxOccurs(int newMaxOccurs) { 178 Integer oldValue = new Integer (maxOccurs); 179 maxOccurs = newMaxOccurs; 180 ISchema schema = getCompositorsSchema(); 181 if (schema != null) 182 schema.fireModelObjectChanged(this, P_MAX_OCCURS, oldValue, 183 new Integer (maxOccurs)); 184 } 185 186 public void setMinOccurs(int newMinOccurs) { 187 Integer oldValue = new Integer (minOccurs); 188 minOccurs = newMinOccurs; 189 ISchema schema = getCompositorsSchema(); 190 if (schema != null) 191 schema.fireModelObjectChanged(this, P_MIN_OCCURS, oldValue, 192 new Integer (minOccurs)); 193 } 194 195 public void setReferencedObject(ISchemaObject referencedObject) { 196 if (referencedObject instanceof ISchemaElement) 197 element = (ISchemaElement) referencedObject; 198 else 199 element = null; 200 } 201 202 public void setReferenceName(String name) { 203 String oldValue = this.referenceName; 204 this.referenceName = name; 205 ISchema schema = getCompositorsSchema(); 206 if (schema != null) 207 schema.fireModelObjectChanged(this, P_REFERENCE_NAME, oldValue, 208 name); 209 } 210 211 public void write(String indent, PrintWriter writer) { 212 writeComments(writer); 213 writer.print(indent + "<element"); writer.print(" ref=\"" + getReferenceName() + "\""); if (getMinOccurs() != 1 || getMaxOccurs() != 1) { 216 String min = "" + getMinOccurs(); String max = getMaxOccurs() == Integer.MAX_VALUE ? "unbounded" : ("" + getMaxOccurs()); writer 220 .print(" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\""); } 222 writer.println("/>"); } 224 225 public void addComments(Node node) { 226 comments = addComments(node, comments); 227 } 228 229 public Vector addComments(Node node, Vector result) { 230 for (Node prev = node.getPreviousSibling(); prev != null; prev = prev 231 .getPreviousSibling()) { 232 if (prev.getNodeType() == Node.TEXT_NODE) 233 continue; 234 if (prev instanceof Comment ) { 235 String comment = prev.getNodeValue(); 236 if (result == null) 237 result = new Vector (); 238 result.add(comment); 239 } else 240 break; 241 } 242 return result; 243 } 244 245 void writeComments(PrintWriter writer) { 246 writeComments(writer, comments); 247 } 248 249 void writeComments(PrintWriter writer, Vector source) { 250 if (source == null) 251 return; 252 for (int i = 0; i < source.size(); i++) { 253 String comment = (String ) source.elementAt(i); 254 writer.println("<!--" + comment + "-->"); } 256 } 257 258 public int getStartLine() { 259 return range == null ? -1 : range[0]; 260 } 261 262 public int getStopLine() { 263 return range == null ? -1 : range[1]; 264 } 265 266 void bindSourceLocation(Node node, Hashtable lineTable) { 267 if (lineTable == null) 268 return; 269 Integer [] data = (Integer []) lineTable.get(node); 270 if (data != null) { 271 range = new int[] { data[0].intValue(), data[1].intValue() }; 272 } 273 } 274 275 280 public boolean hasTranslatableContent() { 281 return false; 282 } 283 284 287 public boolean isDeprecated() { 288 return false; 289 } 290 } 291 | Popular Tags |