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.util.Vector;
014:
015: import org.eclipse.pde.internal.core.ischema.ISchema;
016: import org.eclipse.pde.internal.core.ischema.ISchemaEnumeration;
017: import org.eclipse.pde.internal.core.ischema.ISchemaObject;
018: import org.eclipse.pde.internal.core.ischema.ISchemaRestriction;
019: import org.eclipse.pde.internal.core.ischema.ISchemaSimpleType;
020:
021: public class ChoiceRestriction extends SchemaObject implements
022: ISchemaRestriction {
023:
024: private static final long serialVersionUID = 1L;
025: private ISchemaSimpleType baseType;
026: private Vector children;
027: public static final String P_CHOICES = "choices"; //$NON-NLS-1$
028:
029: public ChoiceRestriction(ISchema schema) {
030: super (schema, "__choice__"); //$NON-NLS-1$
031:
032: }
033:
034: public ChoiceRestriction(ChoiceRestriction source) {
035: this (source.getSchema());
036: children = new Vector();
037: Object[] choices = source.getChildren();
038: for (int i = 0; i < choices.length; i++) {
039: children.add(new SchemaEnumeration(this ,
040: ((ISchemaEnumeration) choices[i]).getName()));
041: }
042: }
043:
044: public ISchemaSimpleType getBaseType() {
045: return baseType;
046: }
047:
048: public Object[] getChildren() {
049: return (children != null) ? children.toArray() : new Object[0];
050: }
051:
052: public String[] getChoicesAsStrings() {
053: if (children == null)
054: return new String[0];
055: Vector result = new Vector();
056: for (int i = 0; i < children.size(); i++) {
057: ISchemaEnumeration enumeration = (ISchemaEnumeration) children
058: .get(i);
059: result.addElement(enumeration.getName());
060: }
061: String[] choices = new String[result.size()];
062: result.copyInto(choices);
063: return choices;
064: }
065:
066: public ISchemaObject getParent() {
067: if (baseType != null)
068: return baseType.getSchema();
069: return super .getParent();
070: }
071:
072: public boolean isValueValid(java.lang.Object value) {
073: if (children == null)
074: return false;
075: String svalue = value.toString();
076:
077: for (int i = 0; i < children.size(); i++) {
078: ISchemaEnumeration enumeration = (ISchemaEnumeration) children
079: .get(i);
080: if (enumeration.getName().equals(svalue))
081: return true;
082: }
083: return false;
084: }
085:
086: public void setBaseType(ISchemaSimpleType baseType) {
087: this .baseType = baseType;
088: }
089:
090: public void setChildren(Vector children) {
091: Vector oldValue = this .children;
092: this .children = children;
093: if (getParent() != null)
094: getSchema().fireModelObjectChanged(this , P_CHOICES,
095: oldValue, children);
096: }
097:
098: public String toString() {
099: if (children == null)
100: return ""; //$NON-NLS-1$
101: StringBuffer buffer = new StringBuffer();
102:
103: for (int i = 0; i < children.size(); i++) {
104: Object child = children.get(i);
105: if (child instanceof ISchemaEnumeration) {
106: ISchemaEnumeration enumeration = (ISchemaEnumeration) child;
107: if (i > 0)
108: buffer.append(", "); //$NON-NLS-1$
109: buffer.append(enumeration.getName());
110: }
111: }
112: return buffer.toString();
113: }
114:
115: public void write(String indent, PrintWriter writer) {
116: writer.println(indent
117: + "<restriction base=\"" + baseType.getName() + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
118: for (int i = 0; i < children.size(); i++) {
119: Object child = children.get(i);
120: if (child instanceof ISchemaEnumeration) {
121: ISchemaEnumeration enumeration = (ISchemaEnumeration) child;
122: enumeration.write(indent + Schema.INDENT, writer);
123: }
124: }
125: writer.println(indent + "</restriction>"); //$NON-NLS-1$
126: }
127: }
|