|
|
|
|
|
*/ |
|
/* |
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
|
* contributor license agreements. See the NOTICE file distributed with |
|
* this work for additional information regarding copyright ownership. |
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
|
* (the "License"); you may not use this file except in compliance with |
|
* the License. You may obtain a copy of the License at |
|
* |
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
* |
|
* Unless required by applicable law or agreed to in writing, software |
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
* See the License for the specific language governing permissions and |
|
* limitations under the License. |
|
*/ |
|
|
|
|
|
package com.sun.org.apache.xml.internal.serialize; |
|
|
|
|
|
import java.io.Writer; |
|
import java.io.StringWriter; |
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@Deprecated |
|
public class IndentPrinter |
|
extends Printer |
|
{ |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private StringBuffer _line; |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private StringBuffer _text; |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private int _spaces; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private int _thisIndent; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private int _nextIndent; |
|
|
|
|
|
public IndentPrinter( Writer writer, OutputFormat format) |
|
{ |
|
super( writer, format ); |
|
|
|
_line = new StringBuffer( 80 ); |
|
_text = new StringBuffer( 20 ); |
|
_spaces = 0; |
|
_thisIndent = _nextIndent = 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void enterDTD() |
|
{ |
|
// Can only enter DTD state once. Once we're out of DTD |
|
|
|
if ( _dtdWriter == null ) { |
|
_line.append( _text ); |
|
_text = new StringBuffer( 20 ); |
|
flushLine( false ); |
|
_dtdWriter = new StringWriter(); |
|
_docWriter = _writer; |
|
_writer = _dtdWriter; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String leaveDTD() |
|
{ |
|
|
|
if ( _writer == _dtdWriter ) { |
|
_line.append( _text ); |
|
_text = new StringBuffer( 20 ); |
|
flushLine( false ); |
|
_writer = _docWriter; |
|
return _dtdWriter.toString(); |
|
} else |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void printText( String text ) |
|
{ |
|
_text.append( text ); |
|
} |
|
|
|
|
|
public void printText( StringBuffer text ) |
|
{ |
|
_text.append( text.toString() ); |
|
} |
|
|
|
|
|
public void printText( char ch ) |
|
{ |
|
_text.append( ch ); |
|
} |
|
|
|
|
|
public void printText( char[] chars, int start, int length ) |
|
{ |
|
_text.append( chars, start, length ); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void printSpace() |
|
{ |
|
// The line consists of the text accumulated in _line, |
|
// followed by one or more spaces as counted by _spaces, |
|
// followed by more space accumulated in _text: |
|
// - Text is printed and accumulated into _text. |
|
// - A space is printed, so _text is added to _line and |
|
// a space is counted. |
|
// - More text is printed and accumulated into _text. |
|
// - A space is printed, the previous spaces are added |
|
// to _line, the _text is added to _line, and a new |
|
// space is counted. |
|
|
|
// If text was accumulated with printText(), then the space |
|
// means we have to move that text into the line and |
|
|
|
if ( _text.length() > 0 ) { |
|
// If the text breaks a line bounary, wrap to the next line. |
|
// The printed line size consists of the indentation we're going |
|
// to use next, the accumulated line so far, some spaces and the |
|
|
|
if ( _format.getLineWidth() > 0 && |
|
_thisIndent + _line.length() + _spaces + _text.length() > _format.getLineWidth() ) { |
|
flushLine( false ); |
|
try { |
|
|
|
_writer.write( _format.getLineSeparator() ); |
|
} catch ( IOException except ) { |
|
// We don't throw an exception, but hold it |
|
|
|
if ( _exception == null ) |
|
_exception = except; |
|
} |
|
} |
|
|
|
// Add as many spaces as we accumulaed before. |
|
|
|
while ( _spaces > 0 ) { |
|
_line.append( ' ' ); |
|
--_spaces; |
|
} |
|
_line.append( _text ); |
|
_text = new StringBuffer( 20 ); |
|
} |
|
// Starting a new word: accumulate the text between the line |
|
|
|
++_spaces; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void breakLine() |
|
{ |
|
breakLine( false ); |
|
} |
|
|
|
|
|
public void breakLine( boolean preserveSpace ) |
|
{ |
|
|
|
if ( _text.length() > 0 ) { |
|
while ( _spaces > 0 ) { |
|
_line.append( ' ' ); |
|
--_spaces; |
|
} |
|
_line.append( _text ); |
|
_text = new StringBuffer( 20 ); |
|
} |
|
flushLine( preserveSpace ); |
|
try { |
|
|
|
_writer.write( _format.getLineSeparator() ); |
|
} catch ( IOException except ) { |
|
// We don't throw an exception, but hold it |
|
|
|
if ( _exception == null ) |
|
_exception = except; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void flushLine( boolean preserveSpace ) |
|
{ |
|
int indent; |
|
|
|
if ( _line.length() > 0 ) { |
|
try { |
|
|
|
if ( _format.getIndenting() && ! preserveSpace ) { |
|
|
|
indent = _thisIndent; |
|
if ( ( 2 * indent ) > _format.getLineWidth() && _format.getLineWidth() > 0 ) |
|
indent = _format.getLineWidth() / 2; |
|
// Print the indentation as spaces and set the current |
|
|
|
while ( indent > 0 ) { |
|
_writer.write( ' ' ); |
|
--indent; |
|
} |
|
} |
|
_thisIndent = _nextIndent; |
|
|
|
// There is no need to print the spaces at the end of the line, |
|
// they are simply stripped and replaced with a single line |
|
|
|
_spaces = 0; |
|
_writer.write( _line.toString() ); |
|
|
|
_line = new StringBuffer( 40 ); |
|
} catch ( IOException except ) { |
|
// We don't throw an exception, but hold it |
|
|
|
if ( _exception == null ) |
|
_exception = except; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void flush() |
|
{ |
|
if ( _line.length() > 0 || _text.length() > 0 ) |
|
breakLine(); |
|
try { |
|
_writer.flush(); |
|
} catch ( IOException except ) { |
|
// We don't throw an exception, but hold it |
|
|
|
if ( _exception == null ) |
|
_exception = except; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public void indent() |
|
{ |
|
_nextIndent += _format.getIndent(); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public void unindent() |
|
{ |
|
_nextIndent -= _format.getIndent(); |
|
if ( _nextIndent < 0 ) |
|
_nextIndent = 0; |
|
// If there is no current line and we're de-identing then |
|
|
|
if ( ( _line.length() + _spaces + _text.length() ) == 0 ) |
|
_thisIndent = _nextIndent; |
|
} |
|
|
|
|
|
public int getNextIndent() |
|
{ |
|
return _nextIndent; |
|
} |
|
|
|
|
|
public void setNextIndent( int indent ) |
|
{ |
|
_nextIndent = indent; |
|
} |
|
|
|
|
|
public void setThisIndent( int indent ) |
|
{ |
|
_thisIndent = indent; |
|
} |
|
|
|
|
|
} |