Exemple #1
0
  public void save() {
    try {
      if (file.getParentFile() != null) {
        file.getParentFile().mkdirs();
      }

      if (!file.exists() && !file.createNewFile()) {
        return;
      }

      if (file.canWrite()) {
        FileOutputStream fos = new FileOutputStream(file);
        BufferedWriter buffer = new BufferedWriter(new OutputStreamWriter(fos, defaultEncoding));

        buffer.write("# Configuration file\r\n");
        buffer.write("# Generated on " + DateFormat.getInstance().format(new Date()) + "\r\n");
        buffer.write("\r\n");

        for (Map.Entry<String, Map<String, Property>> category : categories.entrySet()) {
          buffer.write("####################\r\n");
          buffer.write("# " + category.getKey() + " \r\n");
          if (customCategoryComments.containsKey(category.getKey())) {
            buffer.write("#===================\r\n");
            String comment = customCategoryComments.get(category.getKey());
            Splitter splitter = Splitter.onPattern("\r?\n");
            for (String commentLine : splitter.split(comment)) {
              buffer.write("# ");
              buffer.write(commentLine + "\r\n");
            }
          }
          buffer.write("####################\r\n\r\n");

          String catKey = category.getKey();
          if (!allowedProperties.matchesAllOf(catKey)) {
            catKey = '"' + catKey + '"';
          }
          buffer.write(catKey + " {\r\n");
          writeProperties(buffer, category.getValue().values());
          buffer.write("}\r\n\r\n");
        }

        buffer.close();
        fos.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Exemple #2
0
  public void load() {
    BufferedReader buffer = null;
    try {
      if (file.getParentFile() != null) {
        file.getParentFile().mkdirs();
      }

      if (!file.exists() && !file.createNewFile()) {
        return;
      }

      if (file.canRead()) {
        UnicodeInputStreamReader input =
            new UnicodeInputStreamReader(new FileInputStream(file), defaultEncoding);
        defaultEncoding = input.getEncoding();
        buffer = new BufferedReader(input);

        String line;
        Map<String, Property> currentMap = null;

        while (true) {
          line = buffer.readLine();

          if (line == null) {
            break;
          }

          int nameStart = -1, nameEnd = -1;
          boolean skip = false;
          boolean quoted = false;
          for (int i = 0; i < line.length() && !skip; ++i) {
            if (Character.isLetterOrDigit(line.charAt(i))
                || ALLOWED_CHARS.indexOf(line.charAt(i)) != -1
                || (quoted && line.charAt(i) != '"')) {
              if (nameStart == -1) {
                nameStart = i;
              }

              nameEnd = i;
            } else if (Character.isWhitespace(line.charAt(i))) {
              // ignore space charaters
            } else {
              switch (line.charAt(i)) {
                case '#':
                  skip = true;
                  continue;

                case '"':
                  if (quoted) {
                    quoted = false;
                  }
                  if (!quoted && nameStart == -1) {
                    quoted = true;
                  }
                  break;

                case '{':
                  String scopeName = line.substring(nameStart, nameEnd + 1);

                  currentMap = categories.get(scopeName);
                  if (currentMap == null) {
                    currentMap = new TreeMap<String, Property>();
                    categories.put(scopeName, currentMap);
                  }

                  break;

                case '}':
                  currentMap = null;
                  break;

                case '=':
                  String propertyName = line.substring(nameStart, nameEnd + 1);

                  if (currentMap == null) {
                    throw new RuntimeException("property " + propertyName + " has no scope");
                  }

                  Property prop = new Property();
                  prop.setName(propertyName);
                  prop.value = line.substring(i + 1);
                  i = line.length();

                  currentMap.put(propertyName, prop);

                  break;

                default:
                  throw new RuntimeException("unknown character " + line.charAt(i));
              }
            }
          }
          if (quoted) {
            throw new RuntimeException("unmatched quote");
          }
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (buffer != null) {
        try {
          buffer.close();
        } catch (IOException e) {
        }
      }
    }
  }