KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > dtd > schema > Model


1 /*******************************************************************************
2  * Copyright (c) 2002, 2005 Object Factory Inc.
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  * Object Factory Inc. - Initial implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.dtd.schema;
12 import java.util.Iterator JavaDoc;
13 import java.util.LinkedList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.ant.internal.ui.dtd.IAtom;
17 import org.eclipse.ant.internal.ui.dtd.IModel;
18
19 /**
20  * IModel implementation.
21  * @author Bob Foster
22  */

23 public class Model implements IModel {
24     
25     protected int fKind;
26     protected int fMin = 1;
27     protected int fMax = 1;
28     protected int fNum = 0;
29     protected IModel[] fContents;
30     protected List JavaDoc fContentsList;
31     protected IAtom fLeaf;
32     protected boolean fMixed;
33     
34     private static final IModel[] fEmptyContents = new IModel[0];
35     
36     public Model(int kind) {
37         fKind = kind;
38     }
39     
40     public Model() {
41         fKind = UNKNOWN;
42     }
43     
44     public void setKind(int kind) {
45         fKind = kind;
46     }
47     
48     public void setMinOccurs(int min) {
49         fMin = min;
50     }
51     
52     public void setMaxOccurs(int max) {
53         fMax = max;
54     }
55     
56     public void setContents(IModel[] contents) {
57         fContents = contents;
58     }
59     
60     public void addModel(IModel model) {
61         if (fContents != null)
62             throw new IllegalStateException JavaDoc(AntDTDSchemaMessages.Model_model_may_not_be_changed);
63             
64         if (fContentsList == null)
65             fContentsList = new LinkedList JavaDoc();
66             
67         fContentsList.add(model);
68     }
69     
70     public void setLeaf(IAtom leaf) {
71         fLeaf = leaf;
72     }
73
74     private static final String JavaDoc[] fOps = {"?",",","|","&","!!!"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
75

76     private Nfm qualifyNfm(Nfm nfm) {
77         if (nfm == null)
78             return null;
79         if (fMin == 1 && fMax == 1)
80             return nfm;
81         if (fMin == 0 && fMax == 1) {
82             return Nfm.getQuestion(nfm);
83         }
84         if (fMin == 0 && fMax == UNBOUNDED) {
85             return Nfm.getStar(nfm);
86         }
87         if (fMin == 1 && fMax == UNBOUNDED) {
88             return Nfm.getPlus(nfm);
89         }
90         //the following cases cannot be reached by DTD models
91
if (fMax == 0)
92             return Nfm.getNfm(null);
93         if (fMax == UNBOUNDED) {
94             return Nfm.getUnbounded(nfm, fMin);
95         }
96         
97         return Nfm.getMinMax(nfm, fMin, fMax);
98     }
99
100     public Model shallowCopy() {
101         Model copy = new Model(getKind());
102         copy.fMixed = fMixed;
103         copy.fLeaf = fLeaf;
104         if (fContents != null) {
105             copy.fContentsList = new LinkedList JavaDoc();
106             for (int i = 0; i < fContents.length; i++) {
107                 copy.fContentsList.add(fContents[i]);
108             }
109         }
110         else if (fContentsList != null) {
111             copy.fContentsList = new LinkedList JavaDoc();
112             Iterator JavaDoc it = fContentsList.iterator();
113             while (it.hasNext()) {
114                 copy.fContentsList.add(it.next());
115             }
116         }
117         return copy;
118     }
119
120     /**
121      * @see org.eclipse.ant.internal.ui.dtd.IModel#getKind()
122      */

123     public int getKind() {
124         return 0;
125     }
126
127     /**
128      * @see org.eclipse.ant.internal.ui.dtd.IModel#getMinOccurs()
129      */

130     public int getMinOccurs() {
131         return fMin;
132     }
133
134     /**
135      * @see org.eclipse.ant.internal.ui.dtd.IModel#getMaxOccurs()
136      */

137     public int getMaxOccurs() {
138         return fMax;
139     }
140
141     /**
142      * @see org.eclipse.ant.internal.ui.dtd.IModel#getContents()
143      */

144     public IModel[] getContents() {
145         // A model contents may be referred to many times
146
// it would be inefficient to convert to array each time
147
if (fContents == null) {
148             if (fContentsList != null) {
149                 fContents = (IModel[]) fContentsList.toArray(new IModel[fContentsList.size()]);
150                 fContentsList = null;
151             }
152             else
153                 fContents = fEmptyContents;
154         }
155         return fContents;
156     }
157
158     /**
159      * @see org.eclipse.ant.internal.ui.dtd.IModel#getLeaf()
160      */

161     public IAtom getLeaf() {
162         return fLeaf;
163     }
164
165     /**
166      * @see org.eclipse.ant.internal.ui.dtd.IModel#getOperator()
167      */

168     public String JavaDoc getOperator() {
169         return fOps[fKind];
170     }
171
172     /* (non-Javadoc)
173      * @see org.eclipse.ant.internal.ui.dtd.IModel#stringRep()
174      */

175     public String JavaDoc stringRep() {
176         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
177         stringRep(buf);
178         return buf.toString();
179     }
180     
181     private void stringRep(StringBuffer JavaDoc buf) {
182         switch (getKind()) {
183             case IModel.CHOICE:
184             case IModel.SEQUENCE:
185                 buf.append('(');
186                 Iterator JavaDoc it = fContentsList.iterator();
187                 while (it.hasNext()) {
188                     Model model = (Model) it.next();
189                     model.stringRep(buf);
190                     if (it.hasNext())
191                         buf.append(getOperator());
192                 }
193                 buf.append(')');
194                 buf.append(getQualifier());
195                 break;
196             case IModel.LEAF:
197                 IAtom atom = getLeaf();
198                 buf.append(atom.getName());
199                 break;
200             default:
201                 buf.append(AntDTDSchemaMessages.Model____UNKNOWN____2);
202                 break;
203         }
204     }
205
206     /**
207      * @see org.eclipse.ant.internal.ui.dtd.IModel#getQualifier()
208      */

209     public String JavaDoc getQualifier() {
210         return fMin == 1 ? (fMax == UNBOUNDED ? "+" : "") : (fMax == UNBOUNDED ? "*" : "?"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
211
}
212     
213     /**
214      * @see org.eclipse.ant.internal.ui.dtd.IModel#toNfm()
215      */

216     public Nfm toNfm() {
217         Nfm nfm = null;
218         switch (fKind) {
219             case CHOICE:
220             case SEQUENCE:
221             {
222                 IModel[] contents = getContents();
223                 if (contents == null || contents.length == 0) {
224                     return null;
225                 }
226                 
227                 nfm = contents[0].toNfm();
228                 for (int i = 1; i < contents.length; i++) {
229                     Nfm tmp = contents[i].toNfm();
230                     if (fKind == SEQUENCE) {
231                         nfm = Nfm.getComma(nfm, tmp);
232                     } else {
233                         nfm = Nfm.getOr(nfm, tmp);
234                     }
235                 }
236                 break;
237             }
238             case LEAF:
239             {
240                 nfm = Nfm.getNfm(fLeaf);
241                 break;
242             }
243         }
244         return qualifyNfm(nfm);
245     }
246     
247 }
248
Popular Tags