You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by ar...@locus.apache.org on 2000/02/23 22:41:20 UTC
cvs commit: xml-xerces/java/src/org/apache/xml/serialize BaseMarkupSerializer.java HTMLSerializer.java HTMLdtd.java OutputFormat.java Serializer.java SerializerFactory.java SerializerFactoryImpl.java TextSerializer.java XHTMLSerializer.java XMLSerializer.java
arkin 00/02/23 13:41:20
Modified: java/src/org/apache/xml/serialize BaseMarkupSerializer.java
HTMLSerializer.java HTMLdtd.java OutputFormat.java
Serializer.java SerializerFactory.java
SerializerFactoryImpl.java TextSerializer.java
XHTMLSerializer.java XMLSerializer.java
Log:
Fixes to document type handling and support for DOM Level 2
Revision Changes Path
1.11 +743 -754 xml-xerces/java/src/org/apache/xml/serialize/BaseMarkupSerializer.java
Index: BaseMarkupSerializer.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/BaseMarkupSerializer.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- BaseMarkupSerializer.java 2000/02/16 00:58:09 1.10
+++ BaseMarkupSerializer.java 2000/02/23 21:41:18 1.11
@@ -121,8 +121,8 @@
*/
public abstract class BaseMarkupSerializer
implements ContentHandler, DocumentHandler, LexicalHandler,
- DTDHandler, DeclHandler,
- DOMSerializer, Serializer
+ DTDHandler, DeclHandler,
+ DOMSerializer, Serializer
{
@@ -250,7 +250,18 @@
protected Hashtable _prefixes;
+ /**
+ * The system identifier of the document type, if known.
+ */
+ protected String _docTypePublicId;
+
+
+ /**
+ * The system identifier of the document type, if known.
+ */
+ protected String _docTypeSystemId;
+
//--------------------------------//
// Constructor and initialization //
//--------------------------------//
@@ -263,75 +274,77 @@
*/
protected BaseMarkupSerializer()
{
- int i;
-
- _elementStates = new ElementState[ 10 ];
- for ( i = 0 ; i < _elementStates.length ; ++i )
- _elementStates[ i ] = new ElementState();
+ int i;
+
+ _elementStates = new ElementState[ 10 ];
+ for ( i = 0 ; i < _elementStates.length ; ++i )
+ _elementStates[ i ] = new ElementState();
}
public DocumentHandler asDocumentHandler()
{
- return this;
+ return this;
}
public ContentHandler asContentHandler()
{
- return this;
+ return this;
}
public DOMSerializer asDOMSerializer()
{
- return this;
+ return this;
}
public void setOutputByteStream( OutputStream output )
- throws UnsupportedEncodingException
+ throws UnsupportedEncodingException
{
- String encoding;
-
- if ( _format.getEncoding() == null )
- setOutputCharStream( new OutputStreamWriter( output ) );
- else
- setOutputCharStream( new OutputStreamWriter( output, _format.getEncoding() ) );
+ String encoding;
+
+ if ( _format.getEncoding() == null )
+ setOutputCharStream( new OutputStreamWriter( output ) );
+ else
+ setOutputCharStream( new OutputStreamWriter( output, _format.getEncoding() ) );
}
public void setOutputCharStream( Writer output )
{
- if ( output == null )
- throw new NullPointerException( "SER001 Argument 'output' is null." );
- _writer = output;
- reset();
+ if ( output == null )
+ throw new NullPointerException( "SER001 Argument 'output' is null." );
+ _writer = output;
+ reset();
}
-
-
+
+
public void setOutputFormat( OutputFormat format )
{
- if ( format == null )
- throw new NullPointerException( "SER001 Argument 'format' is null." );
- _format = format;
- // Determine the last printable character based on the output format
- _lastPrintable = _format.getLastPrintable();
- reset();
+ if ( format == null )
+ throw new NullPointerException( "SER001 Argument 'format' is null." );
+ _format = format;
+ // Determine the last printable character based on the output format
+ _lastPrintable = _format.getLastPrintable();
+ reset();
}
-
-
+
+
protected void reset()
{
- // Initialize everything for a first/second run.
- _line = new StringBuffer( 80 );
- _text = new StringBuffer( 20 );
- _spaces = 0;
- _thisIndent = _nextIndent = 0;
- _exception = null;
- _elementStateCount = 0;
- _started = false;
- _dtdWriter = null;
+ // Initialize everything for a first/second run.
+ _line = new StringBuffer( 80 );
+ _text = new StringBuffer( 20 );
+ _spaces = 0;
+ _thisIndent = _nextIndent = 0;
+ _exception = null;
+ _elementStateCount = 0;
+ _started = false;
+ _dtdWriter = null;
+ _docTypePublicId = _format.getDoctypePublic();
+ _docTypeSystemId = _format.getDoctypeSystem();
}
@@ -352,15 +365,15 @@
public void serialize( Element elem )
throws IOException
{
- if ( _writer == null )
- throw new IllegalStateException( "SER002 No writer supplied for serializer" );
- try {
- startDocument();
- } catch ( SAXException except ) { }
- serializeNode( elem );
- flush();
- if ( _exception != null )
- throw _exception;
+ if ( _writer == null )
+ throw new IllegalStateException( "SER002 No writer supplied for serializer" );
+ try {
+ startDocument();
+ } catch ( SAXException except ) { }
+ serializeNode( elem );
+ flush();
+ if ( _exception != null )
+ throw _exception;
}
@@ -376,17 +389,17 @@
public void serialize( DocumentFragment frag )
throws IOException
{
- if ( _writer == null )
- throw new IllegalStateException( "SER002 No writer supplied for serializer" );
- try {
- startDocument();
- } catch ( SAXException except ) { }
- serializeNode( frag );
- flush();
- if ( _exception != null )
- throw _exception;
+ if ( _writer == null )
+ throw new IllegalStateException( "SER002 No writer supplied for serializer" );
+ try {
+ startDocument();
+ } catch ( SAXException except ) { }
+ serializeNode( frag );
+ flush();
+ if ( _exception != null )
+ throw _exception;
}
-
+
/**
* Serializes the DOM document using the previously specified
@@ -400,18 +413,18 @@
public void serialize( Document doc )
throws IOException
{
- if ( _writer == null )
- throw new IllegalStateException( "SER002 No writer supplied for serializer" );
- try {
- startDocument();
- } catch ( SAXException except ) { }
- serializeNode( doc );
- serializePreRoot();
+ if ( _writer == null )
+ throw new IllegalStateException( "SER002 No writer supplied for serializer" );
+ try {
+ startDocument();
+ } catch ( SAXException except ) { }
+ serializeNode( doc );
+ serializePreRoot();
flush();
- if ( _exception != null )
- throw _exception;
+ if ( _exception != null )
+ throw _exception;
}
-
+
//------------------------------------------//
// SAX document handler serializing methods //
@@ -420,126 +433,126 @@
public void characters( char[] chars, int start, int length )
{
- characters( new String( chars, start, length ), false );
+ characters( new String( chars, start, length ), false );
}
-
+
public void ignorableWhitespace( char[] chars, int start, int length )
{
- int i;
-
- content();
-
- // Print ignorable whitespaces only when indenting, after
- // all they are indentation. Cancel the indentation to
- // not indent twice.
- if ( _format.getIndenting() ) {
- _thisIndent = 0;
- for ( i = start ; length-- > 0 ; ++i ) {
- if ( chars[ i ] == '\n' || chars[ i ] == '\r' )
- breakLine( true );
- else
- _text.append( chars[ i ] );
- }
- }
+ int i;
+
+ content();
+
+ // Print ignorable whitespaces only when indenting, after
+ // all they are indentation. Cancel the indentation to
+ // not indent twice.
+ if ( _format.getIndenting() ) {
+ _thisIndent = 0;
+ for ( i = start ; length-- > 0 ; ++i ) {
+ if ( chars[ i ] == '\n' || chars[ i ] == '\r' )
+ breakLine( true );
+ else
+ _text.append( chars[ i ] );
+ }
+ }
}
-
-
+
+
public void processingInstruction( String target, String code )
{
- int index;
- StringBuffer buffer;
- ElementState state;
-
- state = content();
- buffer = new StringBuffer( 40 );
-
- // Create the processing instruction textual representation.
- // Make sure we don't have '?>' inside either target or code.
- index = target.indexOf( "?>" );
- if ( index >= 0 )
- buffer.append( "<?" ).append( target.substring( 0, index ) );
- else
- buffer.append( "<?" ).append( target );
- if ( code != null ) {
- buffer.append( ' ' );
- index = code.indexOf( "?>" );
- if ( index >= 0 )
- buffer.append( code.substring( 0, index ) );
- else
- buffer.append( code );
- }
- buffer.append( "?>" );
-
- // If before the root element (or after it), do not print
- // the PI directly but place it in the pre-root vector.
- if ( state == null ) {
- if ( _preRoot == null )
- _preRoot = new Vector();
- _preRoot.addElement( buffer.toString() );
- }
- else {
- indent();
- printText( buffer, true );
- unindent();
- }
+ int index;
+ StringBuffer buffer;
+ ElementState state;
+
+ state = content();
+ buffer = new StringBuffer( 40 );
+
+ // Create the processing instruction textual representation.
+ // Make sure we don't have '?>' inside either target or code.
+ index = target.indexOf( "?>" );
+ if ( index >= 0 )
+ buffer.append( "<?" ).append( target.substring( 0, index ) );
+ else
+ buffer.append( "<?" ).append( target );
+ if ( code != null ) {
+ buffer.append( ' ' );
+ index = code.indexOf( "?>" );
+ if ( index >= 0 )
+ buffer.append( code.substring( 0, index ) );
+ else
+ buffer.append( code );
+ }
+ buffer.append( "?>" );
+
+ // If before the root element (or after it), do not print
+ // the PI directly but place it in the pre-root vector.
+ if ( state == null ) {
+ if ( _preRoot == null )
+ _preRoot = new Vector();
+ _preRoot.addElement( buffer.toString() );
+ }
+ else {
+ indent();
+ printText( buffer, true );
+ unindent();
+ }
}
-
-
+
+
public void comment( char[] chars, int start, int length )
{
- comment( new String( chars, start, length ) );
+ comment( new String( chars, start, length ) );
}
public void comment( String text )
{
- StringBuffer buffer;
- int index;
- ElementState state;
-
- state = content();
- buffer = new StringBuffer( 40 );
- // Create the processing comment textual representation.
- // Make sure we don't have '-->' inside the comment.
- index = text.indexOf( "-->" );
- if ( index >= 0 )
- buffer.append( "<!--" ).append( text.substring( 0, index ) ).append( "-->" );
- else
- buffer.append( "<!--" ).append( text ).append( "-->" );
-
- // If before the root element (or after it), do not print
- // the comment directly but place it in the pre-root vector.
- if ( state == null ) {
- if ( _preRoot == null )
- _preRoot = new Vector();
- _preRoot.addElement( buffer.toString() );
- }
- else {
- indent();
- printText( buffer, false );
- unindent();
- }
+ StringBuffer buffer;
+ int index;
+ ElementState state;
+
+ state = content();
+ buffer = new StringBuffer( 40 );
+ // Create the processing comment textual representation.
+ // Make sure we don't have '-->' inside the comment.
+ index = text.indexOf( "-->" );
+ if ( index >= 0 )
+ buffer.append( "<!--" ).append( text.substring( 0, index ) ).append( "-->" );
+ else
+ buffer.append( "<!--" ).append( text ).append( "-->" );
+
+ // If before the root element (or after it), do not print
+ // the comment directly but place it in the pre-root vector.
+ if ( state == null ) {
+ if ( _preRoot == null )
+ _preRoot = new Vector();
+ _preRoot.addElement( buffer.toString() );
+ }
+ else {
+ indent();
+ printText( buffer, false );
+ unindent();
+ }
}
public void startCDATA()
{
- ElementState state;
-
- state = getElementState();
- if ( state != null )
- state.doCData = true;
+ ElementState state;
+
+ state = getElementState();
+ if ( state != null )
+ state.doCData = true;
}
-
-
+
+
public void endCDATA()
{
- ElementState state;
-
- state = getElementState();
- if ( state != null )
- state.doCData = false;
+ ElementState state;
+
+ state = getElementState();
+ if ( state != null )
+ state.doCData = false;
}
@@ -554,33 +567,33 @@
public void endDocument()
throws SAXException
{
- // Print all the elements accumulated outside of
- // the root element.
- serializePreRoot();
- // Flush the output, this is necessary for buffered output.
+ // Print all the elements accumulated outside of
+ // the root element.
+ serializePreRoot();
+ // Flush the output, this is necessary for buffered output.
flush();
- // If an exception was thrown during serializing, this would
- // be the best time to report it.
- if ( _exception != null )
- throw new SAXException( _exception );
+ // If an exception was thrown during serializing, this would
+ // be the best time to report it.
+ if ( _exception != null )
+ throw new SAXException( _exception );
}
public void startEntity( String name )
{
- // ???
+ // ???
}
public void endEntity( String name )
{
- // ???
+ // ???
}
public void setDocumentLocator( Locator locator )
{
- // Nothing to do
+ // Nothing to do
}
@@ -590,28 +603,28 @@
public void skippedEntity ( String name )
- throws SAXException
+ throws SAXException
{
- endCDATA();
- content();
- printText( "&" + name + ";" );
+ endCDATA();
+ content();
+ printText( "&" + name + ";" );
}
-
+
public void startPrefixMapping( String prefix, String uri )
- throws SAXException
+ throws SAXException
{
- if ( _prefixes == null )
- _prefixes = new Hashtable();
- _prefixes.put( uri, prefix == null ? "" : prefix );
+ if ( _prefixes == null )
+ _prefixes = new Hashtable();
+ _prefixes.put( uri, prefix == null ? "" : prefix );
}
-
-
+
+
public void endPrefixMapping( String prefix )
- throws SAXException
+ throws SAXException
{
}
-
+
//---------------------------------------//
// SAX DTD/Decl handler serializing methods //
@@ -620,105 +633,104 @@
public void startDTD( String name, String publicId, String systemId )
{
- enterDTD();
- // For the moment this simply overrides any settings performed
- // on the output format.
- _format.setDoctype( publicId, systemId );
+ enterDTD();
+ _docTypePublicId = publicId;
+ _docTypeSystemId = systemId;
}
-
-
+
+
public void endDTD()
{
- // Nothing to do here, all the magic occurs in startDocument(String).
+ // Nothing to do here, all the magic occurs in startDocument(String).
}
-
-
+
+
public void elementDecl( String name, String model )
{
- enterDTD();
- printText( "<!ELEMENT " + name + " " + model + ">" );
- if ( _format.getIndenting() )
- breakLine();
+ enterDTD();
+ printText( "<!ELEMENT " + name + " " + model + ">" );
+ if ( _format.getIndenting() )
+ breakLine();
}
-
-
+
+
public void attributeDecl( String eName, String aName, String type,
- String valueDefault, String value )
+ String valueDefault, String value )
{
- StringBuffer buffer;
-
- enterDTD();
- buffer = new StringBuffer( 40 );
- buffer.append( "<!ATTLIST " ).append( eName ).append( ' ' );
- buffer.append( aName ).append( ' ' ).append( type );
- if ( valueDefault != null )
- buffer.append( ' ' ).append( valueDefault );
- if ( value != null )
- buffer.append( " \"" ).append( escape( value ) ).append( '"' );
- buffer.append( '>' );
- printText( buffer.toString() );
- if ( _format.getIndenting() )
- breakLine();
+ StringBuffer buffer;
+
+ enterDTD();
+ buffer = new StringBuffer( 40 );
+ buffer.append( "<!ATTLIST " ).append( eName ).append( ' ' );
+ buffer.append( aName ).append( ' ' ).append( type );
+ if ( valueDefault != null )
+ buffer.append( ' ' ).append( valueDefault );
+ if ( value != null )
+ buffer.append( " \"" ).append( escape( value ) ).append( '"' );
+ buffer.append( '>' );
+ printText( buffer.toString() );
+ if ( _format.getIndenting() )
+ breakLine();
}
-
-
+
+
public void internalEntityDecl( String name, String value )
{
- enterDTD();
- printText( "<!ENTITY " + name + " \"" + escape( value ) + "\">" );
- if ( _format.getIndenting() )
- breakLine();
+ enterDTD();
+ printText( "<!ENTITY " + name + " \"" + escape( value ) + "\">" );
+ if ( _format.getIndenting() )
+ breakLine();
}
-
-
+
+
public void externalEntityDecl( String name, String publicId, String systemId )
{
- enterDTD();
- unparsedEntityDecl( name, publicId, systemId, null );
+ enterDTD();
+ unparsedEntityDecl( name, publicId, systemId, null );
}
-
-
+
+
public void unparsedEntityDecl( String name, String publicId,
- String systemId, String notationName )
+ String systemId, String notationName )
{
- enterDTD();
- if ( publicId == null ) {
- printText( "<!ENTITY " + name + " SYSTEM " );
- printDoctypeURL( systemId );
- } else {
- printText( "<!ENTITY " + name + " PUBLIC " );
- printDoctypeURL( publicId );
- printText( " " );
- printDoctypeURL( systemId );
- }
- if ( notationName != null )
- printText( " NDATA " + notationName );
- printText( ">" );
- if ( _format.getIndenting() )
- breakLine();
+ enterDTD();
+ if ( publicId == null ) {
+ printText( "<!ENTITY " + name + " SYSTEM " );
+ printDoctypeURL( systemId );
+ } else {
+ printText( "<!ENTITY " + name + " PUBLIC " );
+ printDoctypeURL( publicId );
+ printText( " " );
+ printDoctypeURL( systemId );
+ }
+ if ( notationName != null )
+ printText( " NDATA " + notationName );
+ printText( ">" );
+ if ( _format.getIndenting() )
+ breakLine();
}
-
-
+
+
public void notationDecl( String name, String publicId, String systemId )
{
- enterDTD();
- if ( publicId != null ) {
- printText( "<!NOTATION " + name + " PUBLIC " );
- printDoctypeURL( publicId );
- if ( systemId != null ) {
- printText( " " );
- printDoctypeURL( systemId );
- }
- } else {
- printText( "<!NOTATION " + name + " SYSTEM " );
- printDoctypeURL( systemId );
- }
- printText( ">" );
- if ( _format.getIndenting() )
- breakLine();
+ enterDTD();
+ if ( publicId != null ) {
+ printText( "<!NOTATION " + name + " PUBLIC " );
+ printDoctypeURL( publicId );
+ if ( systemId != null ) {
+ printText( " " );
+ printDoctypeURL( systemId );
+ }
+ } else {
+ printText( "<!NOTATION " + name + " SYSTEM " );
+ printDoctypeURL( systemId );
+ }
+ printText( ">" );
+ if ( _format.getIndenting() )
+ breakLine();
}
-
-
+
+
/**
* Called by any of the DTD handlers to enter DTD mode.
* Once entered, all output will be accumulated in a string
@@ -729,19 +741,19 @@
*/
protected void enterDTD()
{
- // Can only enter DTD state once. Once we're out of DTD
- // state, can no longer re-enter it.
- if ( _dtdWriter == null ) {
- _line.append( _text );
- _text = new StringBuffer( 20 );
- flushLine( false );
- _dtdWriter = new StringWriter();
- _docWriter = _writer;
- _writer = _dtdWriter;
- }
+ // Can only enter DTD state once. Once we're out of DTD
+ // state, can no longer re-enter it.
+ if ( _dtdWriter == null ) {
+ _line.append( _text );
+ _text = new StringBuffer( 20 );
+ flushLine( false );
+ _dtdWriter = new StringWriter();
+ _docWriter = _writer;
+ _writer = _dtdWriter;
+ }
}
-
-
+
+
/**
* Called by the root element to leave DTD mode and if any
* DTD parts were printer, will return a string with their
@@ -749,18 +761,18 @@
*/
protected String leaveDTD()
{
- // Only works if we're going out of DTD mode.
- if ( _writer == _dtdWriter ) {
- _line.append( _text );
- _text = new StringBuffer( 20 );
- flushLine( false );
- _writer = _docWriter;
- return _dtdWriter.toString();
- } else
- return null;
+ // Only works if we're going out of DTD mode.
+ if ( _writer == _dtdWriter ) {
+ _line.append( _text );
+ _text = new StringBuffer( 20 );
+ flushLine( false );
+ _writer = _docWriter;
+ return _dtdWriter.toString();
+ } else
+ return null;
}
-
-
+
+
//------------------------------------------//
// Generic node serializing methods methods //
//------------------------------------------//
@@ -776,110 +788,87 @@
*/
protected void serializeNode( Node node )
{
- // Based on the node type call the suitable SAX handler.
- // Only comments entities and documents which are not
- // handled by SAX are serialized directly.
+ // Based on the node type call the suitable SAX handler.
+ // Only comments entities and documents which are not
+ // handled by SAX are serialized directly.
switch ( node.getNodeType() ) {
- case Node.TEXT_NODE :
- characters( node.getNodeValue(), false );
- break;
-
- case Node.CDATA_SECTION_NODE :
- startCDATA();
- characters( node.getNodeValue(), false );
- endCDATA();
- break;
-
- case Node.COMMENT_NODE :
- comment( node.getNodeValue() );
- break;
-
- case Node.ENTITY_REFERENCE_NODE : {
- Node child;
-
- endCDATA();
- content();
- child = node.getFirstChild();
- while ( child != null ) {
- serializeNode( child );
- child = child.getNextSibling();
- }
- break;
- }
-
-
- case Node.PROCESSING_INSTRUCTION_NODE :
- processingInstruction( node.getNodeName(), node.getNodeValue() );
- break;
-
- case Node.ELEMENT_NODE :
- serializeElement( (Element) node );
- break;
-
- case Node.DOCUMENT_NODE :
- DocumentType docType;
- NamedNodeMap map;
- Entity entity;
- Notation notation;
- int i;
-
- // If there is a document type, use the SAX events to
- // serialize it.
- docType = ( (Document) node ).getDoctype();
- if ( docType != null ) {
- startDTD( docType.getName(), docType.getPublicId(), docType.getSystemId() );
- /* This is only required for internal subset
- map = docType.getEntities();
- if ( map != null ) {
- for ( i = 0 ; i < map.getLength() ; ++i ) {
- entity = (Entity) map.item( i );
- if ( entity.getSystemId() == null && entity.getPublicId() == null ) {
- Node child;
-
- printText( "<!ENTITY " + entity.getNodeName() + " \"" );
- child = entity.getFirstChild();
- while ( child != null ) {
- serializeNode( child );
- child = child.getNextSibling();
- }
- printText( "\">" );
- } else {
- unparsedEntityDecl( entity.getNodeName(), entity.getPublicId(),
- entity.getSystemId(), entity.getNotationName() );
- }
- }
- }
- map = docType.getNotations();
- if ( map != null ) {
- for ( i = 0 ; i < map.getLength() ; ++i ) {
- notation = (Notation) map.item( i );
- notationDecl( notation.getNodeName(), notation.getPublicId(), notation.getSystemId() );
- }
- }
- */
- endDTD();
- }
- // !! Fall through
- case Node.DOCUMENT_FRAGMENT_NODE : {
- Node child;
-
- // By definition this will happen if the node is a document,
- // document fragment, etc. Just serialize its contents. It will
- // work well for other nodes that we do not know how to serialize.
- child = node.getFirstChild();
- while ( child != null ) {
- serializeNode( child );
- child = child.getNextSibling();
- }
- break;
- }
-
- default:
- break;
- }
+ case Node.TEXT_NODE :
+ characters( node.getNodeValue(), false );
+ break;
+
+ case Node.CDATA_SECTION_NODE :
+ startCDATA();
+ characters( node.getNodeValue(), false );
+ endCDATA();
+ break;
+
+ case Node.COMMENT_NODE :
+ comment( node.getNodeValue() );
+ break;
+
+ case Node.ENTITY_REFERENCE_NODE : {
+ Node child;
+
+ endCDATA();
+ content();
+ child = node.getFirstChild();
+ while ( child != null ) {
+ serializeNode( child );
+ child = child.getNextSibling();
+ }
+ break;
+ }
+
+ case Node.PROCESSING_INSTRUCTION_NODE :
+ processingInstruction( node.getNodeName(), node.getNodeValue() );
+ break;
+
+ case Node.ELEMENT_NODE :
+ serializeElement( (Element) node );
+ break;
+
+ case Node.DOCUMENT_NODE :
+ DocumentType docType;
+ DOMImplementation domImpl;
+ NamedNodeMap map;
+ Entity entity;
+ Notation notation;
+ int i;
+
+ // If there is a document type, use the SAX events to
+ // serialize it.
+ docType = ( (Document) node ).getDoctype();
+ domImpl = ( (Document) node ).getImplementation();
+ if ( docType != null && domImpl.hasFeature( "XML", "2.0" ) ) {
+ String internal;
+
+ startDTD( docType.getName(), docType.getPublicId(), docType.getSystemId() );
+ internal = docType.getInternalSubset();
+ if ( internal != null && internal.length() > 0 )
+ printText( internal, true );
+ endDTD();
+ }
+ // !! Fall through
+ case Node.DOCUMENT_FRAGMENT_NODE : {
+ Node child;
+
+ // By definition this will happen if the node is a document,
+ // document fragment, etc. Just serialize its contents. It will
+ // work well for other nodes that we do not know how to serialize.
+ child = node.getFirstChild();
+ while ( child != null ) {
+ serializeNode( child );
+ child = child.getNextSibling();
+ }
+ break;
+ }
+
+ default:
+ break;
+ }
}
-
-
+
+
/**
* Must be called by a method about to print any type of content.
* If the element was just opened, the opening tag is closed and
@@ -890,28 +879,28 @@
*/
protected ElementState content()
{
- ElementState state;
-
- state = getElementState();
- if ( state != null ) {
- // Need to close CData section first
- if ( state.inCData && ! state.doCData ) {
- printText( "]]>" );
- state.inCData = false;
- }
- // If this is the first content in the element,
- // change the state to not-empty and close the
- // opening element tag.
- if ( state.empty ) {
- printText( ">" );
- state.empty = false;
- }
- // Except for one content type, all of them
- // are not last element. That one content
- // type will take care of itself.
- state.afterElement = false;
- }
- return state;
+ ElementState state;
+
+ state = getElementState();
+ if ( state != null ) {
+ // Need to close CData section first
+ if ( state.inCData && ! state.doCData ) {
+ printText( "]]>" );
+ state.inCData = false;
+ }
+ // If this is the first content in the element,
+ // change the state to not-empty and close the
+ // opening element tag.
+ if ( state.empty ) {
+ printText( ">" );
+ state.empty = false;
+ }
+ // Except for one content type, all of them
+ // are not last element. That one content
+ // type will take care of itself.
+ state.afterElement = false;
+ }
+ return state;
}
@@ -927,72 +916,72 @@
*/
protected void characters( String text, boolean unescaped )
{
- ElementState state;
-
- state = content();
- // Check if text should be print as CDATA section or unescaped
- // based on elements listed in the output format (the element
- // state) or whether we are inside a CDATA section or entity.
- if ( state != null ) {
- unescaped = unescaped || state.unescaped;
- }
-
- if ( state != null && ( state.inCData || state.doCData ) ) {
- StringBuffer buffer;
- int index;
- int saveIndent;
-
- // Print a CDATA section. The text is not escaped, but ']]>'
- // appearing in the code must be identified and dealt with.
- // The contents of a text node is considered space preserving.
- buffer = new StringBuffer( text.length() );
- if ( ! state.inCData ) {
- buffer.append( "<![CDATA[" );
- state.inCData = true;
- }
- index = text.indexOf( "]]>" );
- while ( index >= 0 ) {
- buffer.append( text.substring( 0, index + 2 ) ).append( "]]><![CDATA[" );
- text = text.substring( index + 2 );
- index = text.indexOf( "]]>" );
- }
- buffer.append( text );
- saveIndent = _nextIndent;
- _nextIndent = 0;
- printText( buffer, true );
- _nextIndent = saveIndent;
-
- } else {
-
- int saveIndent;
-
- if ( unescaped ) {
- // If the text node of this element should be printed
- // unescaped, then cancel indentation and print it
- // directly without escaping.
- saveIndent = _nextIndent;
- _nextIndent = 0;
- printText( text, true );
- _nextIndent = saveIndent;
-
- } else if ( state != null && state.preserveSpace ) {
- // If preserving space then hold of indentation so no
- // excessive spaces are printed at line breaks, escape
- // the text content without replacing spaces and print
- // the text breaking only at line breaks.
- saveIndent = _nextIndent;
- _nextIndent = 0;
- printText( escape( text ), true );
- _nextIndent = saveIndent;
-
- } else {
- // This is the last, but the most common case of
- // printing without preserving spaces. If indentation was
- // requested, line will wrap at space boundaries.
- // All whitespaces will print as space characters.
- printText( escape( text ), false );
- }
- }
+ ElementState state;
+
+ state = content();
+ // Check if text should be print as CDATA section or unescaped
+ // based on elements listed in the output format (the element
+ // state) or whether we are inside a CDATA section or entity.
+ if ( state != null ) {
+ unescaped = unescaped || state.unescaped;
+ }
+
+ if ( state != null && ( state.inCData || state.doCData ) ) {
+ StringBuffer buffer;
+ int index;
+ int saveIndent;
+
+ // Print a CDATA section. The text is not escaped, but ']]>'
+ // appearing in the code must be identified and dealt with.
+ // The contents of a text node is considered space preserving.
+ buffer = new StringBuffer( text.length() );
+ if ( ! state.inCData ) {
+ buffer.append( "<![CDATA[" );
+ state.inCData = true;
+ }
+ index = text.indexOf( "]]>" );
+ while ( index >= 0 ) {
+ buffer.append( text.substring( 0, index + 2 ) ).append( "]]><![CDATA[" );
+ text = text.substring( index + 2 );
+ index = text.indexOf( "]]>" );
+ }
+ buffer.append( text );
+ saveIndent = _nextIndent;
+ _nextIndent = 0;
+ printText( buffer, true );
+ _nextIndent = saveIndent;
+
+ } else {
+
+ int saveIndent;
+
+ if ( unescaped ) {
+ // If the text node of this element should be printed
+ // unescaped, then cancel indentation and print it
+ // directly without escaping.
+ saveIndent = _nextIndent;
+ _nextIndent = 0;
+ printText( text, true );
+ _nextIndent = saveIndent;
+
+ } else if ( state != null && state.preserveSpace ) {
+ // If preserving space then hold of indentation so no
+ // excessive spaces are printed at line breaks, escape
+ // the text content without replacing spaces and print
+ // the text breaking only at line breaks.
+ saveIndent = _nextIndent;
+ _nextIndent = 0;
+ printText( escape( text ), true );
+ _nextIndent = saveIndent;
+
+ } else {
+ // This is the last, but the most common case of
+ // printing without preserving spaces. If indentation was
+ // requested, line will wrap at space boundaries.
+ // All whitespaces will print as space characters.
+ printText( escape( text ), false );
+ }
+ }
}
@@ -1005,7 +994,7 @@
* @return Character entity name, or null
*/
protected abstract String getEntityRef( char ch );
-
+
/**
* Called to serializee the DOM element. The element is serialized based on
@@ -1026,15 +1015,15 @@
*/
protected void serializePreRoot()
{
- int i;
-
- if ( _preRoot != null ) {
- for ( i = 0 ; i < _preRoot.size() ; ++i ) {
- printText( (String) _preRoot.elementAt( i ), true );
- breakLine();
- }
- _preRoot.removeAllElements();
- }
+ int i;
+
+ if ( _preRoot != null ) {
+ for ( i = 0 ; i < _preRoot.size() ; ++i ) {
+ printText( (String) _preRoot.elementAt( i ), true );
+ breakLine();
+ }
+ _preRoot.removeAllElements();
+ }
}
@@ -1054,17 +1043,17 @@
*/
protected final void printText( String text )
{
- // Add this text to the accumulated text which will not be
- // print until the next space break.
- _text.append( text );
+ // Add this text to the accumulated text which will not be
+ // print until the next space break.
+ _text.append( text );
}
-
-
+
+
protected final void printText( char[] chars, int start, int end )
{
- _text.append( chars, start, end );
+ _text.append( chars, start, end );
}
-
+
/**
* Called to print additional text with whitespace handling.
@@ -1080,75 +1069,75 @@
*/
protected final void printText( String text, boolean preserveSpace )
{
- int index;
- char ch;
-
+ int index;
+ char ch;
+
if ( preserveSpace ) {
- // Preserving spaces: the text must print exactly as it is,
- // without breaking when spaces appear in the text and without
- // consolidating spaces. If a line terminator is used, a line
- // break will occur.
- for ( index = 0 ; index < text.length() ; ++index ) {
- ch = text.charAt( index );
- if ( ch == '\n' || ch == '\r' )
- breakLine( true );
- else
- _text.append( ch );
- }
+ // Preserving spaces: the text must print exactly as it is,
+ // without breaking when spaces appear in the text and without
+ // consolidating spaces. If a line terminator is used, a line
+ // break will occur.
+ for ( index = 0 ; index < text.length() ; ++index ) {
+ ch = text.charAt( index );
+ if ( ch == '\n' || ch == '\r' )
+ breakLine( true );
+ else
+ _text.append( ch );
+ }
}
else
- {
- // Not preserving spaces: print one part at a time, and
- // use spaces between parts to break them into different
- // lines. Spaces at beginning of line will be stripped
- // by printing mechanism. Line terminator is treated
- // no different than other text part.
- for ( index = 0 ; index < text.length() ; ++index ) {
- ch = text.charAt( index );
- if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' )
- printSpace();
- else
- _text.append( ch );
- }
- }
+ {
+ // Not preserving spaces: print one part at a time, and
+ // use spaces between parts to break them into different
+ // lines. Spaces at beginning of line will be stripped
+ // by printing mechanism. Line terminator is treated
+ // no different than other text part.
+ for ( index = 0 ; index < text.length() ; ++index ) {
+ ch = text.charAt( index );
+ if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' )
+ printSpace();
+ else
+ _text.append( ch );
+ }
+ }
}
protected final void printText( StringBuffer text, boolean preserveSpace )
{
- int index;
- char ch;
-
+ int index;
+ char ch;
+
if ( preserveSpace ) {
- // Preserving spaces: the text must print exactly as it is,
- // without breaking when spaces appear in the text and without
- // consolidating spaces. If a line terminator is used, a line
- // break will occur.
- for ( index = 0 ; index < text.length() ; ++index ) {
- ch = text.charAt( index );
- if ( ch == '\n' || ch == '\r' )
- breakLine( true );
- else
- _text.append( ch );
- }
+ // Preserving spaces: the text must print exactly as it is,
+ // without breaking when spaces appear in the text and without
+ // consolidating spaces. If a line terminator is used, a line
+ // break will occur.
+ for ( index = 0 ; index < text.length() ; ++index ) {
+ ch = text.charAt( index );
+ if ( ch == '\n' || ch == '\r' )
+ breakLine( true );
+ else
+ _text.append( ch );
+ }
}
else
- {
- // Not preserving spaces: print one part at a time, and
- // use spaces between parts to break them into different
- // lines. Spaces at beginning of line will be stripped
- // by printing mechanism. Line terminator is treated
- // no different than other text part.
- for ( index = 0 ; index < text.length() ; ++index ) {
- ch = text.charAt( index );
- if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' )
- printSpace();
- else
- _text.append( ch );
- }
- }
+ {
+ // Not preserving spaces: print one part at a time, and
+ // use spaces between parts to break them into different
+ // lines. Spaces at beginning of line will be stripped
+ // by printing mechanism. Line terminator is treated
+ // no different than other text part.
+ for ( index = 0 ; index < text.length() ; ++index ) {
+ ch = text.charAt( index );
+ if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' )
+ printSpace();
+ else
+ _text.append( ch );
+ }
+ }
}
-
+
/**
* Called to print a single space between text parts that may be
@@ -1160,51 +1149,51 @@
*/
protected final 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
- // start accumulating new text with printText().
- 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
- // accumulated text so far.
- if ( _format.getLineWidth() > 0 &&
- _thisIndent + _line.length() + _spaces + _text.length() > _format.getLineWidth() ) {
- flushLine( false );
- try {
- // Print line and new line, then zero the line contents.
- _writer.write( _format.getLineSeparator() );
- } catch ( IOException except ) {
- // We don't throw an exception, but hold it
- // until the end of the document.
- if ( _exception == null )
- _exception = except;
- }
- }
-
- // Add as many spaces as we accumulaed before.
- // At the end of this loop, _spaces is zero.
- while ( _spaces > 0 ) {
- _line.append( ' ' );
- --_spaces;
- }
- _line.append( _text );
- _text = new StringBuffer( 20 );
- }
- // Starting a new word: accumulate the text between the line
- // and this new word; not a new word: just add another space.
- ++_spaces;
+ // 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
+ // start accumulating new text with printText().
+ 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
+ // accumulated text so far.
+ if ( _format.getLineWidth() > 0 &&
+ _thisIndent + _line.length() + _spaces + _text.length() > _format.getLineWidth() ) {
+ flushLine( false );
+ try {
+ // Print line and new line, then zero the line contents.
+ _writer.write( _format.getLineSeparator() );
+ } catch ( IOException except ) {
+ // We don't throw an exception, but hold it
+ // until the end of the document.
+ if ( _exception == null )
+ _exception = except;
+ }
+ }
+
+ // Add as many spaces as we accumulaed before.
+ // At the end of this loop, _spaces is zero.
+ while ( _spaces > 0 ) {
+ _line.append( ' ' );
+ --_spaces;
+ }
+ _line.append( _text );
+ _text = new StringBuffer( 20 );
+ }
+ // Starting a new word: accumulate the text between the line
+ // and this new word; not a new word: just add another space.
+ ++_spaces;
}
@@ -1217,32 +1206,32 @@
*/
protected final void breakLine()
{
- breakLine( false );
+ breakLine( false );
}
protected final void breakLine( boolean preserveSpace )
{
- // Equivalent to calling printSpace and forcing a flushLine.
- if ( _text.length() > 0 ) {
- while ( _spaces > 0 ) {
- _line.append( ' ' );
- --_spaces;
- }
- _line.append( _text );
- _text = new StringBuffer( 20 );
- }
+ // Equivalent to calling printSpace and forcing a flushLine.
+ if ( _text.length() > 0 ) {
+ while ( _spaces > 0 ) {
+ _line.append( ' ' );
+ --_spaces;
+ }
+ _line.append( _text );
+ _text = new StringBuffer( 20 );
+ }
flushLine( preserveSpace );
- try {
- // Print line and new line, then zero the line contents.
- _writer.write( _format.getLineSeparator() );
- } catch ( IOException except ) {
- // We don't throw an exception, but hold it
- // until the end of the document.
- if ( _exception == null )
- _exception = except;
- }
+ try {
+ // Print line and new line, then zero the line contents.
+ _writer.write( _format.getLineSeparator() );
+ } catch ( IOException except ) {
+ // We don't throw an exception, but hold it
+ // until the end of the document.
+ if ( _exception == null )
+ _exception = except;
+ }
}
-
+
/**
* Flushes the line accumulated so far to the writer and get ready
@@ -1254,57 +1243,57 @@
private void flushLine( boolean preserveSpace )
{
int indent;
-
- if ( _line.length() > 0 ) {
- try {
-
- if ( _format.getIndenting() && ! preserveSpace ) {
- // Make sure the indentation does not blow us away.
- indent = _thisIndent;
- if ( ( 2 * indent ) > _format.getLineWidth() && _format.getLineWidth() > 0 )
- indent = _format.getLineWidth() / 2;
- // Print the indentation as spaces and set the current
- // indentation to the next expected indentation.
- 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
- // separator.
- _spaces = 0;
- _writer.write( _line.toString() );
-
- _line = new StringBuffer( 40 );
- } catch ( IOException except ) {
- // We don't throw an exception, but hold it
- // until the end of the document.
- if ( _exception == null )
- _exception = except;
- }
- }
+
+ if ( _line.length() > 0 ) {
+ try {
+
+ if ( _format.getIndenting() && ! preserveSpace ) {
+ // Make sure the indentation does not blow us away.
+ indent = _thisIndent;
+ if ( ( 2 * indent ) > _format.getLineWidth() && _format.getLineWidth() > 0 )
+ indent = _format.getLineWidth() / 2;
+ // Print the indentation as spaces and set the current
+ // indentation to the next expected indentation.
+ 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
+ // separator.
+ _spaces = 0;
+ _writer.write( _line.toString() );
+
+ _line = new StringBuffer( 40 );
+ } catch ( IOException except ) {
+ // We don't throw an exception, but hold it
+ // until the end of the document.
+ if ( _exception == null )
+ _exception = except;
+ }
+ }
}
-
-
+
+
/**
* Flush the output stream. Must be called when done printing
* the document, otherwise some text might be buffered.
*/
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
- // until the end of the document.
- if ( _exception == null )
- _exception = except;
- }
+ if ( _line.length() > 0 || _text.length() > 0 )
+ breakLine();
+ try {
+ _writer.flush();
+ } catch ( IOException except ) {
+ // We don't throw an exception, but hold it
+ // until the end of the document.
+ if ( _exception == null )
+ _exception = except;
+ }
}
@@ -1313,7 +1302,7 @@
*/
protected void indent()
{
- _nextIndent += _format.getIndent();
+ _nextIndent += _format.getIndent();
}
@@ -1322,13 +1311,13 @@
*/
protected void unindent()
{
- _nextIndent -= _format.getIndent();
- if ( _nextIndent < 0 )
- _nextIndent = 0;
- // If there is no current line and we're de-identing then
- // this indentation level is actually the next level.
- if ( ( _line.length() + _spaces + _text.length() ) == 0 )
- _thisIndent = _nextIndent;
+ _nextIndent -= _format.getIndent();
+ if ( _nextIndent < 0 )
+ _nextIndent = 0;
+ // If there is no current line and we're de-identing then
+ // this indentation level is actually the next level.
+ if ( ( _line.length() + _spaces + _text.length() ) == 0 )
+ _thisIndent = _nextIndent;
}
@@ -1342,7 +1331,7 @@
protected void printDoctypeURL( String url )
{
int i;
-
+
_text.append( '"' );
for( i = 0 ; i < url.length() ; ++i ) {
if ( url.charAt( i ) == '"' || url.charAt( i ) < 0x20 || url.charAt( i ) > 0x7F )
@@ -1369,27 +1358,27 @@
int i;
char ch;
String charRef;
-
+
result = new StringBuffer( source.length() );
for ( i = 0 ; i < source.length() ; ++i ) {
ch = source.charAt( i );
- // If the character is not printable, print as character reference.
- // Non printables are below ASCII space but not tab or line
- // terminator, ASCII delete, or above a certain Unicode threshold.
- if ( ( ch < ' ' && ch != '\t' && ch != '\n' && ch != '\r' ) ||
- ch > _lastPrintable || ch == 0xF7 )
- result.append( "&#" ).append( Integer.toString( ch ) ).append( ';' );
- else {
- // If there is a suitable entity reference for this
- // character, print it. The list of available entity
- // references is almost but not identical between
- // XML and HTML.
- charRef = getEntityRef( ch );
- if ( charRef == null )
- result.append( ch );
- else
- result.append( '&' ).append( charRef ).append( ';' );
- }
+ // If the character is not printable, print as character reference.
+ // Non printables are below ASCII space but not tab or line
+ // terminator, ASCII delete, or above a certain Unicode threshold.
+ if ( ( ch < ' ' && ch != '\t' && ch != '\n' && ch != '\r' ) ||
+ ch > _lastPrintable || ch == 0xF7 )
+ result.append( "&#" ).append( Integer.toString( ch ) ).append( ';' );
+ else {
+ // If there is a suitable entity reference for this
+ // character, print it. The list of available entity
+ // references is almost but not identical between
+ // XML and HTML.
+ charRef = getEntityRef( ch );
+ if ( charRef == null )
+ result.append( ch );
+ else
+ result.append( '&' ).append( charRef ).append( ';' );
+ }
}
return result.toString();
}
@@ -1409,10 +1398,10 @@
*/
protected ElementState getElementState()
{
- if ( _elementStateCount == 0 )
- return null;
- else
- return _elementStates[ _elementStateCount - 1 ];
+ if ( _elementStateCount == 0 )
+ return null;
+ else
+ return _elementStates[ _elementStateCount - 1 ];
}
@@ -1424,38 +1413,38 @@
* @return Current element state, or null
*/
protected ElementState enterElementState( String namespaceURI, String localName,
- String rawName, boolean preserveSpace )
+ String rawName, boolean preserveSpace )
{
- ElementState state;
-
- if ( _elementStateCount == _elementStates.length ) {
- ElementState[] newStates;
-
- // Need to create a larger array of states.
- // This does not happen often, unless the document
- // is really deep.
- newStates = new ElementState[ _elementStates.length + 5 ];
- for ( int i = 0 ; i < _elementStates.length ; ++i )
- newStates[ i ] = _elementStates[ i ];
- _elementStates = newStates;
- for ( int i = _elementStateCount ; i < _elementStates.length ; ++i )
- _elementStates[ i ] = new ElementState();
- }
- state = _elementStates[ _elementStateCount ];
- state.namespaceURI = namespaceURI;
- state.localName = localName;
- state.rawName = rawName;
- state.preserveSpace = preserveSpace;
- state.empty = true;
- state.afterElement = false;
- state.doCData = state.inCData = false;
- state.prefixes = _prefixes;
- _prefixes = null;
- ++_elementStateCount;
- return state;
+ ElementState state;
+
+ if ( _elementStateCount == _elementStates.length ) {
+ ElementState[] newStates;
+
+ // Need to create a larger array of states.
+ // This does not happen often, unless the document
+ // is really deep.
+ newStates = new ElementState[ _elementStates.length + 5 ];
+ for ( int i = 0 ; i < _elementStates.length ; ++i )
+ newStates[ i ] = _elementStates[ i ];
+ _elementStates = newStates;
+ for ( int i = _elementStateCount ; i < _elementStates.length ; ++i )
+ _elementStates[ i ] = new ElementState();
+ }
+ state = _elementStates[ _elementStateCount ];
+ state.namespaceURI = namespaceURI;
+ state.localName = localName;
+ state.rawName = rawName;
+ state.preserveSpace = preserveSpace;
+ state.empty = true;
+ state.afterElement = false;
+ state.doCData = state.inCData = false;
+ state.prefixes = _prefixes;
+ _prefixes = null;
+ ++_elementStateCount;
+ return state;
}
-
-
+
+
/**
* Leave the current element state and return to the
* state of the parent element, or no state if this
@@ -1465,40 +1454,40 @@
*/
protected ElementState leaveElementState()
{
- if ( _elementStateCount > 1 ) {
- -- _elementStateCount;
- _prefixes = _elementStates[ _elementStateCount ].prefixes;
- return _elementStates[ _elementStateCount - 1 ];
- } else if ( _elementStateCount == 1 ) {
- -- _elementStateCount;
- _prefixes = _elementStates[ _elementStateCount ].prefixes;
- return null;
- } else
- return null;
+ if ( _elementStateCount > 1 ) {
+ -- _elementStateCount;
+ _prefixes = _elementStates[ _elementStateCount ].prefixes;
+ return _elementStates[ _elementStateCount - 1 ];
+ } else if ( _elementStateCount == 1 ) {
+ -- _elementStateCount;
+ _prefixes = _elementStates[ _elementStateCount ].prefixes;
+ return null;
+ } else
+ return null;
}
-
-
+
+
protected String getPrefix( String namespaceURI )
{
- String prefix;
-
- if ( _prefixes != null ) {
- prefix = (String) _prefixes.get( namespaceURI );
- if ( prefix != null )
- return prefix;
- }
- if ( _elementStateCount == 0 )
- return null;
- else {
- for ( int i = _elementStateCount ; i-- > 0 ; ) {
- if ( _elementStates[ i ].prefixes != null ) {
- prefix = (String) _elementStates[ i ].prefixes.get( namespaceURI );
+ String prefix;
+
+ if ( _prefixes != null ) {
+ prefix = (String) _prefixes.get( namespaceURI );
+ if ( prefix != null )
+ return prefix;
+ }
+ if ( _elementStateCount == 0 )
+ return null;
+ else {
+ for ( int i = _elementStateCount ; i-- > 0 ; ) {
+ if ( _elementStates[ i ].prefixes != null ) {
+ prefix = (String) _elementStates[ i ].prefixes.get( namespaceURI );
if ( prefix != null )
- return prefix;
- }
- }
- }
- return null;
+ return prefix;
+ }
+ }
+ }
+ return null;
}
1.10 +526 -532 xml-xerces/java/src/org/apache/xml/serialize/HTMLSerializer.java
Index: HTMLSerializer.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/HTMLSerializer.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- HTMLSerializer.java 2000/02/16 00:58:09 1.9
+++ HTMLSerializer.java 2000/02/23 21:41:18 1.10
@@ -138,8 +138,8 @@
*/
protected HTMLSerializer( boolean xhtml, OutputFormat format )
{
- this( format );
- _xhtml = xhtml;
+ this( format );
+ _xhtml = xhtml;
}
@@ -150,7 +150,7 @@
*/
public HTMLSerializer()
{
- setOutputFormat( null );
+ setOutputFormat( null );
}
@@ -161,7 +161,7 @@
*/
public HTMLSerializer( OutputFormat format )
{
- setOutputFormat( format );
+ setOutputFormat( format );
}
@@ -176,8 +176,8 @@
*/
public HTMLSerializer( Writer writer, OutputFormat format )
{
- setOutputFormat( format );
- setOutputCharStream( writer );
+ setOutputFormat( format );
+ setOutputCharStream( writer );
}
@@ -191,21 +191,21 @@
*/
public HTMLSerializer( OutputStream output, OutputFormat format )
{
- setOutputFormat( format );
- try {
- setOutputByteStream( output );
- } catch ( UnsupportedEncodingException except ) {
- // Should never happend
- }
+ setOutputFormat( format );
+ try {
+ setOutputByteStream( output );
+ } catch ( UnsupportedEncodingException except ) {
+ // Should never happend
+ }
}
-
-
+
+
public void setOutputFormat( OutputFormat format )
{
- if ( format == null )
- super.setOutputFormat( new OutputFormat( Method.HTML, null, false ) );
- else
- super.setOutputFormat( format );
+ if ( format == null )
+ super.setOutputFormat( new OutputFormat( Method.HTML, null, false ) );
+ else
+ super.setOutputFormat( format );
}
@@ -215,213 +215,212 @@
public void startElement( String namespaceURI, String localName,
- String rawName, Attributes attrs )
+ String rawName, Attributes attrs )
{
- int i;
- boolean preserveSpace;
- ElementState state;
- String name;
- String value;
- String htmlName;
- boolean addNSAttr = false;
-
- if ( _writer == null )
- throw new IllegalStateException( "SER002 No writer supplied for serializer" );
-
- state = getElementState();
- if ( state == null ) {
- // If this is the root element handle it differently.
- // If the first root element in the document, serialize
- // the document's DOCTYPE. Space preserving defaults
- // to that of the output format.
- if ( ! _started )
- startDocument( localName == null ? rawName : localName );
- preserveSpace = _format.getPreserveSpace();
- } else {
- // For any other element, if first in parent, then
- // close parent's opening tag and use the parnet's
- // space preserving.
- if ( state.empty )
- printText( ">" );
- preserveSpace = state.preserveSpace;
- // Indent this element on a new line if the first
- // content of the parent element or immediately
- // following an element.
- if ( _format.getIndenting() && ! state.preserveSpace &&
- ( state.empty || state.afterElement ) )
- breakLine();
- }
- // Do not change the current element state yet.
- // This only happens in endElement().
-
- if ( rawName == null ) {
- rawName = localName;
- if ( namespaceURI != null ) {
- String prefix;
- prefix = getPrefix( namespaceURI );
- if ( prefix.length() > 0 )
- rawName = prefix + ":" + localName;
- }
- addNSAttr = true;
- }
- if ( namespaceURI == null )
- htmlName = rawName;
- else {
- if ( namespaceURI.equals( XHTMLNamespace ) )
- htmlName = localName;
- else
- htmlName = null;
- }
-
-
- // XHTML: element names are lower case, DOM will be different
- if ( _xhtml )
- printText( '<' + rawName.toLowerCase() );
- else
- printText( '<' + rawName );
- indent();
-
- // For each attribute serialize it's name and value as one part,
- // separated with a space so the element can be broken on
- // multiple lines.
- if ( attrs != null ) {
- for ( i = 0 ; i < attrs.getLength() ; ++i ) {
- printSpace();
- name = attrs.getRawName( i ).toLowerCase();;
- value = attrs.getValue( i );
- if ( _xhtml || namespaceURI != null ) {
- // XHTML: print empty string for null values.
- if ( value == null )
- printText( name + "=\"\"" );
- else
- printText( name + "=\"" + escape( value ) + '"' );
- } else {
- // HTML: Empty values print as attribute name, no value.
- // HTML: URI attributes will print unescaped
- if ( value == null || value.length() == 0 )
- printText( name );
- else if ( HTMLdtd.isURI( rawName, name ) )
- printText( name + "=\"" + escapeURI( value ) + '"' );
- else if ( HTMLdtd.isBoolean( rawName, name ) )
- printText( name );
- else
- printText( name + "=\"" + escape( value ) + '"' );
- }
- }
- }
- if ( htmlName != null && HTMLdtd.isPreserveSpace( htmlName ) )
- preserveSpace = true;
-
- if ( addNSAttr ) {
- Enumeration enum;
-
- enum = _prefixes.keys();
- while ( enum.hasMoreElements() ) {
- printSpace();
- value = (String) enum.nextElement();
- name = (String) _prefixes.get( value );
- if ( name.length() == 0 )
- printText( "xmlns=\"" + value + '"' );
- else
- printText( "xmlns:" + name + "=\"" + value + '"' );
- }
- }
-
- // Now it's time to enter a new element state
- // with the tag name and space preserving.
- // We still do not change the curent element state.
- state = enterElementState( namespaceURI, localName, rawName, preserveSpace );
-
- // Prevents line breaks inside A/TD
-
- if ( htmlName != null && ( htmlName.equalsIgnoreCase( "A" ) ||
- htmlName.equalsIgnoreCase( "TD" ) ) ) {
- state.empty = false;
- printText( ">" );
- }
-
- // Handle SCRIPT and STYLE specifically by changing the
- // state of the current element to CDATA (XHTML) or
- // unescaped (HTML).
- if ( htmlName != null && ( rawName.equalsIgnoreCase( "SCRIPT" ) ||
- rawName.equalsIgnoreCase( "STYLE" ) ) ) {
- if ( _xhtml ) {
- // XHTML: Print contents as CDATA section
- state.doCData = true;
- } else {
- // HTML: Print contents unescaped
- state.unescaped = true;
- }
- }
+ int i;
+ boolean preserveSpace;
+ ElementState state;
+ String name;
+ String value;
+ String htmlName;
+ boolean addNSAttr = false;
+
+ if ( _writer == null )
+ throw new IllegalStateException( "SER002 No writer supplied for serializer" );
+
+ state = getElementState();
+ if ( state == null ) {
+ // If this is the root element handle it differently.
+ // If the first root element in the document, serialize
+ // the document's DOCTYPE. Space preserving defaults
+ // to that of the output format.
+ if ( ! _started )
+ startDocument( localName == null ? rawName : localName );
+ preserveSpace = _format.getPreserveSpace();
+ } else {
+ // For any other element, if first in parent, then
+ // close parent's opening tag and use the parnet's
+ // space preserving.
+ if ( state.empty )
+ printText( ">" );
+ preserveSpace = state.preserveSpace;
+ // Indent this element on a new line if the first
+ // content of the parent element or immediately
+ // following an element.
+ if ( _format.getIndenting() && ! state.preserveSpace &&
+ ( state.empty || state.afterElement ) )
+ breakLine();
+ }
+ // Do not change the current element state yet.
+ // This only happens in endElement().
+
+ if ( rawName == null ) {
+ rawName = localName;
+ if ( namespaceURI != null ) {
+ String prefix;
+ prefix = getPrefix( namespaceURI );
+ if ( prefix.length() > 0 )
+ rawName = prefix + ":" + localName;
+ }
+ addNSAttr = true;
+ }
+ if ( namespaceURI == null )
+ htmlName = rawName;
+ else {
+ if ( namespaceURI.equals( XHTMLNamespace ) )
+ htmlName = localName;
+ else
+ htmlName = null;
+ }
+
+ // XHTML: element names are lower case, DOM will be different
+ if ( _xhtml )
+ printText( '<' + rawName.toLowerCase() );
+ else
+ printText( '<' + rawName );
+ indent();
+
+ // For each attribute serialize it's name and value as one part,
+ // separated with a space so the element can be broken on
+ // multiple lines.
+ if ( attrs != null ) {
+ for ( i = 0 ; i < attrs.getLength() ; ++i ) {
+ printSpace();
+ name = attrs.getRawName( i ).toLowerCase();;
+ value = attrs.getValue( i );
+ if ( _xhtml || namespaceURI != null ) {
+ // XHTML: print empty string for null values.
+ if ( value == null )
+ printText( name + "=\"\"" );
+ else
+ printText( name + "=\"" + escape( value ) + '"' );
+ } else {
+ // HTML: Empty values print as attribute name, no value.
+ // HTML: URI attributes will print unescaped
+ if ( value == null || value.length() == 0 )
+ printText( name );
+ else if ( HTMLdtd.isURI( rawName, name ) )
+ printText( name + "=\"" + escapeURI( value ) + '"' );
+ else if ( HTMLdtd.isBoolean( rawName, name ) )
+ printText( name );
+ else
+ printText( name + "=\"" + escape( value ) + '"' );
+ }
+ }
+ }
+ if ( htmlName != null && HTMLdtd.isPreserveSpace( htmlName ) )
+ preserveSpace = true;
+
+ if ( addNSAttr ) {
+ Enumeration enum;
+
+ enum = _prefixes.keys();
+ while ( enum.hasMoreElements() ) {
+ printSpace();
+ value = (String) enum.nextElement();
+ name = (String) _prefixes.get( value );
+ if ( name.length() == 0 )
+ printText( "xmlns=\"" + value + '"' );
+ else
+ printText( "xmlns:" + name + "=\"" + value + '"' );
+ }
+ }
+
+ // Now it's time to enter a new element state
+ // with the tag name and space preserving.
+ // We still do not change the curent element state.
+ state = enterElementState( namespaceURI, localName, rawName, preserveSpace );
+
+ // Prevents line breaks inside A/TD
+
+ if ( htmlName != null && ( htmlName.equalsIgnoreCase( "A" ) ||
+ htmlName.equalsIgnoreCase( "TD" ) ) ) {
+ state.empty = false;
+ printText( ">" );
+ }
+
+ // Handle SCRIPT and STYLE specifically by changing the
+ // state of the current element to CDATA (XHTML) or
+ // unescaped (HTML).
+ if ( htmlName != null && ( rawName.equalsIgnoreCase( "SCRIPT" ) ||
+ rawName.equalsIgnoreCase( "STYLE" ) ) ) {
+ if ( _xhtml ) {
+ // XHTML: Print contents as CDATA section
+ state.doCData = true;
+ } else {
+ // HTML: Print contents unescaped
+ state.unescaped = true;
+ }
+ }
}
-
-
+
+
public void endElement( String namespaceURI, String localName,
- String rawName )
+ String rawName )
{
- ElementState state;
- String htmlName;
-
- // Works much like content() with additions for closing
- // an element. Note the different checks for the closed
- // element's state and the parent element's state.
- unindent();
- state = getElementState();
-
- if ( state.namespaceURI == null )
- htmlName = state.rawName;
- else {
- if ( state.namespaceURI.equals( XHTMLNamespace ) )
- htmlName = state.localName;
- else
- htmlName = null;
- }
-
- if ( _xhtml) {
- if ( state.empty ) {
- printText( " />" );
- } else {
- // Must leave CData section first
- if ( state.inCData )
- printText( "]]>" );
- // XHTML: element names are lower case, DOM will be different
- printText( "</" + state.rawName.toLowerCase() + ">" );
- }
- } else {
- if ( state.empty )
- printText( ">" );
- // This element is not empty and that last content was
- // another element, so print a line break before that
- // last element and this element's closing tag.
- // [keith] Provided this is not an anchor.
- // HTML: some elements do not print closing tag (e.g. LI)
- if ( htmlName == null || ! HTMLdtd.isOnlyOpening( htmlName ) ) {
- if ( _format.getIndenting() && ! state.preserveSpace && state.afterElement )
- breakLine();
- // Must leave CData section first (Illegal in HTML, but still)
- if ( state.inCData )
- printText( "]]>" );
- printText( "</" + state.rawName + ">" );
- }
- }
- // Leave the element state and update that of the parent
- // (if we're not root) to not empty and after element.
- state = leaveElementState();
- if ( state != null ) {
- // Temporary hack to prevent line breaks inside A/TD
- if ( htmlName == null || ( ! htmlName.equalsIgnoreCase( "A" ) &&
- ! htmlName.equalsIgnoreCase( "TD" ) ) )
-
- state.afterElement = true;
- state.empty = false;
- } else {
- // [keith] If we're done printing the document but don't
- // get to call endDocument(), the buffer should be flushed.
- flush();
- }
+ ElementState state;
+ String htmlName;
+
+ // Works much like content() with additions for closing
+ // an element. Note the different checks for the closed
+ // element's state and the parent element's state.
+ unindent();
+ state = getElementState();
+
+ if ( state.namespaceURI == null )
+ htmlName = state.rawName;
+ else {
+ if ( state.namespaceURI.equals( XHTMLNamespace ) )
+ htmlName = state.localName;
+ else
+ htmlName = null;
+ }
+
+ if ( _xhtml) {
+ if ( state.empty ) {
+ printText( " />" );
+ } else {
+ // Must leave CData section first
+ if ( state.inCData )
+ printText( "]]>" );
+ // XHTML: element names are lower case, DOM will be different
+ printText( "</" + state.rawName.toLowerCase() + ">" );
+ }
+ } else {
+ if ( state.empty )
+ printText( ">" );
+ // This element is not empty and that last content was
+ // another element, so print a line break before that
+ // last element and this element's closing tag.
+ // [keith] Provided this is not an anchor.
+ // HTML: some elements do not print closing tag (e.g. LI)
+ if ( htmlName == null || ! HTMLdtd.isOnlyOpening( htmlName ) ) {
+ if ( _format.getIndenting() && ! state.preserveSpace && state.afterElement )
+ breakLine();
+ // Must leave CData section first (Illegal in HTML, but still)
+ if ( state.inCData )
+ printText( "]]>" );
+ printText( "</" + state.rawName + ">" );
+ }
+ }
+ // Leave the element state and update that of the parent
+ // (if we're not root) to not empty and after element.
+ state = leaveElementState();
+ if ( state != null ) {
+ // Temporary hack to prevent line breaks inside A/TD
+ if ( htmlName == null || ( ! htmlName.equalsIgnoreCase( "A" ) &&
+ ! htmlName.equalsIgnoreCase( "TD" ) ) )
+
+ state.afterElement = true;
+ state.empty = false;
+ } else {
+ // [keith] If we're done printing the document but don't
+ // get to call endDocument(), the buffer should be flushed.
+ flush();
+ }
}
-
-
+
+
//------------------------------------------//
// SAX document handler serializing methods //
//------------------------------------------//
@@ -429,130 +428,130 @@
public void characters( char[] chars, int start, int length )
{
- ElementState state;
-
- // HTML: no CDATA section
- state = content();
- if ( state != null )
- state.doCData = false;
- super.characters( chars, start, length );
+ ElementState state;
+
+ // HTML: no CDATA section
+ state = content();
+ if ( state != null )
+ state.doCData = false;
+ super.characters( chars, start, length );
}
-
-
+
+
public void startDocument()
{
- // Do nothing for HTML/XHTML, browser might not respond
- // well to <?xml ...?>
- if ( _writer == null )
- throw new IllegalStateException( "SER002 No writer supplied for serializer" );
+ // Do nothing for HTML/XHTML, browser might not respond
+ // well to <?xml ...?>
+ if ( _writer == null )
+ throw new IllegalStateException( "SER002 No writer supplied for serializer" );
}
-
-
+
+
public void startElement( String tagName, AttributeList attrs )
{
- int i;
- boolean preserveSpace;
- ElementState state;
- String name;
- String value;
-
- if ( _writer == null )
- throw new IllegalStateException( "SER002 No writer supplied for serializer" );
-
- state = getElementState();
- if ( state == null ) {
- // If this is the root element handle it differently.
- // If the first root element in the document, serialize
- // the document's DOCTYPE. Space preserving defaults
- // to that of the output format.
- if ( ! _started )
- startDocument( tagName );
- preserveSpace = _format.getPreserveSpace();
- } else {
- // For any other element, if first in parent, then
- // close parent's opening tag and use the parnet's
- // space preserving.
- if ( state.empty )
- printText( ">" );
- preserveSpace = state.preserveSpace;
- // Indent this element on a new line if the first
- // content of the parent element or immediately
- // following an element.
- if ( _format.getIndenting() && ! state.preserveSpace &&
- ( state.empty || state.afterElement ) )
- breakLine();
- }
- // Do not change the current element state yet.
- // This only happens in endElement().
-
- // XHTML: element names are lower case, DOM will be different
- if ( _xhtml )
- printText( '<' + tagName.toLowerCase() );
- else
- printText( '<' + tagName );
- indent();
-
- // For each attribute serialize it's name and value as one part,
- // separated with a space so the element can be broken on
- // multiple lines.
- if ( attrs != null ) {
- for ( i = 0 ; i < attrs.getLength() ; ++i ) {
- printSpace();
- name = attrs.getName( i ).toLowerCase();;
- value = attrs.getValue( i );
- if ( _xhtml ) {
- // XHTML: print empty string for null values.
- if ( value == null )
- printText( name + "=\"\"" );
- else
- printText( name + "=\"" + escape( value ) + '"' );
- } else {
- // HTML: Empty values print as attribute name, no value.
- // HTML: URI attributes will print unescaped
- if ( value == null || value.length() == 0 )
- printText( name );
- else if ( HTMLdtd.isURI( tagName, name ) )
- printText( name + "=\"" + escapeURI( value ) + '"' );
- else if ( HTMLdtd.isBoolean( tagName, name ) )
- printText( name );
- else
- printText( name + "=\"" + escape( value ) + '"' );
- }
- }
- }
- if ( HTMLdtd.isPreserveSpace( tagName ) )
- preserveSpace = true;
-
- // Now it's time to enter a new element state
- // with the tag name and space preserving.
- // We still do not change the curent element state.
- state = enterElementState( null, null, tagName, preserveSpace );
-
- // Prevents line breaks inside A/TD
- if ( tagName.equalsIgnoreCase( "A" ) || tagName.equalsIgnoreCase( "TD" ) ) {
- state.empty = false;
- printText( ">" );
- }
-
- // Handle SCRIPT and STYLE specifically by changing the
- // state of the current element to CDATA (XHTML) or
- // unescaped (HTML).
- if ( tagName.equalsIgnoreCase( "SCRIPT" ) ||
- tagName.equalsIgnoreCase( "STYLE" ) ) {
- if ( _xhtml ) {
- // XHTML: Print contents as CDATA section
- state.doCData = true;
- } else {
- // HTML: Print contents unescaped
- state.unescaped = true;
- }
- }
+ int i;
+ boolean preserveSpace;
+ ElementState state;
+ String name;
+ String value;
+
+ if ( _writer == null )
+ throw new IllegalStateException( "SER002 No writer supplied for serializer" );
+
+ state = getElementState();
+ if ( state == null ) {
+ // If this is the root element handle it differently.
+ // If the first root element in the document, serialize
+ // the document's DOCTYPE. Space preserving defaults
+ // to that of the output format.
+ if ( ! _started )
+ startDocument( tagName );
+ preserveSpace = _format.getPreserveSpace();
+ } else {
+ // For any other element, if first in parent, then
+ // close parent's opening tag and use the parnet's
+ // space preserving.
+ if ( state.empty )
+ printText( ">" );
+ preserveSpace = state.preserveSpace;
+ // Indent this element on a new line if the first
+ // content of the parent element or immediately
+ // following an element.
+ if ( _format.getIndenting() && ! state.preserveSpace &&
+ ( state.empty || state.afterElement ) )
+ breakLine();
+ }
+ // Do not change the current element state yet.
+ // This only happens in endElement().
+
+ // XHTML: element names are lower case, DOM will be different
+ if ( _xhtml )
+ printText( '<' + tagName.toLowerCase() );
+ else
+ printText( '<' + tagName );
+ indent();
+
+ // For each attribute serialize it's name and value as one part,
+ // separated with a space so the element can be broken on
+ // multiple lines.
+ if ( attrs != null ) {
+ for ( i = 0 ; i < attrs.getLength() ; ++i ) {
+ printSpace();
+ name = attrs.getName( i ).toLowerCase();;
+ value = attrs.getValue( i );
+ if ( _xhtml ) {
+ // XHTML: print empty string for null values.
+ if ( value == null )
+ printText( name + "=\"\"" );
+ else
+ printText( name + "=\"" + escape( value ) + '"' );
+ } else {
+ // HTML: Empty values print as attribute name, no value.
+ // HTML: URI attributes will print unescaped
+ if ( value == null || value.length() == 0 )
+ printText( name );
+ else if ( HTMLdtd.isURI( tagName, name ) )
+ printText( name + "=\"" + escapeURI( value ) + '"' );
+ else if ( HTMLdtd.isBoolean( tagName, name ) )
+ printText( name );
+ else
+ printText( name + "=\"" + escape( value ) + '"' );
+ }
+ }
+ }
+ if ( HTMLdtd.isPreserveSpace( tagName ) )
+ preserveSpace = true;
+
+ // Now it's time to enter a new element state
+ // with the tag name and space preserving.
+ // We still do not change the curent element state.
+ state = enterElementState( null, null, tagName, preserveSpace );
+
+ // Prevents line breaks inside A/TD
+ if ( tagName.equalsIgnoreCase( "A" ) || tagName.equalsIgnoreCase( "TD" ) ) {
+ state.empty = false;
+ printText( ">" );
+ }
+
+ // Handle SCRIPT and STYLE specifically by changing the
+ // state of the current element to CDATA (XHTML) or
+ // unescaped (HTML).
+ if ( tagName.equalsIgnoreCase( "SCRIPT" ) ||
+ tagName.equalsIgnoreCase( "STYLE" ) ) {
+ if ( _xhtml ) {
+ // XHTML: Print contents as CDATA section
+ state.doCData = true;
+ } else {
+ // HTML: Print contents unescaped
+ state.unescaped = true;
+ }
+ }
}
-
-
+
+
public void endElement( String tagName )
{
- endElement( null, null, tagName );
+ endElement( null, null, tagName );
}
@@ -575,58 +574,53 @@
*/
protected void startDocument( String rootTagName )
{
- StringBuffer buffer;
- String publicId;
- String systemId;
-
- // Not supported in HTML/XHTML, but we still have to switch
- // out of DTD mode.
- leaveDTD();
- if ( ! _started ) {
- // If the public and system identifiers were not specified
- // in the output format, use the appropriate ones for HTML
- // or XHTML.
- publicId = _format.getDoctypePublic();
- systemId = _format.getDoctypeSystem();
- if ( publicId == null && systemId == null ) {
- if ( _xhtml ) {
- publicId = OutputFormat.DTD.XHTMLPublicId;
- systemId = OutputFormat.DTD.XHTMLSystemId;
- } else {
- publicId = OutputFormat.DTD.HTMLPublicId;
- systemId = OutputFormat.DTD.HTMLSystemId;
- }
- }
-
- // XHTML: If public idnentifier and system identifier
- // specified, print them, else print just system identifier
- // HTML: If public identifier specified, print it with
- // system identifier, if specified.
- if ( publicId != null && ( ! _xhtml || systemId != null ) ) {
- printText( "<!DOCTYPE HTML PUBLIC " );
- printDoctypeURL( publicId );
- if ( systemId != null ) {
- if ( _format.getIndenting() ) {
- breakLine();
- printText( " " );
- } else {
- printText( " " );
- }
- printDoctypeURL( systemId );
- }
- printText( ">" );
- breakLine();
- } else if ( systemId != null ) {
- printText( "<!DOCTYPE HTML SYSTEM " );
- printDoctypeURL( systemId );
- printText( ">" );
- breakLine();
- }
- }
-
- _started = true;
- // Always serialize these, even if not te first root element.
- serializePreRoot();
+ StringBuffer buffer;
+
+ // Not supported in HTML/XHTML, but we still have to switch
+ // out of DTD mode.
+ leaveDTD();
+ if ( ! _started ) {
+ // If the public and system identifiers were not specified
+ // in the output format, use the appropriate ones for HTML
+ // or XHTML.
+ if ( _docTypePublicId == null && _docTypeSystemId == null ) {
+ if ( _xhtml ) {
+ _docTypePublicId = OutputFormat.DTD.XHTMLPublicId;
+ _docTypeSystemId = OutputFormat.DTD.XHTMLSystemId;
+ } else {
+ _docTypePublicId = OutputFormat.DTD.HTMLPublicId;
+ _docTypeSystemId = OutputFormat.DTD.HTMLSystemId;
+ }
+ }
+
+ // XHTML: If public idnentifier and system identifier
+ // specified, print them, else print just system identifier
+ // HTML: If public identifier specified, print it with
+ // system identifier, if specified.
+ if ( _docTypePublicId != null && ( ! _xhtml || _docTypeSystemId != null ) ) {
+ printText( "<!DOCTYPE HTML PUBLIC " );
+ printDoctypeURL( _docTypePublicId );
+ if ( _docTypeSystemId != null ) {
+ if ( _format.getIndenting() ) {
+ breakLine();
+ printText( " " );
+ } else
+ printText( " " );
+ printDoctypeURL( _docTypeSystemId );
+ }
+ printText( ">" );
+ breakLine();
+ } else if ( _docTypeSystemId != null ) {
+ printText( "<!DOCTYPE HTML SYSTEM " );
+ printDoctypeURL( _docTypeSystemId );
+ printText( ">" );
+ breakLine();
+ }
+ }
+
+ _started = true;
+ // Always serialize these, even if not te first root element.
+ serializePreRoot();
}
@@ -637,149 +631,149 @@
*/
protected void serializeElement( Element elem )
{
- Attr attr;
- NamedNodeMap attrMap;
- int i;
- Node child;
- ElementState state;
- boolean preserveSpace;
- String name;
- String value;
- String tagName;
-
- tagName = elem.getTagName();
- state = getElementState();
- if ( state == null ) {
- // If this is the root element handle it differently.
- // If the first root element in the document, serialize
- // the document's DOCTYPE. Space preserving defaults
- // to that of the output format.
- if ( ! _started )
- startDocument( tagName );
- preserveSpace = _format.getPreserveSpace();
- } else {
- // For any other element, if first in parent, then
- // close parent's opening tag and use the parnet's
- // space preserving.
- if ( state.empty )
- printText( ">" );
- preserveSpace = state.preserveSpace;
- // Indent this element on a new line if the first
- // content of the parent element or immediately
- // following an element.
- if ( _format.getIndenting() && ! state.preserveSpace &&
- ( state.empty || state.afterElement ) )
- breakLine();
- }
- // Do not change the current element state yet.
- // This only happens in endElement().
-
- // XHTML: element names are lower case, DOM will be different
- if ( _xhtml )
- printText( '<' + tagName.toLowerCase() );
- else
- printText( '<' + tagName );
- indent();
-
- // Lookup the element's attribute, but only print specified
- // attributes. (Unspecified attributes are derived from the DTD.
- // For each attribute print it's name and value as one part,
- // separated with a space so the element can be broken on
- // multiple lines.
- attrMap = elem.getAttributes();
- if ( attrMap != null ) {
- for ( i = 0 ; i < attrMap.getLength() ; ++i ) {
- attr = (Attr) attrMap.item( i );
- name = attr.getName().toLowerCase();
- value = attr.getValue();
- if ( attr.getSpecified() ) {
- printSpace();
- if ( _xhtml ) {
- // XHTML: print empty string for null values.
- if ( value == null )
- printText( name + "=\"\"" );
- else
- printText( name + "=\"" + escape( value ) + '"' );
- } else {
- // HTML: Empty values print as attribute name, no value.
- // HTML: URI attributes will print unescaped
- if ( value == null || value.length() == 0 )
- printText( name );
- else if ( HTMLdtd.isURI( tagName, name ) )
- printText( name + "=\"" + escapeURI( value ) + '"' );
- else if ( HTMLdtd.isBoolean( tagName, name ) )
- printText( name );
- else
- printText( name + "=\"" + escape( value ) + '"' );
- }
- }
- }
- }
- if ( HTMLdtd.isPreserveSpace( tagName ) )
- preserveSpace = true;
-
- // If element has children, or if element is not an empty tag,
- // serialize an opening tag.
- if ( elem.hasChildNodes() || ! HTMLdtd.isEmptyTag( tagName ) ) {
- // Enter an element state, and serialize the children
- // one by one. Finally, end the element.
- state = enterElementState( null, null, tagName, preserveSpace );
-
- // Prevents line breaks inside A/TD
- if ( tagName.equalsIgnoreCase( "A" ) || tagName.equalsIgnoreCase( "TD" ) ) {
- state.empty = false;
- printText( ">" );
- }
-
- // Handle SCRIPT and STYLE specifically by changing the
- // state of the current element to CDATA (XHTML) or
- // unescaped (HTML).
- if ( tagName.equalsIgnoreCase( "SCRIPT" ) ||
- tagName.equalsIgnoreCase( "STYLE" ) ) {
- if ( _xhtml ) {
- // XHTML: Print contents as CDATA section
- state.doCData = true;
- } else {
- // HTML: Print contents unescaped
- state.unescaped = true;
- }
- }
- child = elem.getFirstChild();
- while ( child != null ) {
- serializeNode( child );
- child = child.getNextSibling();
- }
- endElement( null, null, tagName );
- } else {
- unindent();
- // XHTML: Close empty tag with ' />' so it's XML and HTML compatible.
- // HTML: Empty tags are defined as such in DTD no in document.
- if ( _xhtml )
- printText( " />" );
- else
- printText( ">" );
- if ( state != null ) {
- // After element but parent element is no longer empty.
- state.afterElement = true;
- state.empty = false;
- }
- }
+ Attr attr;
+ NamedNodeMap attrMap;
+ int i;
+ Node child;
+ ElementState state;
+ boolean preserveSpace;
+ String name;
+ String value;
+ String tagName;
+
+ tagName = elem.getTagName();
+ state = getElementState();
+ if ( state == null ) {
+ // If this is the root element handle it differently.
+ // If the first root element in the document, serialize
+ // the document's DOCTYPE. Space preserving defaults
+ // to that of the output format.
+ if ( ! _started )
+ startDocument( tagName );
+ preserveSpace = _format.getPreserveSpace();
+ } else {
+ // For any other element, if first in parent, then
+ // close parent's opening tag and use the parnet's
+ // space preserving.
+ if ( state.empty )
+ printText( ">" );
+ preserveSpace = state.preserveSpace;
+ // Indent this element on a new line if the first
+ // content of the parent element or immediately
+ // following an element.
+ if ( _format.getIndenting() && ! state.preserveSpace &&
+ ( state.empty || state.afterElement ) )
+ breakLine();
+ }
+ // Do not change the current element state yet.
+ // This only happens in endElement().
+
+ // XHTML: element names are lower case, DOM will be different
+ if ( _xhtml )
+ printText( '<' + tagName.toLowerCase() );
+ else
+ printText( '<' + tagName );
+ indent();
+
+ // Lookup the element's attribute, but only print specified
+ // attributes. (Unspecified attributes are derived from the DTD.
+ // For each attribute print it's name and value as one part,
+ // separated with a space so the element can be broken on
+ // multiple lines.
+ attrMap = elem.getAttributes();
+ if ( attrMap != null ) {
+ for ( i = 0 ; i < attrMap.getLength() ; ++i ) {
+ attr = (Attr) attrMap.item( i );
+ name = attr.getName().toLowerCase();
+ value = attr.getValue();
+ if ( attr.getSpecified() ) {
+ printSpace();
+ if ( _xhtml ) {
+ // XHTML: print empty string for null values.
+ if ( value == null )
+ printText( name + "=\"\"" );
+ else
+ printText( name + "=\"" + escape( value ) + '"' );
+ } else {
+ // HTML: Empty values print as attribute name, no value.
+ // HTML: URI attributes will print unescaped
+ if ( value == null || value.length() == 0 )
+ printText( name );
+ else if ( HTMLdtd.isURI( tagName, name ) )
+ printText( name + "=\"" + escapeURI( value ) + '"' );
+ else if ( HTMLdtd.isBoolean( tagName, name ) )
+ printText( name );
+ else
+ printText( name + "=\"" + escape( value ) + '"' );
+ }
+ }
+ }
+ }
+ if ( HTMLdtd.isPreserveSpace( tagName ) )
+ preserveSpace = true;
+
+ // If element has children, or if element is not an empty tag,
+ // serialize an opening tag.
+ if ( elem.hasChildNodes() || ! HTMLdtd.isEmptyTag( tagName ) ) {
+ // Enter an element state, and serialize the children
+ // one by one. Finally, end the element.
+ state = enterElementState( null, null, tagName, preserveSpace );
+
+ // Prevents line breaks inside A/TD
+ if ( tagName.equalsIgnoreCase( "A" ) || tagName.equalsIgnoreCase( "TD" ) ) {
+ state.empty = false;
+ printText( ">" );
+ }
+
+ // Handle SCRIPT and STYLE specifically by changing the
+ // state of the current element to CDATA (XHTML) or
+ // unescaped (HTML).
+ if ( tagName.equalsIgnoreCase( "SCRIPT" ) ||
+ tagName.equalsIgnoreCase( "STYLE" ) ) {
+ if ( _xhtml ) {
+ // XHTML: Print contents as CDATA section
+ state.doCData = true;
+ } else {
+ // HTML: Print contents unescaped
+ state.unescaped = true;
+ }
+ }
+ child = elem.getFirstChild();
+ while ( child != null ) {
+ serializeNode( child );
+ child = child.getNextSibling();
+ }
+ endElement( null, null, tagName );
+ } else {
+ unindent();
+ // XHTML: Close empty tag with ' />' so it's XML and HTML compatible.
+ // HTML: Empty tags are defined as such in DTD no in document.
+ if ( _xhtml )
+ printText( " />" );
+ else
+ printText( ">" );
+ if ( state != null ) {
+ // After element but parent element is no longer empty.
+ state.afterElement = true;
+ state.empty = false;
+ }
+ }
}
protected void characters( String text, boolean unescaped )
{
- ElementState state;
-
- // HTML: no CDATA section
- state = content();
- if ( state != null )
- state.doCData = false;
- super.characters( text, unescaped );
+ ElementState state;
+
+ // HTML: no CDATA section
+ state = content();
+ if ( state != null )
+ state.doCData = false;
+ super.characters( text, unescaped );
}
-
-
+
+
protected String getEntityRef( char ch )
{
return HTMLdtd.fromChar( ch );
@@ -788,15 +782,15 @@
protected String escapeURI( String uri )
{
- int index;
-
- // XXX Apparently Netscape doesn't like if we escape the URI
- // using %nn, so we leave it as is, just remove any quotes.
- index = uri.indexOf( "\"" );
- if ( index >= 0 )
- return uri.substring( 0, index );
- else
- return uri;
+ int index;
+
+ // XXX Apparently Netscape doesn't like if we escape the URI
+ // using %nn, so we leave it as is, just remove any quotes.
+ index = uri.indexOf( "\"" );
+ if ( index >= 0 )
+ return uri.substring( 0, index );
+ else
+ return uri;
}
1.7 +154 -163 xml-xerces/java/src/org/apache/xml/serialize/HTMLdtd.java
Index: HTMLdtd.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/HTMLdtd.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- HTMLdtd.java 2000/02/08 01:20:23 1.6
+++ HTMLdtd.java 2000/02/23 21:41:18 1.7
@@ -123,36 +123,43 @@
*/
private static final int ELEM_CONTENT = 0x0002;
+
/**
* Element preserve spaces.
*/
private static final int PRESERVE = 0x0004;
+
/**
* Optional closing tag.
*/
private static final int OPT_CLOSING = 0x0008;
+
/**
* Element is empty (also means only opening tag)
*/
private static final int EMPTY = 0x0010 | ONLY_OPENING;
+
/**
* Allowed to appear in head.
*/
private static final int ALLOWED_HEAD = 0x0020;
+
/**
* When opened, closes P.
*/
private static final int CLOSE_P = 0x0040;
+
/**
* When opened, closes DD or DT.
*/
private static final int CLOSE_DD_DT = 0x0080;
+
/**
* When opened, closes itself.
*/
@@ -164,14 +171,13 @@
*/
private static final int CLOSE_TABLE = 0x0200;
+
/**
* When opened, closes TH or TD.
*/
private static final int CLOSE_TH_TD = 0x04000;
-
-
/**
* Returns true if element is declared to be empty. HTML elements are
* defines as empty in the DTD, not by the document syntax.
@@ -181,7 +187,7 @@
*/
public static boolean isEmptyTag( String tagName )
{
- return isElement( tagName, EMPTY );
+ return isElement( tagName, EMPTY );
}
@@ -195,7 +201,7 @@
*/
public static boolean isElementContent( String tagName )
{
- return isElement( tagName, ELEM_CONTENT );
+ return isElement( tagName, ELEM_CONTENT );
}
@@ -209,7 +215,7 @@
*/
public static boolean isPreserveSpace( String tagName )
{
- return isElement( tagName, PRESERVE );
+ return isElement( tagName, PRESERVE );
}
@@ -223,7 +229,7 @@
*/
public static boolean isOptionalClosing( String tagName )
{
- return isElement( tagName, OPT_CLOSING );
+ return isElement( tagName, OPT_CLOSING );
}
@@ -236,7 +242,7 @@
*/
public static boolean isOnlyOpening( String tagName )
{
- return isElement( tagName, ONLY_OPENING );
+ return isElement( tagName, ONLY_OPENING );
}
@@ -252,30 +258,30 @@
*/
public static boolean isClosing( String tagName, String openTag )
{
- // Several elements are defined as closing the HEAD
+ // Several elements are defined as closing the HEAD
if ( openTag.equalsIgnoreCase( "HEAD" ) )
- return ! isElement( tagName, ALLOWED_HEAD );
- // P closes iteself
+ return ! isElement( tagName, ALLOWED_HEAD );
+ // P closes iteself
if ( openTag.equalsIgnoreCase( "P" ) )
- return isElement( tagName, CLOSE_P );
- // DT closes DD, DD closes DT
+ return isElement( tagName, CLOSE_P );
+ // DT closes DD, DD closes DT
if ( openTag.equalsIgnoreCase( "DT" ) || openTag.equalsIgnoreCase( "DD" ) )
- return isElement( tagName, CLOSE_DD_DT );
- // LI and OPTION close themselves
+ return isElement( tagName, CLOSE_DD_DT );
+ // LI and OPTION close themselves
if ( openTag.equalsIgnoreCase( "LI" ) || openTag.equalsIgnoreCase( "OPTION" ) )
- return isElement( tagName, CLOSE_SELF );
- // Each of these table sections closes all the others
+ return isElement( tagName, CLOSE_SELF );
+ // Each of these table sections closes all the others
if ( openTag.equalsIgnoreCase( "THEAD" ) || openTag.equalsIgnoreCase( "TFOOT" ) ||
openTag.equalsIgnoreCase( "TBODY" ) || openTag.equalsIgnoreCase( "TR" ) ||
openTag.equalsIgnoreCase( "COLGROUP" ) )
- return isElement( tagName, CLOSE_TABLE );
- // TD closes TH and TH closes TD
+ return isElement( tagName, CLOSE_TABLE );
+ // TD closes TH and TH closes TD
if ( openTag.equalsIgnoreCase( "TH" ) || openTag.equalsIgnoreCase( "TD" ) )
- return isElement( tagName, CLOSE_TH_TD );
+ return isElement( tagName, CLOSE_TH_TD );
return false;
}
-
-
+
+
/**
* Returns true if the specified attribute it a URI and should be
* escaped appropriately. In HTML URIs are escaped differently
@@ -286,8 +292,8 @@
*/
public static boolean isURI( String tagName, String attrName )
{
- // Stupid checks.
- return ( attrName.equalsIgnoreCase( "href" ) || attrName.equalsIgnoreCase( "src" ) );
+ // Stupid checks.
+ return ( attrName.equalsIgnoreCase( "href" ) || attrName.equalsIgnoreCase( "src" ) );
}
@@ -301,15 +307,15 @@
*/
public static boolean isBoolean( String tagName, String attrName )
{
- String[] attrNames;
-
- attrNames = (String[]) _boolAttrs.get( tagName.toUpperCase() );
- if ( attrNames == null )
- return false;
- for ( int i = 0 ; i < attrNames.length ; ++i )
- if ( attrNames[ i ].equalsIgnoreCase( attrName ) )
- return true;
- return false;
+ String[] attrNames;
+
+ attrNames = (String[]) _boolAttrs.get( tagName.toUpperCase() );
+ if ( attrNames == null )
+ return false;
+ for ( int i = 0 ; i < attrNames.length ; ++i )
+ if ( attrNames[ i ].equalsIgnoreCase( attrName ) )
+ return true;
+ return false;
}
@@ -324,7 +330,7 @@
public static int charFromName( String name )
{
Object value;
-
+
initialize();
value = _byName.get( name );
if ( value != null && value instanceof Character )
@@ -332,8 +338,8 @@
else
return -1;
}
-
-
+
+
/**
* Returns the name of an HTML character reference based on its character
* value. Only valid for entities defined from character references. If no
@@ -345,7 +351,7 @@
public static String fromChar( char value )
{
String name;
-
+
initialize();
name = (String) _byChar.get( String.valueOf( value ) );
if ( name == null )
@@ -353,7 +359,7 @@
else
return name;
}
-
+
/**
* Initialize upon first access. Will load all the HTML character references
@@ -370,12 +376,11 @@
String value;
int code;
String line;
-
+
// Make sure not to initialize twice.
if ( _byName != null )
return;
- try
- {
+ try {
_byName = new Hashtable();
_byChar = new Hashtable();
is = HTMLdtd.class.getResourceAsStream( ENTITIES_RESOURCE );
@@ -383,52 +388,39 @@
throw new RuntimeException( "SER003 The resource [" + ENTITIES_RESOURCE + "] could not be found.\n" + ENTITIES_RESOURCE);
reader = new BufferedReader( new InputStreamReader( is ) );
line = reader.readLine();
- while ( line != null )
- {
- if ( line.length() == 0 || line.charAt( 0 ) == '#' )
- {
+ while ( line != null ) {
+ if ( line.length() == 0 || line.charAt( 0 ) == '#' ) {
line = reader.readLine();
continue;
}
index = line.indexOf( ' ' );
- if ( index > 1 )
- {
+ if ( index > 1 ) {
name = line.substring( 0, index );
++index;
- if ( index < line.length() )
- {
+ if ( index < line.length() ) {
value = line.substring( index );
index = value.indexOf( ' ' );
if ( index > 0 )
value = value.substring( 0, index );
code = Integer.parseInt( value );
- defineEntity( name, (char) code );
+ defineEntity( name, (char) code );
}
}
line = reader.readLine();
}
is.close();
- }
- catch ( Exception except )
- {
+ } catch ( Exception except ) {
throw new RuntimeException( "SER003 The resource [" + ENTITIES_RESOURCE + "] could not load: " +
- except.toString() + "\n" + ENTITIES_RESOURCE + "\t" + except.toString());
- }
- finally
- {
- if ( is != null )
- {
- try
- {
+ except.toString() + "\n" + ENTITIES_RESOURCE + "\t" + except.toString());
+ } finally {
+ if ( is != null ) {
+ try {
is.close();
- }
- catch ( Exception except )
- {
- }
+ } catch ( Exception except ) { }
}
}
}
-
+
/**
* Defines a new character reference. The reference's name and value are
@@ -444,8 +436,7 @@
*/
private static void defineEntity( String name, char value )
{
- if ( _byName.get( name ) == null )
- {
+ if ( _byName.get( name ) == null ) {
_byName.put( name, new Character( value ) );
_byChar.put( String.valueOf( value ), name );
}
@@ -454,114 +445,114 @@
private static void defineElement( String name, int flags )
{
- _elemDefs.put( name, new Integer( flags ) );
+ _elemDefs.put( name, new Integer( flags ) );
}
-
-
+
+
private static void defineBoolean( String tagName, String attrName )
{
- defineBoolean( tagName, new String[] { attrName } );
+ defineBoolean( tagName, new String[] { attrName } );
}
-
-
+
+
private static void defineBoolean( String tagName, String[] attrNames )
{
- _boolAttrs.put( tagName, attrNames );
+ _boolAttrs.put( tagName, attrNames );
}
-
-
+
+
private static boolean isElement( String name, int flag )
{
- Integer flags;
-
- flags = (Integer) _elemDefs.get( name.toUpperCase() );
- if ( flags == null )
- return false;
- else
- return ( ( flags.intValue() & flag ) != 0 );
+ Integer flags;
+
+ flags = (Integer) _elemDefs.get( name.toUpperCase() );
+ if ( flags == null )
+ return false;
+ else
+ return ( ( flags.intValue() & flag ) != 0 );
}
-
-
+
+
static
{
- _elemDefs = new Hashtable();
- defineElement( "ADDRESS", CLOSE_P );
- defineElement( "AREA", EMPTY );
- defineElement( "BASE", EMPTY | ALLOWED_HEAD );
- defineElement( "BASEFONT", EMPTY );
- defineElement( "BLOCKQUOTE", CLOSE_P );
- defineElement( "BODY", OPT_CLOSING );
- defineElement( "BR", EMPTY );
- defineElement( "COL", EMPTY );
- defineElement( "COLGROUP", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE );
- defineElement( "DD", OPT_CLOSING | ONLY_OPENING | CLOSE_DD_DT );
- defineElement( "DIV", CLOSE_P );
- defineElement( "DL", ELEM_CONTENT | CLOSE_P );
- defineElement( "DT", OPT_CLOSING | ONLY_OPENING | CLOSE_DD_DT );
- defineElement( "FIELDSET", CLOSE_P );
- defineElement( "FORM", CLOSE_P );
- defineElement( "FRAME", EMPTY | OPT_CLOSING );
- defineElement( "H1", CLOSE_P );
- defineElement( "H2", CLOSE_P );
- defineElement( "H3", CLOSE_P );
- defineElement( "H4", CLOSE_P );
- defineElement( "H5", CLOSE_P );
- defineElement( "H6", CLOSE_P );
- defineElement( "HEAD", ELEM_CONTENT | OPT_CLOSING );
- defineElement( "HR", EMPTY | CLOSE_P );
- defineElement( "HTML", ELEM_CONTENT | OPT_CLOSING );
- defineElement( "IMG", EMPTY );
- defineElement( "INPUT", EMPTY );
- defineElement( "ISINDEX", EMPTY | ALLOWED_HEAD );
- defineElement( "LI", OPT_CLOSING | ONLY_OPENING | CLOSE_SELF );
- defineElement( "LINK", EMPTY | ALLOWED_HEAD );
- defineElement( "MAP", EMPTY | ALLOWED_HEAD );
- defineElement( "META", EMPTY | ALLOWED_HEAD );
- defineElement( "OL", ELEM_CONTENT | CLOSE_P );
- defineElement( "OPTGROUP", ELEM_CONTENT );
- defineElement( "OPTION", OPT_CLOSING | ONLY_OPENING | CLOSE_SELF );
- defineElement( "P", OPT_CLOSING | CLOSE_P | CLOSE_SELF );
- defineElement( "PARAM", EMPTY );
- defineElement( "PRE", PRESERVE | CLOSE_P );
- defineElement( "SCRIPT", ALLOWED_HEAD | PRESERVE );
- defineElement( "NOSCRIPT", ALLOWED_HEAD | PRESERVE );
- defineElement( "SELECT", ELEM_CONTENT );
- defineElement( "STYLE", ALLOWED_HEAD | PRESERVE );
- defineElement( "TABLE", ELEM_CONTENT | CLOSE_P );
- defineElement( "TBODY", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE );
- defineElement( "TD", OPT_CLOSING | CLOSE_TH_TD );
- defineElement( "TEXTAREA", PRESERVE );
- defineElement( "TFOOT", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE );
- defineElement( "TH", OPT_CLOSING | CLOSE_TH_TD );
- defineElement( "THEAD", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE );
- defineElement( "TITLE", ALLOWED_HEAD );
- defineElement( "TR", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE );
- defineElement( "UL", ELEM_CONTENT | CLOSE_P );
-
- _boolAttrs = new Hashtable();
- defineBoolean( "AREA", "href" );
- defineBoolean( "BUTTON", "disabled" );
- defineBoolean( "DIR", "compact" );
- defineBoolean( "DL", "compact" );
- defineBoolean( "FRAME", "noresize" );
- defineBoolean( "HR", "noshade" );
- defineBoolean( "IMAGE", "ismap" );
- defineBoolean( "INPUT", new String[] { "defaultchecked", "checked", "readonly", "disabled" } );
- defineBoolean( "LINK", "link" );
- defineBoolean( "MENU", "compact" );
- defineBoolean( "OBJECT", "declare" );
- defineBoolean( "OL", "compact" );
- defineBoolean( "OPTGROUP", "disabled" );
- defineBoolean( "OPTION", new String[] { "default-selected", "selected", "disabled" } );
- defineBoolean( "SCRIPT", "defer" );
- defineBoolean( "SELECT", new String[] { "multiple", "disabled" } );
- defineBoolean( "STYLE", "disabled" );
- defineBoolean( "TD", "nowrap" );
- defineBoolean( "TH", "nowrap" );
- defineBoolean( "TEXTAREA", new String[] { "disabled", "readonly" } );
- defineBoolean( "UL", "compact" );
-
- initialize();
+ _elemDefs = new Hashtable();
+ defineElement( "ADDRESS", CLOSE_P );
+ defineElement( "AREA", EMPTY );
+ defineElement( "BASE", EMPTY | ALLOWED_HEAD );
+ defineElement( "BASEFONT", EMPTY );
+ defineElement( "BLOCKQUOTE", CLOSE_P );
+ defineElement( "BODY", OPT_CLOSING );
+ defineElement( "BR", EMPTY );
+ defineElement( "COL", EMPTY );
+ defineElement( "COLGROUP", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE );
+ defineElement( "DD", OPT_CLOSING | ONLY_OPENING | CLOSE_DD_DT );
+ defineElement( "DIV", CLOSE_P );
+ defineElement( "DL", ELEM_CONTENT | CLOSE_P );
+ defineElement( "DT", OPT_CLOSING | ONLY_OPENING | CLOSE_DD_DT );
+ defineElement( "FIELDSET", CLOSE_P );
+ defineElement( "FORM", CLOSE_P );
+ defineElement( "FRAME", EMPTY | OPT_CLOSING );
+ defineElement( "H1", CLOSE_P );
+ defineElement( "H2", CLOSE_P );
+ defineElement( "H3", CLOSE_P );
+ defineElement( "H4", CLOSE_P );
+ defineElement( "H5", CLOSE_P );
+ defineElement( "H6", CLOSE_P );
+ defineElement( "HEAD", ELEM_CONTENT | OPT_CLOSING );
+ defineElement( "HR", EMPTY | CLOSE_P );
+ defineElement( "HTML", ELEM_CONTENT | OPT_CLOSING );
+ defineElement( "IMG", EMPTY );
+ defineElement( "INPUT", EMPTY );
+ defineElement( "ISINDEX", EMPTY | ALLOWED_HEAD );
+ defineElement( "LI", OPT_CLOSING | ONLY_OPENING | CLOSE_SELF );
+ defineElement( "LINK", EMPTY | ALLOWED_HEAD );
+ defineElement( "MAP", EMPTY | ALLOWED_HEAD );
+ defineElement( "META", EMPTY | ALLOWED_HEAD );
+ defineElement( "OL", ELEM_CONTENT | CLOSE_P );
+ defineElement( "OPTGROUP", ELEM_CONTENT );
+ defineElement( "OPTION", OPT_CLOSING | ONLY_OPENING | CLOSE_SELF );
+ defineElement( "P", OPT_CLOSING | CLOSE_P | CLOSE_SELF );
+ defineElement( "PARAM", EMPTY );
+ defineElement( "PRE", PRESERVE | CLOSE_P );
+ defineElement( "SCRIPT", ALLOWED_HEAD | PRESERVE );
+ defineElement( "NOSCRIPT", ALLOWED_HEAD | PRESERVE );
+ defineElement( "SELECT", ELEM_CONTENT );
+ defineElement( "STYLE", ALLOWED_HEAD | PRESERVE );
+ defineElement( "TABLE", ELEM_CONTENT | CLOSE_P );
+ defineElement( "TBODY", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE );
+ defineElement( "TD", OPT_CLOSING | CLOSE_TH_TD );
+ defineElement( "TEXTAREA", PRESERVE );
+ defineElement( "TFOOT", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE );
+ defineElement( "TH", OPT_CLOSING | CLOSE_TH_TD );
+ defineElement( "THEAD", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE );
+ defineElement( "TITLE", ALLOWED_HEAD );
+ defineElement( "TR", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE );
+ defineElement( "UL", ELEM_CONTENT | CLOSE_P );
+
+ _boolAttrs = new Hashtable();
+ defineBoolean( "AREA", "href" );
+ defineBoolean( "BUTTON", "disabled" );
+ defineBoolean( "DIR", "compact" );
+ defineBoolean( "DL", "compact" );
+ defineBoolean( "FRAME", "noresize" );
+ defineBoolean( "HR", "noshade" );
+ defineBoolean( "IMAGE", "ismap" );
+ defineBoolean( "INPUT", new String[] { "defaultchecked", "checked", "readonly", "disabled" } );
+ defineBoolean( "LINK", "link" );
+ defineBoolean( "MENU", "compact" );
+ defineBoolean( "OBJECT", "declare" );
+ defineBoolean( "OL", "compact" );
+ defineBoolean( "OPTGROUP", "disabled" );
+ defineBoolean( "OPTION", new String[] { "default-selected", "selected", "disabled" } );
+ defineBoolean( "SCRIPT", "defer" );
+ defineBoolean( "SELECT", new String[] { "multiple", "disabled" } );
+ defineBoolean( "STYLE", "disabled" );
+ defineBoolean( "TD", "nowrap" );
+ defineBoolean( "TH", "nowrap" );
+ defineBoolean( "TEXTAREA", new String[] { "disabled", "readonly" } );
+ defineBoolean( "UL", "compact" );
+
+ initialize();
}
1.4 +199 -199 xml-xerces/java/src/org/apache/xml/serialize/OutputFormat.java
Index: OutputFormat.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/OutputFormat.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- OutputFormat.java 2000/01/03 19:38:50 1.3
+++ OutputFormat.java 2000/02/23 21:41:18 1.4
@@ -97,59 +97,59 @@
public static class DTD
{
- /**
- * Public identifier for HTML document type.
- */
- public static final String HTMLPublicId = "-//W3C//DTD HTML 4.0//EN";
-
- /**
- * System identifier for HTML document type.
- */
- public static final String HTMLSystemId =
- "http://www.w3.org/TR/WD-html-in-xml/DTD/xhtml1-strict.dtd";
-
- /**
- * Public identifier for XHTML document type.
- */
- public static final String XHTMLPublicId =
- "-//W3C//DTD XHTML 1.0 Strict//EN";
-
- /**
- * System identifier for XHTML document type.
- */
- public static final String XHTMLSystemId =
- "http://www.w3.org/TR/WD-html-in-xml/DTD/xhtml1-strict.dtd";
-
+ /**
+ * Public identifier for HTML document type.
+ */
+ public static final String HTMLPublicId = "-//W3C//DTD HTML 4.0//EN";
+
+ /**
+ * System identifier for HTML document type.
+ */
+ public static final String HTMLSystemId =
+ "http://www.w3.org/TR/WD-html-in-xml/DTD/xhtml1-strict.dtd";
+
+ /**
+ * Public identifier for XHTML document type.
+ */
+ public static final String XHTMLPublicId =
+ "-//W3C//DTD XHTML 1.0 Strict//EN";
+
+ /**
+ * System identifier for XHTML document type.
+ */
+ public static final String XHTMLSystemId =
+ "http://www.w3.org/TR/WD-html-in-xml/DTD/xhtml1-strict.dtd";
+
}
-
-
+
+
public static class Defaults
{
-
- /**
- * If indentation is turned on, the default identation
- * level is 4.
- *
- * @see #setIndenting(boolean)
- */
- public static final int Indent = 4;
-
- /**
- * The default encoding for Web documents it UTF-8.
- *
- * @see #getEncoding()
- */
- public static final String Encoding = "UTF-8";
-
- /**
- * The default line width at which to break long lines
- * when identing. This is set to 72.
- */
- public static final int LineWidth = 72;
-
+
+ /**
+ * If indentation is turned on, the default identation
+ * level is 4.
+ *
+ * @see #setIndenting(boolean)
+ */
+ public static final int Indent = 4;
+
+ /**
+ * The default encoding for Web documents it UTF-8.
+ *
+ * @see #getEncoding()
+ */
+ public static final String Encoding = "UTF-8";
+
+ /**
+ * The default line width at which to break long lines
+ * when identing. This is set to 72.
+ */
+ public static final int LineWidth = 72;
+
}
-
-
+
+
/**
* Holds the output method specified for this document,
* or null if no method was specified.
@@ -264,9 +264,9 @@
*/
public OutputFormat( String method, String encoding, boolean indenting )
{
- setMethod( method );
- setEncoding( encoding );
- setIndenting( indenting );
+ setMethod( method );
+ setEncoding( encoding );
+ setIndenting( indenting );
}
@@ -280,9 +280,9 @@
*/
public OutputFormat( Document doc )
{
- setMethod( whichMethod( doc ) );
- setDoctype( whichDoctypePublic( doc ), whichDoctypeSystem( doc ) );
- setMediaType( whichMediaType( getMethod() ) );
+ setMethod( whichMethod( doc ) );
+ setDoctype( whichDoctypePublic( doc ), whichDoctypeSystem( doc ) );
+ setMediaType( whichMediaType( getMethod() ) );
}
@@ -302,9 +302,9 @@
*/
public OutputFormat( Document doc, String encoding, boolean indenting )
{
- this( doc );
- setEncoding( encoding );
- setIndenting( indenting );
+ this( doc );
+ setEncoding( encoding );
+ setIndenting( indenting );
}
@@ -332,7 +332,7 @@
*/
public void setMethod( String method )
{
- _method = method;
+ _method = method;
}
@@ -347,7 +347,7 @@
*/
public String getVersion()
{
- return _version;
+ return _version;
}
@@ -361,7 +361,7 @@
*/
public void setVersion( String version )
{
- _version = version;
+ _version = version;
}
@@ -375,16 +375,16 @@
*/
public int getIndent()
{
- return _indent;
+ return _indent;
}
-
+
/**
* Returns true if indentation was specified.
*/
public boolean getIndenting()
{
- return ( _indent > 0 );
+ return ( _indent > 0 );
}
@@ -398,10 +398,10 @@
*/
public void setIndent( int indent )
{
- if ( indent < 0 )
- _indent = 0;
- else
- _indent = indent;
+ if ( indent < 0 )
+ _indent = 0;
+ else
+ _indent = indent;
}
@@ -416,13 +416,13 @@
*/
public void setIndenting( boolean on )
{
- if ( on ) {
- _indent = Defaults.Indent;
- _lineWidth = Defaults.LineWidth;
- } else {
- _indent = 0;
- _lineWidth = 0;
- }
+ if ( on ) {
+ _indent = Defaults.Indent;
+ _lineWidth = Defaults.LineWidth;
+ } else {
+ _indent = 0;
+ _lineWidth = 0;
+ }
}
@@ -434,7 +434,7 @@
*/
public String getEncoding()
{
- return _encoding;
+ return _encoding;
}
@@ -449,7 +449,7 @@
*/
public void setEncoding( String encoding )
{
- _encoding = encoding;
+ _encoding = encoding;
}
@@ -462,7 +462,7 @@
*/
public String getMediaType()
{
- return _mediaType;
+ return _mediaType;
}
@@ -474,7 +474,7 @@
*/
public void setMediaType( String mediaType )
{
- _mediaType = mediaType;
+ _mediaType = mediaType;
}
@@ -489,8 +489,8 @@
*/
public void setDoctype( String publicId, String systemId )
{
- _doctypePublic = publicId;
- _doctypeSystem = systemId;
+ _doctypePublic = publicId;
+ _doctypeSystem = systemId;
}
@@ -500,7 +500,7 @@
*/
public String getDoctypePublic()
{
- return _doctypePublic;
+ return _doctypePublic;
}
@@ -510,7 +510,7 @@
*/
public String getDoctypeSystem()
{
- return _doctypeSystem;
+ return _doctypeSystem;
}
@@ -520,7 +520,7 @@
*/
public boolean getOmitXMLDeclaration()
{
- return _omitXmlDeclaration;
+ return _omitXmlDeclaration;
}
@@ -531,7 +531,7 @@
*/
public void setOmitXMLDeclaration( boolean omit )
{
- _omitXmlDeclaration = omit;
+ _omitXmlDeclaration = omit;
}
@@ -541,7 +541,7 @@
*/
public boolean getStandalone()
{
- return _standalone;
+ return _standalone;
}
@@ -554,7 +554,7 @@
*/
public void setStandalone( boolean standalone )
{
- _standalone = standalone;
+ _standalone = standalone;
}
@@ -565,7 +565,7 @@
*/
public String[] getCDataElements()
{
- return _cdataElements;
+ return _cdataElements;
}
@@ -578,14 +578,14 @@
*/
public boolean isCDataElement( String tagName )
{
- int i;
-
- if ( _cdataElements == null )
- return false;
- for ( i = 0 ; i < _cdataElements.length ; ++i )
- if ( _cdataElements[ i ].equals( tagName ) )
- return true;
- return false;
+ int i;
+
+ if ( _cdataElements == null )
+ return false;
+ for ( i = 0 ; i < _cdataElements.length ; ++i )
+ if ( _cdataElements[ i ].equals( tagName ) )
+ return true;
+ return false;
}
@@ -597,7 +597,7 @@
*/
public void setCDataElements( String[] cdataElements )
{
- _cdataElements = cdataElements;
+ _cdataElements = cdataElements;
}
@@ -608,7 +608,7 @@
*/
public String[] getNonEscapingElements()
{
- return _nonEscapingElements;
+ return _nonEscapingElements;
}
@@ -621,14 +621,14 @@
*/
public boolean isNonEscapingElement( String tagName )
{
- int i;
-
- if ( _nonEscapingElements == null )
- return false;
- for ( i = 0 ; i < _nonEscapingElements.length ; ++i )
- if ( _nonEscapingElements[ i ].equals( tagName ) )
- return true;
- return false;
+ int i;
+
+ if ( _nonEscapingElements == null )
+ return false;
+ for ( i = 0 ; i < _nonEscapingElements.length ; ++i )
+ if ( _nonEscapingElements[ i ].equals( tagName ) )
+ return true;
+ return false;
}
@@ -640,7 +640,7 @@
*/
public void setNonEscapingElements( String[] nonEscapingElements )
{
- _nonEscapingElements = nonEscapingElements;
+ _nonEscapingElements = nonEscapingElements;
}
@@ -654,7 +654,7 @@
*/
public String getLineSeparator()
{
- return _lineSeparator;
+ return _lineSeparator;
}
@@ -669,13 +669,13 @@
*/
public void setLineSeparator( String lineSeparator )
{
- if ( lineSeparator == null )
- _lineSeparator = LineSeparator.Web;
- else
- _lineSeparator = lineSeparator;
+ if ( lineSeparator == null )
+ _lineSeparator = LineSeparator.Web;
+ else
+ _lineSeparator = lineSeparator;
}
-
-
+
+
/**
* Returns true if the default behavior for this format is to
* preserve spaces. All elements that do not specify otherwise
@@ -685,7 +685,7 @@
*/
public boolean getPreserveSpace()
{
- return _preserve;
+ return _preserve;
}
@@ -698,7 +698,7 @@
*/
public void setPreserveSpace( boolean preserve )
{
- _preserve = preserve;
+ _preserve = preserve;
}
@@ -710,7 +710,7 @@
*/
public int getLineWidth()
{
- return _lineWidth;
+ return _lineWidth;
}
@@ -725,10 +725,10 @@
*/
public void setLineWidth( int lineWidth )
{
- if ( lineWidth <= 0 )
- _lineWidth = 0;
- else
- _lineWidth = lineWidth;
+ if ( lineWidth <= 0 )
+ _lineWidth = 0;
+ else
+ _lineWidth = lineWidth;
}
@@ -739,11 +739,11 @@
*/
public char getLastPrintable()
{
- if ( getEncoding() != null &&
- ( getEncoding().equalsIgnoreCase( "ASCII" ) ) )
- return 0xFF;
- else
- return 0xFFFF;
+ if ( getEncoding() != null &&
+ ( getEncoding().equalsIgnoreCase( "ASCII" ) ) )
+ return 0xFF;
+ else
+ return 0xFFFF;
}
@@ -761,47 +761,47 @@
public static String whichMethod( Document doc )
{
Node node;
- String value;
- int i;
+ String value;
+ int i;
- // If document is derived from HTMLDocument then the default
- // method is html.
+ // If document is derived from HTMLDocument then the default
+ // method is html.
if ( doc instanceof HTMLDocument )
return Method.HTML;
+
+ // Lookup the root element and the text nodes preceding it.
+ // If root element is html and all text nodes contain whitespace
+ // only, the method is html.
- // Lookup the root element and the text nodes preceding it.
- // If root element is html and all text nodes contain whitespace
- // only, the method is html.
-
- // FIXME (SM) should we care about namespaces here?
-
+ // FIXME (SM) should we care about namespaces here?
+
node = doc.getFirstChild();
while (node != null) {
- // If the root element is html, the method is html.
- if ( node.getNodeType() == Node.ELEMENT_NODE ) {
- if ( node.getNodeName().equalsIgnoreCase( "html" ) ) {
- return Method.HTML;
- } else if ( node.getNodeName().equalsIgnoreCase( "root" ) ) {
- return Method.FOP;
- } else {
- return Method.XML;
- }
- } else if ( node.getNodeType() == Node.TEXT_NODE ) {
- // If a text node preceding the root element contains
- // only whitespace, this might be html, otherwise it's
- // definitely xml.
- value = node.getNodeValue();
- for ( i = 0 ; i < value.length() ; ++i )
- if ( value.charAt( i ) != 0x20 && value.charAt( i ) != 0x0A &&
- value.charAt( i ) != 0x09 && value.charAt( i ) != 0x0D )
- return Method.XML;
- }
- node = node.getNextSibling();
- }
- // Anything else, the method is xml.
- return Method.XML;
+ // If the root element is html, the method is html.
+ if ( node.getNodeType() == Node.ELEMENT_NODE ) {
+ if ( node.getNodeName().equalsIgnoreCase( "html" ) ) {
+ return Method.HTML;
+ } else if ( node.getNodeName().equalsIgnoreCase( "root" ) ) {
+ return Method.FOP;
+ } else {
+ return Method.XML;
+ }
+ } else if ( node.getNodeType() == Node.TEXT_NODE ) {
+ // If a text node preceding the root element contains
+ // only whitespace, this might be html, otherwise it's
+ // definitely xml.
+ value = node.getNodeValue();
+ for ( i = 0 ; i < value.length() ; ++i )
+ if ( value.charAt( i ) != 0x20 && value.charAt( i ) != 0x0A &&
+ value.charAt( i ) != 0x09 && value.charAt( i ) != 0x0D )
+ return Method.XML;
+ }
+ node = node.getNextSibling();
+ }
+ // Anything else, the method is xml.
+ return Method.XML;
}
-
+
/**
* Returns the document type public identifier
@@ -809,21 +809,21 @@
*/
public static String whichDoctypePublic( Document doc )
{
- DocumentType doctype;
-
+ DocumentType doctype;
+
/* XXX Delayed until DOM Level 2 is introduced into the code base
- doctype = doc.getDoctype();
- if ( doctype != null ) {
- // Note on catch: DOM Level 1 does not specify this method
- // and the code will throw a NoSuchMethodError
- try {
- return doctype.getPublicID();
- } catch ( Error except ) { }
- }
- */
- if ( doc instanceof HTMLDocument )
- return DTD.XHTMLPublicId;
- return null;
+ doctype = doc.getDoctype();
+ if ( doctype != null ) {
+ // Note on catch: DOM Level 1 does not specify this method
+ // and the code will throw a NoSuchMethodError
+ try {
+ return doctype.getPublicID();
+ } catch ( Error except ) { }
+ }
+ */
+ if ( doc instanceof HTMLDocument )
+ return DTD.XHTMLPublicId;
+ return null;
}
@@ -833,21 +833,21 @@
*/
public static String whichDoctypeSystem( Document doc )
{
- DocumentType doctype;
-
+ DocumentType doctype;
+
/* XXX Delayed until DOM Level 2 is introduced into the code base
- doctype = doc.getDoctype();
- if ( doctype != null ) {
- // Note on catch: DOM Level 1 does not specify this method
- // and the code will throw a NoSuchMethodError
- try {
- return doctype.getSystemID();
- } catch ( Error except ) { }
- }
- */
- if ( doc instanceof HTMLDocument )
- return DTD.XHTMLSystemId;
- return null;
+ doctype = doc.getDoctype();
+ if ( doctype != null ) {
+ // Note on catch: DOM Level 1 does not specify this method
+ // and the code will throw a NoSuchMethodError
+ try {
+ return doctype.getSystemID();
+ } catch ( Error except ) { }
+ }
+ */
+ if ( doc instanceof HTMLDocument )
+ return DTD.XHTMLSystemId;
+ return null;
}
@@ -857,17 +857,17 @@
*/
public static String whichMediaType( String method )
{
- if ( method.equalsIgnoreCase( Method.XML ) )
- return "text/xml";
- if ( method.equalsIgnoreCase( Method.HTML ) )
- return "text/html";
- if ( method.equalsIgnoreCase( Method.XHTML ) )
- return "text/html";
- if ( method.equalsIgnoreCase( Method.TEXT ) )
- return "text/plain";
- if ( method.equalsIgnoreCase( Method.FOP ) )
- return "application/pdf";
- return null;
+ if ( method.equalsIgnoreCase( Method.XML ) )
+ return "text/xml";
+ if ( method.equalsIgnoreCase( Method.HTML ) )
+ return "text/html";
+ if ( method.equalsIgnoreCase( Method.XHTML ) )
+ return "text/html";
+ if ( method.equalsIgnoreCase( Method.TEXT ) )
+ return "text/plain";
+ if ( method.equalsIgnoreCase( Method.FOP ) )
+ return "application/pdf";
+ return null;
}
1.7 +2 -2 xml-xerces/java/src/org/apache/xml/serialize/Serializer.java
Index: Serializer.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/Serializer.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Serializer.java 2000/02/16 00:58:09 1.6
+++ Serializer.java 2000/02/23 21:41:18 1.7
@@ -116,8 +116,8 @@
* serializer is in the process of serializing a document.
*/
public void setOutputByteStream(OutputStream output)
- throws UnsupportedEncodingException;
-
+ throws UnsupportedEncodingException;
+
/**
* Specifies a writer to which the document should be serialized.
1.2 +42 -42 xml-xerces/java/src/org/apache/xml/serialize/SerializerFactory.java
Index: SerializerFactory.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/SerializerFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SerializerFactory.java 2000/01/03 19:38:50 1.1
+++ SerializerFactory.java 2000/02/23 21:41:18 1.2
@@ -85,35 +85,35 @@
static
{
- SerializerFactory factory;
- String list;
- StringTokenizer token;
- String className;
-
- // The default factories are always registered first,
- // any factory specified in the properties file and supporting
- // the same method will override the default factory.
- factory = new SerializerFactoryImpl( Method.XML );
- registerSerializerFactory( factory );
- factory = new SerializerFactoryImpl( Method.HTML );
- registerSerializerFactory( factory );
- factory = new SerializerFactoryImpl( Method.XHTML );
- registerSerializerFactory( factory );
- factory = new SerializerFactoryImpl( Method.TEXT );
- registerSerializerFactory( factory );
-
- list = System.getProperty( FactoriesProperty );
- if ( list != null ) {
- token = new StringTokenizer( list, " ;,:" );
- while ( token.hasMoreTokens() ) {
- className = token.nextToken();
- try {
- factory = (SerializerFactory) Class.forName( className ).newInstance();
- if ( _factories.contains( factory.getSupportedMethod() ) )
- _factories.put( factory.getSupportedMethod(), factory );
- } catch ( Exception except ) { }
- }
- }
+ SerializerFactory factory;
+ String list;
+ StringTokenizer token;
+ String className;
+
+ // The default factories are always registered first,
+ // any factory specified in the properties file and supporting
+ // the same method will override the default factory.
+ factory = new SerializerFactoryImpl( Method.XML );
+ registerSerializerFactory( factory );
+ factory = new SerializerFactoryImpl( Method.HTML );
+ registerSerializerFactory( factory );
+ factory = new SerializerFactoryImpl( Method.XHTML );
+ registerSerializerFactory( factory );
+ factory = new SerializerFactoryImpl( Method.TEXT );
+ registerSerializerFactory( factory );
+
+ list = System.getProperty( FactoriesProperty );
+ if ( list != null ) {
+ token = new StringTokenizer( list, " ;,:" );
+ while ( token.hasMoreTokens() ) {
+ className = token.nextToken();
+ try {
+ factory = (SerializerFactory) Class.forName( className ).newInstance();
+ if ( _factories.contains( factory.getSupportedMethod() ) )
+ _factories.put( factory.getSupportedMethod(), factory );
+ } catch ( Exception except ) { }
+ }
+ }
}
@@ -123,12 +123,12 @@
*/
public static void registerSerializerFactory( SerializerFactory factory )
{
- String method;
-
- synchronized ( _factories ) {
- method = factory.getSupportedMethod();
- _factories.put( method, factory );
- }
+ String method;
+
+ synchronized ( _factories ) {
+ method = factory.getSupportedMethod();
+ _factories.put( method, factory );
+ }
}
@@ -138,7 +138,7 @@
*/
public static SerializerFactory getSerializerFactory( String method )
{
- return (SerializerFactory) _factories.get( method );
+ return (SerializerFactory) _factories.get( method );
}
@@ -167,9 +167,9 @@
* method is used, the encoding property will be ignored.
*/
public abstract Serializer makeSerializer( Writer writer,
- OutputFormat format );
-
-
+ OutputFormat format );
+
+
/**
* Create a new serializer, based on the {@link OutputFormat} and
* using the output byte stream and the encoding specified in the
@@ -179,9 +179,9 @@
* not supported
*/
public abstract Serializer makeSerializer( OutputStream output,
- OutputFormat format )
- throws UnsupportedEncodingException;
-
+ OutputFormat format )
+ throws UnsupportedEncodingException;
+
}
1.3 +46 -46 xml-xerces/java/src/org/apache/xml/serialize/SerializerFactoryImpl.java
Index: SerializerFactoryImpl.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/SerializerFactoryImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SerializerFactoryImpl.java 2000/02/05 02:24:15 1.2
+++ SerializerFactoryImpl.java 2000/02/23 21:41:18 1.3
@@ -79,72 +79,72 @@
private String _method;
-
-
+
+
SerializerFactoryImpl( String method )
{
- _method = method;
- if ( ! _method.equals( Method.XML ) &&
- ! _method.equals( Method.HTML ) &&
- ! _method.equals( Method.XHTML ) &&
- ! _method.equals( Method.TEXT ) )
- throw new IllegalArgumentException( "SER004 The method '" + method + "' is not supported by this factory\n" + method);
+ _method = method;
+ if ( ! _method.equals( Method.XML ) &&
+ ! _method.equals( Method.HTML ) &&
+ ! _method.equals( Method.XHTML ) &&
+ ! _method.equals( Method.TEXT ) )
+ throw new IllegalArgumentException( "SER004 The method '" + method + "' is not supported by this factory\n" + method);
}
public Serializer makeSerializer( OutputFormat format )
{
- Serializer serializer;
-
- serializer = getSerializer( format );
- serializer.setOutputFormat( format );
- return serializer;
+ Serializer serializer;
+
+ serializer = getSerializer( format );
+ serializer.setOutputFormat( format );
+ return serializer;
}
-
-
-
+
+
+
public Serializer makeSerializer( Writer writer,
- OutputFormat format )
+ OutputFormat format )
{
- Serializer serializer;
-
- serializer = getSerializer( format );
- serializer.setOutputCharStream( writer );
- return serializer;
+ Serializer serializer;
+
+ serializer = getSerializer( format );
+ serializer.setOutputCharStream( writer );
+ return serializer;
}
-
-
+
+
public Serializer makeSerializer( OutputStream output,
- OutputFormat format )
- throws UnsupportedEncodingException
+ OutputFormat format )
+ throws UnsupportedEncodingException
{
- Serializer serializer;
-
- serializer = getSerializer( format );
- serializer.setOutputByteStream( output );
- return serializer;
+ Serializer serializer;
+
+ serializer = getSerializer( format );
+ serializer.setOutputByteStream( output );
+ return serializer;
}
-
+
private Serializer getSerializer( OutputFormat format )
{
- if ( _method.equals( Method.XML ) ) {
- return new XMLSerializer( format );
- } else if ( _method.equals( Method.HTML ) ) {
- return new HTMLSerializer( format );
- } else if ( _method.equals( Method.XHTML ) ) {
- return new XHTMLSerializer( format );
- } else if ( _method.equals( Method.TEXT ) ) {
- return new TextSerializer( format );
- } else {
- throw new IllegalStateException( "SER005 The method '" + _method + "' is not supported by this factory\n" + _method);
- }
+ if ( _method.equals( Method.XML ) ) {
+ return new XMLSerializer( format );
+ } else if ( _method.equals( Method.HTML ) ) {
+ return new HTMLSerializer( format );
+ } else if ( _method.equals( Method.XHTML ) ) {
+ return new XHTMLSerializer( format );
+ } else if ( _method.equals( Method.TEXT ) ) {
+ return new TextSerializer( format );
+ } else {
+ throw new IllegalStateException( "SER005 The method '" + _method + "' is not supported by this factory\n" + _method);
+ }
}
-
-
+
+
protected String getSupportedMethod()
{
- return _method;
+ return _method;
}
1.5 +190 -219 xml-xerces/java/src/org/apache/xml/serialize/TextSerializer.java
Index: TextSerializer.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/TextSerializer.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- TextSerializer.java 2000/02/16 00:58:09 1.4
+++ TextSerializer.java 2000/02/23 21:41:18 1.5
@@ -106,7 +106,7 @@
*/
public TextSerializer()
{
- setOutputFormat( null );
+ setOutputFormat( null );
}
@@ -117,7 +117,7 @@
*/
public TextSerializer( OutputFormat format )
{
- setOutputFormat( format );
+ setOutputFormat( format );
}
@@ -131,8 +131,8 @@
*/
public TextSerializer( Writer writer, OutputFormat format )
{
- setOutputFormat( format );
- setOutputCharStream( writer );
+ setOutputFormat( format );
+ setOutputCharStream( writer );
}
@@ -146,21 +146,21 @@
*/
public TextSerializer( OutputStream output, OutputFormat format )
{
- setOutputFormat( format );
- try {
- setOutputByteStream( output );
- } catch ( UnsupportedEncodingException except ) {
- // Should never happend
- }
+ setOutputFormat( format );
+ try {
+ setOutputByteStream( output );
+ } catch ( UnsupportedEncodingException except ) {
+ // Should never happend
+ }
}
public void setOutputFormat( OutputFormat format )
{
- if ( format == null )
- super.setOutputFormat( new OutputFormat( Method.TEXT, null, false ) );
- else
- super.setOutputFormat( format );
+ if ( format == null )
+ super.setOutputFormat( new OutputFormat( Method.TEXT, null, false ) );
+ else
+ super.setOutputFormat( format );
}
@@ -170,16 +170,16 @@
public void startElement( String namespaceURI, String localName,
- String rawName, Attributes attrs )
+ String rawName, Attributes attrs )
{
- startElement( rawName == null ? localName : rawName, null );
+ startElement( rawName == null ? localName : rawName, null );
}
public void endElement( String namespaceURI, String localName,
- String rawName )
+ String rawName )
{
- endElement( rawName == null ? localName : rawName );
+ endElement( rawName == null ? localName : rawName );
}
@@ -187,64 +187,64 @@
// SAX document handler serializing methods //
//------------------------------000---------//
-
+
public void startDocument()
{
- // Nothing to do here. All the magic happens in startDocument(String)
+ // Nothing to do here. All the magic happens in startDocument(String)
}
-
-
+
+
public void startElement( String tagName, AttributeList attrs )
{
- boolean preserveSpace;
- ElementState state;
-
- state = getElementState();
- if ( state == null ) {
- // If this is the root element handle it differently.
- // If the first root element in the document, serialize
- // the document's DOCTYPE. Space preserving defaults
- // to that of the output format.
- if ( ! _started )
- startDocument( tagName );
- preserveSpace = _format.getPreserveSpace();
- } else {
- // For any other element, if first in parent, then
- // use the parnet's space preserving.
- preserveSpace = state.preserveSpace;
- }
- // Do not change the current element state yet.
- // This only happens in endElement().
-
- // Ignore all other attributes of the element, only printing
- // its contents.
-
- // Now it's time to enter a new element state
- // with the tag name and space preserving.
- // We still do not change the curent element state.
- state = enterElementState( null, null, tagName, preserveSpace );
+ boolean preserveSpace;
+ ElementState state;
+
+ state = getElementState();
+ if ( state == null ) {
+ // If this is the root element handle it differently.
+ // If the first root element in the document, serialize
+ // the document's DOCTYPE. Space preserving defaults
+ // to that of the output format.
+ if ( ! _started )
+ startDocument( tagName );
+ preserveSpace = _format.getPreserveSpace();
+ } else {
+ // For any other element, if first in parent, then
+ // use the parnet's space preserving.
+ preserveSpace = state.preserveSpace;
+ }
+ // Do not change the current element state yet.
+ // This only happens in endElement().
+
+ // Ignore all other attributes of the element, only printing
+ // its contents.
+
+ // Now it's time to enter a new element state
+ // with the tag name and space preserving.
+ // We still do not change the curent element state.
+ state = enterElementState( null, null, tagName, preserveSpace );
}
-
-
+
+
public void endElement( String tagName )
{
- ElementState state;
-
- // Works much like content() with additions for closing
- // an element. Note the different checks for the closed
- // element's state and the parent element's state.
- state = getElementState();
- // Leave the element state and update that of the parent
- // (if we're not root) to not empty and after element.
- state = leaveElementState();
- if ( state != null ) {
- state.afterElement = true;
- state.empty = false;
- } else {
- // [keith] If we're done printing the document but don't
- // get to call endDocument(), the buffer should be flushed.
- flush();
- }
+ ElementState state;
+
+ // Works much like content() with additions for closing
+ // an element. Note the different checks for the closed
+ // element's state and the parent element's state.
+ state = getElementState();
+ // Leave the element state and update that of the parent
+ // (if we're not root) to not empty and after element.
+ state = leaveElementState();
+ if ( state != null ) {
+ state.afterElement = true;
+ state.empty = false;
+ } else {
+ // [keith] If we're done printing the document but don't
+ // get to call endDocument(), the buffer should be flushed.
+ flush();
+ }
}
@@ -265,19 +265,19 @@
public void characters( char[] chars, int start, int length )
{
- characters( new String( chars, start, length ), false );
+ characters( new String( chars, start, length ), false );
}
protected void characters( String text, boolean unescaped )
{
- ElementState state;
-
- state = content();
- if ( state != null ) {
- state.doCData = state.inCData = false;
- }
- printText( text, true );
+ ElementState state;
+
+ state = content();
+ if ( state != null ) {
+ state.doCData = state.inCData = false;
+ }
+ printText( text, true );
}
@@ -297,13 +297,13 @@
*/
protected void startDocument( String rootTagName )
{
- // Required to stop processing the DTD, even though the DTD
- // is not printed.
- leaveDTD();
-
- _started = true;
- // Always serialize these, even if not te first root element.
- serializePreRoot();
+ // Required to stop processing the DTD, even though the DTD
+ // is not printed.
+ leaveDTD();
+
+ _started = true;
+ // Always serialize these, even if not te first root element.
+ serializePreRoot();
}
@@ -314,51 +314,51 @@
*/
protected void serializeElement( Element elem )
{
- Node child;
- ElementState state;
- boolean preserveSpace;
- String tagName;
-
- tagName = elem.getTagName();
- state = getElementState();
- if ( state == null ) {
- // If this is the root element handle it differently.
- // If the first root element in the document, serialize
- // the document's DOCTYPE. Space preserving defaults
- // to that of the output format.
- if ( ! _started )
- startDocument( tagName );
- preserveSpace = _format.getPreserveSpace();
- } else {
- // For any other element, if first in parent, then
- // use the parnet's space preserving.
- preserveSpace = state.preserveSpace;
- }
- // Do not change the current element state yet.
- // This only happens in endElement().
-
- // Ignore all other attributes of the element, only printing
- // its contents.
-
- // If element has children, then serialize them, otherwise
- // serialize en empty tag.
- if ( elem.hasChildNodes() ) {
- // Enter an element state, and serialize the children
- // one by one. Finally, end the element.
- state = enterElementState( null, null, tagName, preserveSpace );
- child = elem.getFirstChild();
- while ( child != null ) {
- serializeNode( child );
- child = child.getNextSibling();
- }
- endElement( tagName );
- } else {
- if ( state != null ) {
- // After element but parent element is no longer empty.
- state.afterElement = true;
- state.empty = false;
- }
- }
+ Node child;
+ ElementState state;
+ boolean preserveSpace;
+ String tagName;
+
+ tagName = elem.getTagName();
+ state = getElementState();
+ if ( state == null ) {
+ // If this is the root element handle it differently.
+ // If the first root element in the document, serialize
+ // the document's DOCTYPE. Space preserving defaults
+ // to that of the output format.
+ if ( ! _started )
+ startDocument( tagName );
+ preserveSpace = _format.getPreserveSpace();
+ } else {
+ // For any other element, if first in parent, then
+ // use the parnet's space preserving.
+ preserveSpace = state.preserveSpace;
+ }
+ // Do not change the current element state yet.
+ // This only happens in endElement().
+
+ // Ignore all other attributes of the element, only printing
+ // its contents.
+
+ // If element has children, then serialize them, otherwise
+ // serialize en empty tag.
+ if ( elem.hasChildNodes() ) {
+ // Enter an element state, and serialize the children
+ // one by one. Finally, end the element.
+ state = enterElementState( null, null, tagName, preserveSpace );
+ child = elem.getFirstChild();
+ while ( child != null ) {
+ serializeNode( child );
+ child = child.getNextSibling();
+ }
+ endElement( tagName );
+ } else {
+ if ( state != null ) {
+ // After element but parent element is no longer empty.
+ state.afterElement = true;
+ state.empty = false;
+ }
+ }
}
@@ -369,102 +369,73 @@
*/
protected void serializeNode( Node node )
{
- // Based on the node type call the suitable SAX handler.
- // Only comments entities and documents which are not
- // handled by SAX are serialized directly.
+ // Based on the node type call the suitable SAX handler.
+ // Only comments entities and documents which are not
+ // handled by SAX are serialized directly.
switch ( node.getNodeType() ) {
- case Node.TEXT_NODE :
- characters( node.getNodeValue(), true );
- break;
-
- case Node.CDATA_SECTION_NODE :
- characters( node.getNodeValue(), true );
- break;
-
- case Node.COMMENT_NODE :
- break;
-
- case Node.ENTITY_REFERENCE_NODE :
- // Ignore.
- break;
-
- case Node.PROCESSING_INSTRUCTION_NODE :
- break;
-
- case Node.ELEMENT_NODE :
- serializeElement( (Element) node );
- break;
-
- case Node.DOCUMENT_NODE :
- DocumentType docType;
- NamedNodeMap map;
- Entity entity;
- Notation notation;
- int i;
-
- // If there is a document type, use the SAX events to
- // serialize it.
- docType = ( (Document) node ).getDoctype();
- if ( docType != null ) {
- startDTD( docType.getName(), null, null );
- map = docType.getEntities();
- if ( map != null ) {
- for ( i = 0 ; i < map.getLength() ; ++i ) {
- entity = (Entity) map.item( i );
- unparsedEntityDecl( entity.getNodeName(), entity.getPublicId(),
- entity.getSystemId(), entity.getNotationName() );
- }
- }
- map = docType.getNotations();
- if ( map != null ) {
- for ( i = 0 ; i < map.getLength() ; ++i ) {
- notation = (Notation) map.item( i );
- notationDecl( notation.getNodeName(), notation.getPublicId(), notation.getSystemId() );
- }
- }
- endDTD();
- }
- // !! Fall through
- case Node.DOCUMENT_FRAGMENT_NODE : {
- Node child;
-
- // By definition this will happen if the node is a document,
- // document fragment, etc. Just serialize its contents. It will
- // work well for other nodes that we do not know how to serialize.
- child = node.getFirstChild();
- while ( child != null ) {
- serializeNode( child );
- child = child.getNextSibling();
- }
- break;
- }
-
- default:
- break;
- }
+ case Node.TEXT_NODE :
+ characters( node.getNodeValue(), true );
+ break;
+
+ case Node.CDATA_SECTION_NODE :
+ characters( node.getNodeValue(), true );
+ break;
+
+ case Node.COMMENT_NODE :
+ break;
+
+ case Node.ENTITY_REFERENCE_NODE :
+ // Ignore.
+ break;
+
+ case Node.PROCESSING_INSTRUCTION_NODE :
+ break;
+
+ case Node.ELEMENT_NODE :
+ serializeElement( (Element) node );
+ break;
+
+ case Node.DOCUMENT_NODE :
+ case Node.DOCUMENT_FRAGMENT_NODE : {
+ Node child;
+
+ // By definition this will happen if the node is a document,
+ // document fragment, etc. Just serialize its contents. It will
+ // work well for other nodes that we do not know how to serialize.
+ child = node.getFirstChild();
+ while ( child != null ) {
+ serializeNode( child );
+ child = child.getNextSibling();
+ }
+ break;
+ }
+
+ default:
+ break;
+ }
}
-
-
+
+
protected ElementState content()
{
- ElementState state;
-
- state = getElementState();
- if ( state != null ) {
- // If this is the first content in the element,
- // change the state to not-empty.
- if ( state.empty ) {
- state.empty = false;
- }
- // Except for one content type, all of them
- // are not last element. That one content
- // type will take care of itself.
- state.afterElement = false;
- }
- return state;
+ ElementState state;
+
+ state = getElementState();
+ if ( state != null ) {
+ // If this is the first content in the element,
+ // change the state to not-empty.
+ if ( state.empty ) {
+ state.empty = false;
+ }
+ // Except for one content type, all of them
+ // are not last element. That one content
+ // type will take care of itself.
+ state.afterElement = false;
+ }
+ return state;
}
-
-
+
+
protected String getEntityRef( char ch )
{
return null;
1.4 +14 -14 xml-xerces/java/src/org/apache/xml/serialize/XHTMLSerializer.java
Index: XHTMLSerializer.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/XHTMLSerializer.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- XHTMLSerializer.java 2000/01/03 19:38:50 1.3
+++ XHTMLSerializer.java 2000/02/23 21:41:18 1.4
@@ -86,7 +86,7 @@
*/
public XHTMLSerializer()
{
- super( true, null );
+ super( true, null );
}
@@ -97,7 +97,7 @@
*/
public XHTMLSerializer( OutputFormat format )
{
- super( true, format );
+ super( true, format );
}
@@ -111,8 +111,8 @@
*/
public XHTMLSerializer( Writer writer, OutputFormat format )
{
- super( true, format );
- setOutputCharStream( writer );
+ super( true, format );
+ setOutputCharStream( writer );
}
@@ -126,21 +126,21 @@
*/
public XHTMLSerializer( OutputStream output, OutputFormat format )
{
- super( true, format );
- try {
- setOutputByteStream( output );
- } catch ( UnsupportedEncodingException except ) {
- // Should never happend
- }
+ super( true, format );
+ try {
+ setOutputByteStream( output );
+ } catch ( UnsupportedEncodingException except ) {
+ // Should never happend
+ }
}
public void setOutputFormat( OutputFormat format )
{
- if ( format == null )
- super.setOutputFormat( new OutputFormat( Method.XHTML, null, false ) );
- else
- super.setOutputFormat( format );
+ if ( format == null )
+ super.setOutputFormat( new OutputFormat( Method.XHTML, null, false ) );
+ else
+ super.setOutputFormat( format );
}
1.10 +423 -430 xml-xerces/java/src/org/apache/xml/serialize/XMLSerializer.java
Index: XMLSerializer.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/XMLSerializer.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- XMLSerializer.java 2000/02/16 00:58:09 1.9
+++ XMLSerializer.java 2000/02/23 21:41:18 1.10
@@ -113,7 +113,7 @@
*/
public XMLSerializer()
{
- setOutputFormat( null );
+ setOutputFormat( null );
}
@@ -124,7 +124,7 @@
*/
public XMLSerializer( OutputFormat format )
{
- setOutputFormat( format );
+ setOutputFormat( format );
}
@@ -138,8 +138,8 @@
*/
public XMLSerializer( Writer writer, OutputFormat format )
{
- setOutputFormat( format );
- setOutputCharStream( writer );
+ setOutputFormat( format );
+ setOutputCharStream( writer );
}
@@ -153,21 +153,21 @@
*/
public XMLSerializer( OutputStream output, OutputFormat format )
{
- setOutputFormat( format );
- try {
- setOutputByteStream( output );
- } catch ( UnsupportedEncodingException except ) {
- // Should never happend
- }
+ setOutputFormat( format );
+ try {
+ setOutputByteStream( output );
+ } catch ( UnsupportedEncodingException except ) {
+ // Should never happend
+ }
}
public void setOutputFormat( OutputFormat format )
{
- if ( format == null )
- super.setOutputFormat( new OutputFormat( Method.XML, null, false ) );
- else
- super.setOutputFormat( format );
+ if ( format == null )
+ super.setOutputFormat( new OutputFormat( Method.XML, null, false ) );
+ else
+ super.setOutputFormat( format );
}
@@ -177,248 +177,248 @@
public void startElement( String namespaceURI, String localName,
- String rawName, Attributes attrs )
+ String rawName, Attributes attrs )
{
- int i;
- boolean preserveSpace;
- ElementState state;
- String name;
- String value;
- boolean addNSAttr = false;
-
- if ( _writer == null )
- throw new IllegalStateException( "SER002 No writer supplied for serializer" );
-
- state = getElementState();
- if ( state == null ) {
- // If this is the root element handle it differently.
- // If the first root element in the document, serialize
- // the document's DOCTYPE. Space preserving defaults
- // to that of the output format.
- if ( ! _started )
- startDocument( localName == null ? rawName : localName );
- preserveSpace = _format.getPreserveSpace();
- } else {
- // For any other element, if first in parent, then
- // close parent's opening tag and use the parnet's
- // space preserving.
- if ( state.empty )
- printText( ">" );
- preserveSpace = state.preserveSpace;
- // Indent this element on a new line if the first
- // content of the parent element or immediately
- // following an element.
- if ( _format.getIndenting() && ! state.preserveSpace &&
- ( state.empty || state.afterElement ) )
- breakLine();
- }
- // Do not change the current element state yet.
- // This only happens in endElement().
-
- if ( rawName == null ) {
- rawName = localName;
- if ( namespaceURI != null ) {
- String prefix;
- prefix = getPrefix( namespaceURI );
- if ( prefix.length() > 0 )
- rawName = prefix + ":" + localName;
- }
- addNSAttr = true;
- }
-
- printText( '<' + rawName );
- indent();
-
- // For each attribute print it's name and value as one part,
- // separated with a space so the element can be broken on
- // multiple lines.
- if ( attrs != null ) {
- for ( i = 0 ; i < attrs.getLength() ; ++i ) {
- printSpace();
-
- name = attrs.getRawName( i );
- if ( name == null ) {
- String prefix;
- String attrURI;
-
- name = attrs.getLocalName( i );
- attrURI = attrs.getURI( i );
- if ( attrURI != null && ( namespaceURI == null ||
- ! attrURI.equals( namespaceURI ) ) ) {
- prefix = getPrefix( attrURI );
- if ( prefix != null && prefix.length() > 0 )
- name = prefix + ":" + name;
- }
- }
-
- value = attrs.getValue( i );
- if ( value == null )
- value = "";
- printText( name + "=\"" + escape( value ) + '"' );
-
- // If the attribute xml:space exists, determine whether
- // to preserve spaces in this and child nodes based on
- // its value.
- if ( name.equals( "xml:space" ) ) {
- if ( value.equals( "preserve" ) )
- preserveSpace = true;
- else
- preserveSpace = _format.getPreserveSpace();
- }
- }
- }
-
- if ( addNSAttr ) {
- Enumeration enum;
-
- enum = _prefixes.keys();
- while ( enum.hasMoreElements() ) {
- printSpace();
- value = (String) enum.nextElement();
- name = (String) _prefixes.get( value );
- if ( name.length() == 0 )
- printText( "xmlns=\"" + value + '"' );
- else
- printText( "xmlns:" + name + "=\"" + value + '"' );
- }
- }
-
- // Now it's time to enter a new element state
- // with the tag name and space preserving.
- // We still do not change the curent element state.
- state = enterElementState( namespaceURI, localName, rawName, preserveSpace );
- state.doCData = _format.isCDataElement( namespaceURI == null ? rawName :
- namespaceURI + "^" + localName );
- state.unescaped = _format.isNonEscapingElement( namespaceURI == null ? rawName :
- namespaceURI + "^" + localName );
+ int i;
+ boolean preserveSpace;
+ ElementState state;
+ String name;
+ String value;
+ boolean addNSAttr = false;
+
+ if ( _writer == null )
+ throw new IllegalStateException( "SER002 No writer supplied for serializer" );
+
+ state = getElementState();
+ if ( state == null ) {
+ // If this is the root element handle it differently.
+ // If the first root element in the document, serialize
+ // the document's DOCTYPE. Space preserving defaults
+ // to that of the output format.
+ if ( ! _started )
+ startDocument( localName == null ? rawName : localName );
+ preserveSpace = _format.getPreserveSpace();
+ } else {
+ // For any other element, if first in parent, then
+ // close parent's opening tag and use the parnet's
+ // space preserving.
+ if ( state.empty )
+ printText( ">" );
+ preserveSpace = state.preserveSpace;
+ // Indent this element on a new line if the first
+ // content of the parent element or immediately
+ // following an element.
+ if ( _format.getIndenting() && ! state.preserveSpace &&
+ ( state.empty || state.afterElement ) )
+ breakLine();
+ }
+ // Do not change the current element state yet.
+ // This only happens in endElement().
+
+ if ( rawName == null ) {
+ rawName = localName;
+ if ( namespaceURI != null ) {
+ String prefix;
+ prefix = getPrefix( namespaceURI );
+ if ( prefix.length() > 0 )
+ rawName = prefix + ":" + localName;
+ }
+ addNSAttr = true;
+ }
+
+ printText( '<' + rawName );
+ indent();
+
+ // For each attribute print it's name and value as one part,
+ // separated with a space so the element can be broken on
+ // multiple lines.
+ if ( attrs != null ) {
+ for ( i = 0 ; i < attrs.getLength() ; ++i ) {
+ printSpace();
+
+ name = attrs.getRawName( i );
+ if ( name == null ) {
+ String prefix;
+ String attrURI;
+
+ name = attrs.getLocalName( i );
+ attrURI = attrs.getURI( i );
+ if ( attrURI != null && ( namespaceURI == null ||
+ ! attrURI.equals( namespaceURI ) ) ) {
+ prefix = getPrefix( attrURI );
+ if ( prefix != null && prefix.length() > 0 )
+ name = prefix + ":" + name;
+ }
+ }
+
+ value = attrs.getValue( i );
+ if ( value == null )
+ value = "";
+ printText( name + "=\"" + escape( value ) + '"' );
+
+ // If the attribute xml:space exists, determine whether
+ // to preserve spaces in this and child nodes based on
+ // its value.
+ if ( name.equals( "xml:space" ) ) {
+ if ( value.equals( "preserve" ) )
+ preserveSpace = true;
+ else
+ preserveSpace = _format.getPreserveSpace();
+ }
+ }
+ }
+
+ if ( addNSAttr ) {
+ Enumeration enum;
+
+ enum = _prefixes.keys();
+ while ( enum.hasMoreElements() ) {
+ printSpace();
+ value = (String) enum.nextElement();
+ name = (String) _prefixes.get( value );
+ if ( name.length() == 0 )
+ printText( "xmlns=\"" + value + '"' );
+ else
+ printText( "xmlns:" + name + "=\"" + value + '"' );
+ }
+ }
+
+ // Now it's time to enter a new element state
+ // with the tag name and space preserving.
+ // We still do not change the curent element state.
+ state = enterElementState( namespaceURI, localName, rawName, preserveSpace );
+ state.doCData = _format.isCDataElement( namespaceURI == null ? rawName :
+ namespaceURI + "^" + localName );
+ state.unescaped = _format.isNonEscapingElement( namespaceURI == null ? rawName :
+ namespaceURI + "^" + localName );
}
-
-
+
+
public void endElement( String namespaceURI, String localName,
- String rawName )
+ String rawName )
{
- ElementState state;
-
- // Works much like content() with additions for closing
- // an element. Note the different checks for the closed
- // element's state and the parent element's state.
- unindent();
- state = getElementState();
- if ( state.empty ) {
- printText( "/>" );
- } else {
- // Must leave CData section first
- if ( state.inCData )
- printText( "]]>" );
- // This element is not empty and that last content was
- // another element, so print a line break before that
- // last element and this element's closing tag.
- if ( _format.getIndenting() && ! state.preserveSpace && state.afterElement )
- breakLine();
- printText( "</" + state.rawName + ">" );
- }
- // Leave the element state and update that of the parent
- // (if we're not root) to not empty and after element.
- state = leaveElementState();
- if ( state != null ) {
- state.afterElement = true;
- state.empty = false;
- } else {
- // [keith] If we're done printing the document but don't
- // get to call endDocument(), the buffer should be flushed.
- flush();
- }
+ ElementState state;
+
+ // Works much like content() with additions for closing
+ // an element. Note the different checks for the closed
+ // element's state and the parent element's state.
+ unindent();
+ state = getElementState();
+ if ( state.empty ) {
+ printText( "/>" );
+ } else {
+ // Must leave CData section first
+ if ( state.inCData )
+ printText( "]]>" );
+ // This element is not empty and that last content was
+ // another element, so print a line break before that
+ // last element and this element's closing tag.
+ if ( _format.getIndenting() && ! state.preserveSpace && state.afterElement )
+ breakLine();
+ printText( "</" + state.rawName + ">" );
+ }
+ // Leave the element state and update that of the parent
+ // (if we're not root) to not empty and after element.
+ state = leaveElementState();
+ if ( state != null ) {
+ state.afterElement = true;
+ state.empty = false;
+ } else {
+ // [keith] If we're done printing the document but don't
+ // get to call endDocument(), the buffer should be flushed.
+ flush();
+ }
}
-
-
+
+
//------------------------------------------//
// SAX document handler serializing methods //
//------------------------------------------//
-
-
+
+
public void startDocument()
{
- if ( _writer == null )
- throw new IllegalStateException( "SER002 No writer supplied for serializer" );
- // Nothing to do here. All the magic happens in startDocument(String)
+ if ( _writer == null )
+ throw new IllegalStateException( "SER002 No writer supplied for serializer" );
+ // Nothing to do here. All the magic happens in startDocument(String)
}
-
-
+
+
public void startElement( String tagName, AttributeList attrs )
{
- int i;
- boolean preserveSpace;
- ElementState state;
- String name;
- String value;
-
- if ( _writer == null )
- throw new IllegalStateException( "SER002 No writer supplied for serializer" );
-
- state = getElementState();
- if ( state == null ) {
- // If this is the root element handle it differently.
- // If the first root element in the document, serialize
- // the document's DOCTYPE. Space preserving defaults
- // to that of the output format.
- if ( ! _started )
- startDocument( tagName );
- preserveSpace = _format.getPreserveSpace();
- } else {
- // For any other element, if first in parent, then
- // close parent's opening tag and use the parnet's
- // space preserving.
- if ( state.empty )
- printText( ">" );
- preserveSpace = state.preserveSpace;
- // Indent this element on a new line if the first
- // content of the parent element or immediately
- // following an element.
- if ( _format.getIndenting() && ! state.preserveSpace &&
- ( state.empty || state.afterElement ) )
- breakLine();
- }
- // Do not change the current element state yet.
- // This only happens in endElement().
-
- printText( '<' + tagName );
- indent();
-
- // For each attribute print it's name and value as one part,
- // separated with a space so the element can be broken on
- // multiple lines.
- if ( attrs != null ) {
- for ( i = 0 ; i < attrs.getLength() ; ++i ) {
- printSpace();
- name = attrs.getName( i );
- value = attrs.getValue( i );
- if ( value == null )
- value = "";
- printText( name + "=\"" + escape( value ) + '"' );
-
- // If the attribute xml:space exists, determine whether
- // to preserve spaces in this and child nodes based on
- // its value.
- if ( name.equals( "xml:space" ) ) {
- if ( value.equals( "preserve" ) )
- preserveSpace = true;
- else
- preserveSpace = _format.getPreserveSpace();
- }
- }
- }
- // Now it's time to enter a new element state
- // with the tag name and space preserving.
- // We still do not change the curent element state.
- state = enterElementState( null, null, tagName, preserveSpace );
- state.doCData = _format.isCDataElement( tagName );
- state.unescaped = _format.isNonEscapingElement( tagName );
+ int i;
+ boolean preserveSpace;
+ ElementState state;
+ String name;
+ String value;
+
+ if ( _writer == null )
+ throw new IllegalStateException( "SER002 No writer supplied for serializer" );
+
+ state = getElementState();
+ if ( state == null ) {
+ // If this is the root element handle it differently.
+ // If the first root element in the document, serialize
+ // the document's DOCTYPE. Space preserving defaults
+ // to that of the output format.
+ if ( ! _started )
+ startDocument( tagName );
+ preserveSpace = _format.getPreserveSpace();
+ } else {
+ // For any other element, if first in parent, then
+ // close parent's opening tag and use the parnet's
+ // space preserving.
+ if ( state.empty )
+ printText( ">" );
+ preserveSpace = state.preserveSpace;
+ // Indent this element on a new line if the first
+ // content of the parent element or immediately
+ // following an element.
+ if ( _format.getIndenting() && ! state.preserveSpace &&
+ ( state.empty || state.afterElement ) )
+ breakLine();
+ }
+ // Do not change the current element state yet.
+ // This only happens in endElement().
+
+ printText( '<' + tagName );
+ indent();
+
+ // For each attribute print it's name and value as one part,
+ // separated with a space so the element can be broken on
+ // multiple lines.
+ if ( attrs != null ) {
+ for ( i = 0 ; i < attrs.getLength() ; ++i ) {
+ printSpace();
+ name = attrs.getName( i );
+ value = attrs.getValue( i );
+ if ( value == null )
+ value = "";
+ printText( name + "=\"" + escape( value ) + '"' );
+
+ // If the attribute xml:space exists, determine whether
+ // to preserve spaces in this and child nodes based on
+ // its value.
+ if ( name.equals( "xml:space" ) ) {
+ if ( value.equals( "preserve" ) )
+ preserveSpace = true;
+ else
+ preserveSpace = _format.getPreserveSpace();
+ }
+ }
+ }
+ // Now it's time to enter a new element state
+ // with the tag name and space preserving.
+ // We still do not change the curent element state.
+ state = enterElementState( null, null, tagName, preserveSpace );
+ state.doCData = _format.isCDataElement( tagName );
+ state.unescaped = _format.isNonEscapingElement( tagName );
}
-
-
+
+
public void endElement( String tagName )
{
- endElement( null, null, tagName );
+ endElement( null, null, tagName );
}
@@ -442,90 +442,83 @@
*/
protected void startDocument( String rootTagName )
{
- int i;
- String dtd;
-
- dtd = leaveDTD();
- if ( ! _started ) {
-
- if ( ! _format.getOmitXMLDeclaration() ) {
- StringBuffer buffer;
-
- // Serialize the document declaration appreaing at the head
- // of very XML document (unless asked not to).
- buffer = new StringBuffer( "<?xml version=\"" );
- if ( _format.getVersion() != null )
- buffer.append( _format.getVersion() );
- else
- buffer.append( "1.0" );
- buffer.append( '"' );
- if ( _format.getEncoding() != null ) {
- buffer.append( " encoding=\"" );
- buffer.append( _format.getEncoding() );
- buffer.append( '"' );
- }
- if ( _format.getStandalone() && _format.getDoctypeSystem() == null &&
- _format.getDoctypePublic() == null )
- buffer.append( " standalone=\"yes\"" );
- buffer.append( "?>" );
- printText( buffer.toString() );
- breakLine();
- }
-
- if ( _format.getDoctypeSystem() != null ) {
- // System identifier must be specified to print DOCTYPE.
- // If public identifier is specified print 'PUBLIC
- // <public> <system>', if not, print 'SYSTEM <system>'.
- printText( "<!DOCTYPE " );
- printText( rootTagName );
- if ( _format.getDoctypePublic() != null ) {
- printText( " PUBLIC " );
- printDoctypeURL( _format.getDoctypePublic() );
- if ( _format.getIndenting() ) {
- breakLine();
- for ( i = 0 ; i < 18 + rootTagName.length() ; ++i )
- printText( " " );
- }
- printDoctypeURL( _format.getDoctypeSystem() );
- }
- else {
- printText( " SYSTEM " );
- printDoctypeURL( _format.getDoctypeSystem() );
- }
-
- // If we accumulated any DTD contents while printing.
- // this would be the place to print it.
- if ( dtd != null && dtd.length() > 0 ) {
- printText( " [" );
- indent();
- if ( _format.getIndenting() )
- breakLine();
- printText( dtd, true );
- unindent();
- printText( "]" );
- }
-
- printText( ">" );
- breakLine();
- } else if ( dtd != null && dtd.length() > 0 ) {
- printText( "<!DOCTYPE " );
- printText( rootTagName );
- printText( " [" );
- indent();
- if ( _format.getIndenting() )
- breakLine();
- printText( dtd, true );
- unindent();
- printText( "]>" );
- breakLine();
- }
- }
- _started = true;
- // Always serialize these, even if not te first root element.
- serializePreRoot();
+ int i;
+ String dtd;
+
+ dtd = leaveDTD();
+ if ( ! _started ) {
+
+ if ( ! _format.getOmitXMLDeclaration() ) {
+ StringBuffer buffer;
+
+ // Serialize the document declaration appreaing at the head
+ // of very XML document (unless asked not to).
+ buffer = new StringBuffer( "<?xml version=\"" );
+ if ( _format.getVersion() != null )
+ buffer.append( _format.getVersion() );
+ else
+ buffer.append( "1.0" );
+ buffer.append( '"' );
+ if ( _format.getEncoding() != null ) {
+ buffer.append( " encoding=\"" );
+ buffer.append( _format.getEncoding() );
+ buffer.append( '"' );
+ }
+ if ( _format.getStandalone() && _docTypeSystemId == null &&
+ _docTypePublicId == null )
+ buffer.append( " standalone=\"yes\"" );
+ buffer.append( "?>" );
+ printText( buffer.toString() );
+ breakLine();
+ }
+
+ if ( _docTypeSystemId != null ) {
+ // System identifier must be specified to print DOCTYPE.
+ // If public identifier is specified print 'PUBLIC
+ // <public> <system>', if not, print 'SYSTEM <system>'.
+ printText( "<!DOCTYPE " );
+ printText( rootTagName );
+ if ( _docTypePublicId != null ) {
+ printText( " PUBLIC " );
+ printDoctypeURL( _docTypePublicId );
+ if ( _format.getIndenting() ) {
+ breakLine();
+ for ( i = 0 ; i < 18 + rootTagName.length() ; ++i )
+ printText( " " );
+ } else
+ printText( " " );
+ printDoctypeURL( _docTypeSystemId );
+ }
+ else {
+ printText( " SYSTEM " );
+ printDoctypeURL( _docTypeSystemId );
+ }
+
+ // If we accumulated any DTD contents while printing.
+ // this would be the place to print it.
+ if ( dtd != null && dtd.length() > 0 ) {
+ printText( " [" );
+ printText( dtd, true );
+ printText( "]" );
+ }
+
+ printText( ">" );
+ breakLine();
+ } else if ( dtd != null && dtd.length() > 0 ) {
+ printText( "<!DOCTYPE " );
+ printText( rootTagName );
+ printText( " [" );
+ printText( dtd, true );
+ printText( "]>" );
+ breakLine();
+ }
+ }
+ _started = true;
+ // Always serialize these, even if not te first root element.
+ serializePreRoot();
}
-
-
+
+
/**
* Called to serialize a DOM element. Equivalent to calling {@link
* #startElement}, {@link #endElement} and serializing everything
@@ -533,120 +526,120 @@
*/
protected void serializeElement( Element elem )
{
- Attr attr;
- NamedNodeMap attrMap;
- int i;
- Node child;
- ElementState state;
- boolean preserveSpace;
- String name;
- String value;
- String tagName;
-
- tagName = elem.getTagName();
- state = getElementState();
- if ( state == null ) {
- // If this is the root element handle it differently.
- // If the first root element in the document, serialize
- // the document's DOCTYPE. Space preserving defaults
- // to that of the output format.
- if ( ! _started )
- startDocument( tagName );
- preserveSpace = _format.getPreserveSpace();
- } else {
- // For any other element, if first in parent, then
- // close parent's opening tag and use the parnet's
- // space preserving.
- if ( state.empty )
- printText( ">" );
- preserveSpace = state.preserveSpace;
- // Indent this element on a new line if the first
- // content of the parent element or immediately
- // following an element.
- if ( _format.getIndenting() && ! state.preserveSpace &&
- ( state.empty || state.afterElement ) )
- breakLine();
- }
- // Do not change the current element state yet.
- // This only happens in endElement().
-
- printText( '<' + tagName );
- indent();
-
- // Lookup the element's attribute, but only print specified
- // attributes. (Unspecified attributes are derived from the DTD.
- // For each attribute print it's name and value as one part,
- // separated with a space so the element can be broken on
- // multiple lines.
- attrMap = elem.getAttributes();
- if ( attrMap != null ) {
- for ( i = 0 ; i < attrMap.getLength() ; ++i ) {
- attr = (Attr) attrMap.item( i );
- name = attr.getName();
- value = attr.getValue();
- if ( value == null )
- value = "";
- if ( attr.getSpecified() ) {
- printSpace();
- printText( name + "=\"" + escape( value ) + '"' );
- }
- // If the attribute xml:space exists, determine whether
- // to preserve spaces in this and child nodes based on
- // its value.
- if ( name.equals( "xml:space" ) ) {
- if ( value.equals( "preserve" ) )
- preserveSpace = true;
- else
- preserveSpace = _format.getPreserveSpace();
- }
- }
- }
-
- // If element has children, then serialize them, otherwise
- // serialize en empty tag.
- if ( elem.hasChildNodes() ) {
- // Enter an element state, and serialize the children
- // one by one. Finally, end the element.
- state = enterElementState( null, null, tagName, preserveSpace );
- state.doCData = _format.isCDataElement( tagName );
- state.unescaped = _format.isNonEscapingElement( tagName );
- child = elem.getFirstChild();
- while ( child != null ) {
- serializeNode( child );
- child = child.getNextSibling();
- }
- endElement( tagName );
- } else {
- unindent();
- printText( "/>" );
- if ( state != null ) {
- // After element but parent element is no longer empty.
- state.afterElement = true;
- state.empty = false;
- }
- }
+ Attr attr;
+ NamedNodeMap attrMap;
+ int i;
+ Node child;
+ ElementState state;
+ boolean preserveSpace;
+ String name;
+ String value;
+ String tagName;
+
+ tagName = elem.getTagName();
+ state = getElementState();
+ if ( state == null ) {
+ // If this is the root element handle it differently.
+ // If the first root element in the document, serialize
+ // the document's DOCTYPE. Space preserving defaults
+ // to that of the output format.
+ if ( ! _started )
+ startDocument( tagName );
+ preserveSpace = _format.getPreserveSpace();
+ } else {
+ // For any other element, if first in parent, then
+ // close parent's opening tag and use the parnet's
+ // space preserving.
+ if ( state.empty )
+ printText( ">" );
+ preserveSpace = state.preserveSpace;
+ // Indent this element on a new line if the first
+ // content of the parent element or immediately
+ // following an element.
+ if ( _format.getIndenting() && ! state.preserveSpace &&
+ ( state.empty || state.afterElement ) )
+ breakLine();
+ }
+ // Do not change the current element state yet.
+ // This only happens in endElement().
+
+ printText( '<' + tagName );
+ indent();
+
+ // Lookup the element's attribute, but only print specified
+ // attributes. (Unspecified attributes are derived from the DTD.
+ // For each attribute print it's name and value as one part,
+ // separated with a space so the element can be broken on
+ // multiple lines.
+ attrMap = elem.getAttributes();
+ if ( attrMap != null ) {
+ for ( i = 0 ; i < attrMap.getLength() ; ++i ) {
+ attr = (Attr) attrMap.item( i );
+ name = attr.getName();
+ value = attr.getValue();
+ if ( value == null )
+ value = "";
+ if ( attr.getSpecified() ) {
+ printSpace();
+ printText( name + "=\"" + escape( value ) + '"' );
+ }
+ // If the attribute xml:space exists, determine whether
+ // to preserve spaces in this and child nodes based on
+ // its value.
+ if ( name.equals( "xml:space" ) ) {
+ if ( value.equals( "preserve" ) )
+ preserveSpace = true;
+ else
+ preserveSpace = _format.getPreserveSpace();
+ }
+ }
+ }
+
+ // If element has children, then serialize them, otherwise
+ // serialize en empty tag.
+ if ( elem.hasChildNodes() ) {
+ // Enter an element state, and serialize the children
+ // one by one. Finally, end the element.
+ state = enterElementState( null, null, tagName, preserveSpace );
+ state.doCData = _format.isCDataElement( tagName );
+ state.unescaped = _format.isNonEscapingElement( tagName );
+ child = elem.getFirstChild();
+ while ( child != null ) {
+ serializeNode( child );
+ child = child.getNextSibling();
+ }
+ endElement( tagName );
+ } else {
+ unindent();
+ printText( "/>" );
+ if ( state != null ) {
+ // After element but parent element is no longer empty.
+ state.afterElement = true;
+ state.empty = false;
+ }
+ }
}
-
-
+
+
protected String getEntityRef( char ch )
{
- // Encode special XML characters into the equivalent character references.
- // These five are defined by default for all XML documents.
+ // Encode special XML characters into the equivalent character references.
+ // These five are defined by default for all XML documents.
switch ( ch ) {
- case '<':
- return "lt";
- case '>':
- return "gt";
- case '"':
- return "quot";
- case '\'':
- return "apos";
- case '&':
- return "amp";
+ case '<':
+ return "lt";
+ case '>':
+ return "gt";
+ case '"':
+ return "quot";
+ case '\'':
+ return "apos";
+ case '&':
+ return "amp";
}
return null;
}
-
+
}