-1

I am working on creating my own Custom file parser (it can read & write a .yml file or any other custom text file)

Right now I am just working on reading a pre-written file. (Yes i have looked at several other similar issues. these didn't seem to even get close to what i'm looking for as i don't want to use an external library. [a link] (How do I parse a YAML file?) )

However, the code I have currently does not seem to be returning the correct results. It keeps returning an empty string, implying that the file didn't contain the values i asked for.


protected String readValue(String value) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getResource("Locale.txt")));
        boolean isMultiline = false;
        String multiLineCombo = "";



        for (String s : reader.lines().toArray(String[]::new)) {
            if (s.startsWith(Pattern.compile("[^a-zA-Z0-9]") + "")) {
                continue;
            }

            if (s.trim().startsWith(Pattern.quote("(?i)") + value + ":")) {
                if (s.contains(": |-")) {
                    isMultiline = true;
                    continue;
                }
                if (isMultiline) {
                    if (!s.contains(":")) {
                        multiLineCombo = multiLineCombo.concat(s + "\n");
                        continue;
                    } else {
                        isMultiline = false;
                        return multiLineCombo;
                    }

                }

                try {
                    reader.close();
                } catch (Exception e) {
                }
                return s.substring(s.indexOf(":"));
            }

        }

        return "";
    }

I'm looking for it to read simple values constructed like so:


# comments...
OwnerName: 'something here'

# more comments
# .
# yeet...
ListOfNames:
- Ann
- Brent
- Crumpled Bananas
- more values

# multiline strings like this
MultipleLines: |-
  Line one
  Line two
  and line three


Yes, I have written the values i'm trying to pull in the target file, the values are in there, i'm just reading them wrongly.

If anybody has some tips or can help me as to what i'm doing wrong here, that would be awesome!

Edit: Seems i need to explain why i'm attempting to create my own file reader instead of using a third-party such as SnakeYaml... Mainly because i do not want to specifically parse only YAML files, I'm looking for this as more of my own custom data storage system. The library i'm working with has a built-in yaml reader/writer, but it ignores comments, deleting them and removing any white spaces and formatting i add in. Additionally, I do not want this to be overly dependent on whether said java application has the specified libraries available. (depending on whether it has access to the Internet or not to download resources) As such, i want to create my own, both to negate having to depend on external libraries and to increase reliability and customize-ability.

  • Have you tried _debugging_ your code? Learning how to use a debugger is probably the best thing for you to do right now. – Tim Biegeleisen Mar 25 '19 at 13:19
  • Completely forgot about that, I will start there. – FlailoftheLord - Mar 25 '19 at 13:24
  • At your level of experience you should definitely use a ready-made YAML parser instead of rolling your own. The YAML specification is over 70 pages long and full of horrible details and edge cases. You won't get anything right by doing it yourself. Therefore it is essential that you _explain_ in your question why you don't want to use a third-party YAML parser. – Roland Illig Mar 25 '19 at 13:53
  • To clarify, i was looking more into reading any filetype formatted as a .yml file, this could be a text file, .java file, anything with the correct encoding and format which i am specifying. – FlailoftheLord - Mar 25 '19 at 14:10
  • Why aren't you using SnakeYaml? – jbx Mar 25 '19 at 14:31
  • Could you read the edit first, before asking the same question that i have already answered. – FlailoftheLord - Mar 25 '19 at 14:36

1 Answers1

0

The startsWith Method is not used like that, maybe i think.
Here is the javadoc.
https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#startsWith-java.lang.String-

If you want use regex, you can use matches method.
Here is the javadoc.
https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#matches-java.lang.String-

A Pattern example link: https://www.tutorialspoint.com/java/java_regular_expressions.htm I think there is something wrong with your usage.

A little suggestions:
1. parse line character by character.
2. make some class save your data. e.g "YAMLToken" ,"YAMLKeyValuePair", "YAMLList"

Bad English, if you save any questions, reply me

hideDragon
  • 344
  • 2
  • 5