001: // The UMLet source code is distributed under the terms of the GPL; see license.txt
002: /*
003: * Created on 10.08.2004
004: *
005: * TODO To change the template for this generated file go to
006: * Window - Preferences - Java - Code Style - Code Templates
007: */
008: package com.umlet.control.io;
009:
010: import com.umlet.element.base.Entity;
011: import java.io.*;
012: import com.sun.tools.javac.*; //java compiler
013: import com.umlet.control.*;
014:
015: /**
016: * @author Ludwig
017: *
018: * TODO To change the template for this generated type comment go to
019: * Window - Preferences - Java - Code Style - Code Templates
020: */
021: public class CustomElementLoader {
022: private static CustomElementLoader _instance;
023: ClassLoader loader;
024: private static int errorCounter = 0;
025: private int compilerReturnCode = 0;
026:
027: public static CustomElementLoader getInstance() {
028: if (_instance == null) {
029: _instance = new CustomElementLoader();
030: }
031: return _instance;
032: }
033:
034: public CustomElementLoader() {
035: errorCounter = 0;
036: }
037:
038: public Entity doLoadClass(String[] fileNameStrArr,
039: boolean cleanBuild, boolean silent) {
040: Entity entity = null;
041: //System.out.println("fileNameStrArr:"+fileNameStrArr[0]+","+fileNameStrArr[1]);
042: try {
043: //int compilerReturnCode=0;
044: String typeName = fileNameStrArr[1];
045: String filePath_woExtension = fileNameStrArr[0].substring(
046: 0, fileNameStrArr[0].indexOf(".java"));
047: File classFile = new File(filePath_woExtension + ".class");
048: StringWriter compilerErrorMessageSW = new StringWriter();
049: if ((cleanBuild) || (!classFile.exists())) {
050: //System.out.println("CLEAN BUILD");
051: compilerErrorMessageSW = new StringWriter(); //catch compiler messages
052: PrintWriter compilerErrorMessagePW = new PrintWriter(
053: compilerErrorMessageSW);
054: String sourceFilePath = filePath_woExtension + ".java";
055: compilerReturnCode = Main
056: .compile(
057: new String[] {
058: "-classpath",
059: Umlet.getInstance()
060: .getHomePath()
061: + "umlet.jar",
062: sourceFilePath },
063: compilerErrorMessagePW);
064: //compilerReturnCode = Main.compile(new String[] {sourceFilePath},compilerErrorMessagePW);
065: }// else System.out.println("JUST LOAD CLASS:"+typeName);
066: if (compilerReturnCode == 0) {
067: loader = new FileClassLoader(filePath_woExtension
068: + ".class"); //filename of the class-file
069: Class c = loader.loadClass(typeName); //load class by type name
070: entity = (Entity) c.newInstance();
071: entity.loadJavaSource(filePath_woExtension + ".java"); //load the java source into the entity
072: } else {
073: System.err.println("Compiler returned:"
074: + compilerReturnCode);
075: if (!silent)
076: Umlet.getInstance().showCompilerMessage(
077: compilerErrorMessageSW.toString());
078: else {
079: //count errors
080: errorCounter++;
081: }
082: }
083: } catch (Exception e) {
084: Umlet.getInstance().showCompilerMessage(e.getMessage());
085: e.printStackTrace();
086:
087: }
088: return entity;
089: }
090:
091: public int getErrorCounter() {
092: return errorCounter;
093: }
094:
095: public void resetErrorCounter() {
096: errorCounter = 0;
097: }
098:
099: public int getCompilerReturnCode() {
100: return compilerReturnCode;
101: }
102: }
|