public Gap getGapByID(String id) {
   for (int i = 0; i < fGapList.size(); i++) {
     Gap aGap = (Gap) fGapList.getBasicElementAt(i);
     if (aGap.getId().equals(id)) return aGap;
   }
   return null;
 }
 public GapText getGapTextByID(String id) {
   for (int i = 0; i < fGapTextList.size(); i++) {
     GapText aGapText = (GapText) fGapTextList.getBasicElementAt(i);
     if (aGapText.getId().equals(id)) return aGapText;
   }
   return null;
 }
 /** Creates a new instance of GapMatchInteraction */
 public GapMatchInteraction(AssessmentItem anAssessmentItem, int choiceNumber) { // used by UI
   setAssessmentItem(anAssessmentItem);
   fShuffle = true; // default
   for (int i = 0; i < choiceNumber; i++) {
     fGapTextList.addElement(new GapText(anAssessmentItem, this));
   }
   ResponseDeclaration aResponseDeclaration = createResponseDeclaration();
   aResponseDeclaration.setAssociatedOutcomeDeclaration(
       createOutcomeForMatchCorrect(aResponseDeclaration));
 }
  public Element toJDOM() {
    Element aInteractionElement = super.toJDOM();

    aInteractionElement.setAttribute(AssessmentElementFactory.SHUFFLE, fShuffle ? "true" : "false");

    for (int i = 0; i < fGapTextList.size(); i++) {
      Element aGapTextElement = ((GapText) fGapTextList.getBasicElementAt(i)).toJDOM();
      if (aGapTextElement != null) aInteractionElement.addContent(aGapTextElement);
    }

    Element aBlockElement = new Element(AssessmentElementFactory.P, getNamespace());

    if (fGapList.size() <= 0) {
      aBlockElement.setText(data);
      aInteractionElement.addContent(aBlockElement);
      return aInteractionElement;
    }

    int lastEnd = 0;
    for (int i = 0; i < fGapList.size(); i++) {
      Gap aGap = (Gap) fGapList.getBasicElementAt(i);

      if (aGap != null) {
        Element aGapElement = aGap.toJDOM();
        int start = aGap.getStart();
        if (start == lastEnd) {
          aBlockElement.addContent(aGapElement);
        } else {
          Text aText = new Text(data.substring(lastEnd, start - 1));
          aBlockElement.addContent(aText);
          aBlockElement.addContent(aGapElement);
        }
        lastEnd = start + aGap.getLength();
      }
    }
    if (lastEnd < data.length()) {
      Text aText = new Text(data.substring(lastEnd));
      aBlockElement.addContent(aText);
    }
    aInteractionElement.addContent(aBlockElement);
    return aInteractionElement;
  }
 public Gap getGapAt(int index) {
   return (Gap) fGapList.getBasicElementAt(index);
 }
 public void setGapAt(int index, Gap aGap) {
   fGapList.setElementAt(index, aGap);
 }
 public void addGapAt(int index, Gap aGap) {
   fGapList.addElementAt(index, aGap);
 }
 public void addGap(Gap aGap) {
   fGapList.addElement(aGap);
 }
  public void fromJDOM(Element element) {
    super.fromJDOM(element);

    for (Object attribute : element.getAttributes()) {
      String tag = ((Attribute) attribute).getName();
      String value = ((Attribute) attribute).getValue();

      if (AssessmentElementFactory.IDENTIFIER.equals(tag)) {
        fID = value;
      } else if (AssessmentElementFactory.SHUFFLE.equals(tag)) {
        fShuffle = "true".equalsIgnoreCase(value);
      }
    }

    for (Object o : element.getChildren()) {
      Element child = (Element) o;
      String tag = child.getName();
      // String value = child.getText();

      if (tag.equals(AssessmentElementFactory.GAP_TEXT)) {
        GapText aGapText = new GapText(fAssessmentItem, this);
        aGapText.fromJDOM((Element) child);
        fGapTextList.addElement(aGapText);
      } else if (tag.equals(AssessmentElementFactory.P)) {
        List grandChildren = child.getContent();
        Iterator iterator = grandChildren.iterator();
        Text textString = new Text("");
        while (iterator.hasNext()) {
          Object grandChild = iterator.next();

          if (grandChild instanceof Element) {
            Element e = (Element) grandChild;
            if (e.getName().equals(AssessmentElementFactory.GAP)) {

              Gap aGap = new Gap(fAssessmentItem, this);
              aGap.fromJDOM((Element) grandChild);

              aGap.setStart(textString.getText().length() + 1);

              InterpretationValue aInterpretationValue =
                  getResponseDeclaration().getCorrectResponse();
              for (int i = 0; i < aInterpretationValue.size(); i++) {
                String valueData = aInterpretationValue.getValueAt(i).getData();
                int index = valueData.indexOf(" ");
                if (index != -1) {
                  String targetID = valueData.substring(index + 1);
                  if (targetID.equals(aGap.getId())) {
                    String sourceID = valueData.substring(0, index);
                    GapText aGapText = getGapTextByID(sourceID);
                    aGap.setData(aGapText.getData());
                    aGap.setMatchedGapText(aGapText);
                    aGap.setLength(aGapText.getData().length());
                    if (!aGapText.getData().equals("")) {
                      textString.append(" " + aGapText.getData());
                    } else {
                      System.out.println("correct choice is an empty string");
                    }
                    fGapList.addElement(aGap);
                  }
                }
              }
            }
          } else if (grandChild instanceof Text) {
            textString.append((Text) grandChild);
          }
        }
        data = textString.getText();
      }
    }
  }
 public void removeGapText(GapText aGapText) {
   fGapTextList.removeElement(aGapText);
 }
 public GapText getGapTextAt(int index) {
   return (GapText) fGapTextList.getBasicElementAt(index);
 }
 public void addGapText(GapText aGapText) {
   fGapTextList.addElement(aGapText);
 }
 public void removeGap(Gap aGap) {
   fGapList.removeElement(aGap);
 }