Parse the xml prolog too
This commit is contained in:
parent
d1d31abb04
commit
3ded93fdb1
5 changed files with 128 additions and 1 deletions
|
|
@ -20,7 +20,7 @@ public class AttributesParser implements Parser<Map<String, String>> {
|
|||
public Map<String, String> read(Block block) throws ParseException {
|
||||
List<Attribute> attributes = new Repeat<>(block1 -> {
|
||||
Repeat.atLeast(1, Literal.literally(" ")).parse(block1);
|
||||
|
||||
|
||||
String name = new Expression(REGEX_NAME).parse(block1);
|
||||
Literal.literally("=").parse(block1);
|
||||
String value = new QuotedString().parse(block1);
|
||||
|
|
|
|||
35
src/main/java/de/plugh/compositeparse/xml/Document.java
Normal file
35
src/main/java/de/plugh/compositeparse/xml/Document.java
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package de.plugh.compositeparse.xml;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Document {
|
||||
|
||||
private final Prolog prolog;
|
||||
private final List<Node> nodes;
|
||||
|
||||
public Document(Prolog prolog, List<Node> nodes) {
|
||||
this.prolog = prolog;
|
||||
this.nodes = nodes;
|
||||
}
|
||||
|
||||
public String prettyPrint() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
if (prolog != null) {
|
||||
result.append(prolog.prettyPrint(true));
|
||||
}
|
||||
|
||||
nodes.forEach(node -> result.append(node.prettyPrint()));
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Document{" +
|
||||
"prolog=" + prolog +
|
||||
", nodes=" + nodes +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package de.plugh.compositeparse.xml;
|
||||
|
||||
import de.plugh.compositeparse.Block;
|
||||
import de.plugh.compositeparse.ParseException;
|
||||
import de.plugh.compositeparse.Parser;
|
||||
import de.plugh.compositeparse.parsers.Default;
|
||||
import de.plugh.compositeparse.parsers.Repeat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DocumentParser implements Parser<Document> {
|
||||
|
||||
@Override
|
||||
public Document read(Block block) throws ParseException {
|
||||
Prolog prolog = new Default<>(null, new PrologParser()).parse(block);
|
||||
List<Node> nodes = new Repeat<>(new NodeParser()).parse(block);
|
||||
|
||||
return new Document(prolog, nodes);
|
||||
}
|
||||
|
||||
}
|
||||
43
src/main/java/de/plugh/compositeparse/xml/Prolog.java
Normal file
43
src/main/java/de/plugh/compositeparse/xml/Prolog.java
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package de.plugh.compositeparse.xml;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class Prolog {
|
||||
|
||||
private final Map<String, String> attributes;
|
||||
|
||||
public Prolog(Map<String, String> attributes) {
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
public String prettyPrint(boolean newline) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
result.append("<?xml");
|
||||
|
||||
attributes.forEach((s, s2) -> {
|
||||
result
|
||||
.append(" ")
|
||||
.append(s)
|
||||
.append("=\"")
|
||||
.append(s2)
|
||||
.append("\"");
|
||||
});
|
||||
|
||||
result.append("?>");
|
||||
|
||||
if (newline) {
|
||||
result.append("\n");
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Prolog{" +
|
||||
"attributes=" + attributes +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
28
src/main/java/de/plugh/compositeparse/xml/PrologParser.java
Normal file
28
src/main/java/de/plugh/compositeparse/xml/PrologParser.java
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package de.plugh.compositeparse.xml;
|
||||
|
||||
import de.plugh.compositeparse.Block;
|
||||
import de.plugh.compositeparse.ParseException;
|
||||
import de.plugh.compositeparse.Parser;
|
||||
import de.plugh.compositeparse.parsers.Literal;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class PrologParser implements Parser<Prolog> {
|
||||
|
||||
@Override
|
||||
public Function<List<Block>, String> getNamingScheme() {
|
||||
return Block.label("xml prolog");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Prolog read(Block block) throws ParseException {
|
||||
Literal.literally("<?xml").parse(block);
|
||||
Map<String, String> attributes = new AttributesParser().parse(block);
|
||||
Literal.literally("?>").parse(block);
|
||||
|
||||
return new Prolog(attributes);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue