[xsl] resolving DTDs while transforming

Subject: [xsl] resolving DTDs while transforming
From: "Pramodh Peddi" <peddip@xxxxxxxxxxxxxxxx>
Date: Wed, 17 Dec 2003 11:26:29 -0500
Hi,
I posted a requestion and I did not get any response back. I thought it was
difficult to understand what I asked in my previous post.
Here is a better way of explaining my question(s):
I have an xml source, to be transformed, which has a relative DTD reference
(just the dtd filename is specified), like:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE CONTENT_FEED SYSTEM "myDTD.dtd">
<root>
......
....
</root>

The problem is myDTD.dtd is available NEITHER in the classpath NOR where the
class file is located. But, it is available in a http-accessible location,
like: http://myserver.com/dtd/myDTD.dtd.

So, I would like to tell the parser to consider
"http://myserver.com/dtd/myDTD.dtd"; instead of myDTD.dtd to validate the
source xml. (I know thats a stupid way of referring to a DTD, but thats the
customer's feed and they cannot change it).

Can any one help me get an approach.

Here is what I am doing: I am using Java1.4 API to transform. I am using
SAXSource to provide the xml source to the transformer. I am using
EntityResolver interface to resolve the DTD (to let the parser consider the
http://myserver.com/dtd/myDTD.dtd in place of myDTD.dtd, mentioned in the
source xml). I just have an idea how to do, but it does not work and no
wonder because I do not yet clearly understand how EntityResolver works.
Questions Like: What is the role of systemID, publicID, when does the parser
try to resolve entities (like DTDs), etc. Can anyone give a clear
understanding of how it works.

I hesitate giving the code, because the email becomes too large and
incognizable. But, in case it helps you help me better, here is the code:

*********************************************code***************************
************
public class SELTransformationService{

private String stylesheet;
private String fileName = null;
private String dtdURL = null;

private void transform(String metadata, String stylesheet,
ServiceRequest req, ServiceResponse res){

//System.out.println("TRANSFORM USING STRING");

try {

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer(new StreamSource(new
URL(stylesheet).openStream()));

InputStream inputStream =
req.getContentObject().getMetadataInputStream();

OutputStream outputStream =
req.getContentObject().getMetadataOutputStream();

SAXParserFactory pfactory= SAXParserFactory.newInstance();

pfactory.setValidating(true);

// Get an XMLReader.

XMLReader reader = pfactory.newSAXParser().getXMLReader();

//create a resolver to resolve the DTD in the source xml

//EntityResolver resolver = new DTDResolver();

reader.setEntityResolver(new DTDResolver());

DTDResolver resolver = (DTDResolver)reader.getEntityResolver();

resolver.setPublicId(this.dtdURL);

SAXSource source = new SAXSource(reader,

new InputSource(new InputStreamReader(inputStream)));

source.setSystemId("myDTD.dtd");

transformer.transform(source, new StreamResult(new
OutputStreamWriter(outputStream, "iso-8859-1")));

outputStream.close();

req.getContentObject().commit();

res.send(req.getContentObject());

log.info(fileName + " - OBJECT SENT OUT OF TRANSFORMATION-2");

} catch (Exception ex) {

res.error(req.getContentObject(), "Exception sending message to bus.");

ex.printStackTrace();

}

}//end SELTransformationService classclass DTDResolver implements
EntityResolver {

String publicId = null;

public void setPublicId(String publicId){

this.publicId = publicId;

System.out.println("Setting publicID");

}

public String getPublicId(){

System.out.println("Getting publicId");

return this.publicId;

}

public InputSource resolveEntity (String publicId, String systemId){

InputStream inputStream = null;

InputSource source = null;

try{

System.out.println("publicID is: " + this.publicId);

if(StringUtils.isNotEmpty(this.publicId)){

URL url = new URL(this.publicId);

inputStream = url.openStream();

System.out.println("got the inputstream");

source = new InputSource(new InputStreamReader(inputStream));

}else{

System.out.println("publicId is not specified!!!");

}

}catch(Exception e){

}

return source;

}

}//end DTDResolver class
**********************************************code**************************
**************

When I do what is given above, the transformed string has empty xml (has
only xml header). I am sure I am doing something wrong. Can any one tell me
what is wrong in that. And making me understand how EntityResolver works
makes me feel lot better.

Thanks,

Pramodh.


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread