The FlexibleReader class
fynngodau upravil túto stránku 4 rokov pred

The FlexibleReader class has been designed to simplify the process of parsing plans with the same layout, but different sortings of content.

Please read the Reader guide before reading this document. As a FlexibleReader is a special type of Reader, steps that have not changed should still be followed even if they aren't mentioned here again.

About FlexibleReader

As it might be a little difficult to understand, let's imagine a school uses the AWESOME substitution plan software and generates the following table and outputs it as an HTML file:

––––––––––––––––––––––––– AWESOME GMBH –
––––––––––––––––––––– MONDAY, 04/12/20 –
 CLASS |  TEACHER  | LESSON |   INFO   |
–––––––+–––––––––––+––––––––+––––––––––+
 9a    | Blackwell |      3 | subst.   |
 9c    | Fury      |      2 | subst.   |
 10d   | Honey     |      5 | subst.   |
––––––––––––––––––––––––––––– SCHOOL A –

Now, another school uses the same software to create its own substitution plan, which looks like this:

–––––––––––––––––––––––––– AWESOME GMBH –
–––––––––––––––––––––– MONDAY, 04/12/20 –
  TEACHER  | LESSON | CLASS |   TYPE    |
–––––––––––+––––––––+–––––––+–––––––––––+
 Phillips  |      1 | 7c    | subst.    |
           |      3 | 10a   | free prd. |
 Dumbey    |      4 | 10a   | subst.    |
–––––––––––––––––––––––––––––– SCHOOL B –

You'll notice that these plans are structurally very similar, aren't they? Really, the advertisement header is the same, the date header is the same, the position the school's name is located is the same… just the columns themselves were reordered and renamed (INFO to TYPE).

And that's what FlexibleReader is made for: it is a subclass of Reader to which you give the data from the table as you find it, and FlexibleReader will process it for you.

Implement addEntries()

As previously, you will implement addEntries(), but there are new methods available to you which you should make use of, which are explained in the following.

Call setMasterTableColumns(…)

Once you found the row in a table that contains its header – this is usually the first one –, call setMasterTableColumns(…).

If you are using JSoup and have an Elements object that contains one Element for each column, you can call setMasterTableColumns(Elements).

Otherwise, create a String array with one String for each column in the correct order and call setMasterTableColumns(String[]). In our example of SCHOOL A, you would pass ["CLASS", "TEACHER", "LESSON", "INFO"].

Call constructEntry(…)

For each row in the table you are parsing, call constructEntry(…) once.

Again, if you have an Elements object that contains one Element for each column, you can call constructEntry(Elements, Date).

If not, create a String array with one String for each column in the correct order and call constructEntry(T[], Date). (Note that it doesn't have to be a String array; if you have any other class that implements toString() in a way that returns the needed data, you can use an array of that class as well.)

Implement getMasterTableColumn(String)

You will also have to implement getMasterTableColumn(String), which associates your column headers to their corresponding EntryField enum. This means that, in our example, for "CLASS" you would have to return EntryField.CLASS, for "TEACHER" you would return EntryField.TEACHER and so on, like seen here.

protected TableColumn getMasterTableColumn(String definition) {

    switch (definition) {
        case "CLASS":
            return EntryField.CLASS;
        case "LESSON":
            return EntryField.LESSON;
        case "TEACHER":
            return EntryField.TEACHER;
        case "INFO":
        case "TYPE":
            return EntryField.INFO;
        default:
            return UNDEFINED;
    }
}

In new substitution plans with previously unknown column titles are found, this is the only method that has to be changed.