flex2.compiler.SourceList Maven / Gradle / Ivy
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package flex2.compiler;
import flex2.compiler.io.FileUtil;
import flex2.compiler.io.LocalFile;
import flex2.compiler.io.VirtualFile;
import flex2.compiler.util.CompilerMessage;
import flex2.compiler.util.ThreadLocalToolkit;
import java.io.File;
import java.util.*;
/**
* A SourceList represents a list of files, following the single
* public definition rule, and an associated path where dependencies
* can be found. When compiling via mxmlc, the files specified on the
* command line are put into the SourceList
. When
* compiling via Flash Builder, the root or application is included in
* the SourceList
.
*
* @author Clement Wong
*/
public final class SourceList
{
public SourceList(List files, VirtualFile[] classPath, VirtualFile appPath, String[] mimeTypes)
throws CompilerException
{
this(files, classPath, appPath, mimeTypes, true);
}
public SourceList(List files, VirtualFile[] classPath, VirtualFile appPath, String[] mimeTypes, boolean lastIsRoot)
throws CompilerException
{
VirtualFile[] vfiles = new VirtualFile[files.size()];
files.toArray(vfiles);
init(vfiles, classPath, appPath, mimeTypes, lastIsRoot);
}
private void init(VirtualFile[] files, VirtualFile[] classPath, VirtualFile appPath, String[] mimeTypes, boolean lastIsRoot)
throws CompilerException
{
this.mimeTypes = mimeTypes;
sources = new LinkedHashMap(files.length);
directories = new ArrayList(classPath == null ? 0 : classPath.length);
SourcePath.addApplicationParentToSourcePath(appPath, classPath, directories);
SourcePath.addPathElements(classPath, directories, true, null);
for (int i = 0, length = files.length; i < length; i++)
{
// No need to check to see if the appPath is supported again.
if ((appPath != null && files[i].getName().equals(appPath.getName())) || isSupported(files[i]))
{
String name = files[i].getName();
VirtualFile pathRoot = calculatePathRoot(files[i]);
if (pathRoot != null)
{
String relativePath = calculateRelativePath(name);
String namespaceURI = relativePath.replace('/', '.');
String localPart = calculateLocalPart(name);
Source source = new Source(files[i], pathRoot, relativePath, localPart, this, false, (i == length - 1) && lastIsRoot);
String className = CompilerAPI.constructClassName(namespaceURI, localPart);
sources.put(className, source);
}
else
{
// Files in SourceList must be in --source-path.
// output an error here...
FileNotInSourcePath ex = new FileNotInSourcePath(name);
ThreadLocalToolkit.log(ex);
throw ex;
}
}
else
{
UnsupportedFileType ex = new UnsupportedFileType(files[i].getName());
ThreadLocalToolkit.log(ex);
throw ex;
}
}
}
private VirtualFile calculatePathRoot(VirtualFile f)
{
return calculatePathRoot(f, directories);
}
static VirtualFile calculatePathRoot(VirtualFile f, List directories)
{
String name = f.getName();
if(directories != null) {
for (File directory : directories) {
String dir = directory.getAbsolutePath();
if (name.startsWith(dir)) {
return new LocalFile(FileUtil.openFile(dir));
}
}
}
// return new LocalFile(FileUtil.openFile(f.getParent()));
return null;
}
private String calculateRelativePath(String name)
{
if(directories != null) {
// C: name is canonical.
for (File directory : directories) {
// Tack on the separatorChar to handle directories, which
// are the same as other, just longer. Like "a" and "a1".
// See SDK-24084.
String dir = directory.getAbsolutePath() + File.separatorChar;
if (name.startsWith(dir)) {
name = name.substring(dir.length());
int index = name.lastIndexOf(File.separatorChar);
if (index != -1) {
return name.substring(0, index).replace(File.separatorChar, '/');
} else {
return "";
}
}
}
}
return "";
}
private String calculateLocalPart(String name)
{
String leafName = name.substring(name.lastIndexOf(File.separatorChar) + 1);
String localPart = leafName.substring(0, leafName.lastIndexOf('.'));
return localPart;
}
private Map sources;
private List directories;
private String[] mimeTypes;
public List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy