HTMLDocument problem
I am using the following code for reading and searching for a String searchText within the HTMLDocument.
privatevoid search(){
file=new FileReader("path_of_file");
htmlEditorKit=new HTMLEditorKit();
document = (HTMLDocument) htmlEditorKit.createDefaultDocument();
callback = document.getReader(0);
parser=new ParserDelegator();
parser.parse(file, callback,true);
docText=document.getText(document.getStartPosition().getOffset(), document.getEndPosition().getOffset());
System.out.println("START\n" + docText +"\nEND\n");
while( (pos=docText.indexOf(searchText, pos+1))!=-1)
{
//store_pos_in_arraylist
}
file.close();
}
The code is working quite well except for the fact that it's not reading the entire HTML Document!!!
The call to System.out.println("START\n" + docText +"\nEND\n");
is not returning the complete file details...it's only part of the file...
I have tried many variations, but I am just not able to figure out why this is happening....is there some buffer thing or something associated with this?
Where exactly is the problem, is it within the reading function call or the callReader function call or whether it is something to do with my string docText....Can anyone help me a bit in understanding where I am going wrong and how I may rectify the problem...?
Thanks & Regards.
Dhruva Sagar
# 10
Well yes I have tried the htmlEditorKit.read method which though seemed like a better option at the time I tried it, it is giving a run-time exception which I just couldn't get around even by searching of ways for it.
Here is the stack trace of it for you just in case your interested or you may know how to get past this.
javax.swing.text.ChangedCharSetException
at javax.swing.text.html.parser.DocumentParser.handleEmptyTag(Unknown Source)
at javax.swing.text.html.parser.Parser.startTag(Unknown Source)
at javax.swing.text.html.parser.Parser.parseTag(Unknown Source)
at javax.swing.text.html.parser.Parser.parseContent(Unknown Source)
at javax.swing.text.html.parser.Parser.parse(Unknown Source)
at javax.swing.text.html.parser.DocumentParser.parse(Unknown Source)
at javax.swing.text.html.parser.ParserDelegator.parse(Unknown Source)
at javax.swing.text.html.HTMLEditorKit.read(Unknown Source)
at source.HashCalculator.search(HashCalculator.java:1013)
at source.HashCalculator.access$26(HashCalculator.java:992)
at source.HashCalculator$16.actionPerformed(HashCalculator.java:916)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicRootPaneUI$Actions.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.KeyboardManager.fireBinding(Unknown Source)
at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.Dialog$1.run(Unknown Source)
at java.awt.Dialog$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at source.HashCalculator.getHelpContents(HashCalculator.java:989)
at source.HashCalculator.access$38(HashCalculator.java:625)
at source.HashCalculator$64.actionPerformed(HashCalculator.java:1894)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Also I tried using callback.flush() method as you suggested, but again it's not working. I am trying everything I can in the given context, everythign that even remotely sounds something similar in logic to what I want to achieve :).
Thanks a lot for your patience in all this, I truely appreciate it.
Thanks and regards.
Dhruva Sagar.