Author: rombert
Date: Wed May 21 10:16:54 2014
New Revision: 1596528

URL: http://svn.apache.org/r1596528
Log:
SLING-3174 - Add new simple project content wizard

Detect potential content projects by looking for a directory containing
both jcr_root and META-INF/vault/filter.xml . For now, we look under the
project's root location and under src/main/content.

Added:
    
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ProjectHelperTest.java
   (with props)
Modified:
    
sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ProjectHelper.java
    
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/ConvertToContentPackageAction.java

Modified: 
sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ProjectHelper.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ProjectHelper.java?rev=1596528&r1=1596527&r2=1596528&view=diff
==============================================================================
--- 
sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ProjectHelper.java
 (original)
+++ 
sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ProjectHelper.java
 Wed May 21 10:16:54 2014
@@ -22,10 +22,13 @@ import javax.xml.parsers.DocumentBuilder
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
+import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.Path;
 import org.eclipse.jdt.core.IJavaModel;
 import org.eclipse.jdt.core.IJavaProject;
 import org.eclipse.jdt.core.JavaCore;
@@ -41,15 +44,42 @@ import org.xml.sax.SAXException;
 
 public class ProjectHelper {
 
+    private static final String[] CONTENT_PACKAGE_STRUCTURE_BASE = new 
String[] { "/", "/content", "/src/main/content" };
+
        public static boolean isPotentialBundleProject(IProject project) {
                String packaging = getMavenProperty(project, "packaging");
                return (packaging!=null && "bundle".equals(packaging));
        }
        
        public static boolean isPotentialContentProject(IProject project) {
-               String packaging = getMavenProperty(project, "packaging");
-               return (packaging!=null && "content-package".equals(packaging));
+
+        return !isContentProject(project) && 
getInferredContentProjectContentRoot(project) != null;
        }
+
+    public static IContainer getInferredContentProjectContentRoot(IProject 
project) {
+
+        for (String base : CONTENT_PACKAGE_STRUCTURE_BASE) {
+            IContainer container;
+            if ("/".equals(base)) {
+                container = project;
+            } else {
+                container = project.getFolder(base);
+            }
+            if (container.exists() && hasContentPackageStructure(container)) {
+                return container;
+            }
+        }
+
+        return null;
+    }
+
+    private static boolean hasContentPackageStructure(IContainer base) {
+
+        IFile filterXml = 
base.getFile(Path.fromPortableString("META-INF/vault/filter.xml"));
+        IFolder jcrRoot = base.getFolder(Path.fromPortableString("jcr_root"));
+
+        return filterXml.exists() && jcrRoot.exists();
+    }
        
        public static String getMavenProperty(IProject project, String name) {
                try{

Added: 
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ProjectHelperTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ProjectHelperTest.java?rev=1596528&view=auto
==============================================================================
--- 
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ProjectHelperTest.java
 (added)
+++ 
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ProjectHelperTest.java
 Wed May 21 10:16:54 2014
@@ -0,0 +1,165 @@
+/*
+ * 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.sling.ide.test.impl;
+
+import static org.junit.Assert.assertThat;
+
+import java.io.ByteArrayInputStream;
+
+import org.apache.sling.ide.eclipse.core.internal.ProjectHelper;
+import org.apache.sling.ide.test.impl.helpers.ProjectAdapter;
+import org.apache.sling.ide.test.impl.helpers.TemporaryProject;
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.Path;
+import org.hamcrest.CoreMatchers;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class ProjectHelperTest {
+
+    @Rule
+    public TemporaryProject projectRule = new TemporaryProject();
+
+    @Test
+    public void inferContentProjectContentRootAtTheTopLevel() throws 
CoreException, InterruptedException {
+
+        // create faceted project
+        IProject contentProject = projectRule.getProject();
+
+        ProjectAdapter project = new ProjectAdapter(contentProject);
+        project.addNatures("org.eclipse.wst.common.project.facet.core.nature");
+
+        // install content facet
+        project.installFacet("sling.content", "1.0");
+
+        // create files
+        
project.createOrUpdateFile(Path.fromPortableString("jcr_root/test/hello.txt"), 
new ByteArrayInputStream(
+                "goodbye, world".getBytes()));
+
+        
project.createOrUpdateFile(Path.fromPortableString("META-INF/vault/filter.xml"),
 new ByteArrayInputStream(
+                "<workspaceFilter version=\"1.0\"/>".getBytes()));
+
+        
assertThat(ProjectHelper.getInferredContentProjectContentRoot(contentProject),
+                CoreMatchers.<IContainer> equalTo(contentProject));
+
+    }
+
+    @Test
+    public void inferContentProjectContentRootNested() throws CoreException, 
InterruptedException {
+
+        // create faceted project
+        IProject contentProject = projectRule.getProject();
+
+        ProjectAdapter project = new ProjectAdapter(contentProject);
+        project.addNatures("org.eclipse.wst.common.project.facet.core.nature");
+
+        // install content facet
+        project.installFacet("sling.content", "1.0");
+
+        // create files
+        
project.createOrUpdateFile(Path.fromPortableString("src/main/content/jcr_root/test/hello.txt"),
+                new ByteArrayInputStream("goodbye, world".getBytes()));
+
+        
project.createOrUpdateFile(Path.fromPortableString("src/main/content/META-INF/vault/filter.xml"),
+                new ByteArrayInputStream("<workspaceFilter 
version=\"1.0\"/>".getBytes()));
+
+        
assertThat(ProjectHelper.getInferredContentProjectContentRoot(contentProject),
+                CoreMatchers.<IContainer> 
equalTo(contentProject.getFolder("src/main/content")));
+
+    }
+
+    @Test
+    public void inferContentProjectContentMissingFilter() throws 
CoreException, InterruptedException {
+
+        // create faceted project
+        IProject contentProject = projectRule.getProject();
+
+        ProjectAdapter project = new ProjectAdapter(contentProject);
+        project.addNatures("org.eclipse.wst.common.project.facet.core.nature");
+
+        // install content facet
+        project.installFacet("sling.content", "1.0");
+
+        // create files
+        
project.createOrUpdateFile(Path.fromPortableString("src/main/content/jcr_root/test/hello.txt"),
+                new ByteArrayInputStream("goodbye, world".getBytes()));
+
+        
assertThat(ProjectHelper.getInferredContentProjectContentRoot(contentProject), 
CoreMatchers.nullValue());
+    }
+
+    @Test
+    public void inferContentProjectContentMissingJcrRoot() throws 
CoreException, InterruptedException {
+
+        // create faceted project
+        IProject contentProject = projectRule.getProject();
+
+        ProjectAdapter project = new ProjectAdapter(contentProject);
+        project.addNatures("org.eclipse.wst.common.project.facet.core.nature");
+
+        // install content facet
+        project.installFacet("sling.content", "1.0");
+
+        // create files
+        
project.createOrUpdateFile(Path.fromPortableString("src/main/content/META-INF/vault/filter.xml"),
+                new ByteArrayInputStream("<workspaceFilter 
version=\"1.0\"/>".getBytes()));
+
+        
assertThat(ProjectHelper.getInferredContentProjectContentRoot(contentProject), 
CoreMatchers.nullValue());
+    }
+
+    @Test
+    public void projectWithInstalledFacetIsCandidate() throws CoreException, 
InterruptedException {
+
+        // create faceted project
+        IProject contentProject = projectRule.getProject();
+
+        ProjectAdapter project = new ProjectAdapter(contentProject);
+        project.addNatures("org.eclipse.wst.common.project.facet.core.nature");
+
+        // create files
+        
project.createOrUpdateFile(Path.fromPortableString("src/main/content/jcr_root/test/hello.txt"),
+                new ByteArrayInputStream("goodbye, world".getBytes()));
+
+        
project.createOrUpdateFile(Path.fromPortableString("src/main/content/META-INF/vault/filter.xml"),
+                new ByteArrayInputStream("<workspaceFilter 
version=\"1.0\"/>".getBytes()));
+
+        assertThat(ProjectHelper.isPotentialContentProject(contentProject), 
CoreMatchers.equalTo(true));
+    }
+
+    @Test
+    public void projectWithoutInstalledFacetIsNotCandidate() throws 
CoreException, InterruptedException {
+
+        // create faceted project
+        IProject contentProject = projectRule.getProject();
+
+        ProjectAdapter project = new ProjectAdapter(contentProject);
+        project.addNatures("org.eclipse.wst.common.project.facet.core.nature");
+
+        // install content facet
+        project.installFacet("sling.content", "1.0");
+
+        // create files
+        
project.createOrUpdateFile(Path.fromPortableString("src/main/content/jcr_root/test/hello.txt"),
+                new ByteArrayInputStream("goodbye, world".getBytes()));
+
+        
project.createOrUpdateFile(Path.fromPortableString("src/main/content/META-INF/vault/filter.xml"),
+                new ByteArrayInputStream("<workspaceFilter 
version=\"1.0\"/>".getBytes()));
+
+        assertThat(ProjectHelper.isPotentialContentProject(contentProject), 
CoreMatchers.equalTo(false));
+    }
+}

Propchange: 
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ProjectHelperTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ProjectHelperTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: 
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/ConvertToContentPackageAction.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/ConvertToContentPackageAction.java?rev=1596528&r1=1596527&r2=1596528&view=diff
==============================================================================
--- 
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/ConvertToContentPackageAction.java
 (original)
+++ 
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/ConvertToContentPackageAction.java
 Wed May 21 10:16:54 2014
@@ -21,6 +21,7 @@ import java.util.Iterator;
 
 import org.apache.sling.ide.eclipse.core.ConfigurationHelper;
 import org.apache.sling.ide.eclipse.core.internal.ProjectHelper;
+import org.apache.sling.ide.eclipse.ui.internal.Activator;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
@@ -60,10 +61,11 @@ public class ConvertToContentPackageActi
                if (fSelection instanceof IStructuredSelection) {
                        final IProject project = (IProject) 
((IStructuredSelection) fSelection).getFirstElement();
 
-                       String jcrRootLocation = "src/main/content/jcr_root";
+            String jcrRootLocation = 
ProjectHelper.getInferredContentProjectContentRoot(project)
+                    
.getProjectRelativePath().append("jcr_root").toPortableString();
                        final InputDialog id = new 
InputDialog(getDisplay().getActiveShell(), "Convert to Sling Content-Package 
Project", 
-                                       "Confirm jcr_root location of 
"+project.getName()+":", jcrRootLocation, new IInputValidator() {
-                                               
+                           "Confirm content sync root location of " + 
project.getName() + ":", jcrRootLocation,
+                           new IInputValidator() {
                                                @Override
                                                public String isValid(String 
newText) {
                                                        if (newText!=null && 
newText.trim().length()>0) {
@@ -74,7 +76,7 @@ public class ConvertToContentPackageActi
                                                                        return 
"Directory not found: "+newText;
                                                                }
                                                        } else {
-                                                               return "Please 
specify location of jcr_root";
+                                return "Please specify location of the content 
sync root";
                                                        }
                                                }
                                        });
@@ -87,7 +89,7 @@ public class ConvertToContentPackageActi
                                                try {
                                                        
ConfigurationHelper.convertToContentPackageProject(project, monitor, 
id.getValue());
                                                } catch (CoreException e) {
-                                                       e.printStackTrace();
+                            
Activator.getDefault().getPluginLogger().warn("Could not convert project", e);
                                                        
MessageDialog.openError(getDisplay().getActiveShell(), "Could not convert 
project",
                                                                        
e.getMessage());
                                                }
@@ -96,7 +98,7 @@ public class ConvertToContentPackageActi
                                try {
                                        
PlatformUI.getWorkbench().getProgressService().busyCursorWhile(r);
                                } catch (Exception e) {
-                                       e.printStackTrace();
+                    Activator.getDefault().getPluginLogger().warn("Could not 
convert project", e);
                                        
MessageDialog.openError(getDisplay().getActiveShell(), "Could not convert 
project",
                                                        e.getMessage());
                                }
@@ -114,7 +116,7 @@ public class ConvertToContentPackageActi
                fSelection = selection;
                if (selection instanceof IStructuredSelection) {
                        final IStructuredSelection iss = (IStructuredSelection) 
selection;
-                       Iterator<Object> it = iss.iterator();
+            Iterator<?> it = iss.iterator();
                        if (!it.hasNext()) {
                                action.setEnabled(false);
                                return;


Reply via email to