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: import java.io.Serializable;
014: import java.util.Hashtable;
015: import java.util.Vector;
016:
017: import org.eclipse.core.runtime.PlatformObject;
018: import org.eclipse.pde.core.ISourceObject;
019: import org.eclipse.pde.internal.core.PDECoreMessages;
020: import org.eclipse.pde.internal.core.ischema.IMetaElement;
021: import org.eclipse.pde.internal.core.ischema.ISchema;
022: import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
023: import org.eclipse.pde.internal.core.ischema.ISchemaCompositor;
024: import org.eclipse.pde.internal.core.ischema.ISchemaDescriptor;
025: import org.eclipse.pde.internal.core.ischema.ISchemaElement;
026: import org.eclipse.pde.internal.core.ischema.ISchemaObject;
027: import org.eclipse.pde.internal.core.ischema.ISchemaObjectReference;
028: import org.eclipse.pde.internal.core.ischema.ISchemaType;
029: import org.w3c.dom.Comment;
030: import org.w3c.dom.Node;
031:
032: public class SchemaElementReference extends PlatformObject implements
033: ISchemaElement, IMetaElement, ISchemaObjectReference,
034: ISourceObject, Serializable {
035:
036: private static final long serialVersionUID = 1L;
037:
038: private ISchemaElement element;
039:
040: private ISchemaCompositor compositor;
041:
042: private String referenceName;
043:
044: public static final String P_MAX_OCCURS = "max_occurs"; //$NON-NLS-1$
045:
046: public static final String P_MIN_OCCURS = "min_occurs"; //$NON-NLS-1$
047:
048: public static final String P_REFERENCE_NAME = "reference_name"; //$NON-NLS-1$
049:
050: private int minOccurs = 1;
051:
052: private int maxOccurs = 1;
053:
054: private Vector comments;
055:
056: private int[] range;
057:
058: public SchemaElementReference(ISchemaCompositor compositor,
059: String ref) {
060: referenceName = ref;
061: this .compositor = compositor;
062: }
063:
064: public ISchemaAttribute getAttribute(String name) {
065: if (element == null)
066: return null;
067: return element.getAttribute(name);
068: }
069:
070: public int getAttributeCount() {
071: if (element == null)
072: return 0;
073: return element.getAttributeCount();
074: }
075:
076: public ISchemaAttribute[] getAttributes() {
077: if (element == null)
078: return new ISchemaAttribute[0];
079: return element.getAttributes();
080: }
081:
082: public String[] getAttributeNames() {
083: if (element == null)
084: return new String[0];
085: return element.getAttributeNames();
086: }
087:
088: public ISchemaCompositor getCompositor() {
089: return compositor;
090: }
091:
092: public String getDescription() {
093: if (element == null)
094: return ""; //$NON-NLS-1$
095: return element.getDescription();
096: }
097:
098: public String getDTDRepresentation(boolean addLinks) {
099: if (element == null)
100: return PDECoreMessages.SchemaElementReference_refElementMissing;
101: return element.getDTDRepresentation(addLinks);
102: }
103:
104: public String getIconProperty() {
105: if (element == null)
106: return ""; //$NON-NLS-1$
107: return element.getIconProperty();
108: }
109:
110: public String getLabelProperty() {
111: if (element == null)
112: return ""; //$NON-NLS-1$
113: return element.getLabelProperty();
114: }
115:
116: public int getMaxOccurs() {
117: return maxOccurs;
118: }
119:
120: public int getMinOccurs() {
121: return minOccurs;
122: }
123:
124: public String getName() {
125: return referenceName;
126: }
127:
128: public ISchemaObject getParent() {
129: return compositor;
130: }
131:
132: public void setParent(ISchemaObject parent) {
133: }
134:
135: public ISchemaElement getReferencedElement() {
136: return element;
137: }
138:
139: public ISchemaObject getReferencedObject() {
140: return element;
141: }
142:
143: public Class getReferencedObjectClass() {
144: return ISchemaElement.class;
145: }
146:
147: public String getReferenceName() {
148: return referenceName;
149: }
150:
151: public ISchema getSchema() {
152: if (element != null) {
153: ISchema schema = element.getSchema();
154: if (schema != null) {
155: ISchemaDescriptor desc = schema.getSchemaDescriptor();
156: if (!(desc instanceof IncludedSchemaDescriptor))
157: return schema;
158: }
159: }
160: return getCompositorsSchema();
161: }
162:
163: public ISchema getCompositorsSchema() {
164: if (compositor != null)
165: return compositor.getSchema();
166: return null;
167: }
168:
169: public ISchemaType getType() {
170: if (element == null)
171: return null;
172: return element.getType();
173: }
174:
175: public boolean isLinked() {
176: return getReferencedObject() != null;
177: }
178:
179: public void setCompositor(ISchemaCompositor newCompositor) {
180: compositor = newCompositor;
181: }
182:
183: public void setMaxOccurs(int newMaxOccurs) {
184: Integer oldValue = new Integer(maxOccurs);
185: maxOccurs = newMaxOccurs;
186: ISchema schema = getCompositorsSchema();
187: if (schema != null)
188: schema.fireModelObjectChanged(this , P_MAX_OCCURS, oldValue,
189: new Integer(maxOccurs));
190: }
191:
192: public void setMinOccurs(int newMinOccurs) {
193: Integer oldValue = new Integer(minOccurs);
194: minOccurs = newMinOccurs;
195: ISchema schema = getCompositorsSchema();
196: if (schema != null)
197: schema.fireModelObjectChanged(this , P_MIN_OCCURS, oldValue,
198: new Integer(minOccurs));
199: }
200:
201: public void setReferencedObject(ISchemaObject referencedObject) {
202: if (referencedObject instanceof ISchemaElement)
203: element = (ISchemaElement) referencedObject;
204: else
205: element = null;
206: }
207:
208: public void setReferenceName(String name) {
209: String oldValue = this .referenceName;
210: this .referenceName = name;
211: ISchema schema = getCompositorsSchema();
212: if (schema != null)
213: schema.fireModelObjectChanged(this , P_REFERENCE_NAME,
214: oldValue, name);
215: }
216:
217: public void write(String indent, PrintWriter writer) {
218: writeComments(writer);
219: writer.print(indent + "<element"); //$NON-NLS-1$
220: writer.print(" ref=\"" + getReferenceName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
221: if (getMinOccurs() != 1 || getMaxOccurs() != 1) {
222: String min = "" + getMinOccurs(); //$NON-NLS-1$
223: String max = getMaxOccurs() == Integer.MAX_VALUE ? "unbounded" //$NON-NLS-1$
224: : ("" + getMaxOccurs()); //$NON-NLS-1$
225: writer
226: .print(" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
227: }
228: writer.println("/>"); //$NON-NLS-1$
229: }
230:
231: public void addComments(Node node) {
232: comments = addComments(node, comments);
233: }
234:
235: public Vector addComments(Node node, Vector result) {
236: for (Node prev = node.getPreviousSibling(); prev != null; prev = prev
237: .getPreviousSibling()) {
238: if (prev.getNodeType() == Node.TEXT_NODE)
239: continue;
240: if (prev instanceof Comment) {
241: String comment = prev.getNodeValue();
242: if (result == null)
243: result = new Vector();
244: result.add(comment);
245: } else
246: break;
247: }
248: return result;
249: }
250:
251: void writeComments(PrintWriter writer) {
252: writeComments(writer, comments);
253: }
254:
255: void writeComments(PrintWriter writer, Vector source) {
256: if (source == null)
257: return;
258: for (int i = 0; i < source.size(); i++) {
259: String comment = (String) source.elementAt(i);
260: writer.println("<!--" + comment + "-->"); //$NON-NLS-1$ //$NON-NLS-2$
261: }
262: }
263:
264: public int getStartLine() {
265: return range == null ? -1 : range[0];
266: }
267:
268: public int getStopLine() {
269: return range == null ? -1 : range[1];
270: }
271:
272: void bindSourceLocation(Node node, Hashtable lineTable) {
273: if (lineTable == null)
274: return;
275: Integer[] data = (Integer[]) lineTable.get(node);
276: if (data != null) {
277: range = new int[] { data[0].intValue(), data[1].intValue() };
278: }
279: }
280:
281: /*
282: * (non-Javadoc)
283: *
284: * @see org.eclipse.pde.internal.core.ischema.IMetaElement#isTranslatable()
285: */
286: public boolean hasTranslatableContent() {
287: return false;
288: }
289:
290: /* (non-Javadoc)
291: * @see org.eclipse.pde.internal.core.ischema.IMetaElement#isDeprecated()
292: */
293: public boolean isDeprecated() {
294: return false;
295: }
296:
297: public int compareTo(Object arg0) {
298: if (element == null) {
299: return -1;
300: } else if (arg0 == null) {
301: return -1;
302: }
303: return element.compareTo(arg0);
304: }
305: }
|