Exemplo n.º 1
0
  /** escape characters */
  protected void string(Object obj) {
    this.add('"');

    CharacterIterator it = new StringCharacterIterator(obj.toString());

    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
      if (c == '"') {
        this.add("\\\"");
      } else if (c == '\\') {
        this.add("\\\\");
      } else if (c == '/') {
        this.add("\\/");
      } else if (c == '\b') {
        this.add("\\b");
      } else if (c == '\f') {
        this.add("\\f");
      } else if (c == '\n') {
        this.add("\\n");
      } else if (c == '\r') {
        this.add("\\r");
      } else if (c == '\t') {
        this.add("\\t");
      } else if (Character.isISOControl(c)) {
        this.unicode(c);
      } else {
        this.add(c);
      }
    }

    this.add('"');
  }
Exemplo n.º 2
0
 // Find and unescape a string.
 private String readString() {
   StringBuilder sb = new StringBuilder();
   char c = next();
   if (c != '"') throw new RuntimeException("expecting start of string");
   while (true)
     switch (c = next()) {
       case '"':
         return sb.toString();
       case '\\':
         sb.append(readEscaped());
         continue;
       default:
         if (Character.isISOControl(c)) throw new RuntimeException("illegal character in string");
         sb.append(c);
     }
 }
 /** Converts a char to a printable String for display */
 public static String charToString(char c) {
   if (c == '\n') {
     return "NL";
   } else if (c == '\r') {
     return "CR";
   } else if (c == '\t') {
     return "TAB";
   } else if (Character.isISOControl(c)) {
     int i = (int) c;
     return "\\u"
         + Character.forDigit((i / 2048) % 16, 16)
         + Character.forDigit((i / 256) % 16, 16)
         + Character.forDigit((i / 16) % 16, 16)
         + Character.forDigit((i) % 16, 16);
   }
   return Character.toString(c);
 }
Exemplo n.º 4
0
 private boolean stranger(String myName) {
   // tty.println("=+= " + (myName.getBytes()[0])+" =" );
   // tty.println("=+= " + Character.isISOControl(myName.charAt(0)));
   if (Character.isISOControl(myName.charAt(0))) myName = "QUIT";
   if (myName.equals("QUIT")) {
     writeLine("! Thank you for coming!");
   }
   if (myName == null || myName.equals("QUIT")) {
     tty.println(" --- from " + remoteAddr + " at " + now.toString());
     return true;
   } // "QUIT" is not allowed as a nick name
   if (myName.equals(SECRET_NAME)) {
     tty.println("GiGi  from " + remoteAddr + " at " + now.toString());
     doList();
     return true;
   }
   return false; // a usual user name
 }
Exemplo n.º 5
0
  public void keyReleased(KeyEvent e) {
    char ch = e.getKeyChar();
    if (ch == KeyEvent.CHAR_UNDEFINED || Character.isISOControl(ch)) return;
    int pos = m_editor.getCaretPosition();
    String str = m_editor.getText();
    if (str.length() == 0) return;

    for (int k = 0; k < m_comboBox.getItemCount(); k++) {
      String item = m_comboBox.getItemAt(k).toString();
      if (item.startsWith(str)) {
        m_editor.setText(item);
        m_editor.setCaretPosition(item.length());
        m_editor.moveCaretPosition(pos);
        m_comboBox.setSelectedItem(item);
        break;
      }
    }
  }
Exemplo n.º 6
0
 String stringLiteral(String value) {
   StringBuilder result = new StringBuilder();
   result.append('"');
   for (int i = 0; i < value.length(); i++) {
     char c = value.charAt(i);
     switch (c) {
       case '"':
         result.append("\\\"");
         break;
       case '\\':
         result.append("\\\\");
         break;
       case '\b':
         result.append("\\b");
         break;
       case '\t':
         result.append("\\t");
         break;
       case '\n':
         result.append("\\n");
         if (i + 1 < value.length()) {
           result.append("\"\n").append("  ").append("  ").append("+ \"");
         }
         break;
       case '\f':
         result.append("\\f");
         break;
       case '\r':
         result.append("\\r");
         break;
       default:
         if (Character.isISOControl(c)) {
           new Formatter(result).format("\\u%04x", (int) c);
         } else {
           result.append(c);
         }
     }
   }
   result.append('"');
   return result.toString();
 }
Exemplo n.º 7
0
  @Override
  public void addMetadata(
      Context context,
      T dso,
      MetadataField metadataField,
      String lang,
      List<String> values,
      List<String> authorities,
      List<Integer> confidences)
      throws SQLException {
    boolean authorityControlled = metadataAuthorityService.isAuthorityControlled(metadataField);
    boolean authorityRequired = metadataAuthorityService.isAuthorityRequired(metadataField);

    // We will not verify that they are valid entries in the registry
    // until update() is called.
    for (int i = 0; i < values.size(); i++) {

      MetadataValue metadataValue = metadataValueService.create(context, dso, metadataField);
      metadataValue.setLanguage(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.get(i) != null && authorities.get(i).length() > 0) {
          metadataValue.setAuthority(authorities.get(i));
          metadataValue.setConfidence(
              confidences == null ? Choices.CF_NOVALUE : confidences.get(i));
        } else {
          metadataValue.setAuthority(null);
          metadataValue.setConfidence(confidences == null ? Choices.CF_UNSET : confidences.get(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
            && (metadataValue.getAuthority() == null
                || metadataValue.getAuthority().length() == 0)) {
          throw new IllegalArgumentException(
              "The metadata field \""
                  + metadataField.toString()
                  + "\" requires an authority key but none was provided. Value=\""
                  + values.get(i)
                  + "\"");
        }
      }
      if (values.get(i) != null) {
        // remove control unicode char
        String temp = values.get(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] = ' ';
          }
        }
        metadataValue.setValue(String.valueOf(dcvalue));
        ;
      } else {
        metadataValue.setValue(null);
      }
      // An update here isn't needed, this is persited upon the merge of the owning object
      //            metadataValueService.update(context, metadataValue);
      dso.addDetails(metadataField.toString());
    }
  }
Exemplo n.º 8
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;
    }
  }