| 1 |  |  /* | 
 
   | 2 |  |   * JBind | 
 
   | 3 |  |   * | 
 
   | 4 |  |   * Copyright (c) by Stefan Wachter. All rights reserved. | 
 
   | 5 |  |   * | 
 
   | 6 |  |   * Usage, modification, and redistribution is subject to license terms that are | 
 
   | 7 |  |   * available at 'http://www.jbind.org'. The JBind license is like the | 
 
   | 8 |  |   * 'Apache Software License V 1.1'. | 
 
   | 9 |  |   */ | 
 
   | 10 |  |  package org.jbind.xml.instance.use; | 
 
   | 11 |  |   | 
 
   | 12 |  |  import java.util.Iterator; | 
 
   | 13 |  |   | 
 
   | 14 |  |  import org.jbind.xml.base.IHasLocation; | 
 
   | 15 |  |  import org.jbind.xml.core.content.IElemUseState; | 
 
   | 16 |  |  import org.jbind.xml.core.content.ISequenceDesc; | 
 
   | 17 |  |  import org.jbind.xml.core.content.IUseState; | 
 
   | 18 |  |  import org.jbind.xml.core.type.IAnyType; | 
 
   | 19 |  |  import org.jbind.xml.msg.XmlException; | 
 
   | 20 |  |  import org.jbind.xml.msg.XmlMessages; | 
 
   | 21 |  |   | 
 
   | 22 |  |  public class SequenceUseState extends GroupUseState { | 
 
   | 23 |  |   | 
 
   | 24 |  |    private Iterator myIterator = null; | 
 
   | 25 |  |   | 
 
   | 26 | 1933 |    public SequenceUseState(ISequenceDesc aContent, IUseState aParent) { | 
 
   | 27 | 1933 |      super(aContent, aParent); | 
 
   | 28 |  |    } | 
 
   | 29 |  |   | 
 
   | 30 | 17 |    protected void doReset() { | 
 
   | 31 | 17 |      super.doReset(); | 
 
   | 32 | 17 |      myIterator = null; | 
 
   | 33 |  |    } | 
 
   | 34 |  |   | 
 
   | 35 | 1835 |    public IElemUseState getNextUseState(String aNamespace, String aName, IAnyType anOverloadingType, IHasLocation aHasLocation) throws XmlException { | 
 
   | 36 | 1835 |      IElemUseState res = null; | 
 
   | 37 | 1835 |      boolean startedFromBeginning = false; | 
 
   | 38 | 1835 |      while (((null != myIterator) || mayIncrementUseCounter()) && (res == null) && !startedFromBeginning) { | 
 
   | 39 | 1957 |        if (null == myIterator) { | 
 
   | 40 | 1564 |          myIterator = iterUseStates(); | 
 
   | 41 | 1564 |          startedFromBeginning = true; | 
 
   | 42 |  |        } | 
 
   | 43 | 1957 |        while ((null == res) && myIterator.hasNext()) { | 
 
   | 44 | 2370 |          IUseState us = (IUseState)myIterator.next(); | 
 
   | 45 | 2370 |          res = us.getNextUseState(aNamespace, aName, anOverloadingType, aHasLocation); | 
 
   | 46 | 2367 |          if (null == res) { | 
 
   | 47 | 745 |            if (!us.getContentDesc().isEmptiable()) { | 
 
   | 48 | 13 |              if (startedFromBeginning) { | 
 
   | 49 |  |                // The iterated use state in the sequence must not be skipped. | 
 
   | 50 |  |                // -> The sequence use state can not return the next use state | 
 
   | 51 | 11 |                myIterator = null; | 
 
   | 52 | 11 |                break; | 
 
   | 53 |  |              } else { | 
 
   | 54 | 2 |                throw new XmlException(XmlMessages.cmSequenceGroupNotSatisfied(us.getContentDesc().getDesc(), getContentDesc().getLocation(), aHasLocation)); | 
 
   | 55 |  |              } | 
 
   | 56 |  |            } | 
 
   | 57 |  |          } | 
 
   | 58 |  |        } | 
 
   | 59 | 1952 |        if ((null != res) && startedFromBeginning) { | 
 
   | 60 | 1374 |          incrementUseCounter(); | 
 
   | 61 |  |        } | 
 
   | 62 | 1952 |        if ((null != myIterator) && !myIterator.hasNext()) { | 
 
   | 63 |  |          // By setting the iterator to null the sequence use state is iterated | 
 
   | 64 |  |          // again when this method is called the next time (if the use counter may | 
 
   | 65 |  |          // be incremented). | 
 
   | 66 | 1460 |          myIterator = null; | 
 
   | 67 |  |        } | 
 
   | 68 |  |      } | 
 
   | 69 | 1830 |      return res; | 
 
   | 70 |  |    } | 
 
   | 71 |  |   | 
 
   | 72 | 1737 |    protected void doIsValidEnd(IHasLocation aHasLocation) throws XmlException { | 
 
   | 73 | 1737 |      StringBuffer descriptions = new StringBuffer(); | 
 
   | 74 | 1737 |      while ((null != myIterator) && myIterator.hasNext()) { | 
 
   | 75 | 112 |        IUseState us = (IUseState)myIterator.next(); | 
 
   | 76 | 112 |        if (!us.getContentDesc().isEmptiable()) { | 
 
   | 77 | 3 |          if (descriptions.length() > 0) { | 
 
   | 78 | 0 |            descriptions.append(", "); | 
 
   | 79 |  |          } | 
 
   | 80 | 3 |          descriptions.append(us.getContentDesc().getDesc()); | 
 
   | 81 |  |        } | 
 
   | 82 |  |      } | 
 
   | 83 | 1737 |      if (descriptions.length() > 0) { | 
 
   | 84 | 3 |        throw new XmlException(XmlMessages.cmSequenceGroupNotSatisfied(descriptions, getContentDesc().getLocation(), aHasLocation)); | 
 
   | 85 |  |      } | 
 
   | 86 |  |    } | 
 
   | 87 |  |   | 
 
   | 88 |  |  } | 
 
   | 89 |  |   |