Exemplo n.º 1
0
 protected static void writeOptions(Writer writer, CeylonConfig config, String section)
     throws IOException {
   String[] names = config.getOptionNames(section);
   if (names != null) {
     for (int i = 0; i < names.length; i++) {
       String name = names[i];
       writeOption(writer, config, section, name);
       config.removeOption(section + "." + name);
       writer.write(System.lineSeparator());
     }
   }
 }
Exemplo n.º 2
0
 private static void writeSections(Writer writer, CeylonConfig config, OutputStream out)
     throws IOException {
   String[] sections = config.getSectionNames(null);
   Arrays.sort(sections);
   for (String section : sections) {
     if (config.getOptionNames(section).length > 0) {
       writer.write(System.lineSeparator());
       writer.write("[");
       writer.write(section);
       writer.write("]");
       writer.write(System.lineSeparator());
       writeOptions(writer, config, section);
     }
   }
 }
Exemplo n.º 3
0
 protected static void writeOption(Writer writer, CeylonConfig config, String section, String name)
     throws IOException {
   String[] values = config.getOptionValues(section + "." + name);
   if (values != null) {
     for (int i = 0; i < values.length; i++) {
       String value = values[i];
       writeOptionValue(writer, name, value);
       if (i < (values.length - 1)) {
         writer.write(System.lineSeparator());
       }
     }
   }
 }
Exemplo n.º 4
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);
 }
Exemplo n.º 5
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();
  }