Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2007

Tip: Looking for answers? Try searching our database.

Trying to produce a valid URL for both Windows and Unix - help

Thread view: 
phillip.s.powell@gmail.com - 12 Jan 2007 21:56 GMT
Consider my code:

          StringBuffer sb = new StringBuffer();
           sb.append("Your new icon can be found ");
           try {
               sb.append("<a href=\"file:///" +

URLEncoder.encode(System.getProperty("user.home"), "UTF-8") +
                       "/" + this.getIconFileName() + "\">here</a>");
           } catch (UnsupportedEncodingException e) {
               sb.append("at: \"" + System.getProperty("user.home") +
File.separator +
                       this.getIconFileName() + "\"");
           }
           String iconPathInfo = sb.toString();

This code snippet does what I want.. if I'm in UNIX.  If I'm in Windows
the hyperlink is completely maligned; everything is "escaped", from "/"
to ":" to every single character, which is completely wrong.

all I want is very very easy:

System.out.println(iconPathInfo)

Your new icon can be found at <a
href="file:///C:/Documents+and+Settings/phil/My+Documents/stave.ico">here</a>

That is what I want, but I can't get it to print out that way.  Instead
I get

Your new icon can be found at <a
href="file%C5%2F%2F%2FDocuments+and+Settings%2Fphil%2FMy+Documents%2Fstave.ico">here</a>

Any ideas? I've been trying all kinds of exotic solutions to no avail;
this is so extremely easy in my native PHP :(

Phil
Steve W. Jackson - 12 Jan 2007 22:28 GMT
> Consider my code:
>
[quoted text clipped - 34 lines]
>
> Phil

My suggestion would be that you use the java.io.File class.  Build a
File object around the item in question, then use the toURI or toURL
method.  I seem to recall that toURI gives a better result, so you'll
want to experiment.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Thomas Fritsch - 13 Jan 2007 01:41 GMT
[...]
> all I want is very very easy:
>
[quoted text clipped - 4 lines]
>
> That is what I want, but I can't get it to print out that way.

File file = new File(System.getProperty("user.home"), "stave.ico");
String s = file.toURL().toString().replace(' ', '+');
// I get here:   "file:/C:/Dokumente+und+Einstellungen/fritscht/stave.ico"

Signature

Thomas

Steve W. Jackson - 15 Jan 2007 17:26 GMT
> [...]
> > all I want is very very easy:
[quoted text clipped - 9 lines]
> String s = file.toURL().toString().replace(' ', '+');
> // I get here:   "file:/C:/Dokumente+und+Einstellungen/fritscht/stave.ico"

Depending on the intended use, the '+' may not work well.  I think
you'll find that using toURI() works better than toURL() does.  It
replaces those spaces with "%20" and will encode anything else needed.  
And URI contains a toURL() method, should it be useful, but on my system
(in US English and with limited knowledge of other languages) I see no
difference in the result.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Thomas Fritsch - 15 Jan 2007 18:21 GMT
Steve W. Jackson schrieb:

>>File file = new File(System.getProperty("user.home"), "stave.ico");
>>String s = file.toURL().toString().replace(' ', '+');
[quoted text clipped - 3 lines]
> you'll find that using toURI() works better than toURL() does.  It
> replaces those spaces with "%20"  
Agreed!
My browser can display
  file:/C:/Dokumente+und+Einstellungen/
but says "File not found" on
  file:/C:/Dokumente%20und%20Einstellungen/
Hence, the better solution for the OP is indeed
  file.toURI().toString()

> and will encode anything else needed.
Not agreed.
I checked toURI().toString(). It does not encode non-ASCII characters
(for example german umlauts):
  "ü" remains "ü", instead of being translated to "%FC".

> And URI contains a toURL() method, should it be useful, but on my system
> (in US English and with limited knowledge of other languages) I see no
> difference in the result.

Signature

Thomas

Thomas Fritsch - 15 Jan 2007 18:29 GMT
Thomas Fritsch schrieb:

> Steve W. Jackson schrieb:
>
[quoted text clipped - 14 lines]
> Hence, the better solution for the OP is indeed
>   file.toURI().toString()
Stupid me. I meant it this way around:
Agreed!
My browser can display
  file:/C:/Dokumente%20und%20Einstellungen/
but says "File not found" on
  file:/C:/Dokumente+und+Einstellungen/
Hence, the better solution for the OP is indeed
  file.toURI().toString()

>> and will encode anything else needed.
>
[quoted text clipped - 6 lines]
>> system (in US English and with limited knowledge of other languages) I
>> see no difference in the result.

Signature

Thomas

phillip.s.powell@gmail.com - 16 Jan 2007 14:09 GMT
> > [...]
> > > all I want is very very easy:
[quoted text clipped - 16 lines]
> (in US English and with limited knowledge of other languages) I see no
> difference in the result.

It appears that toURL() is deprecated within java.io.File.

I tried this instead:

String s = file.toURI().toURL().toString().replace(' ', '+');

Which gave me this as a result:

Your new icon can be found &lt;a
href="file:/C:/Documents%20and%20Settings/me/stave.ico&gt;here&lt;/a&gt;

That was still not clickable, however, when I tried to click onto the
"link" from within the JLabel within the JFrame.

Sorry, I thought that was it! :(

Phil

> = Steve =
Andrew Thompson - 16 Jan 2007 15:10 GMT
..
> Your new icon can be found &lt;a
> href="file:/C:/Documents%20and%20Settings/me/stave.ico&gt;here&lt;/a&gt;
>
> That was still not clickable, however, when I tried to click onto the
> "link" from within the JLabel within the JFrame.

Two approaches to making a clickable link are
1) implement an HyperLinkListener in a JEditorPane
<http://java.sun.com/developer/onlineTraining/GUI/Swing1/shortcourse.html#JFCEdit
orPane
>
AFAIR it can open an image directly from an HTML link.
2) add a MouseListener to the JLabel - the
changing of color for hover and active have to
be separately handled, though.

Andrew T.
phillip.s.powell@gmail.com - 16 Jan 2007 16:37 GMT
> ..
> > Your new icon can be found &lt;a
[quoted text clipped - 7 lines]
> <http://java.sun.com/developer/onlineTraining/GUI/Swing1/shortcourse.html#JFCEdit
orPane
>
> AFAIR it can open an image directly from an HTML link.

That is what I tried to do:

private JEditorPane createHyperlinkPane(String iconPathInfo) {
       JEditorPane pane = new JEditorPane();
       pane.setBackground(UIManager.getColor("control"));
       pane.setOpaque(true);
       pane.setEditorKit(new HTMLEditorKit());
       pane.setFont(IconMaker.font);
       pane.setText(iconPathInfo);
       pane.setContentType("text/html");
       pane.addHyperlinkListener(new HyperlinkListener() {
           public void hyperlinkUpdate(HyperlinkEvent evt) {
               if (evt.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED) {
                   System.out.println("you clicked!");
               }
           }
       });

       return pane;
   }

   private String createIconPathInfoText() {
       StringBuffer sb = new StringBuffer();
       sb.append("Your new icon can be found ");
       try {
           generateIconFilePath();
           sb.append("<a href=\"" + getIconFilePath() +
"\">here</a>");
       } catch (Exception e) {
           sb.append("at: \"" + System.getProperty("user.home") +
File.separator +
                   this.getIconFileName() + "\"");
       }
       String iconPathInfo = sb.toString();
       System.out.println(iconPathInfo);
       return iconPathInfo;
   }

However, the link is still not clickable in spite of the setup via
HyperlinkListener

> 2) add a MouseListener to the JLabel - the
> changing of color for hover and active have to
> be separately handled, though.

Sorry I meant JEditorPane, not JLabel

> Andrew T.
Steve W. Jackson - 16 Jan 2007 15:21 GMT
> It appears that toURL() is deprecated within java.io.File.

I still use Java 5, so I hadn't noticed that in Java 6 it is indeed
deprecated.  But there's a note in the API Javadocs even in Java 5
indicating that toURL doesn't encode everything and recommending the use
of toURI, followed by the URI method toURL to accomplish the task.

> I tried this instead:
>
[quoted text clipped - 11 lines]
>
> Phil

The toString and replace parts should still not be necessary...but the
presence of the &gt and &lt parts suggest that the string in question
had angle brackets in it.  Perhaps that's one part of the problem you
encountered.  Plus, there's the question of a link being clickable on a
JLabel...unless it's new to Java 6 (and therefore beyond my experience),
I don't think that's possible, is it?

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

phillip.s.powell@gmail.com - 16 Jan 2007 16:32 GMT
> > It appears that toURL() is deprecated within java.io.File.
>
[quoted text clipped - 25 lines]
> JLabel...unless it's new to Java 6 (and therefore beyond my experience),
> I don't think that's possible, is it?

I changed it to a JEditorPane, which is what I needed all along,
however, the link is still not clickable:

private JEditorPane createHyperlinkPane(String iconPathInfo) {
       JEditorPane pane = new JEditorPane();
       pane.setBackground(UIManager.getColor("control"));
       pane.setOpaque(true);
       pane.setEditorKit(new HTMLEditorKit());
       pane.setFont(IconMaker.font);
       pane.setText(iconPathInfo);
       pane.setContentType("text/html");
       pane.addHyperlinkListener(new HyperlinkListener() {
           public void hyperlinkUpdate(HyperlinkEvent evt) {
               if (evt.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED) {
                   System.out.println("you clicked!");
               }
           }
       });

       return pane;
   }

   private String createIconPathInfoText() {
       StringBuffer sb = new StringBuffer();
       sb.append("Your new icon can be found ");
       try {
           generateIconFilePath();
           sb.append("<a href=\"" + getIconFilePath() +
"\">here</a>");
       } catch (Exception e) {
           sb.append("at: \"" + System.getProperty("user.home") +
File.separator +
                   this.getIconFileName() + "\"");
       }
       String iconPathInfo = sb.toString();
       System.out.println(iconPathInfo);
       return iconPathInfo;
   }

Still have no clue what's not working.
Phil

> = Steve =
Thomas Fritsch - 16 Jan 2007 17:00 GMT
>   JEditorPane pane = new JEditorPane();
>   pane.setBackground(UIManager.getColor("control"));
[quoted text clipped - 12 lines]
>
> Still have no clue what's not working.
I vaguely remember that you have to do
  pane.setEditable(false);
in order to get the event actually fired by the JEditorpane.

The reasoning behind that was:
When there is an editable JEditorPane, the user clicks into it because he
wants to edit the HTML text, but doesn't want to follow the clicked link.

Signature

Thomas

phillip.s.powell@gmail.com - 16 Jan 2007 17:05 GMT
> >   JEditorPane pane = new JEditorPane();
> >   pane.setBackground(UIManager.getColor("control"));
[quoted text clipped - 19 lines]
> When there is an editable JEditorPane, the user clicks into it because he
> wants to edit the HTML text, but doesn't want to follow the clicked link.

That was it!! Thanx!!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.