Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Red Hat, Inc. and/or its affiliates.
   *
   * Licensed under the Eclipse Public License version 1.0, available at
   * http://www.eclipse.org/legal/epl-v10.html
   */
  package org.jboss.forge.maven.facets;
  
 import java.io.File;
 import java.util.List;
 
 
 import  org.jboss.forge.parser.java.JavaEnum;
 import  org.jboss.forge.parser.java.JavaSource;
 import  org.jboss.forge.parser.java.util.Strings;

Author(s):
Lincoln Baxter, III
 
 @Alias("forge.maven.JavaSourceFacet")
 public class MavenJavaSourceFacet extends BaseFacet implements JavaSourceFacetFacet
 {
    @Override
    {
       List<DirectoryResourceresult = new ArrayList<DirectoryResource>();
       result.add(getSourceFolder());
       result.add(getTestSourceFolder());
       return result;
    }
 
    @Override
    public String calculateName(final JavaResource resource)
    {
       String fullPath = Packages.fromFileSyntax(resource.getFullyQualifiedName());
       String pkg = calculatePackage(resource);
       String name = fullPath.substring(fullPath.lastIndexOf(pkg) + pkg.length() + 1);
       name = name.substring(0, name.lastIndexOf(".java"));
       return name;
    }
 
    @Override
    public String calculatePackage(final JavaResource resource)
    {
       List<DirectoryResourcefolders = getSourceFolders();
       String pkg = null;
       for (DirectoryResource folder : folders)
       {
          String sourcePrefix = folder.getFullyQualifiedName();
          pkg = resource.getParent().getFullyQualifiedName();
          if (pkg.startsWith(sourcePrefix))
          {
             pkg = pkg.substring(sourcePrefix.length() + 1);
             break;
          }
       }
       pkg = Packages.fromFileSyntax(pkg);
 
       return pkg;
    }
 
    @Override
    public String getBasePackage()
    {
    }
 
    @Override
    {
       return getSourceFolder().getChildDirectory(Packages.toFileSyntax(getBasePackage()));
    }
 
    @Override
   {
      MavenCoreFacet mavenFacet = .getFacet(MavenCoreFacet.class);
      Build build = mavenFacet.getPOM().getBuild();
      String srcFolderName;
      if (build != null && build.getSourceDirectory() != null)
      {
         srcFolderName = build.getSourceDirectory();
      }
      else
      {
         srcFolderName = "src" + . + "main" + . + "java";
      }
      DirectoryResource projectRoot = .getProjectRoot();
      return projectRoot.getChildDirectory(srcFolderName);
   }
   {
      MavenCoreFacet mavenFacet = .getFacet(MavenCoreFacet.class);
      Build build = mavenFacet.getPOM().getBuild();
      String srcFolderName;
      if (build != null && build.getTestSourceDirectory() != null)
      {
         srcFolderName = build.getTestSourceDirectory();
      }
      else
      {
         srcFolderName = "src" + . + "test" + . + "java";
      }
      DirectoryResource projectRoot = .getProjectRoot();
      return projectRoot.getChildDirectory(srcFolderName);
   }
   public boolean isInstalled()
   {
      return getSourceFolder().exists();
   }
   public boolean install()
   {
      if (!this.isInstalled())
      {
         for (DirectoryResource folder : this.getSourceFolders())
         {
            folder.mkdirs();
         }
         // FIXME WOW this needs to be simplified somehow...
         MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
         Model pom = maven.getPOM();
         Build build = pom.getBuild();
         if (build == null)
         {
            build = new Build();
         }
         List<Pluginplugins = build.getPlugins();
         Plugin javaSourcePlugin = null;
         for (Plugin plugin : plugins)
         {
            if ("org.apache.maven.plugins".equals(plugin.getGroupId())
                     && "maven-compiler-plugin".equals(plugin.getArtifactId()))
            {
               javaSourcePlugin = plugin;
            }
         }
         if (javaSourcePlugin == null)
         {
            javaSourcePlugin = new Plugin();
            // FIXME this should find the most recent version using DependencyResolver
            javaSourcePlugin.setGroupId("org.apache.maven.plugins");
            javaSourcePlugin.setArtifactId("maven-compiler-plugin");
            javaSourcePlugin.setVersion("2.3.2");
            try
            {
               Xpp3Dom dom = Xpp3DomBuilder.build(
                        new ByteArrayInputStream(
                                 ("<configuration>" +
                                          "<source>1.6</source>" +
                                          "<target>1.6</target>" +
                                          "<encoding>UTF-8</encoding>" +
                                          "</configuration>").getBytes()),
                        "UTF-8");
               javaSourcePlugin.setConfiguration(dom);
            }
            catch (Exception e)
            {
               throw new ProjectModelException(e);
            }
         }
         build.addPlugin(javaSourcePlugin);
         pom.setBuild(build);
         maven.setPOM(pom);
      }
      return true;
   }
   public JavaResource getJavaResource(final JavaSource<?> javaClassthrows FileNotFoundException
   {
      String pkg = Strings.isNullOrEmpty(javaClass.getPackage()) ? "" : javaClass.getPackage() + ".";
      return getJavaResource(pkg + javaClass.getName());
   }
   public JavaResource getEnumTypeResource(final JavaEnum javaEnumthrows FileNotFoundException
   {
      String pkg = Strings.isNullOrEmpty(javaEnum.getPackage()) ? "" : javaEnum.getPackage() + ".";
      return getEnumTypeResource(pkg + javaEnum.getName());
   }
   public JavaResource getTestJavaResource(final JavaSource<?> javaClassthrows FileNotFoundException
   {
      String pkg = Strings.isNullOrEmpty(javaClass.getPackage()) ? "" : javaClass.getPackage() + ".";
      return getTestJavaResource(pkg + javaClass.getName());
   }
   public JavaResource getJavaResource(final String relativePaththrows FileNotFoundException
   {
      return getJavaResource(getSourceFolder(), relativePath);
   }

   

Deprecated:
Use the getJavaResource
   public JavaResource getEnumTypeResource(final String relativePaththrows FileNotFoundException
   {
      return getJavaResource(getSourceFolder(), relativePath);
   }
   public JavaResource getTestJavaResource(final String relativePaththrows FileNotFoundException
   {
      return getJavaResource(getTestSourceFolder(), relativePath);
   }
   private JavaResource getJavaResource(final DirectoryResource sourceDirfinal String relativePath)
   {
      String path = relativePath.trim().endsWith(".java")
               ? relativePath.substring(0, relativePath.lastIndexOf(".java")) : relativePath;
      path = Packages.toFileSyntax(path) + ".java";
      JavaResource target = sourceDir.getChildOfType(JavaResource.classpath);
      return target;
   }
   public JavaResource saveJavaSource(final JavaSource<?> sourcethrows FileNotFoundException
   {
      return getJavaResource(source.getQualifiedName()).setContents(source);
   }

   

Deprecated:
use MavenJavaSourceFacet.saveJavaSource(JavaSource)
   public JavaResource saveEnumTypeSource(final JavaEnum sourcethrows FileNotFoundException
   {
      return getEnumTypeResource(source.getQualifiedName()).setContents(source);
   }
   public JavaResource saveTestJavaSource(final JavaSource<?> sourcethrows FileNotFoundException
   {
      return getTestJavaResource(source.getQualifiedName()).setContents(source);
   }
   public void visitJavaSources(final JavaResourceVisitor visitor)
   {
      visitSources(getSourceFolder(), visitor);
   }
   public void visitJavaTestSources(final JavaResourceVisitor visitor)
   {
      visitSources(getTestSourceFolder(), visitor);
   }
   private void visitSources(final Resource<?> searchFolderfinal JavaResourceVisitor visitor)
   {
      if (searchFolder instanceof DirectoryResource)
      {
         searchFolder.listResources(new ResourceFilter()
         {
            @Override
            public boolean accept(Resource<?> resource)
            {
               if (resource instanceof DirectoryResource)
               {
                  visitSources(resourcevisitor);
               }
               if (resource instanceof JavaResource)
               {
                  visitor.visit((JavaResourceresource);
               }
               return false;
            }
         });
      }
   }
New to GrepCode? Check out our FAQ X