Skip to content

Commit 7fa90e0

Browse files
committed
next steps
1 parent 2d7b941 commit 7fa90e0

File tree

6 files changed

+169
-57
lines changed

6 files changed

+169
-57
lines changed

.classpath

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
<classpath>
33
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
44
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
5+
<classpathentry kind="src" path="src/test/resources" output="target/test-classes" excluding="**/*.java"/>
56
<classpathentry kind="output" path="target/classes"/>
7+
<classpathentry kind="var" path="M2_REPO/javax/json/javax.json-api/1.1.4/javax.json-api-1.1.4.jar" sourcepath="M2_REPO/javax/json/javax.json-api/1.1.4/javax.json-api-1.1.4-sources.jar"/>
68
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
79
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
810
<classpathentry kind="var" path="M2_REPO/org/glassfish/javax.json/1.1.4/javax.json-1.1.4.jar" sourcepath="M2_REPO/org/glassfish/javax.json/1.1.4/javax.json-1.1.4-sources.jar"/>
11+
<classpathentry kind="var" path="M2_REPO/net/sf/saxon/Saxon-HE/9.9.1-1/Saxon-HE-9.9.1-1.jar" sourcepath="M2_REPO/net/sf/saxon/Saxon-HE/9.9.1-1/Saxon-HE-9.9.1-1-sources.jar"/>
912
</classpath>

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ version = '0.1'
88

99
test {
1010
useJUnitPlatform()
11+
testLogging.showStandardStreams = true
1112
}
1213

1314
repositories {
@@ -16,5 +17,6 @@ repositories {
1617

1718
dependencies {
1819
implementation 'org.glassfish:javax.json:1.1.4'
20+
implementation 'net.sf.saxon:Saxon-HE:9.9.1-1'
1921
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.3.2'
2022
}

pom.xml

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,58 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0">
3-
<modelVersion>4.0.0</modelVersion>
4-
<groupId>jcsahnwaldt</groupId>
5-
<artifactId>json-xml</artifactId>
6-
<version>0.1</version>
7-
<properties>
8-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9-
<maven.compiler.source>1.8</maven.compiler.source>
10-
<maven.compiler.target>1.8</maven.compiler.target>
11-
</properties>
12-
<build>
13-
<plugins>
14-
<plugin>
15-
<artifactId>maven-surefire-plugin</artifactId>
16-
<version>2.22.0</version>
17-
</plugin>
18-
<!-- mvn eclipse:eclipse && sed -i '' -E '/NO_M2ECLIPSE_SUPPORT|<projects\/>/d' .project -->
19-
<plugin>
20-
<artifactId>maven-eclipse-plugin</artifactId>
21-
<version>2.10</version>
22-
<configuration>
23-
<downloadSources>true</downloadSources>
24-
<testSourcesLast>true</testSourcesLast><!-- cf. https://issues.apache.org/jira/browse/MNG-3118 -->
25-
<classpathContainers>
26-
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
27-
<classpathContainer>org.eclipse.jdt.junit.JUNIT_CONTAINER/5</classpathContainer>
28-
</classpathContainers>
29-
<excludes>org.junit.jupiter:junit-jupiter-engine,org.apiguardian:apiguardian-api,org.junit.platform:junit-platform-engine,org.junit.platform:junit-platform-commons,org.opentest4j:opentest4j,org.junit.jupiter:junit-jupiter-api</excludes>
30-
</configuration>
31-
</plugin>
32-
</plugins>
33-
</build>
34-
<dependencies>
35-
<dependency>
36-
<groupId>org.glassfish</groupId>
37-
<artifactId>javax.json</artifactId>
38-
<version>1.1.4</version>
39-
</dependency>
40-
<dependency>
41-
<groupId>org.junit.jupiter</groupId>
42-
<artifactId>junit-jupiter-engine</artifactId>
43-
<version>5.3.2</version>
44-
<scope>test</scope>
45-
</dependency>
46-
</dependencies>
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>jcsahnwaldt</groupId>
5+
<artifactId>json-xml</artifactId>
6+
<version>0.1</version>
7+
<properties>
8+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9+
<maven.compiler.source>1.8</maven.compiler.source>
10+
<maven.compiler.target>1.8</maven.compiler.target>
11+
</properties>
12+
<build>
13+
<plugins>
14+
<plugin>
15+
<artifactId>maven-surefire-plugin</artifactId>
16+
<version>2.22.0</version>
17+
</plugin>
18+
<!-- mvn eclipse:eclipse && sed -i '' -E '/NO_M2ECLIPSE_SUPPORT|<projects\/>/d' .project -->
19+
<plugin>
20+
<artifactId>maven-eclipse-plugin</artifactId>
21+
<version>2.10</version>
22+
<configuration>
23+
<downloadSources>true</downloadSources>
24+
<testSourcesLast>true</testSourcesLast><!-- cf. https://issues.apache.org/jira/browse/MNG-3118 -->
25+
<classpathContainers>
26+
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
27+
<classpathContainer>org.eclipse.jdt.junit.JUNIT_CONTAINER/5</classpathContainer>
28+
</classpathContainers>
29+
<excludes>org.junit.jupiter:junit-jupiter-engine,org.apiguardian:apiguardian-api,org.junit.platform:junit-platform-engine,org.junit.platform:junit-platform-commons,org.opentest4j:opentest4j,org.junit.jupiter:junit-jupiter-api</excludes>
30+
</configuration>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
<dependencies>
35+
<dependency>
36+
<!-- org.glassfish:javax.json contains the class files, but not the source files of javax.json-api :-( -->
37+
<groupId>javax.json</groupId>
38+
<artifactId>javax.json-api</artifactId>
39+
<version>1.1.4</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.glassfish</groupId>
43+
<artifactId>javax.json</artifactId>
44+
<version>1.1.4</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>net.sf.saxon</groupId>
48+
<artifactId>Saxon-HE</artifactId>
49+
<version>9.9.1-1</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.junit.jupiter</groupId>
53+
<artifactId>junit-jupiter-engine</artifactId>
54+
<version>5.3.2</version>
55+
<scope>test</scope>
56+
</dependency>
57+
</dependencies>
4758
</project>

src/main/java/XsltJsonReader.java

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import java.io.IOException;
2+
import java.util.Map;
23

34
import javax.json.Json;
45
import javax.json.JsonException;
56
import javax.json.stream.JsonParser;
7+
import javax.json.stream.JsonParser.Event;
68

9+
import org.xml.sax.Attributes;
710
import org.xml.sax.ContentHandler;
811
import org.xml.sax.DTDHandler;
912
import org.xml.sax.EntityResolver;
@@ -13,8 +16,16 @@
1316
import org.xml.sax.SAXNotRecognizedException;
1417
import org.xml.sax.SAXNotSupportedException;
1518
import org.xml.sax.XMLReader;
19+
import org.xml.sax.helpers.AttributesImpl;
1620

1721
public class XsltJsonReader implements XMLReader {
22+
23+
private static final Map<String, Boolean> FEATURES = Map.of(
24+
"http://xml.org/sax/features/namespaces", true,
25+
"http://xml.org/sax/features/namespace-prefixes", false
26+
);
27+
28+
private static final String NS = "http://www.w3.org/2005/xpath-functions";
1829

1930
private EntityResolver entityResolver;
2031
private DTDHandler dtdHandler;
@@ -23,22 +34,26 @@ public class XsltJsonReader implements XMLReader {
2334

2435
@Override
2536
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
26-
throw new SAXNotRecognizedException("Feature: " + name);
37+
Boolean value = FEATURES.get(name);
38+
if (value == null) throw new SAXNotRecognizedException(name);
39+
return value.booleanValue();
2740
}
2841

2942
@Override
30-
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
31-
throw new SAXNotRecognizedException("Feature: " + name);
43+
public void setFeature(String name, boolean v) throws SAXNotRecognizedException, SAXNotSupportedException {
44+
Boolean value = FEATURES.get(name);
45+
if (value == null) throw new SAXNotRecognizedException(name);
46+
if (v != value.booleanValue()) throw new SAXNotSupportedException(name + " = " + v);
3247
}
3348

3449
@Override
3550
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
36-
throw new SAXNotRecognizedException("Property: " + name);
51+
throw new SAXNotRecognizedException(name);
3752
}
3853

3954
@Override
4055
public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
41-
throw new SAXNotRecognizedException("Property: " + name);
56+
throw new SAXNotRecognizedException(name);
4257
}
4358

4459
@Override
@@ -84,7 +99,65 @@ public ErrorHandler getErrorHandler() {
8499
@Override
85100
public void parse(InputSource input) throws IOException, SAXException {
86101
try {
87-
try (JsonParser parser = Json.createParser(input.getCharacterStream())) {
102+
try (JsonParser parser = Json.createParser(input.getByteStream())) {
103+
handler.startDocument();
104+
handler.startPrefixMapping("", NS);
105+
AttributesImpl keyAttrs = new AttributesImpl();
106+
keyAttrs.addAttribute("", "key", "key", "CDATA", null);
107+
AttributesImpl noAttrs = new AttributesImpl();
108+
Attributes attrs = noAttrs;
109+
while (parser.hasNext()) {
110+
Event event = parser.next();
111+
switch (event) {
112+
case START_OBJECT:
113+
handler.startElement(NS, "map", "map", attrs);
114+
attrs = noAttrs;
115+
break;
116+
case END_OBJECT:
117+
handler.endElement(NS, "map", "map");
118+
break;
119+
case START_ARRAY:
120+
handler.startElement(NS, "array", "array", attrs);
121+
attrs = noAttrs;
122+
break;
123+
case END_ARRAY:
124+
handler.endElement(NS, "array", "array");
125+
break;
126+
case KEY_NAME:
127+
keyAttrs.setValue(0, parser.getString());
128+
attrs = keyAttrs;
129+
break;
130+
case VALUE_STRING:
131+
handler.startElement(NS, "string", "string", attrs);
132+
attrs = noAttrs;
133+
char[] str = parser.getString().toCharArray();
134+
handler.characters(str, 0, str.length);
135+
handler.endElement(NS, "string", "string");
136+
break;
137+
case VALUE_NUMBER:
138+
handler.startElement(NS, "number", "number", attrs);
139+
attrs = noAttrs;
140+
char[] num = parser.getString().toCharArray();
141+
handler.characters(num, 0, num.length);
142+
handler.endElement(NS, "number", "number");
143+
break;
144+
case VALUE_TRUE:
145+
case VALUE_FALSE:
146+
handler.startElement(NS, "boolean", "boolean", attrs);
147+
attrs = noAttrs;
148+
String bool = event == Event.VALUE_TRUE ? "true" : "false";
149+
handler.characters(bool.toCharArray(), 0, bool.length());
150+
handler.endElement(NS, "boolean", "boolean");
151+
break;
152+
case VALUE_NULL:
153+
handler.startElement(NS, "null", "null", attrs);
154+
attrs = noAttrs;
155+
handler.endElement(NS, "null", "null");
156+
break;
157+
}
158+
}
159+
handler.endPrefixMapping("");
160+
handler.endDocument();
88161
}
89162
}
90163
catch (JsonException e) {

src/test/java/XsltJsonReaderTest.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
import static org.junit.jupiter.api.Assertions.*;
2-
31
import java.io.IOException;
4-
import java.io.StringReader;
2+
import java.io.InputStream;
3+
4+
import javax.xml.transform.OutputKeys;
5+
import javax.xml.transform.Transformer;
6+
import javax.xml.transform.TransformerException;
7+
import javax.xml.transform.TransformerFactory;
8+
import javax.xml.transform.sax.SAXSource;
9+
import javax.xml.transform.stream.StreamResult;
510

611
import org.junit.jupiter.api.Test;
712
import org.xml.sax.InputSource;
813
import org.xml.sax.SAXException;
14+
import org.xml.sax.XMLReader;
915

1016
class XsltJsonReaderTest {
1117

1218
@Test
13-
void testParse() throws IOException, SAXException {
14-
InputSource source = new InputSource(new StringReader("{}"));
15-
new XsltJsonReader().parse(source);
19+
void testParse() throws IOException, SAXException, TransformerException {
20+
try (InputStream in = getClass().getResourceAsStream("test.json")) {
21+
InputSource input = new InputSource(in);
22+
XMLReader reader = new XsltJsonReader();
23+
SAXSource source = new SAXSource(reader, input);
24+
StreamResult result = new StreamResult(System.out);
25+
Transformer transformer = TransformerFactory.newInstance().newTransformer();
26+
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
27+
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
28+
transformer.transform(source, result);
29+
}
1630
}
17-
31+
1832
}

src/test/resources/test.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"a": [
3+
{
4+
"b": "b",
5+
"c": -1.234e7000324353454
6+
}
7+
],
8+
"d": true
9+
}

0 commit comments

Comments
 (0)