Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35190 > unrolled thread

A little help with some logic

Started byKnute Johnson <nospam@rabbitbrush.frazmtn.com>
First post2016-08-03 21:39 -0700
Last post2016-08-06 11:13 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  A little help with some logic Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-08-03 21:39 -0700
    Re: A little help with some logic Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-04 09:28 -0400
      Re: A little help with some logic Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-08-04 16:44 -0700
        Re: A little help with some logic Robert Klemme <shortcutter@googlemail.com> - 2016-08-06 11:13 +0200

#35190 — A little help with some logic

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2016-08-03 21:39 -0700
SubjectA little help with some logic
Message-ID<nnugtj$78m$1@dont-email.me>
I don't know maybe it is too late but I can't seem to come up with 
anything better than this.  What I need to do is take a String that 
contains the name of a file in my program's JAR file, a URL for a 
network site or the name of a file on the accessible file system and 
turn it into a URI.  I want to check in that order, in the JAR file, a 
file on the filesystem or a network reachable URL.  Even this has some 
holes and it is really ugly.  Thanks...

             URI uri = null;
             URL url = getClass().getResource(fname);
             if (url == null) {
                 try {
                     url = new URL(fname);
                 } catch (MalformedURLException murle) {
                     uri = new File(fname).toURI();
                 }
             }
             if (uri == null) {
                 try {
                     uri = url.toURI();
                 } catch (URISyntaxException urise) {
                     urise.printStackTrace();
                 }
             }

             if (uri != null)
                  // do something with the URI



-- 

Knute Johnson

[toc] | [next] | [standalone]


#35193

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-08-04 09:28 -0400
Message-ID<nnvfu8$vov$1@dont-email.me>
In reply to#35190
On 8/4/2016 12:39 AM, Knute Johnson wrote:
> I don't know maybe it is too late but I can't seem to come up with
> anything better than this.  What I need to do is take a String that
> contains the name of a file in my program's JAR file, a URL for a
> network site or the name of a file on the accessible file system and
> turn it into a URI.  I want to check in that order, in the JAR file, a
> file on the filesystem or a network reachable URL.  Even this has some
> holes and it is really ugly.  Thanks...
>
>             URI uri = null;
>             URL url = getClass().getResource(fname);
>             if (url == null) {
>                 try {
>                     url = new URL(fname);
>                 } catch (MalformedURLException murle) {
>                     uri = new File(fname).toURI();
>                 }
>             }
>             if (uri == null) {
>                 try {
>                     uri = url.toURI();
>                 } catch (URISyntaxException urise) {
>                     urise.printStackTrace();
>                 }
>             }
>
>             if (uri != null)
>                  // do something with the URI

     (This checks in the first order you mention, so I guess the
second order was an "it is too late" slip-up.)

     It seems clear enough to me.  All I'd suggest is that an
exists() test might be a good idea in the File case, and that
something more severe than printStackTrace() is probably called
for if URISyntaxException gets thrown.

     One other item that might or might not interest you: I've seen
that File's toURI() sometimes omits the "authority" part, giving
the unusable "file:/C:/foo.dat" instead of "file:///C:/foo.dat".
When I first ran into this I got around it with some heavy-handed
string-bashing, but (says the JavaDoc) the java.nio.Path{,s} classes
may offer a better way.  Haven't tried them myself (for this purpose).

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#35194

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2016-08-04 16:44 -0700
Message-ID<no0k1b$scf$1@dont-email.me>
In reply to#35193
On 8/4/2016 06:28, Eric Sosman wrote:
>     (This checks in the first order you mention, so I guess the
> second order was an "it is too late" slip-up.)
>
>     It seems clear enough to me.  All I'd suggest is that an
> exists() test might be a good idea in the File case, and that
> something more severe than printStackTrace() is probably called
> for if URISyntaxException gets thrown.
>
>     One other item that might or might not interest you: I've seen
> that File's toURI() sometimes omits the "authority" part, giving
> the unusable "file:/C:/foo.dat" instead of "file:///C:/foo.dat".
> When I first ran into this I got around it with some heavy-handed
> string-bashing, but (says the JavaDoc) the java.nio.Path{,s} classes
> may offer a better way.  Haven't tried them myself (for this purpose).
>

Thanks Eric and Stefan for looking at it.  Here's what I ended up with

         try {
             URI uri = null;
             URL url = getClass().getResource(source);
             if (url != null)
                 uri = url.toURI();
             if (uri == null) {
                 File file = new File(source);
                 if (file.exists())
                     uri = file.toURI();
             }
             if (uri == null)
                 uri = new URL(source).toURI();

             // use the URI
         } catch (URISyntaxException|MalformedURLException ex) {
             // deal with the exceptions
         }


-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#35221

FromRobert Klemme <shortcutter@googlemail.com>
Date2016-08-06 11:13 +0200
Message-ID<e0lo2oFabteU1@mid.individual.net>
In reply to#35194
On 05.08.2016 01:44, Knute Johnson wrote:
> On 8/4/2016 06:28, Eric Sosman wrote:
>>     (This checks in the first order you mention, so I guess the
>> second order was an "it is too late" slip-up.)
>>
>>     It seems clear enough to me.  All I'd suggest is that an
>> exists() test might be a good idea in the File case, and that
>> something more severe than printStackTrace() is probably called
>> for if URISyntaxException gets thrown.
>>
>>     One other item that might or might not interest you: I've seen
>> that File's toURI() sometimes omits the "authority" part, giving
>> the unusable "file:/C:/foo.dat" instead of "file:///C:/foo.dat".
>> When I first ran into this I got around it with some heavy-handed
>> string-bashing, but (says the JavaDoc) the java.nio.Path{,s} classes
>> may offer a better way.  Haven't tried them myself (for this purpose).
>>
>
> Thanks Eric and Stefan for looking at it.  Here's what I ended up with
>
>         try {
>             URI uri = null;
>             URL url = getClass().getResource(source);
>             if (url != null)
>                 uri = url.toURI();
>             if (uri == null) {
>                 File file = new File(source);
>                 if (file.exists())
>                     uri = file.toURI();
>             }
>             if (uri == null)
>                 uri = new URL(source).toURI();
>
>             // use the URI
>         } catch (URISyntaxException|MalformedURLException ex) {
>             // deal with the exceptions
>         }

I would still do it differently: the main change I'd do is to separate 
obtaining the URI from using it.  This gives much clearer code because 
you can get rid of all the null checks and replace it with one:

   private URI parseURI(final String s) {
     final URL url = getClass().getResource(s);

     if (url != null)
       try {
         return url.toURI();
       } catch (URISyntaxException e1) {
         // continue with next
       }

     final File f = new File(s);

     if (f.exists())
       return f.toURI();

     try {
       return new URL(s).toURI();
     } catch (MalformedURLException e) {
       return null;
     } catch (URISyntaxException e) {
       return null;
     }
   }

   public void doSomethingWith(final String source) {
     final URI uri = parseURI(source);

     if (uri == null) {
       // too bad, did not work
     } else {
       // OK, let's do it
     }
   }

Note, we could nest the checks with File and later in the first catch 
block but IMO that would obfuscate what the code does (i.e. try one 
method after the other).

Kind regards

	robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web