| 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.HashSet; | 
 
   | 13 |  |  import java.util.Iterator; | 
 
   | 14 |  |  import java.util.Set; | 
 
   | 15 |  |   | 
 
   | 16 |  |  import org.jbind.xml.base.IHasLocation; | 
 
   | 17 |  |  import org.jbind.xml.core.content.IAllDesc; | 
 
   | 18 |  |  import org.jbind.xml.core.content.IElemUseState; | 
 
   | 19 |  |  import org.jbind.xml.core.content.IUseState; | 
 
   | 20 |  |  import org.jbind.xml.core.type.IAnyType; | 
 
   | 21 |  |  import org.jbind.xml.msg.XmlException; | 
 
   | 22 |  |  import org.jbind.xml.msg.XmlMessages; | 
 
   | 23 |  |   | 
 
   | 24 |  |  public class AllUseState extends GroupUseState { | 
 
   | 25 |  |   | 
 
   | 26 |  |    private Set mySet = null; | 
 
   | 27 |  |   | 
 
   | 28 | 28 |    public AllUseState(IAllDesc aContent, IUseState aParent) { | 
 
   | 29 | 28 |      super(aContent, aParent); | 
 
   | 30 |  |    } | 
 
   | 31 |  |   | 
 
   | 32 | 8 |    protected void doReset() { | 
 
   | 33 | 8 |      super.doReset(); | 
 
   | 34 | 8 |      mySet = null; | 
 
   | 35 |  |    } | 
 
   | 36 |  |   | 
 
   | 37 | 124 |    public IElemUseState getNextUseState(String aNamespace, String aName, IAnyType anOverloadingType, IHasLocation aHasLocation) throws XmlException { | 
 
   | 38 | 124 |      IElemUseState res = null; | 
 
   | 39 | 124 |      boolean startedFromBeginning = false; | 
 
   | 40 | 124 |      while (((null != mySet) || mayIncrementUseCounter()) && (res == null) && !startedFromBeginning) { | 
 
   | 41 | 122 |        if (null == mySet) { | 
 
   | 42 | 36 |          mySet = new HashSet(31); | 
 
   | 43 | 36 |          for (Iterator i = iterUseStates(); i.hasNext(); ) { | 
 
   | 44 | 134 |            mySet.add(i.next()); | 
 
   | 45 |  |          } | 
 
   | 46 | 36 |          startedFromBeginning = true; | 
 
   | 47 |  |        } | 
 
   | 48 | 122 |        for (Iterator i = mySet.iterator(); (res == null) && i.hasNext(); ) { | 
 
   | 49 | 234 |          IUseState us = (IUseState)i.next(); | 
 
   | 50 | 234 |          res = us.getNextUseState(aNamespace, aName, anOverloadingType, aHasLocation); | 
 
   | 51 | 234 |          if (null != res) { | 
 
   | 52 | 114 |            i.remove(); | 
 
   | 53 |  |          } | 
 
   | 54 |  |        } | 
 
   | 55 | 122 |        if (null != res) { | 
 
   | 56 | 114 |          if (startedFromBeginning) { | 
 
   | 57 | 28 |            incrementUseCounter(); | 
 
   | 58 |  |          } | 
 
   | 59 |  |        } else { | 
 
   | 60 | 8 |          if (!startedFromBeginning) { | 
 
   | 61 | 0 |            doIsValidEnd(aHasLocation); | 
 
   | 62 |  |          } | 
 
   | 63 | 8 |          mySet = null; | 
 
   | 64 |  |        } | 
 
   | 65 | 122 |        if ((null != mySet) && mySet.isEmpty()) { | 
 
   | 66 |  |          // By setting the set to null the all use state is filled again | 
 
   | 67 |  |          // when this method is called the next time (if the use counter may | 
 
   | 68 |  |          // be incremented). | 
 
   | 69 | 24 |          mySet = null; | 
 
   | 70 |  |        } | 
 
   | 71 |  |      } | 
 
   | 72 | 124 |      return res; | 
 
   | 73 |  |    } | 
 
   | 74 |  |   | 
 
   | 75 | 23 |    protected void doIsValidEnd(IHasLocation aHasLocation) throws XmlException { | 
 
   | 76 | 23 |      if (null != mySet) { | 
 
   | 77 | 4 |        StringBuffer descriptions = new StringBuffer(); | 
 
   | 78 | 4 |        for (Iterator i = mySet.iterator(); i.hasNext(); ) { | 
 
   | 79 | 4 |          IUseState us = (IUseState)i.next(); | 
 
   | 80 | 4 |          if (!us.getContentDesc().isEmptiable()) { | 
 
   | 81 | 1 |            if (descriptions.length() > 0) { | 
 
   | 82 | 0 |              descriptions.append(", "); | 
 
   | 83 |  |            } | 
 
   | 84 | 1 |            descriptions.append(us.getContentDesc().getDesc()); | 
 
   | 85 |  |          } | 
 
   | 86 |  |        } | 
 
   | 87 | 4 |        if (descriptions.length() > 0) { | 
 
   | 88 | 1 |          throw new XmlException(XmlMessages.cmAllGroupNotSatisfied(descriptions, getContentDesc().getLocation(), aHasLocation)); | 
 
   | 89 |  |        } | 
 
   | 90 |  |      } | 
 
   | 91 |  |    } | 
 
   | 92 |  |   | 
 
   | 93 |  |  } | 
 
   | 94 |  |   |