Remove dependency on IPath (FELIX-1509)
Created new Resource concept to encode BND concepts foo, foo=foo, {foo=foo}, @foo (FELIX-1814)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@980146 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/PathUtil.java b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/PathUtil.java
new file mode 100644
index 0000000..e3a524b
--- /dev/null
+++ b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/PathUtil.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.felix.sigil.eclipse;
+
+import java.io.File;
+
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+
+/**
+ * @author dave
+ *
+ */
+public class PathUtil
+{
+    /**
+     * @param sourceRootPath
+     * @return
+     */
+    public static IPath newPathIfNotNull(String path)
+    {
+        return path == null ? null : new Path(path);
+    }
+
+
+    /**
+     * @param absolutePath
+     * @return
+     */
+    public static IPath newPathIfExists(File file)
+    {
+        if (file == null) return null;
+        if (file.exists()) return new Path(file.getAbsolutePath());
+        // fine
+        return null;
+    }
+
+
+}
diff --git a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/SigilCore.java b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/SigilCore.java
index 3648ad1..8210e0f 100644
--- a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/SigilCore.java
+++ b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/SigilCore.java
@@ -20,6 +20,7 @@
 package org.apache.felix.sigil.eclipse;
 
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
@@ -364,8 +365,8 @@
         {
             public boolean visit( ISigilBundle b )
             {
-                IPath path = b.getLocation();
-                if ( path != null && path.toOSString().equals( bp ) )
+                File path = b.getLocation();
+                if ( path != null && path.getAbsolutePath().equals( bp ) )
                 {
                     flag.set( true );
                     return false;
diff --git a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/internal/model/project/SigilProject.java b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/internal/model/project/SigilProject.java
index 602e5a9..96327cf 100644
--- a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/internal/model/project/SigilProject.java
+++ b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/internal/model/project/SigilProject.java
@@ -22,6 +22,7 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
@@ -34,6 +35,7 @@
 
 import org.apache.felix.sigil.config.BldFactory;
 import org.apache.felix.sigil.config.IBldProject;
+import org.apache.felix.sigil.eclipse.PathUtil;
 import org.apache.felix.sigil.eclipse.SigilCore;
 import org.apache.felix.sigil.eclipse.job.ThreadProgressMonitor;
 import org.apache.felix.sigil.eclipse.model.project.ISigilProjectModel;
@@ -504,7 +506,7 @@
 
     public IPath findBundleLocation() throws CoreException
     {
-        IPath p = getBundle().getLocation();
+        IPath p = PathUtil.newPathIfExists(getBundle().getLocation());
         if ( p == null )
         {
             p = SigilCore.getDefault().findDefaultBundleLocation( this );
diff --git a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/internal/repository/eclipse/OSGiInstallRepository.java b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/internal/repository/eclipse/OSGiInstallRepository.java
index 8dc8461..97085e2 100644
--- a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/internal/repository/eclipse/OSGiInstallRepository.java
+++ b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/internal/repository/eclipse/OSGiInstallRepository.java
@@ -117,9 +117,10 @@
             ISigilBundle bundle = buildBundle( jar.getManifest(), f );
             if ( bundle != null )
             {
-                bundle.setLocation( p );
-                bundle.setSourcePathLocation( source );
-                bundle.setSourceRootPath( new Path( "src" ) );
+                bundle.setLocation( f );
+                bundle.setSourcePathLocation( source.toFile() );
+                // XXX hard coded src location
+                bundle.setSourceRootPath( "src" );
                 bundles.add( bundle );
             }
         }
@@ -162,7 +163,7 @@
         {
             bundle = ModelElementFactory.getInstance().newModelElement( ISigilBundle.class );
             bundle.addChild( info );
-            bundle.setLocation( new Path( f.getAbsolutePath() ) );
+            bundle.setLocation( f );
         }
 
         return bundle;
diff --git a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/model/util/JavaHelper.java b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/model/util/JavaHelper.java
index 4ba57f7..dd49f0b 100644
--- a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/model/util/JavaHelper.java
+++ b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/model/util/JavaHelper.java
@@ -42,7 +42,8 @@
 import java.util.regex.Pattern;
 
 import org.apache.felix.sigil.common.osgi.VersionRange;
-import org.apache.felix.sigil.common.osgi.VersionRangeBoundingRule;
+import org.apache.felix.sigil.config.Resource;
+import org.apache.felix.sigil.eclipse.PathUtil;
 import org.apache.felix.sigil.eclipse.SigilCore;
 import org.apache.felix.sigil.eclipse.model.project.ISigilProjectModel;
 import org.apache.felix.sigil.model.IModelElement;
@@ -65,7 +66,6 @@
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Platform;
-import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.core.runtime.content.IContentDescription;
 import org.eclipse.core.runtime.content.IContentType;
 import org.eclipse.core.runtime.content.IContentTypeManager;
@@ -83,7 +83,6 @@
 import org.eclipse.jdt.core.IJavaProject;
 import org.eclipse.jdt.core.ILocalVariable;
 import org.eclipse.jdt.core.IMethod;
-import org.eclipse.jdt.core.IPackageDeclaration;
 import org.eclipse.jdt.core.IPackageFragment;
 import org.eclipse.jdt.core.IPackageFragmentRoot;
 import org.eclipse.jdt.core.IParent;
@@ -93,7 +92,6 @@
 import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.core.JavaModelException;
 import org.eclipse.jdt.core.Signature;
-import org.eclipse.jface.preference.IPreferenceStore;
 import org.osgi.framework.Version;
 
 
@@ -484,7 +482,7 @@
 
             if ( entries == null )
             {
-                IPath path = bundle.getLocation();
+                Path path = new Path(bundle.getLocation().getAbsolutePath());
 
                 if ( path == null )
                 {
@@ -527,7 +525,8 @@
                 IPath cache = bundleCache.append( name );
                 Collection<String> classpath = bundle.getBundleInfo().getClasspaths();
                 ArrayList<IClasspathEntry> entries = new ArrayList<IClasspathEntry>( classpath.size() );
-                IPath source = bundle.getSourcePathLocation();
+                IPath source = PathUtil.newPathIfExists(bundle.getSourcePathLocation());
+                IPath rootPath = PathUtil.newPathIfNotNull(bundle.getSourceRootPath());
 
                 if ( source != null && !source.toFile().exists() )
                 {
@@ -542,7 +541,7 @@
                         IPath p = ".".equals( cp ) ? path : cache.append( cp );
                         if ( p.toFile().exists() )
                         {
-                            IClasspathEntry e = JavaCore.newLibraryEntry( p, source, bundle.getSourceRootPath(), rules,
+                            IClasspathEntry e = JavaCore.newLibraryEntry( p, source, rootPath, rules,
                                 attributes, exported );
                             entries.add( e );
                         }
@@ -550,7 +549,7 @@
                 }
                 else
                 { // default classpath is .
-                    IClasspathEntry e = JavaCore.newLibraryEntry( path, source, bundle.getSourceRootPath(), rules,
+                    IClasspathEntry e = JavaCore.newLibraryEntry( path, source, rootPath, rules,
                         attributes, exported );
                     entries.add( e );
                 }
@@ -587,7 +586,7 @@
                 FileInputStream fin = null;
                 try
                 {
-                    fin = new FileInputStream( bundle.getLocation().toFile() );
+                    fin = new FileInputStream( bundle.getLocation() );
                     JarInputStream in = new JarInputStream( fin );
                     JarEntry entry;
                     while ( ( entry = in.getNextJarEntry() ) != null )
@@ -867,9 +866,9 @@
     {
         IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
         IContentType txt = contentTypeManager.getContentType( "org.eclipse.core.runtime.text" );
-        for ( IPath p : project.getBundle().getSourcePaths() )
+        for ( Resource p : project.getBundle().getSourcePaths() )
         {
-            IFile f = project.getProject().getFile( p );
+            IFile f = project.getProject().getFile( p.getLocalFile() );
             if ( f.exists() )
             {
                 try
diff --git a/sigil/eclipse/runtime/src/org/apache/felix/sigil/eclipse/runtime/RuntimeBundleResolver.java b/sigil/eclipse/runtime/src/org/apache/felix/sigil/eclipse/runtime/RuntimeBundleResolver.java
index 2c4a399..ace847b 100644
--- a/sigil/eclipse/runtime/src/org/apache/felix/sigil/eclipse/runtime/RuntimeBundleResolver.java
+++ b/sigil/eclipse/runtime/src/org/apache/felix/sigil/eclipse/runtime/RuntimeBundleResolver.java
@@ -62,7 +62,7 @@
                     }
                     else {
                         b.synchronize(null);
-                        uris.add( b.getLocation().toFile().toURI() );
+                        uris.add( b.getLocation().toURI() );
                     }
                 }
             }
diff --git a/sigil/eclipse/search/src/org/apache/felix/sigil/search/SigilSearch.java b/sigil/eclipse/search/src/org/apache/felix/sigil/search/SigilSearch.java
index bf871ff..e3055ca 100644
--- a/sigil/eclipse/search/src/org/apache/felix/sigil/search/SigilSearch.java
+++ b/sigil/eclipse/search/src/org/apache/felix/sigil/search/SigilSearch.java
@@ -34,6 +34,7 @@
 
 import org.apache.bcel.classfile.ClassParser;
 import org.apache.bcel.classfile.JavaClass;
+import org.apache.felix.sigil.eclipse.PathUtil;
 import org.apache.felix.sigil.eclipse.SigilCore;
 import org.apache.felix.sigil.eclipse.model.project.ISigilProjectModel;
 import org.apache.felix.sigil.eclipse.model.util.JavaHelper;
@@ -162,7 +163,7 @@
                 {
                     if ( bundle.isSynchronized() )
                     {
-                        IPath loc = bundle.getLocation();
+                        IPath loc = PathUtil.newPathIfExists(bundle.getLocation());
                         if ( loc == null ) {
                             SigilCore.error("Location is null for " + bundle);
                         }
diff --git a/sigil/eclipse/ui/sigil.properties b/sigil/eclipse/ui/sigil.properties
index bcce8bd..f7ab9a0 100644
--- a/sigil/eclipse/ui/sigil.properties
+++ b/sigil/eclipse/ui/sigil.properties
@@ -27,6 +27,7 @@
 
 -imports: \
 	org.apache.felix.sigil.common.osgi, \
+	org.apache.felix.sigil.config, \
 	org.apache.felix.sigil.eclipse, \
 	org.apache.felix.sigil.eclipse.install, \
 	org.apache.felix.sigil.eclipse.job, \
diff --git a/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/ui/editors/project/ResourceBuildSection.java b/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/ui/editors/project/ResourceBuildSection.java
index 0bd889c..1237678 100644
--- a/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/ui/editors/project/ResourceBuildSection.java
+++ b/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/ui/editors/project/ResourceBuildSection.java
@@ -20,9 +20,11 @@
 package org.apache.felix.sigil.ui.eclipse.ui.editors.project;
 
 
+import java.util.concurrent.Callable;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 
+import org.apache.felix.sigil.config.Resource;
 import org.apache.felix.sigil.eclipse.SigilCore;
 import org.apache.felix.sigil.eclipse.model.project.ISigilProjectModel;
 import org.apache.felix.sigil.model.eclipse.ISigilBundle;
@@ -33,6 +35,7 @@
 import org.eclipse.core.resources.IResourceChangeListener;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
 import org.eclipse.jface.preference.PreferenceDialog;
 import org.eclipse.jface.util.IPropertyChangeListener;
 import org.eclipse.jface.util.PropertyChangeEvent;
@@ -128,22 +131,32 @@
 
         bundle.clearSourcePaths();
 
-        SigilUI.runInUISync( new Runnable()
+        try
         {
-            public void run()
+            SigilUI.runInUISync( new Callable<Void>()
             {
-                for ( Object o : viewer.getCheckedElements() )
+                public Void call() throws Exception
                 {
-                    if ( !viewer.getGrayed( o ) )
+                    for ( Object o : viewer.getCheckedElements() )
                     {
-                        IResource r = ( IResource ) o;
-                        getProjectModel().getBundle().addSourcePath( r.getProjectRelativePath() );
+                        if ( !viewer.getGrayed( o ) )
+                        {
+                            IResource r = ( IResource ) o;
+                            
+                            getProjectModel().getBundle().addSourcePath( toBldResource(r) );
+                        }
                     }
+                    
+                    return null;
                 }
-            }
-        } );
+            } );
 
-        super.commit( onSave );
+            super.commit( onSave );
+        }
+        catch (Exception e)
+        {
+            SigilCore.warn("Failed to update resource", e);
+        }
     }
 
 
@@ -151,9 +164,9 @@
     protected void refreshSelections()
     {
         // zero the state
-        for ( IPath path : getProjectModel().getBundle().getSourcePaths() )
+        for ( Resource path : getProjectModel().getBundle().getSourcePaths() )
         {
-            IResource r = findResource( path );
+            IResource r = findResource( new Path(path.getLocalFile()) );
             if ( r != null )
             {
                 viewer.expandToLevel( r, 0 );
@@ -172,16 +185,35 @@
     @Override
     protected void syncResourceModel( IResource element, boolean checked )
     {
-        if ( checked )
+        try
         {
-            getProjectModel().getBundle().addSourcePath( element.getProjectRelativePath() );
-        }
-        else
-        {
-            getProjectModel().getBundle().removeSourcePath( element.getProjectRelativePath() );
-        }
+            Resource resource = toBldResource(element);
+            
+            if ( checked )
+            {
+                getProjectModel().getBundle().addSourcePath( resource );
+            }
+            else
+            {
+                getProjectModel().getBundle().removeSourcePath( resource );
+            }
 
-        markDirty();
+            markDirty();
+        }
+        catch (CoreException e)
+        {
+            SigilCore.warn("Failed to sync resource " + element, e);
+        }
+    }
+
+    /**
+     * @param element
+     * @return
+     * @throws CoreException 
+     */
+    private Resource toBldResource(IResource element) throws CoreException
+    {
+        return getProjectModel().getBldProject().newResource(element.getProjectRelativePath().toString());
     }
 
     private AtomicBoolean disposed = new AtomicBoolean();