1 11 package org.eclipse.ant.internal.ui.dtd.schema; 12 import java.util.Iterator ; 13 import java.util.LinkedList ; 14 import java.util.List ; 15 16 import org.eclipse.ant.internal.ui.dtd.IAtom; 17 import org.eclipse.ant.internal.ui.dtd.IModel; 18 19 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 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 (AntDTDSchemaMessages.Model_model_may_not_be_changed); 63 64 if (fContentsList == null) 65 fContentsList = new LinkedList (); 66 67 fContentsList.add(model); 68 } 69 70 public void setLeaf(IAtom leaf) { 71 fLeaf = leaf; 72 } 73 74 private static final String [] fOps = {"?",",","|","&","!!!"}; 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 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 (); 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 (); 112 Iterator it = fContentsList.iterator(); 113 while (it.hasNext()) { 114 copy.fContentsList.add(it.next()); 115 } 116 } 117 return copy; 118 } 119 120 123 public int getKind() { 124 return 0; 125 } 126 127 130 public int getMinOccurs() { 131 return fMin; 132 } 133 134 137 public int getMaxOccurs() { 138 return fMax; 139 } 140 141 144 public IModel[] getContents() { 145 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 161 public IAtom getLeaf() { 162 return fLeaf; 163 } 164 165 168 public String getOperator() { 169 return fOps[fKind]; 170 } 171 172 175 public String stringRep() { 176 StringBuffer buf = new StringBuffer (); 177 stringRep(buf); 178 return buf.toString(); 179 } 180 181 private void stringRep(StringBuffer buf) { 182 switch (getKind()) { 183 case IModel.CHOICE: 184 case IModel.SEQUENCE: 185 buf.append('('); 186 Iterator 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 209 public String getQualifier() { 210 return fMin == 1 ? (fMax == UNBOUNDED ? "+" : "") : (fMax == UNBOUNDED ? "*" : "?"); } 212 213 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 |