2 * Copyright (C) 2021 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.common
;
12 import java
.io
.Serializable
;
13 import java
.net
.MalformedURLException
;
14 import java
.net
.URISyntaxException
;
18 * See https://dev.e-taxonomy.eu/redmine/issues/9114
24 implements Comparable
<URI
>, Serializable
{
26 private static final long serialVersionUID
= -8002215586516542076L;
28 private final java
.net
.URI javaUri
;
30 // ***************************** FACTORY METHODS ***************************************/
32 public static URI
fromString(String uri
) throws URISyntaxException
{
39 * @see java.net.URI#create(String)
41 public static URI
create(String uri
) {
44 } catch (URISyntaxException e
) {
45 throw new IllegalArgumentException(e
);
49 public static URI
fromUrl(URL url
) throws URISyntaxException
{
53 public static URI
fromFile(File file
) {
54 return new URI(file
.toURI());
57 // ******************************* CONSTRUCTOR ************************************/
59 @SuppressWarnings("unused")
60 private URI(){javaUri
= null;} //empty constructor required for JAXB (not tested but copied from DOI)
62 public URI(String uriString
) throws URISyntaxException
{
63 javaUri
= parseUriString(uriString
);
66 public URI(java
.net
.URI javaUri
) {
67 this.javaUri
= javaUri
;
70 //TODO maybe we can do encoding in between
71 public URI(URL url
) throws URISyntaxException
{
72 this.javaUri
= url
.toURI();
76 * @see java.net.URI#URI(String, String, String, int, String, String, String)
78 public URI(String protocol
, String userInfo
, String host
, int port
, String path
, String query
, String ref
) throws URISyntaxException
{
79 javaUri
= new java
.net
.URI(protocol
, userInfo
, host
, port
, path
, query
, ref
);
82 public URI(String scheme
, String authority
, String path
, String query
, String fragment
) throws URISyntaxException
{
83 javaUri
= new java
.net
.URI(scheme
, authority
, path
, query
, fragment
);
86 //************************************ GETTER ***********************************/
88 public java
.net
.URI
getJavaUri(){
92 //************************************ METHODS ************************************/
94 private java
.net
.URI
parseUriString(String uriString
) throws URISyntaxException
{
95 java
.net
.URI javaUri
= null;
96 if(uriString
!= null){
98 javaUri
= new java
.net
.URI(uriString
);
101 String encodedUri
= uriString
;
102 URL url
= new URL(encodedUri
);
103 String
[] pathElements
= url
.getPath().split("/");
104 for (String element
: pathElements
){
105 if(element
.contains("\\")){
106 //TODO needs discussion if backslash should be converted to slash instead, for now we keep it more strict
107 throw new URISyntaxException(uriString
, "URI path must not contain backslash ('\')");
109 String replacement
= UrlUtf8Coder
.encode(element
);
110 encodedUri
= encodedUri
.replace(element
, replacement
);
112 if (url
.getQuery() != null){
113 encodedUri
= encodedUri
.replace(url
.getQuery(), UrlUtf8Coder
.encode(url
.getQuery()));
115 String fragmentElement
= uriString
.substring(uriString
.indexOf("#")+1,uriString
.length());
116 if(fragmentElement
.contains("\\")){
117 //TODO needs discussion if backslash should be converted to slash instead, for now we keep it more strict
118 throw new URISyntaxException(uriString
, "URI path must not contain backslash ('\')");
120 String replacement
= UrlUtf8Coder
.encode(fragmentElement
);
121 encodedUri
= encodedUri
.replace(fragmentElement
, replacement
);
123 url
= new URL(encodedUri
);
125 javaUri
= url
.toURI();
126 } catch (MalformedURLException e1
) {
127 throw new URISyntaxException(uriString
, e1
.getMessage());
134 public File
toFile(){
135 return new File(javaUri
);
138 //******************************** Wrapper methods *********************/
140 public URL
toURL() throws MalformedURLException
{
141 return javaUri
.toURL();
144 public String
getHost() {
145 return javaUri
.getHost();
148 public int getPort() {
149 return javaUri
.getPort();
152 public String
getScheme() {
153 return javaUri
.getScheme();
156 public boolean isAbsolute() {
157 return javaUri
.isAbsolute();
160 public String
getPath() {
161 return javaUri
.getPath();
164 public String
getFragment() {
165 return javaUri
.getFragment();
168 public Object
getQuery() {
169 return javaUri
.getQuery();
172 //****************************** equals *****************************/
175 public int hashCode() {
176 return javaUri
.hashCode();
180 public boolean equals(Object obj
) {
181 if (obj
instanceof URI
){
182 return javaUri
.equals(((URI
)obj
).javaUri
);
188 public int compareTo(URI that
) {
189 return this.javaUri
.compareTo(that
.javaUri
);
192 //********************** clone ***********************************/
195 protected URI
clone() throws CloneNotSupportedException
{
196 return new URI(this.javaUri
);
199 //******************************** toString ****************************/
202 public String
toString() {
203 return javaUri
.toString();