MemoryConduit

kris foo at bar.com
Tue Feb 13 10:12:10 PST 2007


Alberto wrote:
> I can write xml on buffer/file without  problems, but I have some little
> problem parsing an xml from a buffer.
> This is part of the sax example:
> 
> void readerTest3()
> {
> 	ISAXReader!(T) reader = new TeqXMLReader!(T)(256);
> 	FileConduit file = new FileConduit("sample-32BE.xml",
> FileConduit.ReadExisting);
> 	MyOutputHandler handler = new MyOutputHandler();
> 	reader.parse(file, handler, Encoding.UTF_32);
> }
> 
> I read the sax source:
> /**
>    All SAX parsers must implement this interface.
>  */
> interface ISAXReader(T=char) {
>   	/**
> 	   Tells the parser to begin processing a document
> 
> 	   Params:
> 	   	source =	The IConduit that the parser should draw xml content from
> 		handler =	The client interface who's methods the parser should call
> 		encoding =	If the client knows it, tell the parser the text encoding
> of the incoming XML.
> 				If not specified, the parser will guess, and failing that assume
> UTF8N.  The
> 				enum for these types is in mango.convert.Unicode.
> 	 */
> 	public void parse(IConduit source, ISAXHandler!(T) handler, Encoding
> encoding=Encoding.Unknown);
> }
> 
> I must pass a conduit, ok.
> I have tried to use MemoryConduit (I suppose that is the right way to do
> what I want) but without success, something like:
> 
> char[] msg = "blabla";
> char[] xml;
> auto mc = new MemoryConduit();
> auto write = new Writer (mc);
> auto read = new Reader (mc);
> write (msg);
> xml.length = msg.length;
> read (xml);
> Stdout(xml);
> mc.close();
> 
> this give me:
> end-of-file whilst reading
> 
> I can't use the method writer because is not accessible, so how can I
> use MemoryConduit?


It's not quite clear what you wish to do, but I'll have a go? First, 
it's worth cleaning up a bit using "auto":

void readerTest3()
{
	auto handler = new MyOutputHandler;
	auto reader = new TeqXMLReader!(T)(256);
	auto file = new FileConduit ("sample-32BE.xml");
	reader.parse (file, handler, Encoding.UTF_32);
}

Now the questions:

1) why do you require MemoryConduit? Have you constructed some in-memory 
XML for parsing?

2) it's not clear why you're using protocols for this (Reader/Writer) 
when you can use mc.write(void[]) and/or mc.read(void[])

3) is there a version of reader.parse() that accepts a Buffer instance 
instead? If not, then there probably ought to be (since it would save 
you some effort).


More information about the Digitalmars-d-learn mailing list