001: package tide.importtools;
002:
003: import java.io.*;
004: import java.util.*;
005: import tide.project.ProjectSettings;
006:
007: public final class NetBeansImport {
008: public static String fileNameEnding = ".properties";
009:
010: final private Properties props = new Properties();
011: final File base;
012:
013: final private Set<String> usedKeys = new HashSet<String>();
014:
015: public NetBeansImport(File propFile, ProjectSettings proj)
016: throws Exception {
017: FileInputStream fis = null;
018: try {
019: fis = new FileInputStream(propFile);
020: props.load(fis);
021: } catch (Exception e) {
022: throw e;
023: } finally {
024: fis.close();
025: }
026:
027: // yes, two times.
028: base = propFile.getParentFile().getParentFile();
029:
030: File srcFile = getFile(getPropertyWithReplacedVars("src.dir"));
031:
032: if (proj != null) {
033: proj.setSources_Home(srcFile);
034: proj.setProjectName(base.getName());
035: proj
036: .setAppArgs(getPropertyWithReplacedVars("application.args"));
037: proj
038: .setCompilerOptions(getPropertyWithReplacedVars("javac.compilerargs"));
039: proj
040: .setRuntimeArgs(getPropertyWithReplacedVars("run.jvmargs"));
041: proj
042: .setClasses_Home(getFile(getPropertyWithReplacedVars("build.classes.dir")));
043: proj.setMainSourceFile(new File(srcFile.getAbsolutePath()
044: + "/"
045: + getPropertyWithReplacedVars("main.class")
046: .replace('.', '/') + ".java"));
047: String cp = getPropertyWithReplacedVars("javac.classpath");
048: String[] cps = cp.split(":");
049: List<File> extJars = new ArrayList<File>();
050: for (String cpi : cps) {
051: File fi = getFile(cpi);
052: extJars.add(fi);
053: if (!fi.exists()) {
054: System.out.println("Class path item " + fi
055: + " doesn't exist!");
056: }
057: }
058: proj.setExternalJars(extJars);
059: }
060: }
061:
062: /** files are relative to the base => this gives the absolute file path
063: * if the file is already absolute and exists, return it as is
064: */
065: private File getFile(String name) {
066: File fn = new File(name);
067: if (fn.isAbsolute() && fn.exists())
068: return fn;
069:
070: File f = new File(base, name);
071: try {
072: return f.getCanonicalFile();
073: } catch (Exception e) {
074: return f;
075: }
076: }
077:
078: private String getProp(String key, String def) {
079: usedKeys.add(key);
080: return props.getProperty(key, def);
081: }
082:
083: /** ${xx} replaced with getProperty(xx,"<?key xx not found?>")
084: */
085: private String getPropertyWithReplacedVars(String key) {
086: String val = getProp(key, "<?key " + key + " not found?>");
087:
088: // find all ${skey} and replace them
089:
090: StringBuilder sb = new StringBuilder();
091: int posStart = 0;
092: int pos = val.indexOf("${");
093: while (pos != -1) {
094: int posEnd = val.indexOf('}', pos);
095: if (posEnd == -1) {
096: System.out.println("Bad format: no end } in " + val);
097: break;
098: }
099: String skey = val.substring(pos + 2, posEnd);
100:
101: String sval = getProp(skey, null);
102: if (sval == null) {
103: sval = "<cannot find prop " + skey + ">";
104: }
105:
106: sb.append(val.substring(posStart, pos));
107: sb.append(sval);
108:
109: // next
110: pos = val.indexOf("${", posEnd);
111: posStart = posEnd + 1;
112: }
113: // the rest
114: sb.append(val.substring(posStart));
115:
116: return sb.toString();
117: }
118:
119: }
|