-
Notifications
You must be signed in to change notification settings - Fork 942
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
5 changed files
with
149 additions
and
92 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
40 changes: 40 additions & 0 deletions
40
src/org/nutz/resource/impl/FileSystemResourceLocation.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,40 @@ | ||
package org.nutz.resource.impl; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
import org.nutz.NutRuntimeException; | ||
import org.nutz.lang.util.Disks; | ||
import org.nutz.resource.NutResource; | ||
import org.nutz.resource.Scans; | ||
|
||
public class FileSystemResourceLocation extends ResourceLocation { | ||
|
||
public String id() { | ||
return root.getAbsolutePath(); | ||
} | ||
|
||
public void scan(final String base, final Pattern pattern, final List<NutResource> list) { | ||
final File baseFile = new File(root.getAbsolutePath()+"/"+base); | ||
if (baseFile.isFile()) { | ||
list.add(new FileResource(baseFile)); | ||
return; | ||
} | ||
|
||
Disks.visitFile(baseFile, new Scans.ResourceFileVisitor(list, base), new Scans.ResourceFileFilter(pattern)); | ||
} | ||
|
||
public String toString() { | ||
return "Dir[path=" + root + "]"; | ||
} | ||
|
||
private File root; | ||
|
||
public FileSystemResourceLocation(File root) throws IOException { | ||
if (root == null) | ||
throw new NutRuntimeException("FileSystemResourceLocation root can't be NULL"); | ||
this.root = root.getAbsoluteFile().getCanonicalFile(); | ||
} | ||
} |
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,67 @@ | ||
package org.nutz.resource.impl; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipInputStream; | ||
|
||
import org.nutz.lang.Lang; | ||
import org.nutz.lang.Streams; | ||
import org.nutz.log.Log; | ||
import org.nutz.log.Logs; | ||
import org.nutz.resource.NutResource; | ||
import org.nutz.resource.Scans; | ||
|
||
public class JarResourceLocation extends ResourceLocation { | ||
|
||
public String id() { | ||
return jarPath; | ||
} | ||
|
||
public void scan(String base, Pattern regex, List<NutResource> list) { | ||
for (String ensName : names) { | ||
String name = ensName; | ||
if (name.contains("/")) | ||
name = name.substring(name.lastIndexOf('/') + 1); | ||
if (ensName.startsWith(base) && (null == regex || regex.matcher(name).find())) { | ||
try { | ||
list.add(Scans.makeJarNutResource(jarPath, ensName, base)); | ||
} catch (IOException e) { | ||
if (log.isInfoEnabled()) | ||
log.info("Jar delete while scan?!! " + jarPath,e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public String toString() { | ||
return "Jar[path=" + jarPath + "]"; | ||
} | ||
|
||
private static final Log log = Logs.get(); | ||
|
||
private List<String> names = new ArrayList<String>(); | ||
|
||
private String jarPath; | ||
|
||
public JarResourceLocation(String jarPath) throws IOException { | ||
this.jarPath = ResourceLocation.getJarPath(jarPath); | ||
ZipInputStream zis = null; | ||
try { | ||
zis = Scans.makeZipInputStream(jarPath); | ||
ZipEntry ens = null; | ||
while (null != (ens = zis.getNextEntry())) { | ||
if (ens.isDirectory()) | ||
continue; | ||
names.add(ens.getName()); | ||
} | ||
} | ||
catch (Throwable e) { | ||
throw Lang.wrapThrow(e); | ||
} finally { | ||
Streams.safeClose(zis); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.nutz.resource.impl; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.nutz.resource.NutResource; | ||
|
||
public class SimpleResource extends NutResource { | ||
|
||
protected InputStream ins; | ||
|
||
public SimpleResource() {} | ||
|
||
public SimpleResource(String name, String source, InputStream ins) { | ||
super(); | ||
this.ins = ins; | ||
this.name = name; | ||
this.source = source; | ||
} | ||
|
||
|
||
public InputStream getInputStream() throws IOException { | ||
return ins; | ||
} | ||
|
||
public void setInputStream(InputStream ins) { | ||
this.ins = ins; | ||
} | ||
} |