Java Doc for Layouter.java in  » Testing » KeY » de » uka » ilkd » key » util » pp » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » KeY » de.uka.ilkd.key.util.pp 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   de.uka.ilkd.key.util.pp.Layouter

Layouter
public class Layouter (Code)
This class pretty-prints information using line breaks and indentation. For instance, it can be used to print
 while (i>0) {
 i--;
 j++;
 }
 
instead of
 while (i>0) { i
 --; j++;}
 
if a maximum line width of 15 characters is chosen.

The formatted output is directed to a backend which might write it to an I/O stream, append it to the text of a GUI componenet or store it in a string. The Backend interface encapsulates the concept of backend. Apart from handling the output, the backend is also asked for the available line width and for the amount of space needed to print a string. This makes it possible to include e.g. HTML markup in the output which does not take up any space. There are two convenience implementations WriterBackend and StringBackend , which write the output to a java.io.Writer , resp. a java.lang.String .

The layouter internally keeps track of a current indentation level. Think of nicely indented Java source code. Then the indentation level at any point is the number of blank columns to be inserted at the begining of the next line if you inserted a line break. To increase the indentation level of parts of the text, the input to the layouter is separated into blocks. The indentation level changes when a block is begun, and it is reset to its previous value when a block is ended. Of course, blocks maybe nested.

In order to break text among several lines, the layouter needs to be told where line breaks are allowed. A break is a position in the text where there is either a line break (with appropriate indentation) or a number of spaces, if enough material fits in one line. In order to handle the indentation level properly, breaks should only occur inside blocks. There are in fact two kinds of blocks: consistent and inconsistent ones. In a consistent block, line are broken either at all or at none of the breaks. In an inconsistent block, as much material as possible is put on one line before it is broken.

Consider the program above. It should be printed either as

 while (i>0) { i--; j++; }
 
or, if there is not enough space on the line, as
 while (i>0) {
 i--;
 j++;
 }
 
Given a Layouter object l, we could say:
 l.begin(true,2).print("while (i>0) {").brk(1,0)
 .print("i--;").brk(1,0)
 .print("j++;").brk(1,-2)
 .print("{").end();
 
The call to Layouter.begin(boolean,int) starts a consistent block, increasing the indentation level by 2. The Layouter.print(String) methods gives some actual text to be output. The call to Layouter.brk(int,int) inserts a break. The first argument means that one space should be printed at this position if the line is not broken. The second argument is an offset to be added to the indentation level for the next line, if the line is broken. The effect of this parameter can be seen in the call brk(1,-2). The offset of -2 outdents the last line by 2 positions, which aligns the closing brace with the with.

If the lines in a block are broken, one sometimes wants to insert spaces up to the current indentation level at a certain position without allowing a line break there. This can be done using the Layouter.ind(int,int) method. For instance, one wants to output either

 ...[Good and Bad and Ugly]...
 
or
 ...[    Good
 and Bad
 and Ugly]...
 
Note the four spaces required before Good. We do this by opening a block which sets the indentation level to the column where the G ends up and outdenting the lines with the and:
 l.print("...[").begin(true,4).ind(0,0).print("Good").brk(1,-4)
 .print("and ").print("Bad").brk(1,-4)
 .print("and ").print("Ugly").end().print("]...");
 
Again, the first argument to Layouter.ind(int,int) is a number of spaces to print if the block we are in is printed on one line. The second argument is an offset to be added to the current indentation level to determine the column to which we should skip.

When all text has been sent to a Layouter and all blocks have been ended, the Layouter.close() method should be closed. This sends all pending output to the backend and invokes the Backend.close method, which usually closes I/O streams, etc.

Some applications need to keep track of where certain parts of the input text end up in the output. For this purpose, the Layouter class provides the Layouter.mark(Object) method.

The public methods of this class may be divided into two categories: A small number of primitive methods, as described above, and a host of convenience methods which simplify calling the primitive ones for often-used arguments. For instance a call to Layouter.beginC() is shorthand for begin(true,ind), where ind is the default indentation selected when the Layouter was constructed.

Most of the methods can throw an UnbalancedBlocksException , which indicates that the sequence of method calls was illegal, i.e. more blocks were ended than begun, the Layouter is closed before all blocks are ended, a break occurs outside of any block, etc.

Also, most methods can throw an java.io.IOException . This only happens if a called method in the backend throws an IOException, in which case that exception is passed through to the caller of the Layouter method.
author:
   Martin Giese
See Also:   Backend



Field Summary
final public static  intDEFAULT_INDENTATION
    
final public static  intDEFAULT_LINE_WIDTH
     = 80 : The line width for some of the convenience constructors.

Constructor Summary
public  Layouter(Backend back, int indentation)
     Construts a newly allocated Layouter which will send output to the given Backend and has the given default indentation.
public  Layouter(java.io.Writer writer)
     Convenience constructor for a Layouter with a WriterBackend . The line width is taken to be Layouter.DEFAULT_LINE_WIDTH , and the default indentation Layouter.DEFAULT_INDENTATION .
public  Layouter(java.io.Writer writer, int lineWidth)
     Convenience constructor for a Layouter with a WriterBackend . The default indentation is taken from Layouter.DEFAULT_INDENTATION .
public  Layouter(java.io.Writer writer, int lineWidth, int indentation)
     Convenience constructor for a Layouter with a WriterBackend .

Method Summary
public  Layouterbegin(boolean consistent, int indent)
     Begin a block.
public  Layouterbegin(boolean consistent)
     Begin a block with default indentation.
public  LayouterbeginC()
     Begin a consistent block.
public  LayouterbeginC(int indent)
     Begin a consistent block.
public  LayouterbeginI()
     Begin an inconsistent block.
public  LayouterbeginI(int indent)
     Begin an inconsistent block.
public  Layouterbrk(int width, int offset)
     Print a break.
public  Layouterbrk(int width)
     Print a break with zero offset.
public  Layouterbrk()
     Print a break with zero offset and width one.
public  voidclose()
     Close the Layouter.
public  Layouterend()
     Ends the innermost block.
public  Layouterflush()
     Output any information currently kept in buffers.
public  Layouterind(int width, int offset)
     Indent to a current indentation level if surrounding block is broken.
public  Layouterind()
     Indent with zero offset and zero width.
public  Layoutermark(Object o)
     This leads to a call of the Backend.mark(Object) method of the backend, when the material preceding the call to mark has been printed to the backend, including any inserted line breaks and indentation.
public  Layouternl()
     Print a break with zero offset and large width.
public  Layouterpre(String s)
     Layout prefromated text.
public  Layouterprint(String s)
     Output text material.

Field Detail
DEFAULT_INDENTATION
final public static int DEFAULT_INDENTATION(Code)
= 2 : The default indentation for some of the convenience constructors



DEFAULT_LINE_WIDTH
final public static int DEFAULT_LINE_WIDTH(Code)
= 80 : The line width for some of the convenience constructors.




Constructor Detail
Layouter
public Layouter(Backend back, int indentation)(Code)
Construts a newly allocated Layouter which will send output to the given Backend and has the given default indentation.
Parameters:
  backend - the Backend
Parameters:
  indentation - the default indentation



Layouter
public Layouter(java.io.Writer writer)(Code)
Convenience constructor for a Layouter with a WriterBackend . The line width is taken to be Layouter.DEFAULT_LINE_WIDTH , and the default indentation Layouter.DEFAULT_INDENTATION .
Parameters:
  writer - the java.io.Writer the Backend is going to use



Layouter
public Layouter(java.io.Writer writer, int lineWidth)(Code)
Convenience constructor for a Layouter with a WriterBackend . The default indentation is taken from Layouter.DEFAULT_INDENTATION .
Parameters:
  writer - the java.io.Writer the Backend is going to use
Parameters:
  lineWidth - the maximum lineWidth the Backend is going to use



Layouter
public Layouter(java.io.Writer writer, int lineWidth, int indentation)(Code)
Convenience constructor for a Layouter with a WriterBackend .
Parameters:
  writer - the java.io.Writer the Backend is going to use
Parameters:
  lineWidth - the maximum lineWidth the Backend is going to use
Parameters:
  indentation - the default indentation




Method Detail
begin
public Layouter begin(boolean consistent, int indent)(Code)
Begin a block. If consistent is set, breaks are either all broken or all not broken. The indentation level is increased by indent.
Parameters:
  consistent - true for consistent block
Parameters:
  indent - increment to indentation level this



begin
public Layouter begin(boolean consistent)(Code)
Begin a block with default indentation. Add this Layouter's default indentation to the indentation level.
Parameters:
  consistent - true for consistent block this



beginC
public Layouter beginC()(Code)
Begin a consistent block. Add this Layouter's default indentation to the indentation level. this



beginC
public Layouter beginC(int indent)(Code)
Begin a consistent block. Add indent to the indentation level.
Parameters:
  indent - the indentation for this block this



beginI
public Layouter beginI()(Code)
Begin an inconsistent block. Add this Layouter's default indentation to the indentation level. this



beginI
public Layouter beginI(int indent)(Code)
Begin an inconsistent block. Add indent to the indentation level.
Parameters:
  indent - the indentation for this block this



brk
public Layouter brk(int width, int offset) throws IOException(Code)
Print a break. This will print width spaces if the line is not broken at this point. If it is broken, indentation is added to the current indentation level, plus the value of offset.
Parameters:
  width - space to insert if not broken
Parameters:
  offset - offset relative to current indentation level this



brk
public Layouter brk(int width) throws IOException(Code)
Print a break with zero offset.
Parameters:
  width - space to insert if not broken this



brk
public Layouter brk() throws IOException(Code)
Print a break with zero offset and width one. this



close
public void close() throws IOException(Code)
Close the Layouter. No more methods should be called after this. All blocks begun must have been ended by this point. Any pending material is written to the backend, before the Backend.close method of the backend is called, which closes any open I/O streams, etc.



end
public Layouter end() throws IOException(Code)
Ends the innermost block. this



flush
public Layouter flush() throws IOException(Code)
Output any information currently kept in buffers. This is essentially passed on to the backend. Note that material in blocks begun but not ended cannot be forced to the output by this method. Finish all blocks and call flush or Layouter.close() then. this



ind
public Layouter ind(int width, int offset) throws IOException(Code)
Indent to a current indentation level if surrounding block is broken. If the surrounding block fits on one line, insert width spaces. Otherwise, indent to the current indentation level, plus offset, unless that position has already been exceeded on the current line. If that is the case, nothing is printed. No line break is possible at this point.
Parameters:
  width - space to insert if not broken
Parameters:
  offset - offset relative to current indentation level this



ind
public Layouter ind() throws IOException(Code)
Indent with zero offset and zero width. Just indents to the current indentation level if the block is broken, and prints nothing otherwise. this



mark
public Layouter mark(Object o)(Code)
This leads to a call of the Backend.mark(Object) method of the backend, when the material preceding the call to mark has been printed to the backend, including any inserted line breaks and indentation. The Object argument to mark is passed through unchanged to the backend and may be used by the application to pass information about the purpose of the mark.
Parameters:
  o - an object to be passed through to the backend.



nl
public Layouter nl() throws IOException(Code)
Print a break with zero offset and large width. As the large number of spaces will never fit into one line, this amounts to a forced line break. this



pre
public Layouter pre(String s) throws IOException(Code)
Layout prefromated text. This amounts to a (consistent) block with indentation 0, where each line of s (separated by \n) gets printed as a string and newlines become forced breaks.
Parameters:
  s - the pre-formatted string this



print
public Layouter print(String s) throws IOException(Code)
Output text material. The string s should not contain newline characters. If you have a string with newline characters, and want to retain its formatting, consider using the Layouter.pre(String s) method. The Layouter will not insert any line breaks in such a string.
Parameters:
  s - the String to print. this



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.