Author: aadamchik
Date: Sun Dec 13 15:55:28 2009
New Revision: 890067

URL: http://svn.apache.org/viewvc?rev=890067&view=rev
Log:
CAY-1329 DI-based cayenne-project module

* fixing URL to file conversion

Modified:
    
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java
    
cayenne/main/trunk/framework/cayenne-project-unpublished/src/main/java/org/apache/cayenne/project2/FileProjectSaver.java

Modified: 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java?rev=890067&r1=890066&r2=890067&view=diff
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java
 (original)
+++ 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java
 Sun Dec 13 15:55:28 2009
@@ -36,6 +36,8 @@
 import java.io.Serializable;
 import java.lang.reflect.Member;
 import java.lang.reflect.Modifier;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.sql.SQLException;
 import java.util.ArrayList;
@@ -65,11 +67,30 @@
 
 /**
  * Contains various unorganized static utility methods used across Cayenne.
- * 
  */
 public class Util {
 
     /**
+     * Converts URL to file. Throws {...@link IllegalArgumentException} if the 
URL is not a
+     * "file://" URL.
+     */
+    public static File toFile(URL url) throws IllegalArgumentException {
+        // must convert spaces to %20, or URL->URI conversion may fail
+        String urlString = url.toExternalForm();
+
+        URI uri;
+        try {
+            uri = new URI(urlString.replace(" ", "%20"));
+        }
+        catch (URISyntaxException e) {
+            throw new IllegalArgumentException("URL "
+                    + urlString
+                    + " can't be converted to URI", e);
+        }
+        return new File(uri);
+    }
+
+    /**
      * Reads file contents, returning it as a String, using System default 
line separator.
      */
     public static String stringFromFile(File file) throws IOException {
@@ -672,14 +693,15 @@
             return Class.forName("[L" + className + ";", true, classLoader);
         }
     }
-    
+
     static void setReverse(
             final Persistent sourceObject,
             String propertyName,
             final Persistent targetObject) {
-        
-        ArcProperty property = (ArcProperty) 
Cayenne.getClassDescriptor(sourceObject).
-            getProperty(propertyName);
+
+        ArcProperty property = (ArcProperty) Cayenne
+                .getClassDescriptor(sourceObject)
+                .getProperty(propertyName);
         ArcProperty reverseArc = property.getComplimentaryReverseArc();
         if (reverseArc != null) {
             reverseArc.visit(new PropertyVisitor() {
@@ -699,12 +721,12 @@
                 }
 
             });
-            
+
             sourceObject.getObjectContext().getGraphManager().arcCreated(
                     targetObject.getObjectId(),
                     sourceObject.getObjectId(),
                     reverseArc.getName());
-    
+
             markAsDirty(targetObject);
         }
     }
@@ -713,9 +735,10 @@
             final Persistent sourceObject,
             String propertyName,
             final Persistent targetObject) {
-        
-        ArcProperty property = (ArcProperty) 
Cayenne.getClassDescriptor(sourceObject).
-            getProperty(propertyName);
+
+        ArcProperty property = (ArcProperty) Cayenne
+                .getClassDescriptor(sourceObject)
+                .getProperty(propertyName);
         ArcProperty reverseArc = property.getComplimentaryReverseArc();
         if (reverseArc != null) {
             reverseArc.visit(new PropertyVisitor() {
@@ -735,7 +758,7 @@
                 }
 
             });
-            
+
             sourceObject.getObjectContext().getGraphManager().arcDeleted(
                     targetObject.getObjectId(),
                     sourceObject.getObjectId(),
@@ -744,7 +767,7 @@
             markAsDirty(targetObject);
         }
     }
-    
+
     /**
      * Changes object state to MODIFIED if needed, returning true if the 
change has
      * occurred, false if not.

Modified: 
cayenne/main/trunk/framework/cayenne-project-unpublished/src/main/java/org/apache/cayenne/project2/FileProjectSaver.java
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-project-unpublished/src/main/java/org/apache/cayenne/project2/FileProjectSaver.java?rev=890067&r1=890066&r2=890067&view=diff
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-project-unpublished/src/main/java/org/apache/cayenne/project2/FileProjectSaver.java
 (original)
+++ 
cayenne/main/trunk/framework/cayenne-project-unpublished/src/main/java/org/apache/cayenne/project2/FileProjectSaver.java
 Sun Dec 13 15:55:28 2009
@@ -26,7 +26,6 @@
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
-import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -38,6 +37,7 @@
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.resource.Resource;
 import org.apache.cayenne.resource.URLResource;
+import org.apache.cayenne.util.Util;
 
 /**
  * A ProjectSaver saving project configuration to the filesystem.
@@ -133,12 +133,12 @@
         URL targetUrl = targetResource.getURL();
 
         try {
-            unit.targetFile = new File(targetUrl.toURI());
+            unit.targetFile = Util.toFile(targetUrl);
         }
-        catch (URISyntaxException e) {
+        catch (IllegalArgumentException e) {
             throw new CayenneRuntimeException(
                     "Can't save configuration to the following location: '%s'. 
"
-                            + "Only file locations are supported. (%s)",
+                            + "Is this a valid file location?. (%s)",
                     e,
                     targetUrl,
                     e.getMessage());
@@ -307,9 +307,9 @@
             URL sourceUrl = unit.sourceConfiguration.getURL();
             File sourceFile;
             try {
-                sourceFile = new File(sourceUrl.toURI());
+                sourceFile = Util.toFile(sourceUrl);
             }
-            catch (URISyntaxException e) {
+            catch (IllegalArgumentException e) {
                 // ignore non-file configurations...
                 continue;
             }


Reply via email to