system/src/sc/parser/OrderedChoice.java
public Object generate(GenerateContext ctx, Object value) {
      if (trace)
         System.out.println("*** tracing generate ordered choice");

      String recName = null;
      if (recordTime) {
         recName = "gen-" + toString();
         PerfMon.start(recName);
      }

      try {
         int progress = 0;

         if (getSemanticValueIsArray()) { // Repeat but not chained
            if (value == null) {
               if (optional)
                  return generateResult(ctx, null);
               else {
                  System.err.println("*** Null value encountered for required array element: " + this);
                  return null;
               }
            }

            ParentParseNode pnode = newGeneratedParseNode(value);

            if (!(value instanceof List)) {
               if (PString.isString(value)) {
                  boolean matchedAny = false;
                  IString strValue = PString.toIString(value);
                  while (strValue != null && strValue.length() > 0) {
                     Object childNode = generateElement(ctx, strValue, true);
                     if (childNode instanceof GenerateError) {
                        if (matchedAny)
                           return generateResult(ctx, pnode);

                        pnode.setSemanticValue(null, true, false);
                        if (optional && emptyValue(ctx, value)) {
                           return generateResult(ctx, null);
                        }
                        ((GenerateError)childNode).progress += progress;
                        return childNode;
                     }
                     matchedAny = true;
                     pnode.addGeneratedNode(childNode);


                     /*
                     * We have a string semantic value... in case it was formed from more than one parselet
                     * we need to pull out the part which was just generated before passing the next chunk on.
                     */
                     if (childNode != null) {
                        int childNodeLen = ParseUtil.toSemanticStringLength(childNode);
                        if (strValue.length() >= childNodeLen) {
                           strValue = strValue.substring(childNodeLen);
                           value = strValue;
                           progress += childNodeLen;
                        }
                        else
                           strValue = null;
                     }
                     else
                        strValue = null;
                  }
                  return generateResult(ctx, pnode);
               }
               return REPEAT_NODE_ERROR;
            }

            // If we are not an array type or our children are all scalars, pull apart the array and
            // process it one by one.
            if (parameterType != ParameterType.ARRAY || !ParseUtil.isArrayParselet(parselets.get(0))) {
               List nodeList = (List) value;
               for (int i = 0; i < nodeList.size(); i++) {
                  Object nodeVal = nodeList.get(i);
                  if (PString.isString(nodeVal)) {
                     IString strValue = PString.toIString(nodeVal);

                     // In this case, we are accepting an array of nodes and must produce back an array of strings
                     // Since our child node is a scalar, we are a repeat node, we must pull apart the string piece
                     // by piece until we generate it all.  Any error at this point will fail.
                     while (strValue != null && strValue.length() > 0) {
                        Object childNode = generateElement(ctx, strValue, true);
                        if (childNode instanceof GenerateError) {
                           pnode.setSemanticValue(null, true, false);
                           if (optional && emptyValue(ctx, value)) {
                              return generateResult(ctx, null);
                           }
                           ((GenerateError) childNode).progress += progress;
                           return childNode;
                        }
                        pnode.addGeneratedNode(childNode);

                        /*
                        * We have a string semantic value... in case it was formed from more than one parselet
                        * we need to pull out the part which was just generated before passing the next chunk on.
                        */
                        if (childNode != null) {
                           int childNodeLen = ParseUtil.toSemanticStringLength(childNode);
                           if (strValue.length() >= childNodeLen) {
                              strValue = strValue.substring(childNodeLen);
                              value = strValue;
                              progress += childNodeLen;
                           }
                           else
                              strValue = null;
                        }
                        else
                           strValue = null;
                     }

                     // If we are a String node and we receive an array of strings, we need to just pull the first
                     // string out and return.  We could not have produced the array.
                     if (parameterType == ParameterType.STRING) {
                        return new PartialArrayResult(i+1, pnode);
                     }
                  }
                  else {
                     Object childNode = generateElement(ctx, nodeVal, true);
                     if (childNode instanceof GenerateError) {
                        if (i > 0)
                           return new PartialArrayResult(i, pnode, (GenerateError) childNode);

                        if (optional && emptyValue(ctx, value)) {
                           pnode.setSemanticValue(null, true, false);
                           return generateResult(ctx, null);
                        }
                        ((GenerateError) childNode).progress += progress;
                        return childNode;
                     }
                     pnode.addGeneratedNode(childNode);
                     progress += ctx.progress(childNode);
                  }
               }
            }
            // Otherwise, we need to let our children process as much of the array as they can and deal
            // with the partial result.
            else {
               pnode = newGeneratedParseNode(value);
               SemanticNodeList nodeList = (SemanticNodeList) value;
               int numProcessed = 0;
               do {
                  Object arrVal = generateElement(ctx, nodeList, false);
                  if (arrVal instanceof PartialArrayResult) {
                     PartialArrayResult par = (PartialArrayResult) arrVal;
                     pnode.copyGeneratedFrom(par.resultNode);
                     nodeList = getRemainingValue(nodeList, par.numProcessed, 0);
                     numProcessed += par.numProcessed;
                     progress += ctx.progress(par.resultNode);
                  }
                  else if (arrVal instanceof GenerateError) {
                     if (numProcessed != 0)
                        return new PartialArrayResult(numProcessed, pnode, (GenerateError) arrVal);
                     else {
                        pnode.setSemanticValue(null, true, false);
                        ((GenerateError) arrVal).progress += progress;
                        return arrVal;
                     }
                  }
                  else
                     return arrVal;

               } while(nodeList.size() > 0);
            }

            return generateResult(ctx, pnode);
         }
         else {
            return generateElement(ctx, value, false);
         }
      }
      finally {
         if (recordTime)
            PerfMon.end(recName);
      }
   }
/**
    * For the OrderedChoice, we have to figure out which choice was gnenerated.
    */
For the OrderedChoice, we have to figure out which choice was gnenerated.
For the OrderedChoice we have to figure out which choice was gnenerated .
public Object generate(GenerateContext ctx, Object value) {
      if (trace)
         System.out.println("*** tracing generate ordered choice");
      String recName = null;
      if (recordTime) {
         recName = "gen-" + toString();
         PerfMon.start(recName);
      }
      try {
         int progress = 0;
         if (getSemanticValueIsArray()) { 
            if (value == null) {
               if (optional)
                  return generateResult(ctx, null);
               else {
                  System.err.println("*** Null value encountered for required array element: " + this);
                  return null;
               }
            }
            ParentParseNode pnode = newGeneratedParseNode(value);
            if (!(value instanceof List)) {
               if (PString.isString(value)) {
                  boolean matchedAny = false;
                  IString strValue = PString.toIString(value);
                  while (strValue != null && strValue.length() > 0) {
                     Object childNode = generateElement(ctx, strValue, true);
                     if (childNode instanceof GenerateError) {
                        if (matchedAny)
                           return generateResult(ctx, pnode);
                        pnode.setSemanticValue(null, true, false);
                        if (optional && emptyValue(ctx, value)) {
                           return generateResult(ctx, null);
                        }
                        ((GenerateError)childNode).progress += progress;
                        return childNode;
                     }
                     matchedAny = true;
                     pnode.addGeneratedNode(childNode);
                     /*
                     * We have a string semantic value... in case it was formed from more than one parselet
                     * we need to pull out the part which was just generated before passing the next chunk on.
                     */
                     if (childNode != null) {
                        int childNodeLen = ParseUtil.toSemanticStringLength(childNode);
                        if (strValue.length() >= childNodeLen) {
                           strValue = strValue.substring(childNodeLen);
                           value = strValue;
                           progress += childNodeLen;
                        }
                        else
                           strValue = null;
                     }
                     else
                        strValue = null;
                  }
                  return generateResult(ctx, pnode);
               }
               return REPEAT_NODE_ERROR;
            }
            if (parameterType != ParameterType.ARRAY || !ParseUtil.isArrayParselet(parselets.get(0))) {
               List nodeList = (List) value;
               for (int i = 0; i < nodeList.size(); i++) {
                  Object nodeVal = nodeList.get(i);
                  if (PString.isString(nodeVal)) {
                     IString strValue = PString.toIString(nodeVal);
                     while (strValue != null && strValue.length() > 0) {
                        Object childNode = generateElement(ctx, strValue, true);
                        if (childNode instanceof GenerateError) {
                           pnode.setSemanticValue(null, true, false);
                           if (optional && emptyValue(ctx, value)) {
                              return generateResult(ctx, null);
                           }
                           ((GenerateError) childNode).progress += progress;
                           return childNode;
                        }
                        pnode.addGeneratedNode(childNode);
                        /*
                        * We have a string semantic value... in case it was formed from more than one parselet
                        * we need to pull out the part which was just generated before passing the next chunk on.
                        */
                        if (childNode != null) {
                           int childNodeLen = ParseUtil.toSemanticStringLength(childNode);
                           if (strValue.length() >= childNodeLen) {
                              strValue = strValue.substring(childNodeLen);
                              value = strValue;
                              progress += childNodeLen;
                           }
                           else
                              strValue = null;
                        }
                        else
                           strValue = null;
                     }
                     if (parameterType == ParameterType.STRING) {
                        return new PartialArrayResult(i+1, pnode);
                     }
                  }
                  else {
                     Object childNode = generateElement(ctx, nodeVal, true);
                     if (childNode instanceof GenerateError) {
                        if (i > 0)
                           return new PartialArrayResult(i, pnode, (GenerateError) childNode);
                        if (optional && emptyValue(ctx, value)) {
                           pnode.setSemanticValue(null, true, false);
                           return generateResult(ctx, null);
                        }
                        ((GenerateError) childNode).progress += progress;
                        return childNode;
                     }
                     pnode.addGeneratedNode(childNode);
                     progress += ctx.progress(childNode);
                  }
               }
            }
            else {
               pnode = newGeneratedParseNode(value);
               SemanticNodeList nodeList = (SemanticNodeList) value;
               int numProcessed = 0;
               do {
                  Object arrVal = generateElement(ctx, nodeList, false);
                  if (arrVal instanceof PartialArrayResult) {
                     PartialArrayResult par = (PartialArrayResult) arrVal;
                     pnode.copyGeneratedFrom(par.resultNode);
                     nodeList = getRemainingValue(nodeList, par.numProcessed, 0);
                     numProcessed += par.numProcessed;
                     progress += ctx.progress(par.resultNode);
                  }
                  else if (arrVal instanceof GenerateError) {
                     if (numProcessed != 0)
                        return new PartialArrayResult(numProcessed, pnode, (GenerateError) arrVal);
                     else {
                        pnode.setSemanticValue(null, true, false);
                        ((GenerateError) arrVal).progress += progress;
                        return arrVal;
                     }
                  }
                  else
                     return arrVal;
               } while(nodeList.size() > 0);
            }
            return generateResult(ctx, pnode);
         }
         else {
            return generateElement(ctx, value, false);
         }
      }
      finally {
         if (recordTime)
            PerfMon.end(recName);
      }
   }
public Object generate ( GenerateContext ctx , Object value ) { if ( trace ) System . out . println ( "*** tracing generate ordered choice" ) ; String recName = null ; if ( recordTime ) { recName = "gen-" + toString ( ) ; PerfMon . start ( recName ) ; } try { int progress = 0 ; if ( getSemanticValueIsArray ( ) ) { if ( value == null ) { if ( optional ) return generateResult ( ctx , null ) ; else { System . err . println ( "*** Null value encountered for required array element: " + this ) ; return null ; } } ParentParseNode pnode = newGeneratedParseNode ( value ) ; if ( ! ( value instanceof List ) ) { if ( PString . isString ( value ) ) { boolean matchedAny = false ; IString strValue = PString . toIString ( value ) ; while ( strValue != null && strValue . length ( ) > 0 ) { Object childNode = generateElement ( ctx , strValue , true ) ; if ( childNode instanceof GenerateError ) { if ( matchedAny ) return generateResult ( ctx , pnode ) ; pnode . setSemanticValue ( null , true , false ) ; if ( optional && emptyValue ( ctx , value ) ) { return generateResult ( ctx , null ) ; } ( ( GenerateError ) childNode ) . progress += progress ; return childNode ; } matchedAny = true ; pnode . addGeneratedNode ( childNode ) ; /*
                     * We have a string semantic value... in case it was formed from more than one parselet
                     * we need to pull out the part which was just generated before passing the next chunk on.
                     */ if ( childNode != null ) { int childNodeLen = ParseUtil . toSemanticStringLength ( childNode ) ; if ( strValue . length ( ) >= childNodeLen ) { strValue = strValue . substring ( childNodeLen ) ; value = strValue ; progress += childNodeLen ; } else strValue = null ; } else strValue = null ; } return generateResult ( ctx , pnode ) ; } return REPEAT_NODE_ERROR ; } if ( parameterType != ParameterType . ARRAY || ! ParseUtil . isArrayParselet ( parselets . get ( 0 ) ) ) { List nodeList = ( List ) value ; for ( int i = 0 ; i < nodeList . size ( ) ; i ++ ) { Object nodeVal = nodeList . get ( i ) ; if ( PString . isString ( nodeVal ) ) { IString strValue = PString . toIString ( nodeVal ) ; while ( strValue != null && strValue . length ( ) > 0 ) { Object childNode = generateElement ( ctx , strValue , true ) ; if ( childNode instanceof GenerateError ) { pnode . setSemanticValue ( null , true , false ) ; if ( optional && emptyValue ( ctx , value ) ) { return generateResult ( ctx , null ) ; } ( ( GenerateError ) childNode ) . progress += progress ; return childNode ; } pnode . addGeneratedNode ( childNode ) ; /*
                        * We have a string semantic value... in case it was formed from more than one parselet
                        * we need to pull out the part which was just generated before passing the next chunk on.
                        */ if ( childNode != null ) { int childNodeLen = ParseUtil . toSemanticStringLength ( childNode ) ; if ( strValue . length ( ) >= childNodeLen ) { strValue = strValue . substring ( childNodeLen ) ; value = strValue ; progress += childNodeLen ; } else strValue = null ; } else strValue = null ; } if ( parameterType == ParameterType . STRING ) { return new PartialArrayResult ( i + 1 , pnode ) ; } } else { Object childNode = generateElement ( ctx , nodeVal , true ) ; if ( childNode instanceof GenerateError ) { if ( i > 0 ) return new PartialArrayResult ( i , pnode , ( GenerateError ) childNode ) ; if ( optional && emptyValue ( ctx , value ) ) { pnode . setSemanticValue ( null , true , false ) ; return generateResult ( ctx , null ) ; } ( ( GenerateError ) childNode ) . progress += progress ; return childNode ; } pnode . addGeneratedNode ( childNode ) ; progress += ctx . progress ( childNode ) ; } } } else { pnode = newGeneratedParseNode ( value ) ; SemanticNodeList nodeList = ( SemanticNodeList ) value ; int numProcessed = 0 ; do { Object arrVal = generateElement ( ctx , nodeList , false ) ; if ( arrVal instanceof PartialArrayResult ) { PartialArrayResult par = ( PartialArrayResult ) arrVal ; pnode . copyGeneratedFrom ( par . resultNode ) ; nodeList = getRemainingValue ( nodeList , par . numProcessed , 0 ) ; numProcessed += par . numProcessed ; progress += ctx . progress ( par . resultNode ) ; } else if ( arrVal instanceof GenerateError ) { if ( numProcessed != 0 ) return new PartialArrayResult ( numProcessed , pnode , ( GenerateError ) arrVal ) ; else { pnode . setSemanticValue ( null , true , false ) ; ( ( GenerateError ) arrVal ) . progress += progress ; return arrVal ; } } else return arrVal ; } while ( nodeList . size ( ) > 0 ) ; } return generateResult ( ctx , pnode ) ; } else { return generateElement ( ctx , value , false ) ; } } finally { if ( recordTime ) PerfMon . end ( recName ) ; } }
For the OrderedChoice, we have to figure out which choice was gnenerated.
For the OrderedChoice we have to figure out which choice was gnenerated .
// Repeat but not chained // If we are not an array type or our children are all scalars, pull apart the array and // process it one by one. // In this case, we are accepting an array of nodes and must produce back an array of strings // Since our child node is a scalar, we are a repeat node, we must pull apart the string piece // by piece until we generate it all.  Any error at this point will fail. // If we are a String node and we receive an array of strings, we need to just pull the first // string out and return.  We could not have produced the array. // Otherwise, we need to let our children process as much of the array as they can and deal // with the partial result.