KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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  * wharley@bea.com - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.apt.core.internal.env;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.io.OutputStreamWriter JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.jdt.apt.core.env.Phase;
25 import org.eclipse.jdt.apt.core.internal.AptPlugin;
26 import org.eclipse.jdt.apt.core.internal.generatedfile.GeneratedSourceFolderManager;
27
28 import com.sun.mirror.apt.Filer;
29
30 /**
31  * @author wharley
32  *
33  */

34 public class BuildFilerImpl extends FilerImpl {
35
36     private boolean _generatedClassFiles = false;
37     private final BuildEnv _env;
38
39     public BuildFilerImpl(BuildEnv env) {
40         _env = env;
41     }
42
43     /**
44      * Creates a new class file, and returns a stream for writing to it. The
45      * file's name and path (relative to the root of all newly created class
46      * files) is based on the name of the type being written.
47      *
48      * @param typeName - canonical (fully qualified) name of the type being written
49      * @return -a stream for writing to the new file
50      */

51     public OutputStream JavaDoc createClassFile(String JavaDoc typeName) throws IOException JavaDoc
52     {
53         if (typeName == null)
54             throw new IllegalArgumentException JavaDoc("Type name cannot be null"); //$NON-NLS-1$
55
if ("".equals(typeName)) //$NON-NLS-1$
56
throw new IllegalArgumentException JavaDoc("Type name cannot be empty"); //$NON-NLS-1$
57

58         _env.checkValid();
59         try {
60             _env.validateTypeName(typeName);
61         } catch (CoreException e) {
62             IOException JavaDoc ioe = new IOException JavaDoc();
63             ioe.initCause(e);
64             throw ioe;
65         }
66         _generatedClassFiles = true;
67         
68         // We do not want to write to disk during reconcile
69
if (_env.getPhase() == Phase.RECONCILE) {
70             return new NoOpOutputStream();
71         }
72         
73         GeneratedSourceFolderManager gsfm = _env.getAptProject().getGeneratedSourceFolderManager();
74         IPath path;
75         try
76         {
77              path = gsfm.getBinaryOutputLocation();
78         }
79         catch ( Exception JavaDoc e )
80         {
81             // TODO - stop throwing this exception
82
AptPlugin.log(e, "Failure getting the output file"); //$NON-NLS-1$
83
throw new IOException JavaDoc();
84         }
85         
86         path = path.append(typeName.replace('.', File.separatorChar) + ".class"); //$NON-NLS-1$
87

88         IFile file = getEnv().getProject().getFile(path);
89         return new BinaryFileOutputStream(file, _env);
90     }
91     
92     public boolean hasGeneratedClassFile(){ return _generatedClassFiles; }
93
94     /**
95      * Creates a new text file, and returns a writer for it. The file is
96      * located along with either the newly created source or newly created
97      * binary files. It may be named relative to some package (as are source
98      * and binary files), and from there by an arbitrary pathname. In a loose
99      * sense, the pathname of the new file will be the concatenation of loc,
100      * pkg, and relPath.
101      *
102      * A charset for encoding the file may be provided. If none is given,
103      * the charset used to encode source files (see createSourceFile(String)) will be used.
104      *
105      * @param loc - location of the new file
106      * @param pkg - package relative to which the file should be named, or the empty string if none
107      * @param relPath - final pathname components of the file
108      * @param charsetName - the name of the charset to use, or null if none is being explicitly specified
109      * @return - a writer for the new file
110      */

111     public PrintWriter JavaDoc createTextFile(Filer.Location loc, String JavaDoc pkg, File JavaDoc relPath, String JavaDoc charsetName)
112         throws IOException JavaDoc
113     {
114         if (relPath == null)
115             throw new IllegalArgumentException JavaDoc("Path cannot be null"); //$NON-NLS-1$
116
if ("".equals(relPath.getPath())) //$NON-NLS-1$
117
throw new IllegalArgumentException JavaDoc("Path cannot be empty"); //$NON-NLS-1$
118

119         _env.checkValid();
120         
121         // If we're reconciling, we do not want to actually create the text file
122
if (_env.getPhase() == Phase.RECONCILE) {
123             return new NoOpPrintWriter();
124         }
125         
126         
127         IPath path = getOutputFileForLocation( loc, pkg, relPath );
128         IFile file = _env.getProject().getFile(path);
129         validateFile(file);
130         OutputStream JavaDoc binaryOut = new EncodedFileOutputStream(file, _env, charsetName);
131  
132         if (charsetName == null) {
133             return new PrintWriter JavaDoc(binaryOut);
134         }
135         else {
136             OutputStreamWriter JavaDoc outWriter = new OutputStreamWriter JavaDoc(binaryOut, charsetName);
137             return new PrintWriter JavaDoc(outWriter);
138         }
139     }
140
141     /**
142      * Creates a new binary file, and returns a stream for writing to it. The
143      * file is located along with either the newly created source or newly
144      * created binary files. It may be named relative to some package (as
145      * are source and binary files), and from there by an arbitrary pathname.
146      * In a loose sense, the pathname of the new file will be the concatenation
147      * of loc, pkg, and relPath.
148      *
149      * @param loc - location of the new file
150      * @param pkg - package relative to which the file should be named, or the empty string if none
151      * @param relPath - final pathname components of the file
152      * @return a stream for writing to the new file
153      */

154     public OutputStream JavaDoc createBinaryFile(Filer.Location loc, String JavaDoc pkg, File JavaDoc relPath)
155         throws IOException JavaDoc
156     {
157         if (relPath == null)
158             throw new IllegalArgumentException JavaDoc("Path cannot be null"); //$NON-NLS-1$
159
if ("".equals(relPath.getPath())) //$NON-NLS-1$
160
throw new IllegalArgumentException JavaDoc("Path cannot be empty"); //$NON-NLS-1$
161

162         _env.checkValid();
163         
164         // We do not want to write to disk during reconcile
165
if (_env.getPhase() == Phase.RECONCILE) {
166             return new NoOpOutputStream();
167         }
168         
169         IPath path = getOutputFileForLocation( loc, pkg, relPath );
170         IFile file = _env.getProject().getFile(path);
171         validateFile(file);
172         return new BinaryFileOutputStream(file, _env);
173     }
174
175     @Override JavaDoc
176     protected AbstractCompilationEnv getEnv() {
177         return _env;
178     }
179     
180     private void validateFile(IFile file) throws IOException JavaDoc
181     {
182         IStatus status = file.getWorkspace().validatePath(file.getFullPath().toOSString(), IResource.FILE);
183         if (!status.isOK()) {
184             CoreException ce = new CoreException(status);
185             IOException JavaDoc ioe = new IOException JavaDoc("Invalid file name"); //$NON-NLS-1$
186
ioe.initCause(ce);
187             throw ioe;
188         }
189     }
190
191 }
192
Popular Tags