|
|
|
|
|
*/ |
|
/* |
|
* 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. |
|
*/ |
|
|
|
|
|
// Sep 14, 2000: |
|
// Fixed serializer to report IO exception directly, instead at |
|
// the end of document processing. |
|
// Reported by Patrick Higgins <phiggins@transzap.com> |
|
|
|
|
|
package com.sun.org.apache.xml.internal.serialize; |
|
|
|
|
|
import java.io.Writer; |
|
import java.io.StringWriter; |
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@Deprecated |
|
public class Printer |
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected final OutputFormat _format; |
|
|
|
|
|
|
|
|
|
*/ |
|
protected Writer _writer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected StringWriter _dtdWriter; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected Writer _docWriter; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected IOException _exception; |
|
|
|
|
|
|
|
|
|
*/ |
|
private static final int BufferSize = 4096; |
|
|
|
|
|
|
|
|
|
*/ |
|
private final char[] _buffer = new char[ BufferSize ]; |
|
|
|
|
|
|
|
|
|
*/ |
|
private int _pos = 0; |
|
|
|
|
|
public Printer( Writer writer, OutputFormat format) |
|
{ |
|
_writer = writer; |
|
_format = format; |
|
_exception = null; |
|
_dtdWriter = null; |
|
_docWriter = null; |
|
_pos = 0; |
|
} |
|
|
|
|
|
public IOException getException() |
|
{ |
|
return _exception; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void enterDTD() |
|
throws IOException |
|
{ |
|
// Can only enter DTD state once. Once we're out of DTD |
|
|
|
if ( _dtdWriter == null ) { |
|
flushLine( false ); |
|
|
|
_dtdWriter = new StringWriter(); |
|
_docWriter = _writer; |
|
_writer = _dtdWriter; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String leaveDTD() |
|
throws IOException |
|
{ |
|
|
|
if ( _writer == _dtdWriter ) { |
|
flushLine( false ); |
|
|
|
_writer = _docWriter; |
|
return _dtdWriter.toString(); |
|
} else |
|
return null; |
|
} |
|
|
|
|
|
public void printText( String text ) |
|
throws IOException |
|
{ |
|
try { |
|
int length = text.length(); |
|
for ( int i = 0 ; i < length ; ++i ) { |
|
if ( _pos == BufferSize ) { |
|
_writer.write( _buffer ); |
|
_pos = 0; |
|
} |
|
_buffer[ _pos ] = text.charAt( i ); |
|
++_pos; |
|
} |
|
} catch ( IOException except ) { |
|
// We don't throw an exception, but hold it |
|
|
|
if ( _exception == null ) |
|
_exception = except; |
|
throw except; |
|
} |
|
} |
|
|
|
|
|
public void printText( StringBuffer text ) |
|
throws IOException |
|
{ |
|
try { |
|
int length = text.length(); |
|
for ( int i = 0 ; i < length ; ++i ) { |
|
if ( _pos == BufferSize ) { |
|
_writer.write( _buffer ); |
|
_pos = 0; |
|
} |
|
_buffer[ _pos ] = text.charAt( i ); |
|
++_pos; |
|
} |
|
} catch ( IOException except ) { |
|
// We don't throw an exception, but hold it |
|
|
|
if ( _exception == null ) |
|
_exception = except; |
|
throw except; |
|
} |
|
} |
|
|
|
|
|
public void printText( char[] chars, int start, int length ) |
|
throws IOException |
|
{ |
|
try { |
|
while ( length-- > 0 ) { |
|
if ( _pos == BufferSize ) { |
|
_writer.write( _buffer ); |
|
_pos = 0; |
|
} |
|
_buffer[ _pos ] = chars[ start ]; |
|
++start; |
|
++_pos; |
|
} |
|
} catch ( IOException except ) { |
|
// We don't throw an exception, but hold it |
|
|
|
if ( _exception == null ) |
|
_exception = except; |
|
throw except; |
|
} |
|
} |
|
|
|
|
|
public void printText( char ch ) |
|
throws IOException |
|
{ |
|
try { |
|
if ( _pos == BufferSize ) { |
|
_writer.write( _buffer ); |
|
_pos = 0; |
|
} |
|
_buffer[ _pos ] = ch; |
|
++_pos; |
|
} catch ( IOException except ) { |
|
// We don't throw an exception, but hold it |
|
|
|
if ( _exception == null ) |
|
_exception = except; |
|
throw except; |
|
} |
|
} |
|
|
|
|
|
public void printSpace() |
|
throws IOException |
|
{ |
|
try { |
|
if ( _pos == BufferSize ) { |
|
_writer.write( _buffer ); |
|
_pos = 0; |
|
} |
|
_buffer[ _pos ] = ' '; |
|
++_pos; |
|
} catch ( IOException except ) { |
|
// We don't throw an exception, but hold it |
|
|
|
if ( _exception == null ) |
|
_exception = except; |
|
throw except; |
|
} |
|
} |
|
|
|
|
|
public void breakLine() |
|
throws IOException |
|
{ |
|
try { |
|
if ( _pos == BufferSize ) { |
|
_writer.write( _buffer ); |
|
_pos = 0; |
|
} |
|
_buffer[ _pos ] = '\n'; |
|
++_pos; |
|
} catch ( IOException except ) { |
|
// We don't throw an exception, but hold it |
|
|
|
if ( _exception == null ) |
|
_exception = except; |
|
throw except; |
|
} |
|
} |
|
|
|
|
|
public void breakLine( boolean preserveSpace ) |
|
throws IOException |
|
{ |
|
breakLine(); |
|
} |
|
|
|
|
|
public void flushLine( boolean preserveSpace ) |
|
throws IOException |
|
{ |
|
|
|
try { |
|
_writer.write( _buffer, 0, _pos ); |
|
} catch ( IOException except ) { |
|
// We don't throw an exception, but hold it |
|
|
|
if ( _exception == null ) |
|
_exception = except; |
|
} |
|
_pos = 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void flush() |
|
throws IOException |
|
{ |
|
try { |
|
_writer.write( _buffer, 0, _pos ); |
|
_writer.flush(); |
|
} catch ( IOException except ) { |
|
// We don't throw an exception, but hold it |
|
|
|
if ( _exception == null ) |
|
_exception = except; |
|
throw except; |
|
} |
|
_pos = 0; |
|
} |
|
|
|
|
|
public void indent() |
|
{ |
|
// NOOP |
|
} |
|
|
|
|
|
public void unindent() |
|
{ |
|
// NOOP |
|
} |
|
|
|
|
|
public int getNextIndent() |
|
{ |
|
return 0; |
|
} |
|
|
|
|
|
public void setNextIndent( int indent ) |
|
{ |
|
} |
|
|
|
|
|
public void setThisIndent( int indent ) |
|
{ |
|
} |
|
|
|
|
|
} |