KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > CreateCompilationUnitOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16
17 import org.eclipse.core.resources.*;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.NullProgressMonitor;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.jobs.ISchedulingRule;
23 import org.eclipse.jdt.core.IBuffer;
24 import org.eclipse.jdt.core.ICompilationUnit;
25 import org.eclipse.jdt.core.IJavaElement;
26 import org.eclipse.jdt.core.IJavaElementDelta;
27 import org.eclipse.jdt.core.IJavaModelStatus;
28 import org.eclipse.jdt.core.IJavaModelStatusConstants;
29 import org.eclipse.jdt.core.IJavaProject;
30 import org.eclipse.jdt.core.IPackageFragment;
31 import org.eclipse.jdt.core.JavaConventions;
32 import org.eclipse.jdt.core.JavaCore;
33 import org.eclipse.jdt.core.JavaModelException;
34 import org.eclipse.jdt.internal.core.util.Messages;
35 import org.eclipse.jdt.internal.core.util.Util;
36
37 /**
38  * <p>This operation creates a compilation unit (CU).
39  * If the CU doesn't exist yet, a new compilation unit will be created with the content provided.
40  * Otherwise the operation will override the contents of an existing CU with the new content.
41  *
42  * <p>Note: It is possible to create a CU automatically when creating a
43  * class or interface. Thus, the preferred method of creating a CU is
44  * to perform a create type operation rather than
45  * first creating a CU and secondly creating a type inside the CU.
46  *
47  * <p>Required Attributes:<ul>
48  * <li>The package fragment in which to create the compilation unit.
49  * <li>The name of the compilation unit.
50  * Do not include the <code>".java"</code> suffix (ex. <code>"Object"</code> -
51  * the <code>".java"</code> will be added for the name of the compilation unit.)
52  * <li>
53   * </ul>
54  */

55 public class CreateCompilationUnitOperation extends JavaModelOperation {
56
57     /**
58      * The name of the compilation unit being created.
59      */

60     protected String JavaDoc fName;
61     /**
62      * The source code to use when creating the element.
63      */

64     protected String JavaDoc fSource= null;
65 /**
66  * When executed, this operation will create a compilation unit with the given name.
67  * The name should have the ".java" suffix.
68  */

69 public CreateCompilationUnitOperation(IPackageFragment parentElement, String JavaDoc name, String JavaDoc source, boolean force) {
70     super(null, new IJavaElement[] {parentElement}, force);
71     fName = name;
72     fSource = source;
73 }
74 /**
75  * Creates a compilation unit.
76  *
77  * @exception JavaModelException if unable to create the compilation unit.
78  */

79 protected void executeOperation() throws JavaModelException {
80     try {
81         beginTask(Messages.operation_createUnitProgress, 2);
82         JavaElementDelta delta = newJavaElementDelta();
83         ICompilationUnit unit = getCompilationUnit();
84         IPackageFragment pkg = (IPackageFragment) getParentElement();
85         IContainer folder = (IContainer) pkg.getResource();
86         worked(1);
87         IFile compilationUnitFile = folder.getFile(new Path(fName));
88         if (compilationUnitFile.exists()) {
89             // update the contents of the existing unit if fForce is true
90
if (force) {
91                 IBuffer buffer = unit.getBuffer();
92                 if (buffer == null) return;
93                 buffer.setContents(fSource);
94                 unit.save(new NullProgressMonitor(), false);
95                 resultElements = new IJavaElement[] {unit};
96                 if (!Util.isExcluded(unit)
97                         && unit.getParent().exists()) {
98                     for (int i = 0; i < resultElements.length; i++) {
99                         delta.changed(resultElements[i], IJavaElementDelta.F_CONTENT);
100                     }
101                     addDelta(delta);
102                 }
103             } else {
104                 throw new JavaModelException(new JavaModelStatus(
105                     IJavaModelStatusConstants.NAME_COLLISION,
106                     Messages.bind(Messages.status_nameCollision, compilationUnitFile.getFullPath().toString())));
107             }
108         } else {
109             try {
110                 String JavaDoc encoding = null;
111                 try {
112                     encoding = folder.getDefaultCharset(); // get folder encoding as file is not accessible
113
}
114                 catch (CoreException ce) {
115                     // use no encoding
116
}
117                 InputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(encoding == null ? fSource.getBytes() : fSource.getBytes(encoding));
118                 createFile(folder, unit.getElementName(), stream, force);
119                 resultElements = new IJavaElement[] {unit};
120                 if (!Util.isExcluded(unit)
121                         && unit.getParent().exists()) {
122                     for (int i = 0; i < resultElements.length; i++) {
123                         delta.added(resultElements[i]);
124                     }
125                     addDelta(delta);
126                 }
127             } catch (IOException JavaDoc e) {
128                 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
129             }
130         }
131         worked(1);
132     } finally {
133         done();
134     }
135 }
136 /**
137  * @see CreateElementInCUOperation#getCompilationUnit()
138  */

139 protected ICompilationUnit getCompilationUnit() {
140     return ((IPackageFragment)getParentElement()).getCompilationUnit(fName);
141 }
142 protected ISchedulingRule getSchedulingRule() {
143     IResource resource = getCompilationUnit().getResource();
144     IWorkspace workspace = resource.getWorkspace();
145     if (resource.exists()) {
146         return workspace.getRuleFactory().modifyRule(resource);
147     } else {
148         return workspace.getRuleFactory().createRule(resource);
149     }
150 }
151 /**
152  * Possible failures: <ul>
153  * <li>NO_ELEMENTS_TO_PROCESS - the package fragment supplied to the operation is
154  * <code>null</code>.
155  * <li>INVALID_NAME - the compilation unit name provided to the operation
156  * is <code>null</code> or has an invalid syntax
157  * <li>INVALID_CONTENTS - the source specified for the compiliation unit is null
158  * </ul>
159  */

160 public IJavaModelStatus verify() {
161     if (getParentElement() == null) {
162         return new JavaModelStatus(IJavaModelStatusConstants.NO_ELEMENTS_TO_PROCESS);
163     }
164     IJavaProject project = getParentElement().getJavaProject();
165     if (JavaConventions.validateCompilationUnitName(fName, project.getOption(JavaCore.COMPILER_SOURCE, true), project.getOption(JavaCore.COMPILER_COMPLIANCE, true)).getSeverity() == IStatus.ERROR) {
166         return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, fName);
167     }
168     if (fSource == null) {
169         return new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS);
170     }
171     return JavaModelStatus.VERIFIED_OK;
172 }
173 }
174
Popular Tags