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/08/27 14:04:40 $
022: @author Daniel Bonniot
023: */
024:
025: class DirectorySourceContent extends SourceContent {
026: static DirectorySourceContent create(Package pkg, File directory) {
027: if (!directory.exists())
028: return null;
029:
030: DirectorySourceContent res = new DirectorySourceContent(pkg,
031: directory);
032: if (res.isValid())
033: return res;
034:
035: return null;
036: }
037:
038: static DirectorySourceContent create(Package pkg, java.net.URL url) {
039: if (!url.getProtocol().equals("file"))
040: User.error("Cannot use " + url
041: + " to get sources for package " + pkg);
042:
043: return create(pkg, new File(url.getFile()));
044: }
045:
046: DirectorySourceContent(Package pkg, File directory) {
047: this .pkg = pkg;
048: this .directory = directory;
049:
050: this .sources = getSources();
051: lastModification = maxLastModification(sources);
052: }
053:
054: private File[] sources;
055:
056: /** @return true if this directory indeed hosts a Nice package. */
057: private boolean isValid() {
058: return sources.length > 0;
059: }
060:
061: Content.Unit[] getDefinitions() {
062: if (sources.length == 0)
063: User.error(pkg.name, "Package " + pkg.getName()
064: + " has no source file in "
065: + nice.tools.util.System.prettyPrint(directory));
066:
067: sourcesRead = true;
068: Content.Unit[] res = new Content.Unit[sources.length];
069: for (int i = 0; i < res.length; i++)
070: res[i] = new Content.Unit(read(sources[i]), sources[i]);
071:
072: return res;
073: }
074:
075: private long maxLastModification(File[] files) {
076: long res = 0;
077: for (int i = 0; i < files.length; i++) {
078: long time = files[i].lastModified();
079: if (time > res)
080: res = time;
081: }
082: return res;
083: }
084:
085: private File[] getSources() {
086: File[] res = directory.listFiles(new FileFilter() {
087: public boolean accept(File f) {
088: return f.getPath().endsWith(sourceExtension)
089: && f.isFile();
090: }
091: });
092: if (res == null)
093: User.error(pkg, "Could not list source files in "
094: + getName());
095:
096: // put nice.lang.prelude first if it exists
097: if (pkg.name.toString().equals("nice.lang"))
098: for (int i = 0; i < res.length; i++)
099: if (res[i].getName()
100: .equals("prelude" + sourceExtension)) {
101: File tmp = res[i];
102: res[i] = res[0];
103: res[0] = tmp;
104: break;
105: }
106:
107: return res;
108: }
109:
110: private BufferedReader read(File f) {
111: try {
112: String encoding = pkg.compilation.sourceEncoding;
113: if (encoding != null)
114: try {
115: FileInputStream fis = new FileInputStream(f);
116: // No need to wrap the FileInputStream with BufferedInputStream here:
117: // InputStreamReader already contains an input buffer, both in Sun and GNU implementations.
118: InputStreamReader isr = new InputStreamReader(fis,
119: encoding);
120: return new BufferedReader(isr);
121: } catch (UnsupportedEncodingException badEncoding) {
122: User.warning("Encoding '" + encoding
123: + "' was rejected while reading "
124: + nice.tools.util.System.prettyPrint(f));
125: }
126: return new BufferedReader(new FileReader(f));
127: } catch (FileNotFoundException e) {
128: User.error(nice.tools.util.System.prettyPrint(f)
129: + " of package " + pkg.getName()
130: + " could not be found");
131: return null;
132: }
133: }
134:
135: File getOutputDirectory() {
136: return directory;
137: }
138:
139: public String getName() {
140: return nice.tools.util.System.prettyPrint(directory);
141: }
142:
143: public String toString() {
144: return "Source files in: " + getName();
145: }
146:
147: private Package pkg;
148: private File directory;
149:
150: final static String sourceExtension = ".nice";
151: }
|