wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Custom URIResolver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Custom URIResolver" Watch "Custom URIResolver" New topic
Author

Custom URIResolver

David P Brown
Greenhorn

Joined: Apr 10, 2004
Posts: 7
I need a custom URIResolver for use with Saxon but know too little about Java to tackle it, and hope you might be able to help.
Specifically, I need a URIResolver that will respond to FileNotFoundException by copying the file 'null.xml' from a known location and then responding, that the file was found.
So, when an XSLT calls, with document(), a file that doesn't exist, its processing continues uninterupted because, at the very least it will find a copy of null.xml in that place, which the XSLT can then write to..
I'm wondering if the following is partly what I need. In my ignorance I'm expecting that this requires wrapping in a
"class MyResolver implements URIResolver {}"
but that's a guess.

(I know enough that I can compile java and use this custom URIResolver with Saxon)
Regards
davidpbrown
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Hello David,
May I ask you to add some spaces into your display name so that it complies with our naming policy?
Thanks, and welcome to the 'ranch!


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
All resolve has to do is to return a Source - looking at the Source JavaDocs we find that there are several classes that implement Source. The javax.xml.transform.stream.StreamSource for example can be created from a wide variety of inputs, perhaps you could use one of them.
Bill
David P Brown
Greenhorn

Joined: Apr 10, 2004
Posts: 7
Forgive me, my knowledge of Java is *very limited*
I need not simply to return the location of a source but copy a null file to the location requested. This file will then be written to as if it already existed.
It's not just input I'm needing, it's the creation of a copy of null.xml to requested.xml
Then I'm expecting
try {s = systemURIResolver.resolve(href, base);} will work.
?
David P Brown
Greenhorn

Joined: Apr 10, 2004
Posts: 7
The problem, ~obviously, is that XSLT (Saxon) can't work with files that don't exist.
Therefore it must find at least
<?xml version="1.0"?>
<null/>
to be successful.
There is a way for XSLT to detect a file doesn't exist but that is of no use to me.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
I'm not clear on what you are trying to do. Is a custom URIResolver all you need? If so, then it does not have to do this manipulation with the creation of a null.xml file, it just has to return a Source, like I said.

Or is there some other requirement for creation of null.xml
Bill
David P Brown
Greenhorn

Joined: Apr 10, 2004
Posts: 7
I expect I'm not being clear. (a little out of my depth
What I know is that I've been advised that a custom URIResolver is what I need. This from Micheal Kay author of the Saxon processor.
I grabbed the idea of the java code required from other talk of URIResolver as it looked like it made sense.
What I need is that the XSLT processor is able to write to a file suggested to it and it can't do that if the file doesn't exist.
Accepting what you suggest I've constructed the following which crashes with one error..
MyURIResolver.java:5: illegal start of expression
public Source resolve(href, base) {

If you can suggest a correction for that error then I'll see if it does work for me.
Regards
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5

Actually this doesn't crash. It just doesn't compile.
The reason is that you haven't specified types for the method arguments. It should probably be something like "public Source resolve(String href, String base)"
David P Brown
Greenhorn

Joined: Apr 10, 2004
Posts: 7
Thanks Lasse, but I'm still getting the same compilation error.
?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
What I need is that the XSLT processor is able to write to a file suggested to it and it can't do that if the file doesn't exist.

I don't understand this requirement - if you are writing to a file, it does not have to previously exist - all you need is a valid path and filename, the file will be created.
A URIResolver creates a source - that can be based on a file to be read or other sources of character streams and has nothing to do with defining a file to write to.
Bill
David P Brown
Greenhorn

Joined: Apr 10, 2004
Posts: 7
That is an interesting question, Bill, which prompted me to try simply writing to the file.

However, it doesn't work and I think it is due to XSLT being a compiled language. The error I get is 'Variable name must be a valid QName' which is directly due to the file doesn't exist and consequently the xsl can't be compiled.
This is consistent with Micheal Kay's suggesting that I would need to create a custom URIResolver.
Evidently this is more about the file not existing at compilation, than it is about the output.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Are you sure that

is what you want. That doesn't look right to me - of course I struggle with XSLT every time I have to use it.
Isn't that error saying that "document($filename)" is not a valid name for a xsl:variable???
According to the reference I am looking at "The name of a variable is governed by the QName rules of XML Namespaces" ... Names can't begin with digits or most special characters.... They also cannot contain most special characters"
So the error has nothing to do with the existance of the file.
Also, XSLT is an interpreted language in most cases.
Bill
David P Brown
Greenhorn

Joined: Apr 10, 2004
Posts: 7
Quite right, but then

simply gives 'Source file input does not exist'.
Regardless of my confusion, I think we have to acknowledge Micheal Kay's suggestion. I understand this is the same MK who is the name at the W3C
---------
from http://www.w3.org/TR/#last-call
XSL Transformations (XSLT) Version 2.0
12 November 2003, Michael Kay
---------
Any suggestions why the java code doesn't compile?
Thanks
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5

You might want to use a real IDE -- it would tell you about this kind of syntax errors quicker than you can say "Java"
[ April 13, 2004: Message edited by: Lasse Koskela ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Custom URIResolver
 
Similar Threads
XSLT Task in ant gives waring "Warning: encoding "UTF-8" not supported, using UTF-8"
Howto use cached templates in URIResolver
Installing new MSXML parser for IE5
FOP URIResolver and FOUserAgent
Any solutions to have one xml source file, one XSLT file and create many html files??