You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by "Lors, Ben" <Lo...@principal.com> on 2001/05/31 23:46:38 UTC

Casting down from Document

Hello all.  I have a Document object that I need to cast down to its specific implementation type (DocumentImpl or perhaps a different implementation like TXDocument).  My code looks something like this:

//Get a Document object from a Document factory
Document doc = factory.getDocument();
return process(doc);

private Document process(DocumentImpl doc) {
 //Process the document using implementation specific code
}

private Document process(TXDocument doc) {
 //Process the document using implementation specific code
}


Obviously before I call the process method I need to convert my Document object to its specific implementation (which could change depending on what the factory gives me).  I'm rather stumped as to how to do this.  Conceptually, I'm thinking something along the lines of:

String className = doc.getClass().getName();
//Now cast the doc object to my className
return process( (className)doc);

However, obviously that is not legal.  But hopefully you get the idea of what I'm trying to do--it's a little difficult to explain.  This is probably a little off-topic because it's not necessarily a Xerces specific question, but I thought that with all the Java/Document experience out there someone might have an idea for me.  Thanks in advance!

Ben Lors

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Casting down from Document

Posted by Tom Bradford <br...@dbxmlgroup.com>.
"Lors, Ben" wrote:
> Obviously before I call the process method I need to convert my Document object to its specific implementation (which could change depending on what the factory gives me).  I'm rather stumped as to how to do this.  Conceptually, I'm thinking something along the lines of:
> 
> String className = doc.getClass().getName();
> //Now cast the doc object to my className
> return process( (className)doc);
> 
> However, obviously that is not legal.  But hopefully you get the idea of what I'm trying to do--it's a little difficult to explain.  This is probably a little off-topic because it's not necessarily a Xerces specific question, but I thought that with all the Java/Document experience out there someone might have an idea for me.  Thanks in advance!

Try the instanceof operator.  

	if ( doc instanceof DocumentImpl ) 
		return process((DocumentImpl)doc);

If you won't know the classes ahead of time, you can always create an
interface, implement a handler for specific class types and then map the
class name to the handler.

-- 
Tom Bradford --- The dbXML Project --- http://www.dbxml.org/
We store your XML data a hell of a lot better than /dev/null

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org