1 29 30 package com.caucho.relaxng.program; 31 32 import com.caucho.log.Log; 33 import com.caucho.relaxng.RelaxException; 34 import com.caucho.util.CharBuffer; 35 import com.caucho.util.L10N; 36 import com.caucho.xml.QName; 37 38 import java.util.HashSet ; 39 import java.util.Iterator ; 40 import java.util.NoSuchElementException ; 41 import java.util.logging.Logger ; 42 43 46 abstract public class Item { 47 protected final static L10N L = new L10N(Item.class); 48 protected final static Logger log = Log.open(Item.class); 49 50 private static final Iterator <Item> EMPTY_ITEM_ITERATOR; 51 52 55 public void firstSet(HashSet <QName> set) 56 { 57 } 58 59 62 public void requiredFirstSet(HashSet <QName> set) 63 { 64 if (! allowEmpty()) 65 firstSet(set); 66 } 67 68 71 public boolean allowEmpty() 72 { 73 return false; 74 } 75 76 79 public Iterator <Item> getItemsIterator() 80 { 81 return emptyItemIterator(); 82 } 83 84 protected Iterator <Item> emptyItemIterator() 85 { 86 return EMPTY_ITEM_ITERATOR; 87 } 88 89 protected Iterator <Item> itemIterator( final Item item ) 90 { 91 if (item == null) 92 return emptyItemIterator(); 93 94 return new Iterator <Item>() { 95 private boolean _done; 96 97 public boolean hasNext() 98 { 99 return !_done; 100 } 101 102 public Item next() 103 { 104 if ( ! hasNext() ) 105 throw new NoSuchElementException (); 106 107 _done = true; 108 109 return item; 110 } 111 112 public void remove() 113 { 114 throw new UnsupportedOperationException (); 115 } 116 }; 117 } 118 119 126 public Item startElement(QName name) 127 throws RelaxException 128 { 129 return null; 130 } 131 132 141 public boolean allowsElement(QName name) 142 { 143 return false; 144 } 145 146 149 public void attributeSet(HashSet <QName> set) 150 { 151 } 152 153 161 public boolean allowAttribute(QName name, String value) 162 throws RelaxException 163 { 164 return false; 165 } 166 167 175 public Item setAttribute(QName name, String value) 176 throws RelaxException 177 { 178 return this; 179 } 180 181 184 public Item attributeEnd() 185 { 186 return this; 187 } 188 189 192 public Item text(String text) 193 throws RelaxException 194 { 195 return null; 196 } 197 198 201 public Item endElement() 202 throws RelaxException 203 { 204 if (allowEmpty()) 205 return EmptyItem.create(); 206 else 207 return null; 208 } 209 210 213 public Item interleaveContinuation(Item cont) 214 { 215 throw new IllegalStateException (String.valueOf(getClass().getName())); 216 } 217 218 221 public Item inElementContinuation(Item cont) 222 { 223 throw new IllegalStateException (String.valueOf(getClass().getName())); 224 } 225 226 229 public Item groupContinuation(Item cont) 230 { 231 throw new IllegalStateException (String.valueOf(getClass().getName())); 232 } 233 234 237 public String toSyntaxDescription(int depth) 238 { 239 return toString(); 240 } 241 242 245 protected boolean isSimpleSyntax() 246 { 247 return false; 248 } 249 250 253 protected void addSyntaxNewline(CharBuffer cb, int depth) 254 { 255 cb.append('\n'); 256 for (int i = 0; i < depth; i++) 257 cb.append(' '); 258 } 259 260 263 protected RelaxException error(String msg) 264 { 265 return new RelaxException(msg); 266 } 267 268 static { 269 EMPTY_ITEM_ITERATOR = 270 new Iterator <Item>() { 271 public boolean hasNext() 272 { 273 return false; 274 } 275 276 public Item next() 277 { 278 throw new NoSuchElementException (); 279 } 280 281 public void remove() 282 { 283 throw new UnsupportedOperationException (); 284 } 285 }; 286 } 287 } 288 289 | Popular Tags |