-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
234 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
lib/src/sortPom/java/com/diffplug/spotless/pom/DelegatingClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2021 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.pom; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.ByteBuffer; | ||
import java.util.Enumeration; | ||
import java.util.Vector; | ||
|
||
public class DelegatingClassLoader extends ClassLoader { | ||
|
||
private final ClassLoader[] delegateClassLoaders; | ||
|
||
public DelegatingClassLoader(ClassLoader... delegateClassLoaders) { | ||
super(null); | ||
this.delegateClassLoaders = delegateClassLoaders; | ||
} | ||
|
||
protected Class<?> findClass(String name) throws ClassNotFoundException { | ||
String path = name.replace('.', '/') + ".class"; | ||
URL url = findResource(path); | ||
if (url == null) { | ||
throw new ClassNotFoundException(name); | ||
} | ||
try { | ||
ByteBuffer byteCode = loadResource(url); | ||
return defineClass(name, byteCode, null); | ||
} catch (IOException e) { | ||
throw new ClassNotFoundException(name, e); | ||
} | ||
} | ||
|
||
private ByteBuffer loadResource(URL url) throws IOException { | ||
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
|
||
int nRead; | ||
byte[] data = new byte[1024]; | ||
|
||
InputStream inputStream = url.openStream(); | ||
while ((nRead = inputStream.read(data, 0, data.length)) != -1) { | ||
buffer.write(data, 0, nRead); | ||
} | ||
|
||
buffer.flush(); | ||
|
||
return ByteBuffer.wrap(buffer.toByteArray()); | ||
} | ||
|
||
protected URL findResource(String name) { | ||
for (ClassLoader delegate : delegateClassLoaders) { | ||
URL resource = delegate.getResource(name); | ||
if (resource != null) { | ||
return resource; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
protected Enumeration<URL> findResources(String name) throws IOException { | ||
Vector<URL> vector = new Vector<>(); | ||
for (ClassLoader delegate : delegateClassLoaders) { | ||
Enumeration<URL> enumeration = delegate.getResources(name); | ||
while (enumeration.hasMoreElements()) { | ||
vector.add(enumeration.nextElement()); | ||
} | ||
} | ||
return vector.elements(); | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
lib/src/sortPom/java/com/diffplug/spotless/pom/SortPomFormatterFunc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2021 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.pom; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.util.logging.Logger; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
|
||
import sortpom.SortPomImpl; | ||
import sortpom.logger.SortPomLogger; | ||
import sortpom.parameter.PluginParameters; | ||
|
||
class SortPomFormatterFunc implements FormatterFunc { | ||
private static final Logger logger = Logger.getLogger(SortPomStep.class.getName()); | ||
private final SortPomStep.InternalState state; | ||
|
||
public SortPomFormatterFunc(SortPomStep.InternalState state) { | ||
this.state = state; | ||
} | ||
|
||
@Override | ||
public String apply(String input) throws Exception { | ||
// SortPom expects a file to sort, so we write the inpout into a temporary file | ||
File pom = File.createTempFile("pom", ".xml"); | ||
pom.deleteOnExit(); | ||
try (FileWriter fw = new FileWriter(pom)) { | ||
fw.write(input); | ||
} | ||
SortPomImpl sortPom = new SortPomImpl(); | ||
sortPom.setup(new SortPomLogger() { | ||
@Override | ||
public void warn(String content) { | ||
logger.warning(content); | ||
} | ||
|
||
@Override | ||
public void info(String content) { | ||
logger.info(content); | ||
} | ||
|
||
@Override | ||
public void error(String content) { | ||
logger.severe(content); | ||
} | ||
}, PluginParameters.builder() | ||
.setPomFile(pom) | ||
.setFileOutput(false, null, null, false) | ||
.setEncoding(state.encoding) | ||
.setFormatting(state.lineSeparator, state.expandEmptyElements, state.spaceBeforeCloseEmptyElement, state.keepBlankLines) | ||
.setIndent(state.nrOfIndentSpace, state.indentBlankLines, state.indentSchemaLocation) | ||
.setSortOrder(state.sortOrderFile, state.predefinedSortOrder) | ||
.setSortEntities(state.sortDependencies, state.sortDependencyExclusions, state.sortPlugins, state.sortProperties, state.sortModules, state.sortExecutions) | ||
.setTriggers(false) | ||
.build()); | ||
sortPom.sortPom(); | ||
return IOUtils.toString(new FileReader(pom)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters