Configuration files (was Re: IBM XML for Java 1.0.0)

John Cowan cowan at locke.ccil.org
Fri Jun 26 19:51:51 BST 1998


I sent this off-list to Peter, but since Michael Kay's post,
I think I'd better broadcast it.

Peter Murray-Rust writes:

> The installation involved re-compilation because the program holds a list
> of SAXDriver classes internally (6 so far). I'd really like to do this sort
> of thing (including menu re-generation) driven by declarative files from
> outside the program (so recompilation isn't necessary). My question is:
> *What is the best way of associating files with Java applications in a
> platform independent manner?*

I'll talk about that point below, but I'm not sure why you need it
for this use.  You can create parsers with the
class method "org.xml.sax.helpers.ParserFactory.makeParser()":
it will return a SAX parser of the class whose name is specified
by the "org.xml.sax.parser" system property.  If you want to give
the user a choice within the program, there is also a method
"makeParser(aString)", where the argument is the class name
of the parser.  This class is supplied with SAX and should be
supplied with every SAX-compatible parser.

> Traditionally I would use a different method for each OS (.jumborc for
> UNIX, C:\jumbo.ini, etc.) I would like to avoid having to tell installers
> what they have to do. So - assume I have files myResource.xml,
> icons/jumbo.gif, etc. which I wish to make available to the application.
>         - is there a standard place to put these?
>         - is it a good idea to use a jar file - if so how do I reference them from
> the program? [The *.class are referenced by classpath - but what about the
> others?]

To read an arbitrary resource from the CLASSPATH, call the static method
ClassLoader.getSystemResource(aString) where aString is the file name
relative to one of the directories or {jar,zip}files in the path.
That returns a java.net.URL.  If you want an InputStream instead,
call getSystemResourceAsStream instead.

Here's a sample Java class which fetches a resource (residing in the
same directory as the .class file), first as an URL object so as
to print the URL (what it prints is system-dependent); second as
an InputStream so that the contents can be printed.  You can test
it by moving ibtwsh.dtd (or substitute your own file name) to any
directory on the CLASSPATH.


-------cut here--------
import java.io.*;
import java.util.*;
import java.net.*;
class testres {

public static void main(String[] argv) {
	URL u = ClassLoader.getSystemResource("ibtwsh.dtd");
	InputStream i = ClassLoader.getSystemResourceAsStream("ibtwsh.dtd");

	System.out.println(u.toExternalForm());
	BufferedReader b = new BufferedReader(new InputStreamReader(i));
	String s;

	try {
	while ((s = b.readLine()) != null)
		System.out.println(s);
	}
	catch (IOException e) {}
	}
}
-------cut here--------
 
-- 
John Cowan	http://www.ccil.org/~cowan		cowan at ccil.org
	You tollerday donsk?  N.  You tolkatiff scowegian?  Nn.
	You spigotty anglease?  Nnn.  You phonio saxo?  Nnnn.
		Clear all so!  'Tis a Jute.... (Finnegans Wake 16.5)

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/
To (un)subscribe, mailto:majordomo at ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)




More information about the Xml-dev mailing list