📄 207-211.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:Futil Recipes for Feudal Times</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=4//-->
<!--PAGES=207-211//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="203-207.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="211-212.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading54"></A><FONT COLOR="#000077">Futil.Print</FONT></H4>
<P>To ease the implementation of output to files and the console, the futils package provides its own <I>Print</I> class. This class is a public final class that has a private constructor to prevent instantiation. The key point is that the <I>Print</I> class stores an instance of <I>PrintOutputStream</I> in a settable variable. Thus, output can be directed to a file or, by default, to the console. The trick is that the change of the <I>PrintOutputStream</I> is centralized, and all methods that use the <I>Print</I> class benefit from this centralization. For example, in the <I>Geometry</I> class we write the following:</P>
<!-- CODE SNIP //-->
<PRE>
1. static public void print(PrintStream out) {
</PRE>
<!-- END CODE SNIP //-->
<P>We invoke the <I>setPrintStream</I> method in the <I>Print</I> class, invoke <I>print</I>, and then reset the stream.</P>
<!-- CODE SNIP //-->
<PRE>
2. Print.setPrintStream( out);
3. print();
4. Print.setPrintStream( System.out);
5. } // print
</PRE>
<!-- END CODE SNIP //-->
<P>Every shape knows how to print (using the <I>Print</I> class) and so will use the new output stream when printing. If the <I>print</I> method of line 6 is invoked without line 2 being executed, the output will be directed to the console.</P>
<!-- CODE SNIP //-->
<PRE>
6. static public void print() {
7. laser.print();
8. camera.print();
9. wedge.print();
10. grating.print();
11. System.out.println("Lambda = "+ lambda.getValue());
12. System.out.println("s = " + s());
13. } // print
</PRE>
<!-- END CODE SNIP //-->
<P>The code for <I>futils.Print</I> follows:</P>
<!-- CODE //-->
<PRE>
1. package futils;
2. import java.util.*;
3. import java.io.*;
4. public final class Print {
5. // prevent instantiation
6. private Print() {}
7. private static PrintStream output = System.out;
8. public static void setPrintStream(PrintStream ps) {
9. output = ps;
10. }
11. public static PrintStream getPrintStream() {
12. return output;
13. }
14. public static void d(double i)
15. {Print.output.print(i+"\t");}
16. public static void ln(double d)
17. {Print.output.println(d);}
18. public static void ln(String str)
19. {Print.output.println(str);}
20. public static void print(String str)
21. {Print.output.print(str);}
22. public static void print(double d)
23. {Print.output.print(d);}
24. public static void print(int d)
25. {Print.output.print(d);}
26. public static void className(Object o)
27. {Print.output.print(o.getClass().getName() + "\t");}
28. }
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading55"></A><FONT COLOR="#000077">Futil.writeFilteredHrefFile</FONT></H4>
<P>A buggy Web authoring tool (such as Netscape 3.01 Gold) will produce HTML that has hrefs (hypertext references) that contain embedded spaces. The API documentation shows an href on line 8. The space between “API” and “Documentation” is a bug. Browsers will stop reading the href name after the first space. To get browsers to recognize the space, we must replace spaces with their hexadecimal equivalent, '%20'.
</P>
<!-- CODE SNIP //-->
<PRE>
1. <html>
2. <head>
3. <title>
4. API User's Guide
5. </title>
6. </head>
7. <body>
8. <a href= "API Documentation/packages.html" > Java API</a>
</PRE>
<!-- END CODE SNIP //-->
<P>The corrected version of line 8 follows:
</P>
<!-- CODE SNIP //-->
<PRE>
<a href= "API%20Documentation/packages.html" > Java API</a>
</PRE>
<!-- END CODE SNIP //-->
<P>To perform this transformation, we build a custom tokenizer that looks for quoted strings and replaces all spaces that they contain with '%20'. This approach has the bug of also transforming the spaces in non-href strings to '%20' (a bug we have been able to live with).
</P>
<!-- CODE SNIP //-->
<PRE>
1. public static void writeFilteredHrefFile(String inputName, String
outputName) {
2. System.out.println("Filtering:\t" + inputName +"\t>\t"+outputName);
3. try {
</PRE>
<!-- END CODE SNIP //-->
<P>Lines 4-7 make an input stream and a <I>StreamTokenizer</I> instance.</P>
<!-- CODE SNIP //-->
<PRE>
4. FileInputStream is = new FileInputStream(inputName);
5. StreamTokenizer tokens = new StreamTokenizer(is);
6. FileOutputStream os = new FileOutputStream(outputName);
7. PrintStream output = new PrintStream(os);
8. int i;
9. int next = 0;
</PRE>
<!-- END CODE SNIP //-->
<P>The interesting bit is line 10. Recall that <I>resetSyntax</I> will make all characters ordinary. This means that there are no comment characters.</P>
<!-- CODE SNIP //-->
<PRE>
10. tokens.resetSyntax();
</PRE>
<!-- END CODE SNIP //-->
<P>Line 11 specifies that all characters are word characters. Line 12 identifies the only character of interest, the quote character. Recall that <I>nextToken</I> will read until the <I>quoteCharacter</I>, setting <I>sval</I> to the value of the body of the string contained in quotes.</P>
<!-- CODE SNIP //-->
<PRE>
11. tokens.wordChars(0,255);
12. tokens.quoteChar(`"`);
13. while ((next = tokens.nextToken()) != tokens.TT_EOF) {
14. switch (next) {
15. case `"`:
16. output.print(`"`);
</PRE>
<!-- END CODE SNIP //-->
<P>We now output a quote and then scan the string for a space. If a space is found, we output a “%20”; otherwise, we output the character.
</P>
<!-- CODE SNIP //-->
<PRE>
17. for (i=0;i<tokens.sval.length();i++)
18. if (tokens.sval.charAt(i) == ` `)
19. output.print("%20");
20. else
21. output.print(tokens.sval.charAt(i));
22. output.print(`"`);
23. break;
</PRE>
<!-- END CODE SNIP //-->
<P>In all other cases we output the string read. This technique is nice, because we read only up to the point at which a delimiting character occurs.
</P>
<!-- CODE //-->
<PRE>
24. case tokens.TT_WORD:
25. output.print(tokens.sval+" ");
26. break;
27. case tokens.TT_NUMBER:
28. output.print(tokens.nval+" ");
29. break;
30. case tokens.TT_EOL:
31. output.println();
32. break;
33. } // end switch
34. } // end while
35. is.close();
36. os.close();
37. } // end try
38. catch (Exception exe)
39. {System.out.println("writeFilteredHrefFile:er!");}
40. }
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="203-207.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="211-212.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -