Skip to content

Commit

Permalink
Bumped jDOM version so that we can get rid of the ugly reflection ove…
Browse files Browse the repository at this point in the history
…rride for PatchedXMLWriter

Added JDK19 to build.
  • Loading branch information
Björn Ekryd committed Jan 29, 2023
1 parent 77e5626 commit 24af1ab
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 105 deletions.
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ jobs:
docker:
- image: cimg/openjdk:18.0
<<: *shared
jdk19:
docker:
- image: cimg/openjdk:19.0
<<: *shared
jdk11:
docker:
- image: cimg/openjdk:11.0
Expand All @@ -51,6 +55,9 @@ workflows:
- jdk18:
requires:
- jdk17
- jdk19:
requires:
- jdk17
- jdk11:
filters: # using regex filters requires the entire branch to match
branches:
Expand All @@ -59,4 +66,5 @@ workflows:
requires:
- jdk17
- jdk18
- jdk19

3 changes: 2 additions & 1 deletion misc/welcome.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* mvn clean install (compile without signing the jar-file) *
* *
* mvn clean install (to deploy new version of the plugin) *
* echo "test" | gpg --clearsign (test the gpg first) *
* mvn release:clean *
* mvn release:prepare *
* mvn release:perform *
* *
* mvn versions:display-dependency-updates *
* mvn versions:display-plugin-updates *
* mvn versions:display-dependency-updates *
* *
******************************************************************************
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

Expand All @@ -20,7 +20,7 @@
<properties>
<compileSource>11</compileSource>
<junit.jupiter.version>5.9.2</junit.jupiter.version>
<mockito.version>5.0.0</mockito.version>
<mockito.version>5.1.0</mockito.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>2.0.6</slf4j.version>
</properties>
Expand Down Expand Up @@ -114,12 +114,12 @@
<plugin>
<groupId>com.github.ekryd.echo-maven-plugin</groupId>
<artifactId>echo-maven-plugin</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
</plugin>
<plugin>
<groupId>com.github.ekryd.sortpom</groupId>
<artifactId>sortpom-maven-plugin</artifactId>
<version>3.1.2</version>
<version>3.2.0</version>
<configuration>
<createBackupFile>false</createBackupFile>
<predefinedSortOrder>custom_1</predefinedSortOrder>
Expand Down
54 changes: 0 additions & 54 deletions sorter/src/main/java/sortpom/output/PatchedXMLWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@

import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Field;
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.DefaultText;
import org.dom4j.tree.NamespaceStack;
import sortpom.content.NewlineText;
import sortpom.exception.FailureException;

/** Overriding XMLWriter to be able to handle SortPom formatting options */
class PatchedXMLWriter extends XMLWriter {
Expand All @@ -24,7 +19,6 @@ class PatchedXMLWriter extends XMLWriter {
private final boolean indentBlankLines;
private final boolean indentSchemaLocation;
private final boolean spaceBeforeCloseEmptyElement;
private final NamespaceStack parentNamespaceStack;

public PatchedXMLWriter(
Writer writer,
Expand All @@ -37,18 +31,6 @@ public PatchedXMLWriter(
this.indentBlankLines = indentBlankLines;
this.indentSchemaLocation = indentSchemaLocation;
this.spaceBeforeCloseEmptyElement = spaceBeforeCloseEmptyElement;
this.parentNamespaceStack = findParentNamespaceStack();
}

private NamespaceStack findParentNamespaceStack() {
try {
Field field = XMLWriter.class.getDeclaredField("namespaceStack");
field.setAccessible(true);
return (NamespaceStack) field.get(this);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new FailureException(
"Internal error: Cannot access internal namespace stack in XMLWriter", e);
}
}

/** Handle spaceBeforeCloseEmptyElement option */
Expand Down Expand Up @@ -110,42 +92,6 @@ private void writeTrimmedText(Node node) throws IOException {
}
}

/**
* Support for indentSchemaLocation, where the original method really should call the
* writeAttribute method
*/
@Override
protected void writeAttributes(Element element) throws IOException {
for (int i = 0, size = element.attributeCount(); i < size; i++) {
Attribute attribute = element.attribute(i);
Namespace ns = attribute.getNamespace();

if ((ns != null) && (ns != Namespace.NO_NAMESPACE) && (ns != Namespace.XML_NAMESPACE)) {
String prefix = ns.getPrefix();
String uri = parentNamespaceStack.getURI(prefix);

if (!ns.getURI().equals(uri)) {
writeNamespace(ns);
parentNamespaceStack.push(ns);
}
}

String attName = attribute.getName();

if (attName.startsWith("xmlns:")) {
String prefix = attName.substring(6);

if (parentNamespaceStack.getNamespaceForPrefix(prefix) == null) {
String uri = attribute.getValue();
parentNamespaceStack.push(prefix, uri);
writeNamespace(prefix, uri);
}
} else if (!attName.equals("xmlns")) {
writeAttribute(attribute);
}
}
}

/** Handle indentSchemaLocation option */
@Override
protected void writeAttribute(Attribute attribute) throws IOException {
Expand Down
46 changes: 0 additions & 46 deletions sorter/src/test/java/sortpom/output/XmlOutputGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
import static org.mockito.Mockito.when;
import static sortpom.sort.XmlFragment.createXmlFragment;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader;
import org.dom4j.Document;
import org.dom4j.tree.DefaultAttribute;
import org.dom4j.tree.DefaultElement;
Expand Down Expand Up @@ -118,45 +113,4 @@ void attributeCalledXmlnsShouldNotBePrinted() {
assertThat(
sortedXml, is(equalTo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Gurka></Gurka>\n")));
}

@Test
void simulateNamespaceStackNotFound() throws Exception {
var classloader =
new URLClassLoader(
new URL[] {
new File("target/classes").toURI().toURL(),
new File("target/test-classes").toURI().toURL(),
// Contains the dummy XMLWriter class that does not have the namespace stack field
new File("src/test/resources/mock").toURI().toURL()
}) {

protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (name.equals("org.dom4j.io.XMLWriter")) {
return findClass(name);
// With no fallback to parent classloader
}
try {
return findClass(name);
} catch (ClassNotFoundException e) {
// Could not find the class so load it from the parent
return super.loadClass(name, resolve);
}
}
};

Class<?> clazz = classloader.loadClass("sortpom.output.PatchedXMLWriter");
Constructor<?> constructor = clazz.getConstructors()[0];
constructor.setAccessible(true);

var exception =
assertThrows(
InvocationTargetException.class,
() -> constructor.newInstance(null, null, false, false, false));

var cause = exception.getCause();
assertThat(cause.getClass().getName(), is("sortpom.exception.FailureException"));
assertThat(
cause.getMessage(),
is("Internal error: Cannot access internal namespace stack in XMLWriter"));
}
}

0 comments on commit 24af1ab

Please sign in to comment.