KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > schema > ChoiceRestriction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.schema;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.Vector JavaDoc;
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 JavaDoc children;
29     public static final String JavaDoc P_CHOICES = "choices"; //$NON-NLS-1$
30

31     public ChoiceRestriction(ISchema schema) {
32         super(schema, "__choice__"); //$NON-NLS-1$
33

34     }
35     public ChoiceRestriction(ChoiceRestriction source) {
36         this(source.getSchema());
37         children = new Vector JavaDoc();
38         Object JavaDoc[] 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 JavaDoc[] getChildren() {
50         return (children != null) ? children.toArray() : new Object JavaDoc[0];
51     }
52     public String JavaDoc[] getChoicesAsStrings() {
53         if (children == null)
54             return new String JavaDoc[0];
55         Vector JavaDoc result = new Vector JavaDoc();
56         for (int i = 0; i < children.size(); i++) {
57             ISchemaEnumeration enumeration = (ISchemaEnumeration) children.get(i);
58             result.addElement(enumeration.getName());
59         }
60         String JavaDoc[] choices = new String JavaDoc[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 JavaDoc value) {
70         if (children == null)
71             return false;
72         String JavaDoc 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 JavaDoc children) {
85         Vector JavaDoc 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 JavaDoc toString() {
95         if (children == null)
96             return ""; //$NON-NLS-1$
97
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
98
99         for (int i = 0; i < children.size(); i++) {
100             Object JavaDoc child = children.get(i);
101             if (child instanceof ISchemaEnumeration) {
102                 ISchemaEnumeration enumeration = (ISchemaEnumeration) child;
103                 if (i > 0)
104                     buffer.append(", "); //$NON-NLS-1$
105
buffer.append(enumeration.getName());
106             }
107         }
108         return buffer.toString();
109     }
110     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
111         writer.println(
112             indent + "<restriction base=\"" + baseType.getName() + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
113
for (int i = 0; i < children.size(); i++) {
114             Object JavaDoc 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>"); //$NON-NLS-1$
121
}
122 }
123
Popular Tags