View Javadoc

1   /*
2    * ASTASProject.java
3    * 
4    * Copyright (c) 2006-2007 David Holroyd
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package uk.co.badgersinfoil.metaas.impl;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStreamWriter;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Map;
32  import uk.co.badgersinfoil.metaas.ActionScriptProject;
33  import uk.co.badgersinfoil.metaas.ActionScriptFactory;
34  import uk.co.badgersinfoil.metaas.dom.ASCompilationUnit;
35  
36  // TODO: some checks that types are not created that clash with existing types,
37  // or at least filenames wont collide in the outputDir, would be good.
38  
39  public class ASTActionScriptProject implements ActionScriptProject {
40  	private List classpath = new ArrayList();
41  	private Map resourceRoots = new HashMap();
42  	private List compilationUnits = new ArrayList();
43  	private String outputLocation;
44  	private ActionScriptFactory fact;
45  
46  	public ASTActionScriptProject(ActionScriptFactory fact) {
47  		this.fact = fact;
48  	}
49  
50  	// TODO: interface for cp entries; not just strings.  Add support for
51  	//       .swc classpath entries
52  	public void addClasspathEntry(String classpathEntry) {
53  		ResourceRoot root = resourceRootFor(classpathEntry);
54  		resourceRoots.put(classpathEntry, root);
55  		classpath.add(classpathEntry);
56  	}
57  
58  	private ResourceRoot resourceRootFor(String classpathEntry) {
59  		File path = new File(classpathEntry);
60  		if (path.isDirectory()) {
61  			return new SourceFolderResourceRoot(path);
62  		}
63  		if (classpathEntry.endsWith(".swc")) {
64  			try {
65  				return new SWCResourceRoot(classpathEntry);
66  			} catch (IOException e) {
67  				throw new RuntimeException(e);
68  			}
69  		}
70  		throw new IllegalArgumentException("Unknown resorce type: "+classpathEntry);
71  	}
72  	
73  	/**
74  	 * Returns a list of ResourceRoot implementations pertaining to the
75  	 * classpath entries that have been added to this project.
76  	 */
77  	public Collection getResourceRoots() {
78  		return resourceRoots.values();
79  	}
80  
81  	public void removeClasspathEntry(String classpathEntry) {
82  		classpath.remove(classpathEntry);
83  		resourceRoots.remove(classpathEntry);
84  	}
85  
86  	public List getClasspathEntries() {
87  		return Collections.unmodifiableList(classpath);
88  	}
89  
90  	public void addCompilationUnit(ASCompilationUnit cu) {
91  		compilationUnits.add(cu);
92  	}
93  
94  	public void removeCompilationUnit(ASCompilationUnit cu) {
95  		compilationUnits.remove(cu);
96  	}
97  	public ASCompilationUnit newClass(String qualifiedClassName) {
98  		ASCompilationUnit cu = fact.newClass(qualifiedClassName);
99  		addCompilationUnit(cu);
100 		return cu;
101 	}
102 	public ASCompilationUnit newInterface(String qualifiedInterfaceName) {
103 		ASCompilationUnit cu = fact.newInterface(qualifiedInterfaceName);
104 		addCompilationUnit(cu);
105 		return cu;
106 	}
107 
108 	public List getCompilationUnits() {
109 		return Collections.unmodifiableList(compilationUnits);
110 	}
111 
112 	public String getOutputLocation() {
113 		return outputLocation;
114 	}
115 
116 	public void setOutputLocation(String outputLocation) {
117 		this.outputLocation = outputLocation;
118 	}
119 
120 	public void writeAll() throws IOException {
121 		for (Iterator i=compilationUnits.iterator(); i.hasNext(); ) {
122 			ASCompilationUnit cu = (ASCompilationUnit)i.next();
123 			write(outputLocation, cu);
124 		}
125 	}
126 
127 	public void performAutoImport() {
128 		AutoImporter autoImporter = new AutoImporter();
129 		autoImporter.performAutoImport(this);
130 	}
131 
132 	/**
133 	 * Writes the ActionScript code in the given CompilationUnit to the
134 	 * given directory, creating any subfolders for package hierarchy as
135 	 * appropriate, and deriving the filename from the name of the type
136 	 * defined by the compilation unit.
137 	 */
138 	private void write(String destinationDir, ASCompilationUnit cu) throws IOException {
139 		String filename = filenameFor(cu);
140 		File destFile = new File(destinationDir, filename);
141 		destFile.getParentFile().mkdirs();
142 		FileOutputStream os = new FileOutputStream(destFile);
143 		OutputStreamWriter out = new OutputStreamWriter(os);
144 		fact.newWriter().write(out, cu);
145 		out.close();
146 	}
147 
148 	private static String filenameFor(ASCompilationUnit unit) {
149 		String name;
150 		String pkg = unit.getPackageName();
151 		if (pkg == null || pkg.equals("")) {
152 			name = unit.getType().getName();
153 		} else {
154 			name = unit.getPackageName() + "." + unit.getType().getName();
155 		}
156 		return name.replace('.', File.separatorChar) + ".as";
157 	}
158 }