KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > shared > wg > URI


1
2
3 /*
4  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
5  * See end of file.
6  */

7 package com.hp.hpl.jena.shared.wg;
8 import com.hp.hpl.jena.rdf.arp.MalformedURIException;
9
10 /**
11  * Only for test package, not part of public API.
12  * Make arp.URI look like java.net.URI.
13  * @author Jeremy Carroll
14  *
15  *
16  */

17 public class URI extends com.hp.hpl.jena.rdf.arp.URI {
18     private String JavaDoc relative;
19     public static URI create(String JavaDoc s) {
20         try {
21             return new URI(s);
22         }
23         catch (MalformedURIException e) {
24             if ( e.toString().indexOf("No scheme")!= -1) {
25                 try {
26                 return new URI(s,false);
27                 }
28                 catch (MalformedURIException ee) {
29                 }
30             }
31             throw new IllegalArgumentException JavaDoc(e.toString());
32         }
33     }
34     
35     private URI(String JavaDoc s,boolean x) throws MalformedURIException {
36         super("http://foo");
37         relative = s;
38     }
39     private URI(String JavaDoc s) throws MalformedURIException {
40         super(s);
41     }
42     
43     private URI(URI x, URI y) throws MalformedURIException {
44         super(x, y.toString());
45     }
46     public boolean isAbsolute() {
47         return relative == null;
48     }
49     public URI resolve(URI rel) {
50         try {
51         return new URI(this,rel);
52         }
53         catch (MalformedURIException e) {
54             throw new IllegalArgumentException JavaDoc(e.toString());
55         }
56     }
57     
58     public java.net.URL JavaDoc toURL() throws java.net.MalformedURLException JavaDoc {
59         return new java.net.URL JavaDoc(getURIString());
60     }
61         
62     public URI relativize(URI x) {
63         String JavaDoc me = toString();
64         String JavaDoc xx = x.toString();
65         if ( !xx.startsWith(me) ) {
66             throw new IllegalArgumentException JavaDoc(xx + " is not relative to " + me);
67         }
68         String JavaDoc sub = xx.substring( me.length() );
69         return create( sub.charAt(0) == '/' ? sub.substring( 1 ) : sub );
70     }
71
72     public String JavaDoc getURIString()
73         { return relative == null ? super.getURIString() : relative; }
74         
75     public String JavaDoc toString()
76         { return getURIString(); }
77 }
78 /*
79  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
80  * All rights reserved.
81  *
82  * Redistribution and use in source and binary forms, with or without
83  * modification, are permitted provided that the following conditions
84  * are met:
85  * 1. Redistributions of source code must retain the above copyright
86  * notice, this list of conditions and the following disclaimer.
87  * 2. Redistributions in binary form must reproduce the above copyright
88  * notice, this list of conditions and the following disclaimer in the
89  * documentation and/or other materials provided with the distribution.
90  * 3. The name of the author may not be used to endorse or promote products
91  * derived from this software without specific prior written permission.
92
93  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
94  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
95  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
96  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
97  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
99  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
100  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
101  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
102  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
103  */

104
Popular Tags