Skip to content

Instantly share code, notes, and snippets.

@prietopa
Last active August 29, 2015 14:10
Show Gist options
  • Save prietopa/688698382a958c83eb20 to your computer and use it in GitHub Desktop.
Save prietopa/688698382a958c83eb20 to your computer and use it in GitHub Desktop.
crea una lista de "schemasList" y de "classesToBeBoundList" para la configuracion de Spring
package net.pp.jm.spring.oxm.config.test;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
public class ListDirectoryTest {
@Test
public void test_schemasList() throws IOException {
StringBuffer buf = new StringBuffer();
buf.append("\t<util:list id=\"schemasList\">\n");
File directory = new File("src/main/resources");
String[] extensions = new String[] {"xsd"};
Collection<File> files = FileUtils.listFiles(directory, extensions, true);
for (File file: files) {
buf.append("\t\t<value>classpath:");
String path = file.getPath();
path = path.substring("src/main/resources".length() + 1, path.length());
path = path.replace("\\", "/");
buf.append(path);
buf.append("</value>\n");
}
buf.append("\t</util:list>\n");
System.out.println(buf.toString());
}
@Test
public void test_classesToBeBoundList() throws IOException {
String[] extensions = new String[] {"java"};
String[] exclude = new String[] {"es"};
StringBuffer buf = new StringBuffer();
buf.append("\t<util:list id=\"classesToBeBoundList\">\n");
fillValuesFrom(buf, "src/main/java", extensions, exclude, true);
buf.append("\t</util:list>\n");
System.out.println(buf.toString());
}
private void fillValuesFrom(StringBuffer buf, String directoryPath, String[] extensions, String[] exclude, boolean recursive) {
File directory = new File(directoryPath);
Collection<File> files = FileUtils.listFiles(directory, extensions, recursive);
for (File file: files) {
String path = file.getPath();
if(StringUtils.contains(file.getName(), "package-info")) {
continue;
}
if(StringUtils.contains(file.getName(), "ObjectFactory")) {
continue;
}
path = path.substring(directoryPath.length() + 1, path.length());
path = path.replace("\\", ".");
if(isPacakageExclude(path, exclude)) {
continue;
}
buf.append("\t\t<value>");
buf.append(StringUtils.substringBefore(path, ".java"));
buf.append("</value>\n");
}
}
private boolean isPacakageExclude(String path, String[] excludes) {
boolean encontrado = false;
for(String str: excludes) {
if(StringUtils.startsWithIgnoreCase(path, str)) {
encontrado = true;
}
}
return encontrado;
}
}
@prietopa
Copy link
Author

mejoras:

  • quitado la referencia a package-info
  • quitado la referencia a ObjectFactory
  • quitado el .java de las clases en el listado

@prietopa
Copy link
Author

mejora:

  • uso de StringUtils en el metodo isPackageExclude

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment