* Implement createResource() method so child resources can be used even if they don't exist

* Fix horrible bug in update() where child items were created that weren't child resources at all
This commit is contained in:
hns 2005-03-24 18:23:50 +00:00
parent cb02290e3d
commit 7af9cc6a22
3 changed files with 66 additions and 19 deletions

View file

@ -55,10 +55,14 @@ public abstract class AbstractRepository implements Repository {
String shortName; String shortName;
/** /**
* * Called to check the repository's content.
*/ */
public abstract void update(); public abstract void update();
/**
* Called to create a child resource for this repository
*/
protected abstract Resource createResource(String name);
/** /**
* *
@ -83,7 +87,13 @@ public abstract class AbstractRepository implements Repository {
public Resource getResource(String name) { public Resource getResource(String name) {
update(); update();
return (Resource) resources.get(name); Resource res = (Resource) resources.get(name);
// if resource does not exist, create it
if (res == null) {
res = createResource(name);
resources.put(name, res);
}
return res;
} }
public Iterator getResources() { public Iterator getResources() {

View file

@ -165,6 +165,13 @@ public class FileRepository extends AbstractRepository {
} }
} }
/**
* Called to create a child resource for this repository
*/
protected Resource createResource(String name) {
return new FileResource(new File(dir, name), this);
}
public int hashCode() { public int hashCode() {
return 17 + (37 * dir.hashCode()); return 17 + (37 * dir.hashCode());
} }

View file

@ -32,6 +32,8 @@ public final class ZipRepository extends AbstractRepository {
// the nested directory depth of this repository // the nested directory depth of this repository
private int depth; private int depth;
String entryPath;
private long lastModified = -1; private long lastModified = -1;
/** /**
@ -52,10 +54,10 @@ public final class ZipRepository extends AbstractRepository {
} }
/** /**
* Constructs a ZipRepository using the zip entry belonging to the given * Constructs a ZipRepository using the zip entryName belonging to the given
* zip file and top-level repository * zip file and top-level repository
* @param file a zip file * @param file a zip file
* @param zipentry zip entry * @param zipentry zip entryName
* @param parent repository * @param parent repository
*/ */
private ZipRepository(File file, Repository parent, ZipEntry zipentry) { private ZipRepository(File file, Repository parent, ZipEntry zipentry) {
@ -65,10 +67,12 @@ public final class ZipRepository extends AbstractRepository {
if (zipentry == null) { if (zipentry == null) {
name = shortName = file.getName(); name = shortName = file.getName();
depth = 0; depth = 0;
entryPath = "";
} else { } else {
String[] entrypath = StringUtils.split(zipentry.getName(), "/"); String[] pathArray = StringUtils.split(zipentry.getName(), "/");
depth = entrypath.length; depth = pathArray.length;
shortName = entrypath[depth - 1]; shortName = pathArray[depth - 1];
entryPath = zipentry.getName();
name = new StringBuffer(parent.getName()) name = new StringBuffer(parent.getName())
.append('/').append(shortName).toString(); .append('/').append(shortName).toString();
} }
@ -92,31 +96,40 @@ public final class ZipRepository extends AbstractRepository {
try { try {
zipfile = getZipFile(); zipfile = getZipFile();
Enumeration en = zipfile.entries(); Enumeration en = zipfile.entries();
ArrayList newRepositories = new ArrayList(); HashMap newRepositories = new HashMap();
HashMap newResources = new HashMap(); HashMap newResources = new HashMap();
while (en.hasMoreElements()) { while (en.hasMoreElements()) {
ZipEntry entry = (ZipEntry) en.nextElement(); ZipEntry entry = (ZipEntry) en.nextElement();
String entryname = entry.getName(); String eName = entry.getName();
String[] entrypath = StringUtils.split(entryname, "/");
if (!eName.regionMatches(0, entryPath, 0, entryPath.length())) {
// not a child of ours
continue;
}
String[] entrypath = StringUtils.split(eName, "/");
// create new repositories and resources for all entries with a // create new repositories and resources for all entries with a
// path depth of this.depth + 1 // path depth of this.depth + 1
if (entrypath.length == depth + 1) { if (entrypath.length == depth + 1 && !entry.isDirectory()) {
if (entry.isDirectory()) { // create a new child resource
newRepositories.add(new ZipRepository(file, this, entry)); ZipResource resource = new ZipResource(file, entry, this);
} else { newResources.put(resource.getShortName(), resource);
ZipResource resource = new ZipResource(file, entry, this); } else if (entrypath.length > depth) {
newResources.put(resource.getShortName(), resource); // create a new child repository
if (!newRepositories.containsKey(entrypath[depth])) {
ZipEntry child = composeChildEntry(entrypath[depth]);
ZipRepository rep = new ZipRepository(file, this, child);
newRepositories.put(entrypath[depth], rep);
} }
} }
} }
repositories = (Repository[]) repositories = (Repository[]) newRepositories.values()
newRepositories.toArray(new Repository[newRepositories.size()]); .toArray(new Repository[newRepositories.size()]);
resources = newResources; resources = newResources;
} catch (IOException ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
repositories = new Repository[0]; repositories = new Repository[0];
if (resources == null) { if (resources == null) {
@ -134,6 +147,23 @@ public final class ZipRepository extends AbstractRepository {
} }
} }
private ZipEntry composeChildEntry(String name) {
if (entryPath == null || entryPath.length() == 0) {
return new ZipEntry(name);
} else if (entryPath.endsWith("/")) {
return new ZipEntry(entryPath + name);
} else {
return new ZipEntry(entryPath + "/" + name);
}
}
/**
* Called to create a child resource for this repository
*/
protected Resource createResource(String name) {
return new ZipResource(file, new ZipEntry(entryPath + "/" + name), this);
}
public long getChecksum() { public long getChecksum() {
return file.lastModified(); return file.lastModified();
} }