Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: [SOLVED] difficult question on Java

  1. #1
    Join Date
    Feb 2007
    Location
    Heaven
    Beans
    486
    Distro
    Ubuntu 8.04 Hardy Heron

    [SOLVED] difficult question on Java

    Hi,
    Can anyone help solving an issue? I'm displaying the contents of an HTML page using the (standard) class
    Code:
    javax.swing.text.html.HTMLEditorKit
    and I need to access its images which are represented by the following (standard) class:
    Code:
    javax.swing.text.html.ImageView
    Does anybody know how to do this? I searched a lot on this issue, there seems to be no straightforward method like HTMLEditorKit.getListOfImages();
    62°23′30″N 145°09′0″W
    ёёмаёё..

  2. #2
    Join Date
    Aug 2006
    Location
    Germany
    Beans
    396

    Re: difficult question on Java

    i know u want a straight answer... sorry i cant provide that but if u could explain what u are trying todo a bit more in detail . I might be able to help u.

  3. #3
    Join Date
    Feb 2007
    Location
    Heaven
    Beans
    486
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: difficult question on Java

    Quote Originally Posted by Shin_Gouki2501 View Post
    i know u want a straight answer... sorry i cant provide that but if u could explain what u are trying todo a bit more in detail . I might be able to help u.
    Well there's a site that does translations and I'm creating an app that behaves like a browser and fetches translations for different words, it also remembers all the words "to the disk" so next time the user asks for the same words the answer comes a lot faster, there will be other enhancements as well.
    For now I can save the text that Java loads into the JTextPane, but I can't save the images (the transcription characters are served as images by the server).
    This is what the program looks so far:
    http://xlinuks.googlepages.com/lingvo.png

    Here's the program:
    http://xlinuks.googlepages.com/lingvo.zip
    You should use Java 6 to run it. After unzipping the file you get the jLingvo.jar file which is the executable (which also contains the source code if you're interested helping) and a folder called "lib" which is the location where I store the saved translations and the image that is the Icon of the main frame.
    The .jar file relies on the "lib" folder to be in the same directory.
    Last edited by xlinuks; February 3rd, 2008 at 06:58 PM. Reason: The image looked stretched on the page, so I replaced it by a link to it.
    62°23′30″N 145°09′0″W
    ёёмаёё..

  4. #4
    Join Date
    Feb 2007
    Location
    Heaven
    Beans
    486
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: difficult question on Java

    thus,
    to display a document of type "text/html" inside a JTextPane, Java uses the "javax.swing.text.html.HTMLEditorKit" class, which in turn uses (as I found out) the class "javax.swing.text.html.ImageView" to display the images.
    62°23′30″N 145°09′0″W
    ёёмаёё..

  5. #5
    Join Date
    Aug 2006
    Location
    Germany
    Beans
    396

    Re: difficult question on Java

    ok i try look into that , i answer u tomorrow!

  6. #6
    Join Date
    Aug 2006
    Location
    Germany
    Beans
    396

    Re: difficult question on Java

    Ok i've checked ur Sources!
    And i am somewhat gettign what you are trying to implement. Infact i have done something similar before.

    What i did was i parsed an XHTML website to filter some of its content into an XML File.
    ussing this:

    Code:
    private void parse() throws Exception{
    		URL url = new URL("http://localhost/wiki/index.php/File");            
    	InputStream in = url.openStream();
    	XMLInputFactory factory = XMLInputFactory.newInstance();
    	XMLStreamReader parser = factory.createXMLStreamReader(in);	
    	  
    	//Do something with parser -> process XHTML tags... to XML-File
         while ( parser.hasNext() )
    Same way u can access simply images from urls:
    Code:
    public UrlImageTest() throws Exception{
    		URL myImageURL = new URL("http://localhost/wiki/index.gif");
    		FileOutputStream fos= new FileOutputStream("index.gif");
    		byte[] buffer = new byte[ 0xFFFF ]; 
    		
    		 for ( int len; (len = myImageURL.openStream().read(buffer)) != -1; ) 
    			 fos.write( buffer, 0, len );
    The Basic diffrence IMO is that u see ur read "ByteStream" as HTML WebPage while i see it as XML ( XHTML) but u should be able todo all u want with those.
    So basically u need a combination of those with some serialization of the parsed content.
    Should work , what u think?!

  7. #7
    Join Date
    Feb 2007
    Location
    Heaven
    Beans
    486
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: difficult question on Java

    Thanks for taking the time to dive into this issue!
    I solved it almost accidentally, by trying to extend the corresponding classes, and I actually got what I wanted, boy it took me so long to figure it out!
    So here are the relevant classes, if interested in the whole source code see the updated links above.
    HKit.java
    Code:
    package xlinuks;
    
    import xlinuks.*;
    
    import java.util.Vector;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    
    public class HKit extends HTMLEditorKit {
    	private HFactory hFactory = null;
    	
    	public HKit() {
    		hFactory = new HFactory();
    	}
    	
    	public ViewFactory getViewFactory() {
    		return hFactory;
    	}
    	
    	public final Vector<javax.swing.text.html.ImageView> getAllImages() {
    		return hFactory.getAllImages();
    	}
    	
    	public void clear() {
    		hFactory.clear();
    	}
    	
    }
    HFactory.java
    Code:
    package xlinuks;
    
    import xlinuks.*;
    
    import java.util.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    
    public class HFactory extends HTMLEditorKit.HTMLFactory {
    	private Vector<javax.swing.text.View> vViews = null;
    	
    	public HFactory() {
    		vViews = new Vector<javax.swing.text.View>();
    	}
    	
    	public javax.swing.text.View create( javax.swing.text.Element elem ) {
    		View view = super.create( elem );
    		vViews.add( view );
    		return view;
    	}
    	
    	public void clear() {
    		vViews.removeAllElements();
    	}
    	
    	public Vector<javax.swing.text.html.ImageView> getAllImages() {
    		
    		int iCount = vViews.size();
    		Vector<javax.swing.text.html.ImageView> vImages = new Vector<javax.swing.text.html.ImageView>();
    		
    		for( int i=iCount-1; i>=0; i-- ) {
    			View v = vViews.get( i );
    			if( v instanceof ImageView ) {
    				vImages.add( (ImageView)v );
    			}
    		}
    		
    		return vImages;
    	}
    }
    62°23′30″N 145°09′0″W
    ёёмаёё..

  8. #8
    Join Date
    Aug 2006
    Location
    Germany
    Beans
    396

    Re: difficult question on Java

    ok if your application is finished, i would like to see it

  9. #9
    Join Date
    Feb 2007
    Location
    Heaven
    Beans
    486
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: difficult question on Java

    This issue is finished/solved, not the application, besides, as you probably noted, its meant for Russian people

    ps: the updated version is in place of the old one http://xlinuks.googlepages.com/lingvo.zip
    62°23′30″N 145°09′0″W
    ёёмаёё..

  10. #10
    Join Date
    Aug 2006
    Location
    Germany
    Beans
    396

    Re: difficult question on Java

    yes i saw u take documentation from englsh, french german and itelaian documentaion and translate it locally to russian?
    Last edited by Shin_Gouki2501; February 3rd, 2008 at 07:41 PM.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •