KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > tools > OPP > srcgen > streamfactory > ResourceInputStreamFactory


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: ResourceInputStreamFactory.java,v 1.2 2003/11/07 21:34:22 per_nyfelt Exp $
8
package org.ozoneDB.tools.OPP.srcgen.streamfactory;
9
10 import org.ozoneDB.tools.OPP.OPPHelper;
11
12 import java.io.InputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 /**
16  *
17  * @author Joakim Ohlrogge
18  */

19 public class ResourceInputStreamFactory implements InputStreamFactory {
20     private String JavaDoc root;
21     private String JavaDoc extension;
22
23
24     public ResourceInputStreamFactory(String JavaDoc extension) {
25         this("/", extension);
26     }
27
28     public ResourceInputStreamFactory(String JavaDoc root, String JavaDoc extension) {
29         this.root = root;
30         this.extension = extension;
31         if (!root.endsWith("/"))
32             root += "/";
33     }
34
35     public InputStream JavaDoc newInstance(String JavaDoc className) throws IOException JavaDoc {
36         String JavaDoc name = root + OPPHelper.classFileBasename(className, '/') + "." + extension;
37         InputStream JavaDoc is = getClass().getResourceAsStream(name);
38
39         if (is == null) {
40             throw new IOException JavaDoc("Resource \"" + name + "\" was not found!");
41         }
42         return is;
43     }
44 }
45
Popular Tags