Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle Processing instructions in SimpleXmlParser #785

Merged
merged 3 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ default List<String> readAttributeList(String name, String qName, Map<String, St
Collections.addAll(values, value.split(","));
return values;
}

/**
* Receive notification of content data inside a processing instruction element.
*
* @param data the content data of a processing instruction
* @param target the name of an application to which the instruction is directed
* @throws XMLReaderException if any error occurs
*/
default void processingInstruction(String target, String data) {
}
}

private enum STATE {
Expand All @@ -236,10 +246,14 @@ private enum STATE {
ATTRIBUTE_VALUE,
SINGLE_QUOTED_VALUE,
DOUBLE_QUOTED_VALUE,
PI_TARGET,
PI_CONTENT
}

private static final String PROLOG_START = "<?";
private static final String PROLOG_START = "<?xml";
private static final String PROLOG_END = "?>";
private static final String PI_START = "<?";
private static final String PI_END = "?>";
private static final String COMMENT_START = "<!--";
private static final String COMMENT_END = "-->";
private static final String ELEMENT_SELF_CLOSE = "/>";
Expand Down Expand Up @@ -367,6 +381,13 @@ private void processElement() throws IOException {
} else if (hasToken(CLOSE_MARKUP_START)) {
state = STATE.END_ELEMENT;
position++;
} else if (hasToken(PI_START)) {
resumeState = STATE.PI_CONTENT;
state = STATE.PI_TARGET;
position += PI_START.length();
} else if (hasToken(PI_END)) {
state = STATE.END_ELEMENT;
position += PI_END.length();
} else if (hasToken(MARKUP_START)) {
resumeState = STATE.ATTRIBUTES;
state = STATE.NAME;
Expand Down Expand Up @@ -511,6 +532,36 @@ private void processCdata() throws IOException {
}
}

private void processPIContent() throws IOException {
if (hasToken(PI_END)) {
position += PI_END.length();
state = STATE.ELEMENT;
String target = nameBuilder.toString();
reader.startElement(target, attributes);
reader.processingInstruction(target, decode(textBuilder.toString()));
reader.endElement(target);
nameBuilder = new StringBuilder();
textBuilder = new StringBuilder();
} else {
position++;
textBuilder.append(c);
}
}

private void processPITarget() throws IOException {
if (hasToken(PI_END)) {
state = resumeState;
} else if (Character.isWhitespace(c)) {
position++;
state = resumeState;
} else {
validateNameChar(c, nameBuilder.length() == 0);
position++;
nameBuilder.append(c);
}
}


/**
* Start parsing.
*
Expand Down Expand Up @@ -564,6 +615,12 @@ public void parse() throws IOException {
case CDATA:
processCdata();
break;
case PI_TARGET:
processPITarget();
break;
case PI_CONTENT:
processPIContent();
break;
default:
throw new IllegalStateException(String.format(
"Unknown state: %s, line: %d, char: %d", state, lineNo, charNo));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,6 +37,15 @@
*/
class SimpleMXLParserTest {

@Test
void testParseProcessInstruction() throws IOException {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("test1.xml");
assertThat(inputStream, is(not(nullValue())));
Test1Reader reader = new Test1Reader();
SimpleXMLParser.parse(inputStream, reader);
assertThat(reader.m2e, is("execute onConfiguration,onIncremental"));
}

@Test
void testParse() throws IOException {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("test1.xml");
Expand Down Expand Up @@ -117,6 +126,7 @@ private static final class Test1Reader implements Reader {
String rootQName;
String foo;
String bob;
String m2e;

@Override
public void startElement(String name, Map<String, String> attributes) {
Expand All @@ -142,5 +152,14 @@ public void elementText(String data) {
}
}
}

@Override
public void processingInstruction(String target, String data) {
if (target != null) {
if ("m2e".equals(target)) {
m2e = data;
}
}
}
}
}
5 changes: 3 additions & 2 deletions common/xml/src/test/resources/test1.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2021 Oracle and/or its affiliates.
Copyright (c) 2021, 2022 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,8 @@
<document xmlns="http://acme.com/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://acme.com/1.0 http://acme.com/xsd/acme-1.0.xsd">
<foo>bar</foo>
<bob>alice</bob>
<bob attribute="name">alice</bob>
<?m2e execute onConfiguration,onIncremental?>
<bar/>
<booh />
</document>