[jboss-dev-forums] [Design of JBossXB] - Problem with repeated but not repeatable particles.

adrian@jboss.org do-not-reply at jboss.com
Fri Oct 13 08:43:06 EDT 2006


In the ejb-jar xsd there is a repeated particle that is not a repeatable
particle, this causes problems for JBossXB which wants to
treat it as a repeatable.

I added a test for this, see RepeatedElementsUnitTestCase.

I have a patch that fixes the problem, but this is obviously a hack,
besides the fact that it doesn't cater for the more compliated case:

  | <xsd:sequence>
  |    <xsd:element ref="child"/>
  |    <xsd:element ref="another" minoccurs="0"/>
  |    <xsd:element ref="child"/>
  | </xsd:sequence>
  | 

The patch only marks the repeated element if they are consecutive.


  | Index: SequenceBinding.java
  | ===================================================================
  | --- SequenceBinding.java	(revision 2115)
  | +++ SequenceBinding.java	(working copy)
  | @@ -54,6 +54,25 @@
  |  
  |     public void addParticle(ParticleBinding particle)
  |     {
  | +      // TODO Revisit this is a hack and incomplete
  | +      // see RepeatedElementUnitTestCase
  | +      if (sequence.isEmpty() == false)
  | +      {
  | +         TermBinding term = particle.getTerm();
  | +         if (term instanceof ElementBinding)
  | +         {
  | +            QName name = ((ElementBinding) term).getQName();
  | +            ParticleBinding previous = (ParticleBinding) sequence.get(sequence.size()-1);
  | +            term = previous.getTerm();
  | +            if (term instanceof ElementBinding)
  | +            {
  | +               QName previousName = ((ElementBinding) term).getQName();
  | +               if (previousName.equals(name))
  | +                  previous.setRepeated(true);
  | +            }
  | +         }
  | +      }
  | +      
  |        switch(sequence.size())
  |        {
  |           case 0:
  | @@ -61,10 +80,7 @@
  |              if(particle.isRepeatable() && particle.getTerm() instanceof ElementBinding)
  |              {
  |                 ElementBinding element = (ElementBinding)particle.getTerm();
  | -               if(particle.isRepeatable())
  | -               {
  |                    arrayItem = element;
  | -               }
  |              }
  |              break;
  |           case 1:
  | Index: ParticleBinding.java
  | ===================================================================
  | --- ParticleBinding.java	(revision 2115)
  | +++ ParticleBinding.java	(working copy)
  | @@ -31,6 +31,7 @@
  |     private int minOccurs = 1;
  |     private int maxOccurs = -1;
  |     private boolean maxOccursUnbounded;
  | +   private boolean repeated;
  |  
  |     public ParticleBinding(TermBinding term, int minOccurs, int maxOccurs, boolean maxOccursUnbounded)
  |     {
  | @@ -100,6 +101,16 @@
  |        return minOccurs > occurs && (!term.isModelGroup() || ((ModelGroupBinding)term).hasRequiredParticle());
  |     }
  |     
  | +   public boolean isRepeated()
  | +   {
  | +      return repeated;
  | +   }
  | +   
  | +   public void setRepeated(boolean repeated)
  | +   {
  | +      this.repeated = repeated;
  | +   }
  | +   
  |     public String toString()
  |     {
  |        return term.toString();
  | Index: CharactersHandler.java
  | ===================================================================
  | --- CharactersHandler.java	(revision 2115)
  | +++ CharactersHandler.java	(working copy)
  | @@ -27,6 +27,7 @@
  |  import javax.xml.namespace.NamespaceContext;
  |  
  |  import org.jboss.xb.binding.Constants;
  | +import org.jboss.xb.binding.JBossXBException;
  |  import org.jboss.xb.binding.SimpleTypeBindings;
  |  import org.jboss.xb.binding.JBossXBRuntimeException;
  |  import org.jboss.xb.binding.metadata.ValueMetaData;
  | @@ -108,7 +109,14 @@
  |        }
  |        else if(typeQName != null && Constants.NS_XML_SCHEMA.equals(typeQName.getNamespaceURI()))
  |        {
  | -         o = SimpleTypeBindings.unmarshal(typeQName.getLocalPart(), value, nsCtx);
  | +         try
  | +         {
  | +            o = SimpleTypeBindings.unmarshal(typeQName.getLocalPart(), value, nsCtx);
  | +         }
  | +         catch (IllegalStateException e)
  | +         {
  | +            throw new JBossXBRuntimeException("Characters are not allowed here", e);
  | +         }
  |        }
  |        else
  |        {
  | Index: SundayContentHandler.java
  | ===================================================================
  | --- SundayContentHandler.java	(revision 2115)
  | +++ SundayContentHandler.java	(working copy)
  | @@ -29,6 +29,7 @@
  |  import org.apache.xerces.xs.XSTypeDefinition;
  |  import org.jboss.logging.Logger;
  |  import org.jboss.util.StringPropertyReplacer;
  | +import org.jboss.util.Strings;
  |  import org.jboss.xb.binding.Constants;
  |  import org.jboss.xb.binding.GenericValueContainer;
  |  import org.jboss.xb.binding.JBossXBRuntimeException;
  | @@ -206,9 +207,9 @@
  |                 ElementBinding element = (ElementBinding)term;
  |                 if(item.ended)
  |                 {
  | -                  if(element.getQName().equals(startName))
  | +                  particle = item.particle;
  | +                  if(element.getQName().equals(startName) && particle.isRepeated() == false)
  |                    {
  | -                     particle = item.particle;
  |                       repeated = true;
  |                       item.reset();
  |  
  | @@ -1056,7 +1057,7 @@
  |           {
  |              binding = particle.getTerm();
  |           }
  | -         log.trace("pushed " + qName + "=" + o + ", binding=" + binding);
  | +         log.trace("pushed " + qName + "=" + o + ", binding=" + Strings.defaultToString(binding));
  |        }
  |     }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3978132#3978132

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3978132



More information about the jboss-dev-forums mailing list