KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > env > FilerImpl


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * mkaufman@bea.com - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.apt.core.internal.env;
13
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.io.StringWriter JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.jdt.apt.core.internal.AptPlugin;
22 import org.eclipse.jdt.apt.core.internal.generatedfile.GeneratedSourceFolderManager;
23 import org.eclipse.jdt.apt.core.internal.util.FileSystemUtil;
24 import org.eclipse.jdt.core.JavaModelException;
25
26 import com.sun.mirror.apt.Filer;
27
28
29 public abstract class FilerImpl implements Filer {
30
31     abstract protected AbstractCompilationEnv getEnv();
32     
33     /**
34      * Creates a new source file and returns a writer for it. The file's name
35      * and path (relative to the root of all newly created source files) is
36      * based on the type to be declared in that file. If more than one type is
37      * being declared, the name of the principal top-level type (the public
38      * one, for example) should be used.
39      *
40      * Character set used is the default character set for the platform
41      *
42      * @param typeName - canonical (fully qualified) name of the principal type being declared in this file
43      */

44     public PrintWriter JavaDoc createSourceFile(String JavaDoc typeName) throws IOException JavaDoc
45     {
46         if (typeName == null)
47             throw new IllegalArgumentException JavaDoc("Type name cannot be null"); //$NON-NLS-1$
48
if ("".equals(typeName)) //$NON-NLS-1$
49
throw new IllegalArgumentException JavaDoc("Type name cannot be empty"); //$NON-NLS-1$
50

51         getEnv().checkValid();
52         
53         PrintWriter JavaDoc pw;
54         try {
55             pw = new JavaSourceFilePrintWriter( typeName, new StringWriter JavaDoc(), getEnv() );
56         } catch (CoreException e) {
57             IOException JavaDoc ioe = new IOException JavaDoc();
58             ioe.initCause(e);
59             throw ioe;
60         }
61         return pw;
62     }
63
64
65     /**
66      * Return a project-relative path
67      */

68     protected IPath getOutputFileForLocation( Filer.Location loc, String JavaDoc pkg, File JavaDoc relPath )
69         throws IOException JavaDoc
70     {
71         GeneratedSourceFolderManager gsfm = getEnv().getAptProject().getGeneratedSourceFolderManager();
72         IPath path = null;
73         if ( loc == Filer.Location.CLASS_TREE )
74         {
75             try
76             {
77                 path = gsfm.getBinaryOutputLocation();
78             }
79             catch ( JavaModelException e )
80             {
81                 AptPlugin.log(e, "Failure getting the output file"); //$NON-NLS-1$
82
throw new IOException JavaDoc();
83             }
84         }
85         else if ( loc == Filer.Location.SOURCE_TREE ) {
86             path = gsfm.getFolder().getProjectRelativePath();
87         }
88         
89         if( pkg != null )
90             path = path.append(pkg.replace('.', File.separatorChar) );
91
92         path = path.append(relPath.getPath() );
93         
94         // Create the parent folder (need an absolute path temporarily)
95
IPath absolutePath = getEnv().getProject().getLocation().append(path);
96         File JavaDoc parentFile = absolutePath.toFile().getParentFile();
97         FileSystemUtil.mkdirs( parentFile );
98         
99         return path;
100     }
101     
102     
103 }
104
Popular Tags