001: /**************************************************************************/
002: /* N I C E */
003: /* A high-level object-oriented research language */
004: /* (c) Daniel Bonniot 2001 */
005: /* */
006: /* This program is free software; you can redistribute it and/or modify */
007: /* it under the terms of the GNU General Public License as published by */
008: /* the Free Software Foundation; either version 2 of the License, or */
009: /* (at your option) any later version. */
010: /* */
011: /**************************************************************************/package bossa.modules;
012:
013: import bossa.util.*;
014: import java.io.*;
015:
016: import gnu.bytecode.ClassType;
017:
018: /**
019: A package located in a directory.
020:
021: @version $Date: 2005/04/12 12:38:11 $
022: @author Daniel Bonniot
023: */
024:
025: class DirectoryCompiledContent extends CompiledContent {
026: static DirectoryCompiledContent create(Package pkg, File directory) {
027: if (!directory.exists())
028: return null;
029:
030: DirectoryCompiledContent res = new DirectoryCompiledContent(
031: pkg, directory);
032: if (res.isValid())
033: return res;
034:
035: return null;
036: }
037:
038: DirectoryCompiledContent(Package pkg, File directory) {
039: this .pkg = pkg;
040: this .directory = directory;
041:
042: this .itf = getInterface();
043: }
044:
045: private File itf;
046:
047: /** @return true if this directory indeed hosts a Nice package. */
048: private boolean isValid() {
049: return itf != null;
050: }
051:
052: Content.Unit[] getDefinitions() {
053: return new Content.Unit[] { new Content.Unit(read(itf), itf) };
054: }
055:
056: ClassType readClass(String name) {
057: InputStream s = getFileStream(bossa.util.Util.simpleName(name)
058: + ".class");
059: if (s == null)
060: return null;
061:
062: ClassType res = null;
063: s = new BufferedInputStream(s);
064: try {
065: res = gnu.bytecode.ClassFileInput.readClassType(name, s);
066: } catch (LinkageError e) {
067: } catch (IOException e) {
068: }
069:
070: return res;
071: }
072:
073: private File getInterface() {
074: File itf = new File(directory, "package.nicei");
075: File dispatchFile = new File(directory, "dispatch.class");
076:
077: if (!itf.exists())
078: return null;
079:
080: bytecode = readClass(pkg.getName() + "."
081: + Package.packageClassName);
082: dispatch = readClass(pkg.getName() + ".dispatch");
083:
084: if (bytecode == null || dispatch == null)
085: return null;
086:
087: lastCompilation = Math.min(itf.lastModified(), dispatchFile
088: .lastModified());
089:
090: return itf;
091: }
092:
093: private BufferedReader read(File f) {
094: try {
095: return new BufferedReader(new FileReader(f));
096: } catch (FileNotFoundException e) {
097: User.error(nice.tools.util.System.prettyPrint(f)
098: + " of package " + pkg.getName()
099: + " could not be found");
100: return null;
101: }
102: }
103:
104: void addClasses(java.util.Set/*<Content.Stream>*/classes) {
105: addClasses(classes, directory);
106: }
107:
108: static void addClasses(java.util.Set/*<Content.Stream>*/classes,
109: File directory) {
110: File[] files = directory.listFiles(new FileFilter() {
111: public boolean accept(File f) {
112: String name = f.getName();
113:
114: if (name.equals("package.nicei"))
115: return true;
116:
117: return name.endsWith(".class") && f.isFile();
118: }
119: });
120:
121: for (int i = 0; i < files.length; i++)
122: try {
123: classes.add(new Content.Stream(new BufferedInputStream(
124: new FileInputStream(files[i])), files[i]
125: .getName()));
126: } catch (FileNotFoundException e) {
127: }
128: }
129:
130: private InputStream getFileStream(String name) {
131: File f = new File(directory, name);
132:
133: try {
134: return new FileInputStream(f);
135: } catch (FileNotFoundException e) {
136: }
137:
138: return null;
139: }
140:
141: public String getName() {
142: return nice.tools.util.System.prettyPrint(directory);
143: }
144:
145: public String toString() {
146: return "Compiled package in: " + getName();
147: }
148:
149: private Package pkg;
150: private File directory;
151: }
|