|
1 |
| -# KDL4j |
| 1 | +# KDL4j v2 |
2 | 2 |
|
3 |
| -A Java implementation of a parser for the [KDL Document Language](https://github.com/kdl-org/kdl). |
| 3 | +A Java implementation of a parser for the [KDL Document Language](https://github.com/kdl-org/kdl). Supports KDL |
| 4 | +version `2.0.0-draft.4`. |
| 5 | + |
| 6 | +This library targets Java 11 as a minimum version. |
4 | 7 |
|
5 | 8 | ## Status
|
6 | 9 |
|
7 | 10 | 
|
8 | 11 |
|
9 |
| -This project is beta-quality. It's been extensively tested, but the spec it implements is still in flux. |
10 |
| - |
11 | 12 | ## Usage
|
12 | 13 |
|
13 | 14 | ### Parsing
|
14 | 15 |
|
15 | 16 | ```java
|
16 |
| -final KDLParser parser = new KDLParser(); |
17 |
| - |
18 |
| -final KDLDocument documentFromString = parser.parse("node_name \"arg\"") |
19 |
| -// OR |
20 |
| -final KDLDocument documentFromReader = parser.parse(new FileReader("some/file.kdl")) |
| 17 | +import kdl.parse.KDLParser; |
| 18 | + |
| 19 | +// Parse from a String |
| 20 | +var documentFromString = KDLParser.parse("node_name \"arg\""); |
| 21 | +// Parse from an InputStream |
| 22 | +var documentFromReader = KDLParser.parse(new ByteArrayInputStream(/* … */)); |
| 23 | +// Parse from a file |
| 24 | +var documentFromReader = KDLParser.parse(Paths.get("path", "to", "file")); |
21 | 25 | ```
|
22 | 26 |
|
23 |
| -`KDLDocument` objects, and all descendants of `KDLObject`, are immutable and threadsafe, though that is not true of their |
24 |
| -`Builder` objects. If you need to make changes to a `KDLDocument`, use the `filter()` and `mutate()` functions explained below. |
25 |
| - |
26 |
| -### Searching and Mutating Documents |
27 |
| - |
28 |
| -Several utilities are provided for finding nodes in documents. Each presents the same interface, but the way they search |
29 |
| -the document differs. There are three search types: |
30 |
| - |
31 |
| -* RootSearch - Searches entirely at the root, primarily used for mutations to the root as discussed below |
32 |
| -* GeneralSearch - Searches for nodes anywhere in the tree matching a single, possibly compound, node predicate |
33 |
| -* PathedSearch - Searches for nodes down a specified path. At each level a different node predicate can be specified |
34 |
| - |
35 |
| -Each provides four methods for searching or mutating documents: |
36 |
| - |
37 |
| -* `anyMatch(document)` - Returns true if any node matches the search, false otherwise |
38 |
| -* `filter(document, trim)` - Removes all nodes from the tree not on a branch that matches the predicates of the search. if |
39 |
| - `trim` is set, removes all their non-matching children |
40 |
| -* `list(document, trim)` - Produces a new document with all matching nodes at the root. If `trim` is set, removes all |
41 |
| - their non-matching children |
42 |
| -* `mutate(document, mutation)` - Applies a provided `Mutation` to every matching node in the tree, depth first. |
43 |
| - |
44 |
| -There are 3 types of `Mutations` provided, and users may provide custom mutations. Provided are `AddMutation`, |
45 |
| -`SubtractMutation`, and `SetMutation`. Each performs functions hinted at by the name. See individual javadocs for details. |
46 |
| - |
47 | 27 | ### Printing
|
48 | 28 |
|
49 |
| -By default, calling `document.toKDL()` or `document.writeKDL(writer)` will print the structure with: |
50 |
| - |
51 |
| -* 4 space indents |
52 |
| -* No semicolons |
53 |
| -* Printable ASCII characters which can be escaped, escaped |
54 |
| -* Empty children printed |
55 |
| -* `null` arguments and properties with `null` values printed |
56 |
| -* `\n` (unicode `\u{0a}`) for newlines |
| 29 | +The `KDLPrinter` class allows printing a KDL document to a `String`, a `Writer`, an `OutputStream` or to a file. By |
| 30 | +default, it: |
| 31 | + |
| 32 | +- prints one character tabulation for each indentation level |
| 33 | +- does not print node separators (`;`) |
| 34 | +- does not print braces for nodes without children |
| 35 | +- prints arguments and properties with null value |
| 36 | +- uses `E` as the exponent character in decimal values |
57 | 37 |
|
58 |
| -Any of these can be changed by creating a new PrintConfig object and passing it into the print method. See the javadocs |
59 |
| -on PrintConfig for more information. |
| 38 | +Any of these can be changed by creating a `PrintConfiguration` and passing it to the `KDLPrinter` constructor. |
60 | 39 |
|
61 | 40 | ## Contributing
|
62 | 41 |
|
63 | 42 | Please read the Code of Conduct before opening any issues or pull requests.
|
64 | 43 |
|
65 |
| -Besides code fixes, the easiest way to contribute is by generating test cases. Check out |
66 |
| -[the test cases directory](https://github.com/hkolbeck/kdl4j/tree/trunk/src/test/resources/test_cases) to see the existing ones. |
| 44 | +Besides code fixes, the easiest way to contribute is by generating test cases. Check out |
| 45 | +[the test cases directory](src/test/resources/test_cases) to see the |
| 46 | +existing ones. |
67 | 47 | See the README there for more details.
|
0 commit comments