Begin move to use standardized URIs

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@615572 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrUpdate.java b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrUpdate.java
index 94b9f45..cbc9389 100644
--- a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrUpdate.java
+++ b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrUpdate.java
@@ -74,12 +74,12 @@
     /**
      * name and path to the obr.xml file.
      */
-    private String m_obrXml;
+    private URI m_obrXml;
 
     /**
      * name and path to the bundle jar file.
      */
-    private String m_bundlePath;
+    private URI m_bundlePath;
 
     /**
      * maven project description.
@@ -107,9 +107,9 @@
     private ResourcesBundle m_resourceBundle;
 
     /**
-     * base directory used to relativize bundle URIs.
+     * base URI used to relativize bundle URIs.
      */
-    private PathFile m_baseDir;
+    private URI m_baseURI;
 
 
     /**
@@ -126,9 +126,9 @@
         String mavenRepositoryPath, Config userConfig, Log logger )
     {
         // m_localRepo = localRepo;
-        m_bundlePath = bundlePath;
-        m_repositoryXml = repositoryXml.getUri();
-        m_obrXml = obrXml;
+        m_bundlePath = ObrUtils.toFileURI( bundlePath );
+        m_repositoryXml = repositoryXml.getFile().toURI(); // FIXME: remove when PathFile is gone
+        m_obrXml = ObrUtils.toFileURI( obrXml );
         m_project = project;
         m_logger = logger;
 
@@ -138,11 +138,11 @@
 
         if ( userConfig.isRemotely() )
         {
-            m_baseDir = new PathFile( mavenRepositoryPath );
+            m_baseURI = ObrUtils.toFileURI( mavenRepositoryPath );
         }
         else
         {
-            m_baseDir = repositoryXml;
+            m_baseURI = m_repositoryXml;
         }
     }
 
@@ -166,20 +166,17 @@
         }
 
         // get the file size
-        PathFile pf = new PathFile( m_bundlePath );
-        File bundleFile = pf.getFile();
-        pf.setBaseDir( m_baseDir.getOnlyAbsolutePath() );
+        File bundleFile = new File( m_bundlePath );
         if ( bundleFile.exists() )
         {
-            m_resourceBundle.setSize( String.valueOf( bundleFile.length() ) );
+            URI bundleURI = m_bundlePath;
             if ( m_userConfig.isPathRelative() )
             {
-                m_resourceBundle.setUri( pf.getOnlyRelativeFilename().replace( '\\', '/' ) );
+                bundleURI = ObrUtils.getRelativeURI( m_baseURI, bundleURI );
             }
-            else
-            {
-                m_resourceBundle.setUri( "file:" + m_bundlePath );
-            }
+
+            m_resourceBundle.setSize( String.valueOf( bundleFile.length() ) );
+            m_resourceBundle.setUri( bundleURI.toASCIIString() );
         }
         else
         {
@@ -213,7 +210,7 @@
         try
         {
             // use bindex to extract bundle information
-            bindexExtractor = new ExtractBindexInfo( m_repositoryXml, m_bundlePath );
+            bindexExtractor = new ExtractBindexInfo( m_repositoryXml, m_bundlePath.getPath() );
         }
         catch ( MojoExecutionException e )
         {
@@ -273,20 +270,8 @@
         File fout = new File( m_repositoryXml );
         if ( !fout.exists() )
         {
-            // create the repository.xml
-            try
-            {
-                fout.createNewFile();
-                m_logger.info( "Created " + fout.getAbsolutePath() );
-            }
-            catch ( IOException e )
-            {
-                m_logger.error( "Cannot create file " + fout.getAbsolutePath() );
-                e.printStackTrace();
-                return -1;
-            }
-
             Document doc = m_documentBuilder.newDocument();
+
             // create xml tree
             Date d = new Date();
             d.setTime( System.currentTimeMillis() );
@@ -305,7 +290,7 @@
         }
 
         // now we parse the repository.xml file
-        m_repositoryDoc = parseFile( fout.getAbsolutePath(), m_documentBuilder );
+        m_repositoryDoc = parseFile( m_repositoryXml, m_documentBuilder );
         if ( m_repositoryDoc == null )
         {
             return -1;
@@ -321,7 +306,7 @@
      * @param documentBuilder DocumentBuilder get from xerces
      * @return Document which describe this file
      */
-    private Document parseFile( String filename, DocumentBuilder documentBuilder )
+    private Document parseFile( URI filename, DocumentBuilder documentBuilder )
     {
         if ( documentBuilder == null )
         {
@@ -465,6 +450,7 @@
         DOMSource input = new DOMSource( treeToBeWrite );
 
         File fichier = new File( outputFilename );
+        fichier.getParentFile().mkdirs();
         FileOutputStream flux = null;
         try
         {
diff --git a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrUtils.java b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrUtils.java
index ec1d020..50b99a9 100644
--- a/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrUtils.java
+++ b/maven-obr-plugin/src/main/java/org/apache/felix/obr/plugin/ObrUtils.java
@@ -34,6 +34,7 @@
  */
 public class ObrUtils
 {
+    private static final String DOT_XML = ".xml";
     private static final String REPO_XML = "repository.xml";
     private static final String OBR_XML = "obr.xml";
 
@@ -51,7 +52,7 @@
         {
             obrRepository = mavenRepository + '/' + REPO_XML;
         }
-        else if ( !obrRepository.endsWith( ".xml" ) )
+        else if ( !obrRepository.toLowerCase().endsWith( DOT_XML ) )
         {
             obrRepository = obrRepository + '/' + REPO_XML;
         }
@@ -103,24 +104,51 @@
 
 
     /**
-     * @param repositoryXml URI pointing to repository.xml
-     * @param bundlePath local path to bundle jarfile
-     * @return relative path to bundle jarfile
+     * @param path filesystem path
+     * @return file URI for the path
      */
-    public static String relativize( URI repositoryXml, String bundlePath )
+    public static URI toFileURI( String path )
+    {
+        if ( null == path )
+        {
+            return null;
+        }
+        else if ( path.startsWith( "file:" ) )
+        {
+            return URI.create( path );
+        }
+        else
+        {
+            return new File( path ).toURI();
+        }
+    }
+
+
+    /**
+     * @param repositoryXml URI pointing to repository.xml, or directory containing it
+     * @param bundleJar URI pointing to bundle jarfile
+     * @return relative URI to bundle jarfile
+     */
+    public static URI getRelativeURI( URI repositoryXml, URI bundleJar )
     {
         try
         {
             String repositoryPath = repositoryXml.getPath();
-            int lastFolderIndex = repositoryPath.lastIndexOf( '/' );
+            if ( repositoryPath.toLowerCase().endsWith( DOT_XML ) )
+            {
+                // remove filename to get containing directory
+                int dirnameIndex = repositoryPath.lastIndexOf( '/' );
+                repositoryPath = repositoryPath.substring( 0, dirnameIndex );
+            }
 
-            URI rootURI = new URI( null, repositoryPath.substring( 0, lastFolderIndex ), null );
+            URI rootURI = new URI( null, repositoryPath, null );
+            URI localURI = new URI( null, bundleJar.getPath(), null );
 
-            return rootURI.relativize( new URI( null, bundlePath, null ) ).toASCIIString();
+            return rootURI.relativize( localURI );
         }
         catch ( Exception e )
         {
-            return bundlePath;
+            return bundleJar;
         }
     }
 }