Re: [xmlpull-user] Can you clone a pull parser? Particularly the xpp3 parser?

Aleksander Slominski <[email protected]> Fri, 13 Feb 2004 16:17:54 -0500
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
Nathaniel Hughes wrote:

>Ahh, I see. I just gave this a try and it appears to be doing the job. 
>Thank you so much for your time! I'll post the whole class once I test it.
>
>  
>
hi Nate,

take look on examples cloeneable file reader and cloning parser sample i 
have added to XmlPull CVS (based on our emails):
http://www.xmlpull.org/viewcvs/~checkout~/xmlpull-api-v1/src/java/samples/

i have also made Cloneable standard XPP3 parser (not the minimum version).

changes are in XPP3 CVS and will make into next release.

if you run sample with XPP3 from CVS you should get:

java CloneParser
parser implementation class is class org.xmlpull.mxp1.MXParserCachingStrings
START_TAG foo
START_TAG baz
 >>> SPLIT POINT org.xmlpull.mxp1.MXParserCachingStrings@6b97fd
TEXT 'bar'
END_TAG baz
START_TAG moo
TEXT 'maz'
END_TAG moo
END_TAG foo
END_DOCUMENT
 >>> CLONED PARSER org.xmlpull.mxp1.MXParserCachingStrings@1004901
TEXT 'bar'
END_TAG baz
START_TAG moo
TEXT 'maz'
END_TAG moo
END_TAG foo
END_DOCUMENT
 >>> END

alek

>On Tue, 10 Feb 2004, Stefan Haustein wrote:
>
>  
>
>>Nathaniel,
>>
>>you need to keep track of the stream position (re-implement all methods 
>>that move the stream position by calling the super method) and create a 
>>new CloneableFileReader in the clone method that moves to the position 
>>of the original stream:
>>
>>public class CloneableFileReader extends FilerReader implements Cloneable {
>>
>>    String filename;
>>    long position;
>>
>>    CloneableFileReader(String filename){
>>       super(filename);
>>       this.filename = filename;
>>    }
>>
>>    public Object clone() throws CloneNotSupportedException {
>>       CloneableFileReader clone = new CloneableFileReader(filename);
>>       clone.skip(position);
>>       return clone;
>>    }
>>
>>    public int read(char[] cbuf,
>>                 int offset,
>>                 int length)
>>          throws IOException
>>
>>       int actual = super.read(cbuf, offset, length);
>>       position += Math.max(actual, 0);
>>       return actual;
>>    }
>>
>>    // re-implement remaining functions similar to read above...
>>
>>
>>}
>>
>>    
>>
>>>public class CloneableFileReader extends FileReader implements Cloneable 
>>>{
>>>    public CloneableFileReader(String filename) throws Exception {
>>>      super(filename);
>>>    }
>>>
>>>    public Object clone ()  throws CloneNotSupportedException {
>>>      CloneableFileReader cloned = (CloneableFileReader) super.clone();
>>>      return cloned;
>>>    }
>>>  }
>>>
>>>
>>>On Tue, 10 Feb 2004, Aleksander Slominski wrote:
>>>
>>>
>>>      
>>>
>>>>Nathaniel Hughes wrote:
>>>>
>>>>
>>>>        
>>>>
>>>>>Alek, thanks for your response on this, sorry for my delay, I've been out 
>>>>>of town. 
>>>>>
>>>>>So I suspect you're right, I think my cloneable filereader was the 
>>>>>problem. Is it even possible to clone a FileReader? It seems that the pull 
>>>>>parser clone function works properly since it is successful with your 
>>>>>CloneableCharArrayReader.
>>>>>
>>>>>It seems that if FileReader isn't cloneable, then there really isn't a bug 
>>>>>in your xpp3 parser....or am I wrong?
>>>>>
>>>>>
>>>>>
>>>>>          
>>>>>
>>>>hi Nate,
>>>>
>>>>just write new class something like CloneableFileReader extends Reader 
>>>>implements Cloneable {
>>>>   private File ...
>>>>   private long filepPos;}
>>>>and makes sure that its clone() will correctly deal with cloning File.
>>>>
>>>>alek
>>>>
>>>>
>>>>        
>>>>
>>>>>On Fri, 6 Feb 2004, Aleksander Slominski wrote:
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>          
>>>>>
>>>>>>hi Nate,
>>>>>>
>>>>>>this is now tracked in bugzilla: 
>>>>>>http://www.extreme.indiana.edu/bugzilla/show_bug.cgi?id=177
>>>>>>
>>>>>>please login to bugzilla and add files with source code necessary to 
>>>>>>reproduce the problem.
>>>>>>
>>>>>>i suspect your cloneable reader did not work correctly so when cloned 
>>>>>>parser reads input it reads form original file and there is nothing to 
>>>>>>read ("java.io.EOFException: no more data available").
>>>>>>
>>>>>>thanks,
>>>>>>
>>>>>>alek
>>>>>>
>>>>>>Nathaniel Hughes wrote:
>>>>>>
>>>>>>  
>>>>>>
>>>>>>
>>>>>>            
>>>>>>
>>>>>>>So I'm having one slight problem using my cloned pull parser and I was 
>>>>>>>wondering if someone could help. Here's what I'm doing:
>>>>>>>
>>>>>>>I'm parsing a file, and at a certain point in the file, I clone my parser 
>>>>>>>(I actually do this multiple times) and store it. Then at a later point, I 
>>>>>>>retrieve that parser and begin parsing again. Well, I'm cloning my parser 
>>>>>>>like this:
>>>>>>>
>>>>>>>do {
>>>>>>>     if(eventType == XmlPullParser.START_DOCUMENT) {
>>>>>>>         System.out.println("Start document");
>>>>>>>     } else if(eventType == XmlPullParser.END_DOCUMENT) {
>>>>>>>         System.out.println("End document");
>>>>>>>     } else if(eventType == XmlPullParser.START_TAG) {
>>>>>>>       ...
>>>>>>>	 if(//time to clone) {
>>>>>>>	   try {
>>>>>>>	     clonedParser = (CloneableMXParser) xpp.clone();
>>>>>>>	   }
>>>>>>>	   catch (CloneNotSupportedException e) {
>>>>>>>	     e.printStackTrace();
>>>>>>>	   }
>>>>>>>	   parserVec.add(clonedParser);
>>>>>>>	}
>>>>>>>         processStartElement(xpp);
>>>>>>>     } else if(eventType == XmlPullParser.END_TAG) {
>>>>>>>         processEndElement(xpp);
>>>>>>>       } else if(eventType == XmlPullParser.TEXT) {
>>>>>>>         processText(xpp);
>>>>>>>     }
>>>>>>>     eventType = xpp.next();
>>>>>>> } while (eventType != XmlPullParser.END_DOCUMENT);
>>>>>>>
>>>>>>>
>>>>>>>When I try to begin parsing with my cloned parser again (running through 
>>>>>>>the same loop), I get this error (top of the stack trace here):
>>>>>>>
>>>>>>>java.io.EOFException: no more data available - expected end tags </TAGTWO></TAGONE> to close start tag <TAGTWO> from line 43 and start tag <TAGONE> from line 3, parser stopped on TEXT seen ... blah="366" blah2="34" blah3="this" blah4="2065" ... @43:162
>>>>>>>	at org.xmlpull.mxp1.MXParser.fillBuf(MXParser.java:2987)
>>>>>>>	at org.xmlpull.mxp1.MXParser.more(MXParser.java:2996)
>>>>>>>	at org.xmlpull.mxp1.MXParser.parseStartTag(MXParser.java:1769)
>>>>>>>	at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1126)
>>>>>>>	at org.xmlpull.mxp1.MXParser.next(MXParser.java:1092)
>>>>>>>	at com.watchguard.tests.PullParser.processDocument(PullParser.java:129)
>>>>>>>
>>>>>>>The parser starts parsing alright, it processes the first start, end and 
>>>>>>>text elements it pulls, but then, when it does a next and recieves the 
>>>>>>>next start tag, it throws this error.
>>>>>>>
>>>>>>>The depth at this point is 1 (which it should be) and the elRawName 
>>>>>>>contains both the TAGONE and TAGTWO char arrays, but TAGONE is the only 
>>>>>>>one that is still open, so this shouldn't be an issue.
>>>>>>>
>>>>>>>Hope this makes a little sense, any help would be great!
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>    
>>>>>>>
>>>>>>>              
>>>>>>>
>>>>>>  
>>>>>>
>>>>>>            
>>>>>>
>>>>>
>>>>>          
>>>>>
>>>>
>>>>        
>>>>
>>>
>>>
>>> 
>>>Yahoo! Groups Links
>>>
>>>
>>>
>>> 
>>>
>>>      
>>>
>>
>>
>>
>> 
>>Yahoo! Groups Links
>>
>>
>>
>> 
>>
>>    
>>
>
>
>
>
> 
>Yahoo! Groups Links
>
>
>
> 
>
>  
>


-- 
The best way to predict the future is to invent it - Alan Kay



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/xmlpull-dev/

<*> To unsubscribe from this group, send an email to:
     [email protected]

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/