Exemplo n.º 1
0
  /**
   * Writes the configuration to the given stream, flushing it at the end
   *
   * @param out Output destination
   * @param comments Comments which are added to the beginning of the stream. One comment is written
   *     on each line, and they are all prefixed with the comment character
   */
  public void outputConfiguration(OutputStream out, String... comments) throws IOException {
    PrintWriter w = new PrintWriter(out);

    if (comments.length > 0) {
      for (String comment : comments) w.append("% " + comment + "\n");
      w.append("\n");
    }

    outputConfiguration(w, "");

    w.flush();
  }
Exemplo n.º 2
0
  private void outputConfiguration(Appendable str, String pre) throws IOException {
    StringBuilder output = MapConfiguration.output.get();
    for (String key : config.keySet()) {
      Object obj = config.get(key);

      if (obj instanceof String)
        str.append(pre).append(key).append(' ').append((String) obj).append('\n');
      else if (obj instanceof Array) {
        // write the type and size hints to output if the array is of a
        // native type (will optimize reading the array back in)
        String length = Integer.toString(Array.getLength(obj));
        if (obj instanceof double[]) {
          str.append(pre).append("double ").append(length).append(' ');
        } else if (obj instanceof int[]) {
          str.append(pre).append("int ").append(length).append(' ');
        }

        // all arrays have this prefix, regardless of type
        str.append(pre).append("array ").append(key).append('\n');

        if (obj instanceof double[]) {
          double[] array = (double[]) obj;
          for (int i = 0; i < array.length; ) {
            output.setLength(0);
            output.append(pre).append('\t');
            for (; i < array.length && output.length() < LINE_LENGTH; i++) {
              output.append(array[i]).append(' ');
            }
            output.append('\n');
            str.append(output);
          }
        } else if (obj instanceof int[]) {
          int[] array = (int[]) obj;
          for (int i = 0; i < array.length; ) {
            output.setLength(0);
            output.append(pre).append('\t');
            for (; i < array.length && output.length() < LINE_LENGTH; i++) {
              output.append(array[i]).append(' ');
            }
            output.append('\n');
            str.append(output);
          }
        } else { // if (obj instanceof String[]) {
          String[] array = (String[]) obj;
          for (int i = 0; i < array.length; ) {
            output.setLength(0);
            output.append(pre).append('\t');
            for (; i < array.length && output.length() < LINE_LENGTH; i++) {
              output.append(array[i]).append(' ');
            }
            output.append('\n');
            str.append(output);
          }
        }

        str.append(pre).append("end").append('\n');
      } else if (obj instanceof Configuration) {
        str.append(pre).append("begin ").append(key).append('\n');
        MapConfiguration sub = (MapConfiguration) obj;
        sub.outputConfiguration(str, pre + '\t');
        str.append(pre).append("end").append('\n');
      }
    }
  }