001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.support.builder;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Set;
025:
026: import org.eclipse.jdt.core.compiler.CategorizedProblem;
027:
028: import spoon.processing.Builder;
029: import spoon.reflect.Factory;
030: import spoon.support.builder.support.CtVirtualFolder;
031:
032: public class SpoonBuildingManager implements Builder {
033:
034: Factory factory;
035:
036: private boolean build = false;
037:
038: CtVirtualFolder sources = new CtVirtualFolder();
039:
040: CtVirtualFolder templates = new CtVirtualFolder();
041:
042: public SpoonBuildingManager(Factory factory) {
043: this .factory = factory;
044: }
045:
046: public void addInputSource(CtResource source) throws IOException {
047: if (source.isFile())
048: this .sources.addFile((CtFile) source);
049: else
050: this .sources.addFolder((CtFolder) source);
051: }
052:
053: public void addInputSource(File source) throws IOException {
054: if (FileFactory.isFile(source))
055: this .sources.addFile(FileFactory.createFile(source));
056: else
057: this .sources.addFolder(FileFactory.createFolder(source));
058: }
059:
060: public void addTemplateSource(CtResource source) throws IOException {
061: if (source.isFile())
062: this .templates.addFile((CtFile) source);
063: else
064: this .templates.addFolder((CtFolder) source);
065: }
066:
067: public void addTemplateSource(File source) throws IOException {
068: if (FileFactory.isFile(source))
069: this .templates.addFile(FileFactory.createFile(source));
070: else
071: this .templates.addFolder(FileFactory.createFolder(source));
072: }
073:
074: JDTCompiler compiler = null;
075:
076: public boolean build() throws Exception {
077: if (factory == null) {
078: throw new Exception("Factory not initialized");
079: }
080: if (build) {
081: throw new Exception("Model already built");
082: }
083: build = true;
084: JDTCompiler.JAVA_COMPLIANCE = factory.getEnvironment()
085: .getComplianceLevel();
086: boolean srcSuccess, templateSuccess;
087: factory.getEnvironment().debugMessage(
088: "compiling sources: " + sources.getAllJavaFiles());
089: long t = System.currentTimeMillis();
090: compiler = new JDTCompiler();
091: initCompiler();
092: srcSuccess = compiler.compileSrc(factory, sources
093: .getAllJavaFiles());
094: if (!srcSuccess) {
095: for (CategorizedProblem[] cps : compiler.probs) {
096: for (int i = 0; i < cps.length; i++) {
097: CategorizedProblem problem = cps[i];
098: if (problem != null)
099: getProblems().add(problem.getMessage());
100: }
101: }
102: }
103: factory.getEnvironment().debugMessage(
104: "compiled in " + (System.currentTimeMillis() - t)
105: + " ms");
106: factory.getEnvironment().debugMessage(
107: "compiling templates: " + templates.getAllJavaFiles());
108: t = System.currentTimeMillis();
109: templateSuccess = compiler.compileTemplate(factory, templates
110: .getAllJavaFiles());
111: factory.Template().parseTypes();
112: factory.getEnvironment().debugMessage(
113: "compiled in " + (System.currentTimeMillis() - t)
114: + " ms");
115: return srcSuccess && templateSuccess;
116: }
117:
118: public void initCompiler() {
119: // does nothing by default
120: }
121:
122: public List<String> problems;
123:
124: public List<String> getProblems() {
125: if (problems == null) {
126: problems = new ArrayList<String>();
127: }
128: return problems;
129: }
130:
131: public Set<File> getInputSources() {
132: // TODO Auto-generated method stub
133: throw new RuntimeException("not implemented");
134: }
135:
136: public CtVirtualFolder getSource() {
137: return sources;
138: }
139:
140: public CtVirtualFolder getTemplates() {
141: return templates;
142: }
143:
144: public Set<File> getTemplateSources() {
145: // TODO Auto-generated method stub
146: throw new RuntimeException("not implemented");
147: }
148:
149: public Factory getFactory() {
150: return factory;
151: }
152:
153: public void setFactory(Factory factory) {
154: this .factory = factory;
155: }
156:
157: public JDTCompiler getCompiler() {
158: return compiler;
159: }
160:
161: }
|