예제 #1
0
 /**
  * Add metadata fields. These are appended to existing values. Use <code>clearDC</code> to remove
  * values. The ordering of values passed in is maintained.
  *
  * <p>If metadata authority control is available, try to get authority values. The authority
  * confidence depends on whether authority is <em>required</em> or not.
  *
  * @param schema the schema for the metadata field. <em>Must</em> match the <code>name</code> of
  *     an existing metadata schema.
  * @param element the metadata element name
  * @param qualifier the metadata qualifier name, or <code>null</code> for unqualified
  * @param lang the ISO639 language code, optionally followed by an underscore and the ISO3166
  *     country code. <code>null</code> means the value has no language (for example, a date).
  * @param values the values to add.
  */
 public void addMetadata(
     String schema, String element, String qualifier, String lang, String[] values) {
   MetadataAuthorityManager mam = MetadataAuthorityManager.getManager();
   String fieldKey = MetadataAuthorityManager.makeFieldKey(schema, element, qualifier);
   if (mam.isAuthorityControlled(fieldKey)) {
     String authorities[] = new String[values.length];
     int confidences[] = new int[values.length];
     for (int i = 0; i < values.length; ++i) {
       getAuthoritiesAndConfidences(fieldKey, values, authorities, confidences, i);
     }
     addMetadata(schema, element, qualifier, lang, values, authorities, confidences);
   } else {
     addMetadata(schema, element, qualifier, lang, values, null, null);
   }
 }
예제 #2
0
  /**
   * Add metadata fields. These are appended to existing values. Use <code>clearDC</code> to remove
   * values. The ordering of values passed in is maintained.
   *
   * @param schema the schema for the metadata field. <em>Must</em> match the <code>name</code> of
   *     an existing metadata schema.
   * @param element the metadata element name
   * @param qualifier the metadata qualifier name, or <code>null</code> for unqualified
   * @param lang the ISO639 language code, optionally followed by an underscore and the ISO3166
   *     country code. <code>null</code> means the value has no language (for example, a date).
   * @param values the values to add.
   * @param authorities the external authority key for this value (or null)
   * @param confidences the authority confidence (default 0)
   */
  public void addMetadata(
      String schema,
      String element,
      String qualifier,
      String lang,
      String[] values,
      String authorities[],
      int confidences[]) {
    List<DCValue> dublinCore = getMetadata();
    MetadataAuthorityManager mam = MetadataAuthorityManager.getManager();
    boolean authorityControlled = mam.isAuthorityControlled(schema, element, qualifier);
    boolean authorityRequired = mam.isAuthorityRequired(schema, element, qualifier);
    String fieldName = schema + "." + element + ((qualifier == null) ? "" : "." + qualifier);

    // We will not verify that they are valid entries in the registry
    // until update() is called.
    for (int i = 0; i < values.length; i++) {
      DCValue dcv = new DCValue();
      dcv.schema = schema;
      dcv.element = element;
      dcv.qualifier = qualifier;
      dcv.language = (lang == null ? null : lang.trim());

      // Logic to set Authority and Confidence:
      //  - normalize an empty string for authority to NULL.
      //  - if authority key is present, use given confidence or NOVALUE if not given
      //  - otherwise, preserve confidence if meaningful value was given since it may document a
      // failed authority lookup
      //  - CF_UNSET signifies no authority nor meaningful confidence.
      //  - it's possible to have empty authority & CF_ACCEPTED if e.g. user deletes authority key
      if (authorityControlled) {
        if (authorities != null && authorities[i] != null && authorities[i].length() > 0) {
          dcv.authority = authorities[i];
          dcv.confidence = confidences == null ? Choices.CF_NOVALUE : confidences[i];
        } else {
          dcv.authority = null;
          dcv.confidence = confidences == null ? Choices.CF_UNSET : confidences[i];
        }
        // authority sanity check: if authority is required, was it supplied?
        // XXX FIXME? can't throw a "real" exception here without changing all the callers to expect
        // it, so use a runtime exception
        if (authorityRequired && (dcv.authority == null || dcv.authority.length() == 0)) {
          throw new IllegalArgumentException(
              "The metadata field \""
                  + fieldName
                  + "\" requires an authority key but none was provided. Vaue=\""
                  + dcv.value
                  + "\"");
        }
      }
      if (values[i] != null) {
        // remove control unicode char
        String temp = values[i].trim();
        char[] dcvalue = temp.toCharArray();
        for (int charPos = 0; charPos < dcvalue.length; charPos++) {
          if (Character.isISOControl(dcvalue[charPos])
              && !String.valueOf(dcvalue[charPos]).equals("\u0009")
              && !String.valueOf(dcvalue[charPos]).equals("\n")
              && !String.valueOf(dcvalue[charPos]).equals("\r")) {
            dcvalue[charPos] = ' ';
          }
        }
        dcv.value = String.valueOf(dcvalue);
      } else {
        dcv.value = null;
      }
      dublinCore.add(dcv);
      addDetails(fieldName);
    }

    if (values.length > 0) {
      modifiedMetadata = true;
    }
  }