Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the best way to parse this configuration file?
    primarykey
    data
    text
    <p>I am working on a personal project that uses a custom config file. The basic format of the file looks like this:</p> <pre><code>[users] name: bob attributes: hat: brown shirt: black another_section: key: value key2: value2 name: sally sex: female attributes: pants: yellow shirt: red </code></pre> <p>There can be an arbitrary number of users and each can have different key/value pairs and there can be nested keys/values under a section using tab-stops. I know that I can use json, yaml, or even xml for this config file, however, I'd like to keep it custom for now.</p> <p>Parsing shouldn't be difficult at all as I have already written code to do parse it. My question is, what is the best way to go about parsing this using <strong>clean</strong> and <strong>structured</strong> code as well as writing in a way that won't make changes in the future difficult (there might be multiple nests in the future). Right now, my code looks utterly disgusting. For example,</p> <pre><code>private void parseDocument() { String current; while((current = reader.readLine()) != null) { if(current.equals("") || current.startsWith("#")) { continue; //comment } else if(current.startsWith("[users]")) { parseUsers(); } else if(current.startsWith("[backgrounds]")) { parseBackgrounds(); } } } private void parseUsers() { String current; while((current = reader.readLine()) != null) { if(current.startsWith("attributes:")) { while((current = reader.readLine()) != null) { if(current.startsWith("\t")) { //add user key/values to User object } else if(current.startsWith("another_section:")) { while((current = reader.readLine()) != null) { if(current.startsWith("\t")) { //add user key/values to new User object } else if (current.equals("")) { //newline means that a new user is up to parse next } } } } } else if(!current.isEmpty()) { // } } } </code></pre> <p>As you can see, the code is pretty messy, and I have cut it short for the presentation here. I feel there are better ways to do this as well maybe not using BufferedReader. Can someone please provide possibly a better way or approach that is not as convoluted as mine? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI'll just add this as a comment since I'm not really answering your question in regards to that particular config setup. As this is a personal project, is it feasible to change to something like XML? Then you could use something like JAXP.
      singulars
    2. COChrist what is it with Java developers and XML? Cletus' suggestion of YAML is spot on because that is what the OP's configuration looks most like. You'd know this if you expanded your horizons through Ruby on Rails or even the PHP framework Symfony. I'm a Java developer too, and this constant reliance on XML by most other Java developers is somewhere between comical and pathetic. I once showed a micro MVC framework I've developed to a Java "architect" and he remarked "Where's the mapping", by which he meant XML. Better question: why can't other formats be used to populate map(ping)s?
      singulars
    3. COIn a few comments you state that the braces, brackets, quotes, etc of standard formats are too much clutter. I just wanted to point out that there is a good reason for that "clutter". Currently you are depending on tabs, which is really brittle. What happens when someone opens the config file in something that converts tabs to spaces? Minimally, you should support spaces there as well.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload