📄 preprocessorbuilder.java
字号:
public PreprocessorBuilder() {
super();
buildLoggingConfig = BuildLoggingConfiguration.instance;
// During the first actual build, we will check to see
// if the hook has been installed. If not, the build
// will continue, but the user will be warned via the error
// log that it won't actually work.
checkedForHook = false;
}
/**
* @see org.eclipse.core.resources.IncrementalProjectBuilder#build(int, java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
*/
protected IProject[] build(int kind, Map args, IProgressMonitor monitor)
throws CoreException
{
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("> PreprocessorBuilder.build");
}
if (!checkedForHook) {
if (!SourceMapperAccess.isHookCodeInstalled()) {
EclipseMECorePlugin.log(
IStatus.WARNING,
"Preprocessor invoked, but hook is not installed. Consult the installation instructions for EclipseME.");
}
checkedForHook = true;
}
doPreprocessing(kind, args, monitor);
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("< PreprocessorBuilder.build");
}
return null;
}
/**
* @see org.eclipse.core.resources.IncrementalProjectBuilder#clean(org.eclipse.core.runtime.IProgressMonitor)
*/
protected void clean(IProgressMonitor monitor)
throws CoreException
{
IProject srcProject = getProject();
IFolder folder = srcProject.getFolder(PROCESSED_PATH);
Utils.clearContainer(folder, monitor);
}
/**
* Create the parent folders of this file.
*
* @param outputFile
* @param monitor
* @throws CoreException
*/
private void createParentFolders(IResource resource, IProgressMonitor monitor)
throws CoreException
{
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("> PreprocessorBuilder.createParentFolders; resource = " + resource);
}
IContainer parent = resource.getParent();
if (parent.getType() == IResource.FOLDER) {
createParentFolders(parent, monitor);
IFolder folder = (IFolder) parent;
if (!folder.exists()) {
folder.create(true, true, monitor);
folder.setDerived(true);
}
}
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("< PreprocessorBuilder.createParentFolders");
}
}
/**
* Do the preprocessing build.
*
* @param kind
* @param args
* @param monitor
* @throws CoreException
*/
private void doPreprocessing(int kind, Map args, IProgressMonitor monitor)
throws CoreException
{
// Collect the java files that were updated
IProject project = getProject();
SymbolDefinitionSet symbols = getProjectSymbolDefinitions(project);
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln(
"> PreprocessorBuilder.doPreprocessing; project = " +
project +
"; symbols = " +
((symbols == null) ? "none" : symbols.getName()));
}
// Wrap up the symbol definition set in another wrapper set. Changes
// made to the set while processing the source will not propagate
// to the base definition set
symbols = new SymbolDefinitionSet(symbols);
// Traverse the changes in the resource delta, processing the
// resources along the way.
IResourceDelta delta = getDelta(project);
if (delta == null) {
ResourceVisitor visitor = new ResourceVisitor(symbols, monitor);
project.accept(visitor);
} else {
ResourceDeltaVisitor visitor = new ResourceDeltaVisitor(symbols, monitor);
delta.accept(visitor);
}
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("< PreprocessorBuilder.doPreprocessing");
}
}
/**
* Return the symbols defined for the specified project.
*
* @param project
* @return
*/
private SymbolDefinitionSet getProjectSymbolDefinitions(IProject project) {
SymbolDefinitionSet symbols = null;
IMidletSuiteProject midletSuite = MidletSuiteFactory.getMidletSuiteProject(JavaCore.create(project));
try {
symbols = midletSuite.getEnabledSymbolDefinitionSet();
} catch (CoreException e) {
EclipseMECorePlugin.log(IStatus.ERROR, e);
} catch (PersistenceException e) {
EclipseMECorePlugin.log(IStatus.ERROR, e);
}
if (symbols == null) {
symbols = new SymbolDefinitionSet("__default" + System.currentTimeMillis());
}
return symbols;
}
/**
* Return an UTF-8 stream on the source document bytes.
*
* @param sourceDocument
* @return
*/
private ByteArrayInputStream getSourceStream(IDocument sourceDocument) {
byte[] documentBytes = null;
try {
documentBytes = sourceDocument.get().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// Should not happen
}
return new ByteArrayInputStream(documentBytes);
}
/**
* Return a boolean indicating whether or not the specified resource
* is on the build path.
*
* @param resource
* @param monitor
* @return
*/
private boolean isOnBuildPath(IResource resource, IProgressMonitor monitor) {
boolean onBuildPath = false;
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("> PreprocessorBuilder.isOnBuildPath; resource = " + resource);
}
IJavaElement javaElement = JavaCore.create((IFile) resource);
if (javaElement != null) {
IPackageFragmentRoot root =
(IPackageFragmentRoot) javaElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
onBuildPath = root.exists();
}
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("< PreprocessorBuilder.isOnBuildPath; returning = " + onBuildPath);
}
return onBuildPath;
}
/**
* Preprocess the specified java resource.
*
* @param resource
* @param symbols
* @param monitor
* @throws CoreException
*/
protected void preprocess(IResource resource, SymbolDefinitionSet symbols, IProgressMonitor monitor)
throws CoreException
{
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("> PreprocessorBuilder.preprocess; resource = " + resource);
}
Preprocessor preprocessor = new Preprocessor();
IDocument document = preprocessor.preprocess(resource, symbols, monitor);
writeProcessedResults(resource, symbols, document, monitor);
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("< PreprocessorBuilder.preprocess");
}
}
/**
* Set the specified file to be read-only or not based on the
* specified flag.
*
* @param outputFile
* @param readOnly
* @throws CoreException
*/
private void setReadOnly(IFile outputFile, boolean readOnly)
throws CoreException
{
ResourceAttributes attributes = new ResourceAttributes();
attributes.setReadOnly(readOnly);
outputFile.setResourceAttributes(attributes);
}
/**
* Return a boolean indicating whether the specified resource
* should be processed.
*
* @param resource
* @param monitor
* @return
* @throws CoreException
*/
private boolean shouldBeProcessed(IResource resource, IProgressMonitor monitor)
throws CoreException
{
return
(resource.getType() == IResource.FILE) &&
(resource.getName().endsWith(".java")) &&
!isPreprocessed(resource) &&
isOnBuildPath(resource, monitor);
}
/**
* Write out the results of the processing.
*
* @param resource
* @param symbols
* @param sourceDocument
* @param monitor
* @throws CoreException
*/
private void writeProcessedResults(
final IResource resource,
SymbolDefinitionSet symbols,
IDocument sourceDocument,
IProgressMonitor monitor)
throws CoreException
{
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("> PreprocessorBuilder.writeProcessedResults");
}
ByteArrayInputStream is = getSourceStream(sourceDocument);
final IFile outputFile = getOutputFile(resource);
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("- PreprocessorBuilder.writeProcessedResults; outputFile = " + outputFile);
}
if (outputFile.exists()) {
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("- PreprocessorBuilder.writeProcessedResults; outputFile exists, setting new contents");
}
setReadOnly(outputFile, false);
outputFile.setContents(is, true, false, monitor);
} else {
IContainer container = outputFile.getParent();
if (!container.exists()) {
createParentFolders(outputFile, monitor);
}
outputFile.create(is, false, monitor);
}
outputFile.setCharset("UTF-8", monitor);
outputFile.setDerived(true);
setReadOnly(outputFile, true);
// It seems this should not be necessary, but let's see if it
// helps on some problems we are seeing...
// outputFile.getProject().build(AUTO_BUILD, monitor);
if (buildLoggingConfig.isPreprocessorTraceEnabled()) {
BuildConsoleProxy.instance.traceln("< PreprocessorBuilder.writeProcessedResults");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -