encode invalid character in URI
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / UriWithLabelElement.java
index 82dd7389c583d243a5b047747b4a63070e5da752..4801c2234fed52ec86b40e163744fc1f0390f4f8 100644 (file)
@@ -11,6 +11,7 @@ package eu.etaxonomy.taxeditor.ui.element;
 
 import java.io.IOException;
 import java.net.URI;
+import java.net.URL;
 
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
@@ -20,6 +21,7 @@ import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 
+import eu.etaxonomy.cdm.common.UrlUtf8Coder;
 import eu.etaxonomy.taxeditor.l10n.Messages;
 import eu.etaxonomy.taxeditor.model.ImageResources;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
@@ -109,7 +111,24 @@ public class UriWithLabelElement extends AbstractUriWithExceptionLabelElement<UR
     protected URI getParsedText() throws Exception {
         String uriText = super.getText();
         if(uriText!=null){
-            return new URI(super.getText());
+            try{
+                return new URI(super.getText());
+            }catch(Exception e){
+                URL url = new URL(uriText);
+                String[] pathElements =  url.getPath().split("/");
+
+                for (String element: pathElements){
+                    String replacement = UrlUtf8Coder.encode(element);
+                    uriText = uriText.replace(element, replacement);
+                }
+                if (url.getQuery() != null){
+                    uriText = uriText.replace(url.getQuery(), UrlUtf8Coder.encode(url.getQuery()));
+                }
+                url = new URL(uriText);
+                return url.toURI();
+
+            }
+
         }
         return null;
     }