01: /* ContentLoader.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Fri Jun 3 12:40:06 2005, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.util.resource;
20:
21: import java.io.File;
22: import java.io.InputStream;
23: import java.io.FileInputStream;
24: import java.io.InputStreamReader;
25: import java.io.IOException;
26: import java.net.URL;
27:
28: import org.zkoss.io.Files;
29:
30: /**
31: * A {@link Loader} that loads the resource by use URL.getContent()
32: * if the source is URL, or loads into a String if the source is a File
33: * (and assumging UTF-8).
34: *
35: * @author tomyeh
36: */
37: public class ContentLoader extends AbstractLoader {
38: //-- Loader --//
39: public Object load(Object src) throws Exception {
40: final InputStream is;
41: if (src instanceof URL) {
42: is = ((URL) src).openStream();
43: } else if (src instanceof File) {
44: is = new FileInputStream((File) src);
45: } else if (src == null) {
46: throw new NullPointerException();
47: } else {
48: throw new IllegalArgumentException("Unknown soruce: " + src
49: + "\nOnly File and URL are supported");
50: }
51: return Files.readAll(new InputStreamReader(is, "UTF-8"))
52: .toString();
53: }
54: }
|