KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > n3 > IndentedWriter


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package com.hp.hpl.jena.n3;
7
8 import java.io.* ;
9 import com.hp.hpl.jena.JenaRuntime ;
10
11 /** Simple class that provides output with moving left margin.
12  * Does not cope with tabs or newlines in output strings.
13  *
14  * @author Andy Seaborne
15  * @version $Id: IndentedWriter.java,v 1.7 2005/02/21 12:04:04 andy_seaborne Exp $
16  */

17
18 // Not robust/complete enough for public use
19
/*public*/ class IndentedWriter //extends Writer
20
{
21     String JavaDoc lineSeparator = JenaRuntime.getLineSeparator() ;
22     
23     Writer writer ;
24     int column ;
25     int row ;
26     int currentIndent ;
27     
28     public IndentedWriter(Writer w)
29     {
30         writer = w ;
31         column = 0 ;
32         row = 0 ;
33         currentIndent = 0 ;
34     }
35
36     public Writer getWriter() { return writer ; }
37
38     public int getRow() { return row ; }
39     public int getCol() { return column ; }
40     public int getIndent() { return currentIndent ; }
41     
42     public void incIndent(int x) { currentIndent += x ; }
43     public void decIndent(int x) { currentIndent -= x ; }
44     public void setIndent(int x) { currentIndent = x ; }
45     
46     public void print(String JavaDoc s)
47     {
48         try { writer.write(s); column += s.length() ; }
49         catch (java.io.IOException JavaDoc ex) {}
50     }
51
52     public void println(String JavaDoc s)
53     {
54         try { writer.write(s); println() ; }
55         catch (java.io.IOException JavaDoc ex) { }
56     }
57     
58     public void println()
59     {
60         try {
61             writer.write(lineSeparator);
62             writer.flush() ;
63             column = 0 ;
64             row++ ;
65             padTo() ;
66         }
67         catch (java.io.IOException JavaDoc ex) { }
68     }
69     
70     public void padTo() throws IOException
71     {
72         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc() ;
73         for ( int i = 0 ; i < currentIndent ; i++ )
74             writer.write(' ') ;
75         column = column + currentIndent ;
76     }
77     
78     public void flush() { try { writer.flush() ; } catch (IOException ioEx) {} }
79     public void close() { try { writer.close() ; } catch (IOException ioEx) {} }
80
81 }
82
83
84 /*
85  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
86  * All rights reserved.
87  *
88  * Redistribution and use in source and binary forms, with or without
89  * modification, are permitted provided that the following conditions
90  * are met:
91  * 1. Redistributions of source code must retain the above copyright
92  * notice, this list of conditions and the following disclaimer.
93  * 2. Redistributions in binary form must reproduce the above copyright
94  * notice, this list of conditions and the following disclaimer in the
95  * documentation and/or other materials provided with the distribution.
96  * 3. The name of the author may not be used to endorse or promote products
97  * derived from this software without specific prior written permission.
98  *
99  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
100  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
101  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
102  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
103  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
104  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
105  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
106  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
107  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
108  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
109  */

110
Popular Tags