0,0 → 1,77 |
package net.outlyer.plugins; |
|
/* |
* Copyright (c) 2008, Toni Corvera. All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions are met: |
* |
* * Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* * Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* |
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
|
import java.io.File; |
import java.io.FileNotFoundException; |
import java.io.FileReader; |
import java.io.IOException; |
import java.io.InputStream; |
import java.io.InputStreamReader; |
import java.io.Reader; |
import java.net.URI; |
|
/** |
* java.io.Reader for plugins, tries to abstract the plugin location (file or jar) |
*/ |
class PluginReader extends Reader { |
|
private final Reader readerImpl; |
|
PluginReader(final URI uri) throws IOException { |
super(); |
assert null != uri; |
|
try { |
if (uri.getScheme().equals("file")) { |
readerImpl = new FileReader(new File(uri)); |
} |
else { |
final String path = uri.getSchemeSpecificPart().split("!")[1]; |
final InputStream is = getClass().getResourceAsStream(path); |
|
if (null == is) { |
throw new IOException("Failed to get resource for " + uri); |
} |
|
readerImpl = new InputStreamReader(is); |
} |
// TODO: Add exception for other schemes |
} |
catch (final FileNotFoundException e) { |
throw new IOException("Can\'t read input " + uri); |
} |
} |
|
@Override |
public int read(char[] cbuf, int off, int len) throws IOException { |
return readerImpl.read(cbuf, off, len); |
} |
|
@Override |
public void close() throws IOException { |
readerImpl.close(); |
} |
} |