提代替码列表
对付本书每一个完整的代码列表(不是代码段),各人无疑会留意到它们都用非凡的注释暗号起始与竣事(’//:’和’///:~’)。之所以要包罗这种符号信息,是为了能将代码从本书自动提取到兼容的源码文件中。在我的前一本书里,我设计了一个系统,可将测试过的代码文件自动归并到书中。但对付这本书,我发明一种更轻便的做法是一旦通过了最初的测试,就把代码粘贴到书中。并且由于很难第一次就编译通过,所以我在书的内部编辑代码。但如何提取并测试代码呢?这个措施就是要害。假如你规划办理一个文字处理惩罚的问题,那么它也很有操作代价。该例也演示了String类的很多特性。
我首先将整本书都以ASCII文本名目生存成一个独立的文件。CodePackager措施有两种运行模式(在usageString有相应的描写):假如利用-p符号,措施就会查抄一个包括了ASCII文本(即本书的内容)的一个输入文件。它会遍历这个文件,凭据注释暗号提取出代码,并用位于第一行的文件名来抉择建设文件利用什么名字。除此以外,在需要将文件置入一个非凡目次的时候,它还会查抄package语句(按照由package语句指定的路径选择)。
但这样还不足。措施还要对包(package)名举办跟踪,从而监督章内产生的变革。由于每一章利用的所有包都以c02,c03,c04等等起头,用于标志它们所属的是哪一章(除那些以com起头的以外,它们在对差异的章举办跟踪的时候会被忽略)——只要每一章的第一个代码列表包括了一个package,所以CodePackager措施能知道每一章产生的变革,并将后续的文件放到新的子目次里。
每个文件提取出来时,城市置入一个SourceCodeFile工具,随后再将谁人工具置入一个荟萃(后头还会详尽报告这个进程)。这些SourceCodeFile工具可以简朴地生存在文件中,那正是本项目标第二个用途。假如直接挪用CodePackager,不添加-p符号,它就会将一个“打包”文件作为输入。谁人文件随后会被提取(释放)进入单独的文件。所以-p符号的意思就是提取出来的文件已被“打包”(packed)进入这个单一的文件。
但为什么还要如此贫苦地利用打包文件呢?这是由于差异的计较机平台用差异的方法在文件里生存文本信息。个中最大的问题是换行字符的暗示要领;虽然,尚有大概存在另一些问题。然而,Java有一种非凡范例的IO数据流——DataOutputStream——它可以担保“无论数据来自何种呆板,只要利用一个DataInputStream收取这些数据,就可用本机正确的名目生存它们”。也就是说,Java认真节制与差异平台有关的所有细节,而这正是Java最具魅力的一点。所以-p符号能将所有对象都生存到单一的文件里,并回收通用的名目。用户可从Web下载这个文件以及Java措施,然后对这个文件运行CodePackager,同时不指定-p符号,文件便会释放到系统中正确的场合(亦可指定另一个子目次;不然就在当前目次建设子目次)。为确保不会留下与特定平台有关的名目,每每需要描写一个文件或路径的时候,我们就利用File工具。除此以外,尚有一项出格的安详法子:在每个子目次里都放入一个空文件;谁人文件的名字指出在谁人子目次里应找到几多个文件。
下面是完整的代码,后头会对它举办具体的说明:
//: CodePackager.java
// "Packs" and "unpacks" the code in "Thinking
// in Java" for cross-platform distribution.
/* Commented so CodePackager sees it and starts
a new chapter directory, but so you don't
have to worry about the directory where this
program lives:
package c17;
*/
import java.util.*;
import java.io.*;
class Pr {
static void error(String e) {
System.err.println("ERROR: " + e);
System.exit(1);
}
}
class IO {
static BufferedReader disOpen(File f) {
BufferedReader in = null;
try {
in = new BufferedReader(
new FileReader(f));
} catch(IOException e) {
Pr.error("could not open " + f);
}
return in;
}
static BufferedReader disOpen(String fname) {
return disOpen(new File(fname));
}
static DataOutputStream dosOpen(File f) {
DataOutputStream in = null;
try {
in = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(f)));
} catch(IOException e) {
Pr.error("could not open " + f);
}
return in;
}
static DataOutputStream dosOpen(String fname) {
return dosOpen(new File(fname));
}
static PrintWriter psOpen(File f) {
PrintWriter in = null;
try {
in = new PrintWriter(
new BufferedWriter(
new FileWriter(f)));
} catch(IOException e) {
Pr.error("could not open " + f);
}
return in;
}
static PrintWriter psOpen(String fname) {
return psOpen(new File(fname));
}
static void close(Writer os) {
try {
os.close();
} catch(IOException e) {
Pr.error("closing " + os);
}
}
static void close(DataOutputStream os) {
try {
os.close();
} catch(IOException e) {
Pr.error("closing " + os);
}
}
static void close(Reader os) {
try {
os.close();
} catch(IOException e) {
Pr.error("closing " + os);
}
}
}
class SourceCodeFile {
public static final String
startMarker = "//:", // Start of source file
endMarker = "} ///:~", // End of source
endMarker2 = "}; ///:~", // C++ file end
beginContinue = "} ///:Continued",
endContinue = "///:Continuing",
packMarker = "###", // Packed file header tag
eol = // Line separator on current system
System.getProperty("line.separator"),
filesep = // System's file path separator
System.getProperty("file.separator");
public static String copyright = "";
static {
try {
BufferedReader cr =
new BufferedReader(
new FileReader("Copyright.txt"));
String crin;
while((crin = cr.readLine()) != null)
copyright += crin + "\n";
cr.close();
} catch(Exception e) {
copyright = "";
}
}
private String filename, dirname,
contents = new String();
private static String chapter = "c02";
// The file name separator from the old system:
public static String oldsep;
public String toString() {
return dirname + filesep + filename;
}
// Constructor for parsing from document file:
public SourceCodeFile(String firstLine,
BufferedReader in) {
dirname = chapter;
// Skip past marker:
filename = firstLine.substring(
startMarker.length()).trim();
// Find space that terminates file name:
if(filename.indexOf(' ') != -1)
filename = filename.substring(
0, filename.indexOf(' '));
System.out.println("found: " + filename);
contents = firstLine + eol;
if(copyright.length() != 0)
contents += copyright + eol;
String s;
boolean foundEndMarker = false;
try {
while((s = in.readLine()) != null) {
if(s.startsWith(startMarker))
Pr.error("No end of file marker for " +
filename);
// For this program, no spaces before
// the "package" keyword are allowed
// in the input source code:
else if(s.startsWith("package")) {
// Extract package name:
String pdir = s.substring(
s.indexOf(' ')).trim();
pdir = pdir.substring(
0, pdir.indexOf(';')).trim();
// Capture the chapter from the package
// ignoring the 'com' subdirectories:
if(!pdir.startsWith("com")) {
int firstDot = pdir.indexOf('.');
if(firstDot != -1)
chapter =
pdir.substring(0,firstDot);
else
chapter = pdir;
}
// Convert package name to path name:
pdir = pdir.replace(
'.', filesep.charAt(0));
System.out.println("package " + pdir);
dirname = pdir;
}
contents += s + eol;
// Move past continuations:
if(s.startsWith(beginContinue))
while((s = in.readLine()) != null)
if(s.startsWith(endContinue)) {
contents += s + eol;
break;
}
// Watch for end of code listing:
if(s.startsWith(endMarker) ||
s.startsWith(endMarker2)) {
foundEndMarker = true;
break;
}
}
if(!foundEndMarker)
Pr.error(
"End marker not found before EOF");
System.out.println("Chapter: " + chapter);
} catch(IOException e) {
Pr.error("Error reading line");
}
}
// For recovering from a packed file:
public SourceCodeFile(BufferedReader pFile) {
try {
String s = pFile.readLine();
if(s == null) return;
if(!s.startsWith(packMarker))
Pr.error("Can't find " + packMarker
+ " in " + s);
s = s.substring(
packMarker.length()).trim();
dirname = s.substring(0, s.indexOf('#'));
filename = s.substring(s.indexOf('#') + 1);
dirname = dirname.replace(
oldsep.charAt(0), filesep.charAt(0));
filename = filename.replace(
oldsep.charAt(0), filesep.charAt(0));
System.out.println("listing: " + dirname
+ filesep + filename);
while((s = pFile.readLine()) != null) {
// Watch for end of code listing:
if(s.startsWith(endMarker) ||
s.startsWith(endMarker2)) {
contents += s;
break;
}
contents += s + eol;
}
} catch(IOException e) {
System.err.println("Error reading line");
}
}
public boolean hasFile() {
return filename != null;
}
public String directory() { return dirname; }
public String filename() { return filename; }
public String contents() { return contents; }
// To write to a packed file:
public void writePacked(DataOutputStream out) {
try {
out.writeBytes(
packMarker + dirname + "#"
+ filename + eol);
out.writeBytes(contents);
} catch(IOException e) {
Pr.error("writing " + dirname +
filesep + filename);
}
}
// To generate the actual file:
public void writeFile(String rootpath) {
File path = new File(rootpath, dirname);
path.mkdirs();
PrintWriter p =
IO.psOpen(new File(path, filename));
p.print(contents);
IO.close(p);
}
}
class DirMap {
private Hashtable t = new Hashtable();
private String rootpath;
DirMap() {
rootpath = System.getProperty("user.dir");
}
DirMap(String alternateDir) {
rootpath = alternateDir;
}
public void add(SourceCodeFile f){
String path = f.directory();
if(!t.containsKey(path))
t.put(path, new Vector());
((Vector)t.get(path)).addElement(f);
}
public void writePackedFile(String fname) {
DataOutputStream packed = IO.dosOpen(fname);
try {
packed.writeBytes("###Old Separator:" +
SourceCodeFile.filesep + "###\n");
} catch(IOException e) {
Pr.error("Writing separator to " + fname);
}
Enumeration e = t.keys();
while(e.hasMoreElements()) {
String dir = (String)e.nextElement();
System.out.println(
"Writing directory " + dir);
Vector v = (Vector)t.get(dir);
for(int i = 0; i < v.size(); i++) {
SourceCodeFile f =
(SourceCodeFile)v.elementAt(i);
f.writePacked(packed);
}
}
IO.close(packed);
}
// Write all the files in their directories:
public void write() {
Enumeration e = t.keys();
while(e.hasMoreElements()) {
String dir = (String)e.nextElement();
Vector v = (Vector)t.get(dir);
for(int i = 0; i < v.size(); i++) {
SourceCodeFile f =
(SourceCodeFile)v.elementAt(i);
f.writeFile(rootpath);
}
// Add file indicating file quantity
// written to this directory as a check:
IO.close(IO.dosOpen(
new File(new File(rootpath, dir),
Integer.toString(v.size())+".files")));
}
}
}
public class CodePackager {
private static final String usageString =
"usage: java CodePackager packedFileName" +
"\nExtracts source code files from packed \n" +
"version of Tjava.doc sources into " +
"directories off current directory\n" +
"java CodePackager packedFileName newDir\n" +
"Extracts into directories off newDir\n" +
"java CodePackager -p source.txt packedFile" +
"\nCreates packed version of source files" +
"\nfrom text version of Tjava.doc";
private static void usage() {
System.err.println(usageString);
System.exit(1);
}
public static void main(String[] args) {
if(args.length == 0) usage();
if(args[0].equals("-p")) {
if(args.length != 3)
usage();
createPackedFile(args);
}
else {
if(args.length > 2)
usage();
extractPackedFile(args);
}
}
private static String currentLine;
private static BufferedReader in;
private static DirMap dm;
private static void
createPackedFile(String[] args) {
dm = new DirMap();
in = IO.disOpen(args[1]);
try {
while((currentLine = in.readLine())
!= null) {
if(currentLine.startsWith(
SourceCodeFile.startMarker)) {
dm.add(new SourceCodeFile(
currentLine, in));
}
else if(currentLine.startsWith(
SourceCodeFile.endMarker))
Pr.error("file has no start marker");
// Else ignore the input line
}
} catch(IOException e) {
Pr.error("Error reading " + args[1]);
}
IO.close(in);
dm.writePackedFile(args[2]);
}
private static void
extractPackedFile(String[] args) {
if(args.length == 2) // Alternate directory
dm = new DirMap(args[1]);
else // Current directory
dm = new DirMap();
in = IO.disOpen(args[0]);
String s = null;
try {
s = in.readLine();
} catch(IOException e) {
Pr.error("Cannot read from " + in);
}
// Capture the separator used in the system
// that packed the file:
if(s.indexOf("###Old Separator:") != -1 ) {
String oldsep = s.substring(
"###Old Separator:".length());
oldsep = oldsep.substring(
0, oldsep. indexOf('#'));
SourceCodeFile.oldsep = oldsep;
}
SourceCodeFile sf = new SourceCodeFile(in);
while(sf.hasFile()) {
dm.add(sf);
sf = new SourceCodeFile(in);
}
dm.write();
}
} ///:~
#p#分页标题#e#
我们留意到package语句已经作为注释符号出来了。由于这是本章的第一个措施,所以package语句是必须的,用它汇报CodePackager已换取到另一章。可是把它放入包里却会成为一个问题。当我们建设一个包的时候,需要将功效措施同一个特定的目次布局接洽在一起,这一做法对本书的大大都例子都是合用的。但在这里,CodePackager措施必需在一个专用的目次里编译和运行,所以package语句作为注释标志出去。但对CodePackager来说,它“看起来”依然象一个普通的package语句,因为措施还不是出格巨大,不能侦查到多行注释(没有须要做得这么巨大,这里只要求利便就行)。
头两个类是“支持/东西”类,浸染是使措施剩余的部门在编写时越发连贯,也更便于阅读。第一个是Pr,它雷同ANSI C的perror库,两者都能打印出一条错误提示动静(但同时也会退出措施)。第二个类将文件的建设进程封装在内,这个进程已在第10章先容过了;各人已经知道,这样做很快就会变得很是累赘和贫苦。为办理这个问题,第10章提供的方案致力于新类的建设,但这儿的“静态”要领已经利用过了。在那些要领中,正常的违例会被捕捉,并相应地举办处理惩罚。这些要领使剩余的代码显得越发清爽,更易阅读。
辅佐办理问题的第一个类是SourceCodeFile(源码文件),它代表本书一个源码文件包括的所有信息(内容、文件名以及目次)。它同时还包括了一系列String常数,别离代表一个文件的开始与竣事;在打包文件内利用的一个标志;当前系统的换行符;文件路径脱离符(留意要用System.getProperty()侦查当地版本是什么);以及一大段版权声明,它是从下面这个Copyright.txt文件里提取出来的:
////////////////////////////////////////////////// // Copyright (c) Bruce Eckel, 1998 // Source code file from the book "Thinking in Java" // All rights reserved EXCEPT as allowed by the // following statements: You may freely use this file // for your own work (personal or commercial), // including modifications and distribution in // executable form only. Permission is granted to use // this file in classroom situations, including its // use in presentation materials, as long as the book // "Thinking in Java" is cited as the source. // Except in classroom situations, you may not copy // and distribute this code; instead, the sole // distribution point is http://www.BruceEckel.com // (and official mirror sites) where it is // freely available. You may not remove this // copyright and notice. You may not distribute // modified versions of the source code in this // package. You may not use this file in printed // media without the express permission of the // author. Bruce Eckel makes no representation about // the suitability of this software for any purpose. // It is provided "as is" without express or implied // warranty of any kind, including any implied // warranty of merchantability, fitness for a // particular purpose or non-infringement. The entire // risk as to the quality and performance of the // software is with you. Bruce Eckel and the // publisher shall not be liable for any damages // suffered by you or any third party as a result of // using or distributing software. In no event will // Bruce Eckel or the publisher be liable for any // lost revenue, profit, or data, or for direct, // indirect, special, consequential, incidental, or // punitive damages, however caused and regardless of // the theory of liability, arising out of the use of // or inability to use software, even if Bruce Eckel // and the publisher have been advised of the // possibility of such damages. Should the software // prove defective, you assume the cost of all // necessary servicing, repair, or correction. If you // think you've found an error, please email all // modified files with clearly commented changes to: // [email protected] (please use the same // address for non-code errors found in the book). //////////////////////////////////////////////////
#p#分页标题#e#
从一个打包文件中提取文件时,当初所用系统的文件脱离符也会标注出来,以便用当地系统合用的标记替换它。
当前章的子目次生存在chapter字段中,它初始化成c02(各人可留意一下第2章的列表正好没有包括一个打包语句)。只有在当前文件里发明一个package(打包)语句时,chapter字段才会产生改变。
1. 构建一个打包文件
第一个构建器用于从本书的ASCII文本版里提取出一个文件。发出挪用的代码(在列内外较深的处所)会读入并查抄每一行,直到找到与一个列表的开头相符的为止。在这个时候,它就会新建一个SourceCodeFile工具,将第一行的内容(已经过挪用代码读入了)通报给它,同时还要通报BufferedReader工具,以便在这个缓冲区中提取源码列表剩余的内容。
从这时起,各人会发明String要领被频繁运用。为提取出文件名,需挪用substring()的过载版本,令其从一个起始偏移开始,一直读到字串的末端,从而形成一个“子串”。为算出这个起始索引,先要用length()得出startMarker的总长,再用trim()删除字串头尾多余的空格。第一行在文件名后也大概有一些字符;它们是用indexOf()侦测出来的。若没有发明找到我们想寻找的字符,就返回-1;若找到那些字符,就返回它们第一次呈现的位置。留意这也是indexOf()的一个过载版本,回收一个字串作为参数,而非一个字符。
理会出并生存好文件名后,第一行会被置入字串contents中(该字串用于生存源码清单的完整正文)。随后,将剩余的代码行读入,并归并进入contents字串。虽然工作并没有想象的那么简朴,因为特定的环境需加以出格的节制。一种环境是错误查抄:若直接碰着一个startMarker(起始标志),表白当前操纵的这个代码列表没有配置一个竣事标志。这属于一个堕落条件,需要退出措施。
另一种非凡环境与package要害字有关。尽量Java是一种自由形式的语言,但这个措施要求package要害字必需位于行首。若发明package要害字,就通过查抄位于开头的空格以及位于末端的分号,从而提取出包名(留意亦可一次单独的操纵实现,要领是利用过载的substring(),令其同时查抄起始和竣事索引位置)。随后,将包名中的点号替换成特定的文件脱离符——虽然,这里要假设文件脱离符仅有一个字符的长度。尽量这个假设大概对今朝的所有系统都是合用的,但一旦碰着问题,必然不要忘了查抄一下这里。
默认操纵是将每一行都毗连到contents里,同时尚有换行字符,直到碰着一个endMarker(竣事标志)为止。该标志指出构建器该当遏制了。若在endMarker之前碰着了文件末了,就认为存在一个错误。
2. 从打包文件中提取
第二个构建器用于将源码文件从打包文件中规复(提取)出来。在这儿,作为挪用者的要领不必担忧会跳过一些中间文本。打包文件包括了所有源码文件,它们彼此间细密地靠在一起。需要通报给该构建器的仅仅是一个BufferedReader,它代表着“信息源”。构建器会从中提取出本身需要的信息。但在每个代码列表开始的处所尚有一些设置信息,它们的身份是用packMarker(打包标志)指出的。若packMarker不存在,意味着挪用者试图用错误的要领来利用这个构建器。
一旦发明packMarker,就会将其剥离出来,并提取出目次名(用一个’#’末了)以及文件名(直到行末)。不管在哪种环境下,旧脱离符城市被替换成当地合用的一个脱离符,这是用String replace()要领实现的。老的脱离符被置于打包文件的开头,在代码列表稍靠后的一部门即可看到是如何把它提取出来的。
构建器剩下的部门就很是简朴了。它读入每一行,把它归并到contents里,直到碰见endMarker为止。
3. 措施列表的存取
接下来的一系列要领是简朴的会见器:directory()、filename()(留意要领大概与字段有沟通的拼写和巨细写形式)和contents()。而hasFile()用于指出这个工具是否包括了一个文件(很快就会知道为什么需要这个)。
最后三个要领致力于将这个代码列表写进一个文件——要么通过writePacked()写入一个打包文件,要么通过writeFile()写入一个Java源码文件。writePacked()需要的独一对象就是DataOutputStream,它是在此外处所打开的,代表着筹备写入的文件。它先把头信息置入第一行,再挪用writeBytes()将contents(内容)写成一种“通用”名目。
筹备写Java源码文件时,必需先把文件建好。这是用IO.psOpen()实现的。我们需要向它通报一个File工具,个中不只包括了文件名,也包括了路径信息。但此刻的问题是:这个路径实际存在吗?用户大概抉择将所有源码目次都置入一个完全差异的子目次,谁人目次大概是尚不存在的。所以在正式写每个文件之前,都要挪用File.mkdirs()要领,建好我们想向个中写入文件的目次路径。它可一次性建好整个路径。
4. 整套列表的海涵
以子目次的形式组织代码列表长短常利便的,尽量这要求先在内存中建好整套列表。之所以要这样做,尚有另一个很有说服力的原因:为了构建更“康健”的系统。也就是说,在建设代码列表的每个子目次时,城市插手一个特另外文件,它的名字包括了谁人目次内应有的文件数目。
DirMap类可辅佐我们实现这一结果,并有效地演示了一个“多重映射”的概述。这是通过一个散列表(Hashtable)实现的,它的“键”是筹备建设的子目次,而“值”是包括了谁人特定目次中的SourceCodeFile工具的Vector工具。所以,我们在这儿并不是将一个“键”映射(或对应)到一个值,而是通过对应的Vector,将一个键“多重映射”到一系列值。尽量这听起来好像很巨大,但详细实现时却长短常简朴和直接的。各人可以看到,DirMap类的大大都代码都与向文件中的写入有关,而非与“多重映射”有关。与它有关的代码仅少少数罢了。
可通过两种方法成立一个DirMap(目次映射或对应)干系:默认构建器假定我们但愿目次从当前位置向下展开,而另一个构建器让我们为起始目次指定一个备用的“绝对”路径。
add()要领是一个采纳的动作较量麋集的场合。首先将directory()从我们想添加的SourceCodeFile里提取出来,然后查抄散列表(Hashtable),看看个中是否已经包括了谁人键。假如没有,就向散列表插手一个新的Vector,并将它同谁人键关联到一起。到这时,不管采纳的是什么途径,Vector都已经就位了,可以将它提取出来,以便添加SourceCodeFile。由于Vector可象这样同散列表利便地归并到一起,所以我们从两方面都能感受得很是利便。
写一个打包文件时,需打开一个筹备写入的文件(看成DataOutputStream打开,使数据具有“通用”性),并在第一行写入与老的脱离符有关的头信息。接着发生对Hashtable键的一个Enumeration(列举),并遍历个中,选择每一个目次,并取得与谁人目次有关的Vector,使谁人Vector中的每个SourceCodeFile都能写入打包文件中。
用write()将Java源码文件写入它们对应的目次时,回收的要领险些与writePackedFile()完全一致,因为两个要领都只需简朴挪用SourceCodeFile中适当的要领。但在这里,根路径会通报给SourceCodeFile.writeFile()。所有文件都写好后,名字中指定了已写文件数量的谁人附加文件也会被写入。
5. 主措施
前面先容的那些类都要在CodePackager顶用到。各人首先看到的是用法字串。一旦最终用户不正确地挪用了措施,就会打印出先容正确用法的这个字串。挪用这个字串的是usage()要领,同时还要退出措施。main()独一的任务就是判定我们但愿建设一个打包文件,照旧但愿从一个打包文件中提取什么对象。随后,它认真担保利用的是正确的参数,并挪用适当的要领。
建设一个打包文件时,它默认位于当前目次,所以我们用默认构建器建设DirMap。打开文件后,个中的每一行城市读入,并查抄是否切合非凡的条件:
(1) 若行首是一个用于源码列表的起始标志,就新建一个SourceCodeFile工具。构建器会读入源码列表剩下的所有内容。功效发生的句柄将直接插手DirMap。
(2) 若行首是一个用于源码列表的竣事标志,表白某个处所呈现错误,因为竣事标志该当只能由SourceCodeFile构建器发明。
提取/释放一个打包文件时,提取出来的内容可进入当前目次,亦可进入另一个备用目次。所以需要相应地建设DirMap工具。打开文件,并将第一行读入。老的文件路径脱离符信息将从这一行中提取出来。随后按照输入来建设第一个SourceCodeFile工具,它会插手DirMap。只要包括了一个文件,新的SourceCodeFile工具就会建设并插手(建设的最后一个用光输入内容后,会简朴地返回,然后hasFile()会返回一个错误)。