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

Replace jaxb with jackson #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ repositories {
mavenCentral()
}

dependencies {
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.14.1")
}

// Set the JVM language level used to build project. Use Java 11 for 2020.3+, and Java 17 for 2022.2+.
kotlin {
jvmToolchain(17)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.github.aarcangeli.ideaclangformat;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;

public class ClangFormatReplacement {
@XmlAttribute
@JacksonXmlProperty(isAttribute = true)
public int offset;
@XmlAttribute

@JacksonXmlProperty(isAttribute = true)
public int length;
@XmlValue

@JacksonXmlText(false)
public String value;
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
package com.github.aarcangeli.ideaclangformat;

import com.github.aarcangeli.ideaclangformat.exceptions.ClangFormatError;
import com.intellij.openapi.util.NlsSafe;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import org.jetbrains.annotations.NotNull;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "replacements")
@JacksonXmlRootElement(localName = "replacements")
public class ClangFormatResponse {
static final JAXBContext REPLACEMENTS_CTX;

static {
public static ClangFormatResponse unmarshal(@NotNull String stdout) {
try {
REPLACEMENTS_CTX = JAXBContext.newInstance(ClangFormatResponse.class);
XmlMapper xmlMapper = new XmlMapper();
return xmlMapper.readValue(stdout, ClangFormatResponse.class);
}
catch (JAXBException e) {
throw new RuntimeException("Failed to load JAXB context", e);
catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

public static ClangFormatResponse unmarshal(@NotNull @NlsSafe String stdout) {
try {
// JAXB closes the InputStream.
return (ClangFormatResponse) REPLACEMENTS_CTX.createUnmarshaller().unmarshal(new StringReader(stdout));
}
catch (JAXBException e) {
throw new ClangFormatError("Failed to parse clang-format XML replacements\n" + stdout, e);
}
}
@JacksonXmlProperty(localName = "space", isAttribute = true)
public String space;

@XmlElement(name = "replacement")
public List<ClangFormatReplacement> replacements;
}
@JacksonXmlProperty(localName = "incomplete_format", isAttribute = true)
public boolean incompleteFormat;

@JacksonXmlProperty(localName = "replacement")
@JacksonXmlElementWrapper(useWrapping = false)
public final List<ClangFormatReplacement> replacements = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.aarcangeli.ideaclangformat

import com.fasterxml.jackson.dataformat.xml.XmlMapper
import org.junit.Assert.assertEquals
import org.junit.Test

class ClangFormatResponseTest {
@Test
fun marshal() {
val value = ClangFormatResponse()
for (i in 0..10) {
val replacement = ClangFormatReplacement()
replacement.offset = 23
replacement.value = "asd $i"
value.replacements.add(replacement)
}
val writeValueAsString = XmlMapper().writeValueAsString(value)
println(writeValueAsString)
}

@Test
fun unmarshal() {
val stdout = """
<?xml version='1.0'?>
<replacements xml:space='preserve' incomplete_format='false'>
<replacement offset='106' length='8'> </replacement>
</replacements>
""".trimIndent()
val response = ClangFormatResponse.unmarshal(stdout)
assertEquals(false, response.incompleteFormat)
assertEquals(1, response.replacements.size)
assertEquals(106, response.replacements[0].offset)
assertEquals(8, response.replacements[0].length)
assertEquals(" ", response.replacements[0].value)
}
}