001: /*
002: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: package com.hp.hpl.jena.n3;
007:
008: import java.io.*;
009: import com.hp.hpl.jena.JenaRuntime;
010:
011: /** Simple class that provides output with moving left margin.
012: * Does not cope with tabs or newlines in output strings.
013: *
014: * @author Andy Seaborne
015: * @version $Id: IndentedWriter.java,v 1.10 2008/01/02 12:04:48 andy_seaborne Exp $
016: */
017:
018: // Not robust/complete enough for public use
019: /*public*/class IndentedWriter //extends Writer
020: {
021: String lineSeparator = JenaRuntime.getLineSeparator();
022:
023: Writer writer;
024: int column;
025: int row;
026: int currentIndent;
027:
028: public IndentedWriter(Writer w) {
029: writer = w;
030: column = 0;
031: row = 0;
032: currentIndent = 0;
033: }
034:
035: public Writer getWriter() {
036: return writer;
037: }
038:
039: public int getRow() {
040: return row;
041: }
042:
043: public int getCol() {
044: return column;
045: }
046:
047: public int getIndent() {
048: return currentIndent;
049: }
050:
051: public void incIndent(int x) {
052: currentIndent += x;
053: }
054:
055: public void decIndent(int x) {
056: currentIndent -= x;
057: }
058:
059: public void setIndent(int x) {
060: currentIndent = x;
061: }
062:
063: public void print(String s) {
064: try {
065: writer.write(s);
066: column += s.length();
067: } catch (java.io.IOException ex) {
068: }
069: }
070:
071: public void println(String s) {
072: try {
073: writer.write(s);
074: println();
075: } catch (java.io.IOException ex) {
076: }
077: }
078:
079: public void println() {
080: try {
081: writer.write(lineSeparator);
082: writer.flush();
083: column = 0;
084: row++;
085: padTo();
086: } catch (java.io.IOException ex) {
087: }
088: }
089:
090: public void padTo() throws IOException {
091: StringBuffer sBuff = new StringBuffer();
092: for (int i = 0; i < currentIndent; i++)
093: writer.write(' ');
094: column = column + currentIndent;
095: }
096:
097: public void flush() {
098: try {
099: writer.flush();
100: } catch (IOException ioEx) {
101: }
102: }
103:
104: public void close() {
105: try {
106: writer.close();
107: } catch (IOException ioEx) {
108: }
109: }
110:
111: }
112:
113: /*
114: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
115: * All rights reserved.
116: *
117: * Redistribution and use in source and binary forms, with or without
118: * modification, are permitted provided that the following conditions
119: * are met:
120: * 1. Redistributions of source code must retain the above copyright
121: * notice, this list of conditions and the following disclaimer.
122: * 2. Redistributions in binary form must reproduce the above copyright
123: * notice, this list of conditions and the following disclaimer in the
124: * documentation and/or other materials provided with the distribution.
125: * 3. The name of the author may not be used to endorse or promote products
126: * derived from this software without specific prior written permission.
127: *
128: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
129: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
130: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
131: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
132: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
133: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
134: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
135: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
136: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
137: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
138: */
|