コード例 #1
0
ファイル: ProjectParser.java プロジェクト: periclesjr/Sapelli
  @Override
  protected void parseStartElement(
      String uri, String localName, String qName, XMLAttributes attributes) throws Exception {
    try {
      // <Sapelli-Collector-Project>, or <ExCiteS-Collector-Project> (for backwards compatibility)
      if (qName.equals(TAG_PROJECT) || qName.equals(TAG_PROJECT_V1X)) {
        if (project != null) {
          addWarning("Ignoring additional " + TAG_PROJECT + " or " + TAG_PROJECT_V1X + " element.");
          return;
        }

        // Detect format version...
        int formatVersion =
            attributes.getInteger(
                ATTRIBUTE_PROJECT_FORMAT,
                qName.equals(TAG_PROJECT_V1X)
                    ? Format.v1_x.ordinal()
                    : DEFAULT_FORMAT
                        .ordinal()); // default: v1.x for ExCiteS tag, DEFAULT_FORMAT for Sapelli
        // tag.
        // 	too low:
        if (formatVersion < LOWEST_SUPPORTED_FORMAT.ordinal())
          throw new SAXException("Unsupported format version: " + formatVersion);
        // 	too high:
        else if (formatVersion
            > HIGHEST_SUPPORTED_FORMAT
                .ordinal()) { // issue warning and then try to parse as highest supported format
          // (might fail)
          addWarning(
              "Format version reported in XML file ("
                  + formatVersion
                  + ") is unsupported ("
                  + LOWEST_SUPPORTED_FORMAT
                  + " <= supported <= "
                  + HIGHEST_SUPPORTED_FORMAT
                  + "), attempting parsing with rules for version "
                  + HIGHEST_SUPPORTED_FORMAT);
          format = HIGHEST_SUPPORTED_FORMAT;
        }
        //	within range (or default because missing attribute):
        else format = Format.values()[formatVersion];

        // Project...
        project =
            new Project(
                (format == Format.v1_x)
                    ? Project.PROJECT_ID_V1X_TEMP
                    : // for format = 1 we set a temp id value (will be replaced by Form:schema-id)
                    attributes.getRequiredInteger(
                        qName,
                        ATTRIBUTE_PROJECT_ID,
                        "because format is >= 2"), // id is required for format >= 2
                attributes.getRequiredString(TAG_PROJECT, ATTRIBUTE_PROJECT_NAME, true, false),
                attributes.getString(ATTRIBUTE_PROJECT_VARIANT, null, true, false),
                attributes.getString(
                    ATTRIBUTE_PROJECT_VERSION, Project.DEFAULT_VERSION, true, false),
                fingerPrint);

        // Set default language (or "en" if not specified):
        String lang = attributes.getString(ATTRIBUTE_PROJECT_DEFAULT_LANG, null, true, false);
        project.setDefaultLanguage(lang != null ? lang : Project.DEFAULT_DEFAULT_LANGUAGE);
        if (lang == null && format == Format.v2_x)
          addWarning(
              "No valid default language has been specified for this project. Languages should be declared using the BCP-47 syntax (e.g. \"fr-CA\" for Canadian French). English (en) will be set as the default language for this project.");

        // Read startForm ID:
        startFormID = attributes.getString(ATTRIBUTE_PROJECT_START_FORM, null, true, false);

        // Add subtree parsers:
        addSubtreeParser(new ConfigurationParser(this));
        addSubtreeParser(new FormParser(this));
      }
      // <?>
      else addWarning("Ignored unrecognised or invalidly placed/repeated element <" + qName + ">.");
    } catch (Exception e) {
      e.printStackTrace(System.err);
      throw new Exception("Error while parsing element <" + qName + ">: " + e.getMessage(), e);
    }
  }