I have a project where I generate lots of code against many XSD. To keep things separate each set of XSD are bundled together within a project. I have multiple project that will see XSD in resources and generate code against them.
My problem is when I try to access the XSD that are stored in the jar files I cannot get the code to access the XSD from a perticular jar. Instead it will access the first XSD that matches the criterion regardless of the jar.
Here is the code I use to list the ressources, All the jars have the same structure meaning the XSDs are always stored in the xsd folder at the root of the jar file. The code below lists the XSD in the folder.
URL dirURL = clazz.getClassLoader().getResource(path);
System.out.println(dirURL.toURI());
if (dirURL != null && dirURL.getProtocol().equals("file")) {
/* A file path: easy enough */
System.out.println(dirURL.toURI());
return new File(dirURL.toURI()).list();
}
if (dirURL == null) {
/*
* In case of a jar file, we can't actually find a directory.
* Have to assume the same jar as clazz.
*/
String me = clazz.getName().replace(".", "/") + ".class";
dirURL = clazz.getClassLoader().getResource(me);
System.out.println(dirURL.toURI());
System.out.println(me);
}
if (dirURL.getProtocol().equals("jar")) {
/* A JAR path */
String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file
System.out.println(jarPath);
JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
Set<String> result = new HashSet<String>(); //avoid duplicates in case it is a subdirectory
String name = null;
while (entries.hasMoreElements()) {
name = entries.nextElement().getName();
if (name.startsWith(path)) { //filter according to the path
String entry = name.substring(path.length());
int checkSubdir = entry.indexOf("/");
if (checkSubdir >= 0) {
// if it is a subdirectory, we just return the directory name
entry = entry.substring(0, checkSubdir);
}
result.add(entry);
}
}
return result.toArray(new String[result.size()]);