I come from a C/C++ background, and what you're talking about is referred to as parsing or writing a parser, this was done with lex and yacc for C/C++. It's a bit of an advanced topic if you're willing to write your own. Otherwise, there's a
good amount of them already written in Java. Find one that matches what you're looking for, or if you write your own - your grammar specification would be rules according to line indentation, for example.