Esempio n. 1
0
  /**
   * Create a new Item - reading the contents from the tab separated value set of columns. Loads it
   * into the Node object
   *
   * @param lang the Triceps context
   * @param sourceLine the line number within the source file (for debugging purposes)
   * @param sourceFile the name of the source file (for debugging purposes)
   * @param tsv the tab separated list of colums representing the node contents
   * @param numLanguage to know how many langauges to parsse?
   */
  Node(Triceps lang, int sourceLine, String sourceFile, String tsv, int numLanguages) {
    triceps = /*(lang == null) ? new Triceps() :*/ lang;
    String token;
    int field = 0;

    if (numLanguages < 1) {
      if (AUTHORABLE) {
        setParseError(triceps.get("numLanguages_must_be_greater_than_zero") + numLanguages);
      } else {
        setParseError("syntax error");
      }
      numLanguages = 1; // the default
    }

    this.sourceLine = sourceLine;
    this.sourceFile = sourceFile;
    this.numLanguages = numLanguages; // needs to be validated?

    int numLanguagesFound = 0;
    int numAnswersFound = 0;

    StringTokenizer ans = new StringTokenizer(tsv, "\t", true);

    while (ans.hasMoreTokens()) {
      String s = null;
      s = ans.nextToken();

      if (s.equals("\t")) {
        ++field;
        if (field == 7) {
          ++numLanguagesFound; // since once that field has been entered, has successfully coded a
          // language as present
        }
        if (field == 9 && numLanguagesFound < numLanguages) {
          field = 5; // so that next element is readback for the next language
        }
        continue;
      }

      switch (field) {
          /* there should be one copy of each of these */
        case 0:
          conceptName = (new ExcelDecoder()).decode(s);
          break;
        case 1:
          localName = (new ExcelDecoder()).decode(s);
          break;
        case 2:
          externalName = (new ExcelDecoder()).decode(s);
          break;
        case 3:
          dependencies = (new ExcelDecoder()).decode(s);
          break;
        case 4:
          questionOrEvalTypeField = (new ExcelDecoder()).decode(s);
          break;
          /* there are as many copies of each of these are there are languages */
        case 5:
          readback.addElement((new ExcelDecoder()).decode(s));
          break;
        case 6:
          questionOrEval.addElement((new ExcelDecoder()).decode(s));
          break;
        case 7:
          answerChoicesStr.addElement((new ExcelDecoder()).decode(s));
          break;
        case 8:
          helpURL.addElement((new ExcelDecoder()).decode(s));
          break;
          /* there are as many copies of each of these are there are answers - rudimentary support for arrays? */
        case 9:
          {
            int i = 0;
            try {
              i = Integer.parseInt((new ExcelDecoder()).decode(s));
            } catch (NumberFormatException t) {
              Logger.getLogger(LoggerName).log(Level.SEVERE, "", t);
              if (AUTHORABLE) {
                setParseError(triceps.get("languageNum_must_be_an_integer") + t.getMessage());
              } else {
                setParseError("syntax error");
              }
              i = 0; // default language
            }
            if (i < 0 || i >= numLanguages) {
              if (AUTHORABLE) {
                setParseError(
                    triceps.get("languageNum_must_be_in_range_zero_to")
                        + (numLanguages - 1)
                        + "): "
                        + i);
              } else {
                setParseError("syntax error");
              }
              i = 0; // default language
            }
            answerLanguageNum = i;
          }
          break;
        case 10:
          questionAsAsked = (new ExcelDecoder()).decode(s);
          break;
        case 11:
          answerGiven = (new ExcelDecoder()).decode(s);
          break;
        case 12:
          comment = (new ExcelDecoder()).decode(s);
          break;
        case 13:
          answerTimeStampStr = (new ExcelDecoder()).decode(s);
          break;
        default:
          break; // ignore extras
      }
    }
    if (dependencies == null || dependencies.trim().length() == 0) {
      if (AUTHORABLE) {
        setParseError(triceps.get("dependencies_column_is_missing"));
      } else {
        setParseError("syntax error");
      }
    }
    if (localName != null && localName.trim().length() > 0) {
      localName = localName.trim();
      if (Character.isDigit(localName.charAt(0))) {
        if (AUTHORABLE) {
          setNamingError(triceps.get("localName_may_not_begin_with_a_digit") + localName);
        } else {
          setParseError("syntax error");
        }
        localName = "_" + localName;
      }
      if (!isNMTOKEN(localName)) {
        if (AUTHORABLE) {
          setNamingError(
              triceps.get("localName_should_only_contain_letters_digits_and_underscores")
                  + localName);
        } else {
          setParseError("syntax error");
        }
      }
    } else {
      setNamingError(triceps.get("localName_must_be_specified"));
    }

    parseQuestionOrEvalTypeField();

    if (questionOrEvalType == BADTYPE) {
      if (AUTHORABLE) {
        setParseError(triceps.get("invalid_questionOrEvalType") + questionOrEvalTypeField);
      } else {
        setParseError("syntax error");
      }
    }

    for (int i = 0; i < answerChoicesStr.size(); ++i) {
      parseAnswerOptions(i, (String) answerChoicesStr.elementAt(i));
    }

    if (datumType == Datum.INVALID) {
      if (AUTHORABLE) {
        setParseError(triceps.get("invalid_dataType"));
      } else {
        setParseError("syntax error");
      }
    }
  }