Re: DOMWrite an HTMLDocument

"Roger C. Soares" <[email protected]>
Newsgroups gmane.comp.java.enhydra.barracuda.general
Message-ID <[email protected]>
Hi Jake,

Thanks for the information and code samples. I have to study it better 
to see if it fits my case. You gave me lots of new information! :)

Well, internally, my system works with XML and the W3C Document. HTML is 
just one source of data, and somehow it needs to be imported to an XML 
Document so I can do something with it. In the end I have an XML 
Document, but sometimes it is interesting to format it to HTML to send 
to a browser, that's where I was using the HTMLDocumentFactory. I was 
creating a basic HTML document and importing the nodes from the XML 
Document, so the DOMWriter would use the HTML formatter.

As the createBasicDocument() returns an HTMLDocument I dont't have how 
to set the encoding without getting the DOMImplementation. Also, I can't 
dynamically populate this HTMLDocument and serialize it any more:

HTMLDocument themeDoc = HTMLDocumentFactory.createBasicDocument();
themeDoc.getBody().appendChild(themeDoc.createTextNode("test"));
new DefaultDOMWriter().write(themeDoc, context.getResponse());

I don't really understand well how XMLC works, maybe this 
HTMLDocumentFactory was intended to be an XMLC only class. But as it 
returns an org.w3c.dom.html.HTMLDocument I was expecting it to be valid...

Thanks,
Roger.

--
Jacob Kjome wrote:
> At 12:10 AM 7/14/2003 -0300, you wrote:
> 
>> Hi Jake,
>>
>> For the transformation I read XML files from the disk and change 
>> dynamically the DOM tree with the parameters from the request.
>>
>> The JAXP API has a method to load an XML from an InputStream:
>> javax.xml.parsers.DocumentBuilder.parse(InputStream is)
>> doesn't it work for you?
> 
> 
> Sure it does.  I was wondering specifically how you were using XMLC to 
> do this.  Apparently you are not using XMLC to do this, though.
> 
> In the example reduced test case you provided where XMLC was bombing on 
> a NullPointerException, that is actually to be expected.  Here is the 
> explanation from David Li of XMLC fame...
> 
> <quote name="David Li">
> It should throw a null point exception. When you call 
> createBasicDocument(), it creates a XercesLinkedHTMLDocument whose 
> xmlobject is null.
> 
> encoding = ((XMLObjectLink)document).getXMLObject().getEncoding();
> 
> Is calling getEncoding on the null object. There is no reason to call 
> the createBasicDocument and serialize it right away.
> </quote>
> 
> That's why I was wondering how you were creating the document.  It seems 
> you had to be using XMLC to load up your document because it had to be 
> an instance of XMLObjectLink to call getEncoding().  Until I see how you 
> were attempting to load the document via XMLC and getting the 
> NullPointerException, this isn't a bug and it won't be fixed in the 
> XMLC-2.2 final release.  The release will be done very early this week, 
> so get as much info to me now as you can if you want it working the way 
> you think it should work.
> 
>> Now, to the HTML parsing. I don't use XMLC, I call JTidy directly 
>> instead. It is an ugly hack, I remember having some problems when I 
>> wrote this code, but if there is another way to do it that doesn't 
>> involve too many hours to implement I'm willing to update it. Tell me 
>> what you need and I'll research if I can come up with a code you can 
>> use and then I'll just use DOMLoader.
>>
>> My HTML parser works like this:
>> 1) Read the HTML file from the web
>> 2) Save it to a temp file
>> 3) Parse the temp file with JTidy and store the output in an String
>> 4) Parse this String with Xerces to finally have a DOM tree
>>
>> I don't remember why I had to create a temp file, I think I can remove 
>> this step because JTidy can read from an InputStream. And I had to get 
>> JTidy's output on a String and parse it again because the DOM tree 
>> generated by JTidy was not compatible with the W3C DOM API... :(
>>
>> Here is the code:
>> -- 
>> Tidy tidy = new Tidy();
>> FileInputStream in = new FileInputStream(tmpfile);
>> tidy.setMakeClean(false);
>> tidy.setTidyMark(false);        // Don't add Tidy META tag.
>> tidy.setXmlTags(false);
>> tidy.setXmlOut(true);
>> tidy.setRawOut(true);   // avoid mapping values > 127 to entities
>>
>> ByteArrayOutputStream strWriter = new ByteArrayOutputStream();
>> Document tDoc = tidy.parseDOM(in, strWriter);
>>
>> String pageStr = strWriter.toString();
>> pageStr = pageStr.substring(pageStr.indexOf("<html"), 
>> pageStr.length()-1);
>> StringReader strReader = new StringReader("<?xml version=\"1.0\" 
>> encoding=\"ISO-8859-1\"?>\n" + htmlDtd + pageStr);
>>
>> DOMParser domParser = new DOMParser();
>> domParser.parse(new InputSource(strReader));
>> Document doc = domParser.getDocument();
>> -- 
> 
> 
> So, this is what you are doing as a work around to the issue with XMLC 
> that you are having?
> 
>> Humm.. look at that, If DOMLoader can read HTML files I can change all 
>> that by: DOMLoader.load(tmpfile); !!!! Wow! :)
> 
> 
> Yep, but this functionality all depends on the DOMFactory backing the 
> DOMLoader.  The default one for DefaultDOMLoader is the 
> DeferredParsingDOMFactory which, in order to load documents from a file, 
> you need to configure by providing the ServletContext to said factory.  
> This allows it to read context parameters from the web.xml.  Also note 
> that this duplicates the functionality of the XMLCContext from XMLC 
> which would allow you to completely bypass Barracuda's DOMLoader and 
> DOMWriter.  It all depends on what works best for you.  You can see an 
> example of deferred parsing in the XMLC tomcat example.  See the latest 
> CVS for XMLC...
> http://forge.objectweb.org/cvs/?group_id=49
> 
> Note that there are some bug fixes and changes in deferred parsing 
> behavior in the latest XMLC CVS compared to that in Barracuda-1.2.0.  
> Again, XMLC-2.2 will be released this week, so you won't have to wait 
> long for it.  I'm attaching a zip file that has examples of using 
> XMLCContext -vs- using Barracuda's dom loading and writing.  See the 
> web.xml file for examples of the context param configuration I mentioned.
> 
> Note that in addition to the ability to load an hml doc from file, 
> deferred parsing also automatically reloads the file if any change has 
> been made to it.
> 
> Jake
> 
>> Cheers,
>> Roger.
>>
>> -- 
>> Jacob Kjome wrote:
>>
>>> Hi Roger,
>>> Ah... that is quite novel
>>> Out of curiosity, can you provide the code snippets you use to load 
>>> the generated XSL transformation using XMLC for both XML and HTML 
>>> documents?  This might be functionality we'd want to add to the 
>>> DOMLoader.  Right now the DOMLoader loads documents from classes and 
>>> either a file from the file system or classpath, but does not yet 
>>> load a stream (if that is, in fact, what you are doing).  Anyway, 
>>> rather than speculate on what you are doing, I'll just wait for you 
>>> to tell me what you are doing....and we'll try to get the bug worked 
>>> out on the XMLC side of things.
>>> thanks,
>>> Jake
>>> At 10:20 AM 7/11/2003 -0300, you wrote:
>>>
>>>> Hi Jake,
>>>>
>>>> Thanks a lot for the workaround. Unfortunatly it doesn't work for me 
>>>> because my Document comes from an XSL transformation, so it is not a 
>>>> class. I'm using it to generate a preview:
>>>> http://www.livesidebar.com/servlet/SetTheme.event?themeId=doubleTab
>>>>
>>>> Fortunatly, it works for XML Documents, so I'm now generating XHTML 
>>>> which is fine for now. Hopefully this bug will be fixed when I need 
>>>> to generate HTML again, so that's fine :)
>>>>
>>>> Thanks again!
>>>> Cheers,
>>>> Roger.
>>>>
>>>> -- 
>>>> Jacob Kjome wrote:
>>>>
>>>>> Hi Roger,
>>>>> Hopefully this bug will get tracked down and fixed in XMLC before 
>>>>> making the XMLC-2.2 release.  In the meantime, you should be able 
>>>>> to load your document successfully using (exception handling not 
>>>>> provided for brevity)...
>>>>> DOMLoader loader = DefaultDOMLoader.getGlobalInstance();
>>>>> Document doc = null;
>>>>> doc = loader.getDOM(MyPageHTML.class);
>>>>> new DefaultDOMWriter().write(doc, resp);
>>>>> Jake
>>>>> At 10:06 AM 7/10/2003 -0300, you wrote:
>>>>>
>>>>>> Hi Jake,
>>>>>>
>>>>>> I did test the reduced test case. It produces the exact same 
>>>>>> exception from when I get the HTMLDocument from my business object.
>>>>>>
>>>>>> Thanx,
>>>>>> Roger.
>>>>>>
>>>>>> -- 
>>>>>> Jacob Kjome wrote:
>>>>>>
>>>>>>> Hi Roger,
>>>>>>> See comments below...
>>>>>>> At 11:30 PM 7/9/2003 -0300, you wrote:
>>>>>>>
>>>>>>>> Hey Barracuda wizards!
>>>>>>>>
>>>>>>>> I'm updating an application from Barracuda 0.something to 
>>>>>>>> Barracuda 1.2 and I got stuck on this:
>>>>>>>>
>>>>>>>> -- 
>>>>>>>> java.lang.NullPointerException
>>>>>>>>         at 
>>>>>>>> org.enhydra.xml.io.BaseDOMFormatter.getEncoding(BaseDOMFormatter.java:326) 
>>>>>>>>
>>>>>>>>         at 
>>>>>>>> org.enhydra.xml.io.BaseDOMFormatter.(BaseDOMFormatter.java:374)
>>>>>>>>         at org.enhydra.xml.io.HTMLFormatter.(HTMLFormatter.java:99)
>>>>>>>>         at 
>>>>>>>> org.enhydra.xml.io.DOMFormatter.getFormatter(DOMFormatter.java:156)
>>>>>>>>         at 
>>>>>>>> org.enhydra.xml.io.DOMFormatter.write(DOMFormatter.java:192)
>>>>>>>>         at 
>>>>>>>> org.enhydra.barracuda.core.util.dom.DefaultDOMWriter.write(DefaultDOMWriter.java:181) 
>>>>>>>>
>>>>>>>>         at 
>>>>>>>> org.enhydra.barracuda.core.util.dom.DefaultDOMWriter.write(DefaultDOMWriter.java:146) 
>>>>>>>>
>>>>>>>>         at 
>>>>>>>> lsb.presentation.screens.WizardScreen$RenderTabPreviewHandler.handleViewEvent(WizardScreen.java:336) 
>>>>>>>>
>>>>>>>> -- 
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> Hmm... curious.  Looks like it has more to do with XMLC than 
>>>>>>> Barracuda, though.  As such, I'm cc'ing the XMLC list.
>>>>>>>
>>>>>>>> when writing an HTMLDocument with DefaultDOMWriter.
>>>>>>>>
>>>>>>>> Do you guys have an idea what it could be? The reduced testcase is:
>>>>>>>>
>>>>>>>> HTMLDocument themeDoc = 
>>>>>>>> org.enhydra.xml.xmlc.html.HTMLDocumentFactory.createBasicDocument(); 
>>>>>>>>
>>>>>>>> new DefaultDOMWriter().write(themeDoc, context.getResponse());
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> What exactly are you trying to do?  createBasicDocument() will 
>>>>>>> create an empty HTMLDocument.  Why would you want to write that 
>>>>>>> immediately to output?  It still shouldn't fail with a 
>>>>>>> NullPointerException in BaseDOMFormatter, however.  Looks like it 
>>>>>>> bombs on the second line below...
>>>>>>> } else if (document instanceof XMLObjectLink) {
>>>>>>>         encoding = 
>>>>>>> ((XMLObjectLink)document).getXMLObject().getEncoding();
>>>>>>> }
>>>>>>> Not sure how that would cause a null pointer exception?  If the 
>>>>>>> document is an instance of XMLObjectLink, then the document is, 
>>>>>>> obviously, not null and that whole line should succeed.  Richard, 
>>>>>>> David, Mark, any ideas here?
>>>>>>> Roger, did you actually test your reduced testcase or are you 
>>>>>>> assuming that it is equivalent to another piece of code that is 
>>>>>>> bombing?  If you haven't tested the reduced testcase, can you 
>>>>>>> post your actual code?  It might make a bit more sense.
>>>>>>> Jake
>>>>>>>
>>>>>>>> Cheers,
>>>>>>>> Roger.
>>>>>>>>
>>>>>>>> _______________________________________________
>>>>>>>> Barracuda mailing list
>>>>>>>> [email protected]
>>>>>>>> http://barracudamvc.org/lists/listinfo/barracuda
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> Barracuda mailing list
>>>>>>> [email protected]
>>>>>>> http://barracudamvc.org/lists/listinfo/barracuda
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> Barracuda mailing list
>>>>>> [email protected]
>>>>>> http://barracudamvc.org/lists/listinfo/barracuda
>>>>>
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> Barracuda mailing list
>>>>> [email protected]
>>>>> http://barracudamvc.org/lists/listinfo/barracuda
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Barracuda mailing list
>>>> [email protected]
>>>> http://barracudamvc.org/lists/listinfo/barracuda
>>>
>>>
>>> _______________________________________________
>>> Barracuda mailing list
>>> [email protected]
>>> http://barracudamvc.org/lists/listinfo/barracuda
>>
>>
>> _______________________________________________
>> Barracuda mailing list
>> [email protected]
>> http://barracudamvc.org/lists/listinfo/barracuda
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.