Ejemplo n.º 1
0
 /** Write the given configuration to the given output stream. */
 public static void write(CeylonConfig orgconfig, OutputStream out) throws IOException {
   final CeylonConfig config = orgconfig.copy();
   final Writer writer = new BufferedWriter(new OutputStreamWriter(out, Charset.forName("UTF-8")));
   writeSections(writer, config, out);
 }
Ejemplo n.º 2
0
  /**
   * Reads config from the given input, updating it using the given configuration and writing in to
   * the given output.
   */
  public static void write(CeylonConfig orgconfig, InputStream in, OutputStream out)
      throws IOException {
    final CeylonConfig config = orgconfig.copy();
    final Writer writer = new BufferedWriter(new OutputStreamWriter(out, Charset.forName("UTF-8")));
    ConfigReader reader =
        new ConfigReader(
            in,
            new ImprovedConfigReaderListenerAdapter(
                new ImprovedConfigReaderListener() {
                  private boolean skipToNewline = false;

                  @Override
                  public void setup() throws IOException {
                    // Ignoring setup
                  }

                  @Override
                  public void onSection(String section, String text) throws IOException {
                    if (config.isSectionDefined(section)) {
                      writer.write(text);
                      skipToNewline = false;
                    } else {
                      skipToNewline = true;
                    }
                  }

                  @Override
                  public void onSectionEnd(String section) throws IOException {
                    writeOptions(writer, config, section);
                  }

                  @Override
                  public void onOption(String name, String value, String text) throws IOException {
                    if (config.isOptionDefined(name)) {
                      String[] newValues = config.getOptionValues(name);
                      if (value.equals(newValues[0])) {
                        // The value hasn't changed, we'll write the option *exactly* as it was
                        writer.write(text);
                      } else {
                        // The value has changed, we will write a new option
                        CeylonConfig.Key k = new CeylonConfig.Key(name);
                        writeOptionValue(writer, k.getOptionName(), newValues[0]);
                      }
                      removeOptionValue(name);
                      skipToNewline = false;
                    } else {
                      skipToNewline = true;
                    }
                  }

                  @Override
                  public void onComment(String text) throws IOException {
                    if (skipToNewline) {
                      skipToNewline = !text.contains("\n");
                    } else {
                      writer.write(text);
                    }
                  }

                  @Override
                  public void onWhitespace(String text) throws IOException {
                    if (skipToNewline) {
                      skipToNewline = !text.contains("\n");
                    } else {
                      writer.write(text);
                    }
                  }

                  @Override
                  public void cleanup() throws IOException {
                    // Ignoring cleanup
                  }

                  private void removeOptionValue(String name) {
                    String[] values = config.getOptionValues(name);
                    if (values.length > 1) {
                      values = Arrays.copyOfRange(values, 1, values.length);
                      config.setOptionValues(name, values);
                    } else {
                      config.removeOption(name);
                    }
                  }
                }));
    reader.process();
    writer.flush();
    // Now write what's left of the configuration to the output
    writeSections(writer, config, out);
    writer.flush();
  }