|
|
|
|
|
*/ |
|
/* |
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
|
* contributor license agreements. See the NOTICE file distributed with |
|
* this work for additional information regarding copyright ownership. |
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
|
* (the "License"); you may not use this file except in compliance with |
|
* the License. You may obtain a copy of the License at |
|
* |
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
* |
|
* Unless required by applicable law or agreed to in writing, software |
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
* See the License for the specific language governing permissions and |
|
* limitations under the License. |
|
*/ |
|
|
|
|
|
// Sep 14, 2000: |
|
// Fixed serializer to report IO exception directly, instead at |
|
// the end of document processing. |
|
// Reported by Patrick Higgins <phiggins@transzap.com> |
|
|
|
|
|
package com.sun.org.apache.xml.internal.serialize; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
import org.w3c.dom.Element; |
|
import org.w3c.dom.Node; |
|
import org.xml.sax.AttributeList; |
|
import org.xml.sax.Attributes; |
|
import org.xml.sax.SAXException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@Deprecated |
|
public class TextSerializer |
|
extends BaseMarkupSerializer |
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public TextSerializer() |
|
{ |
|
super( new OutputFormat( Method.TEXT, null, false ) ); |
|
} |
|
|
|
|
|
public void setOutputFormat( OutputFormat format ) |
|
{ |
|
super.setOutputFormat( format != null ? format : new OutputFormat( Method.TEXT, null, false ) ); |
|
} |
|
|
|
|
|
//-----------------------------------------// |
|
// SAX content handler serializing methods // |
|
//-----------------------------------------// |
|
|
|
|
|
public void startElement( String namespaceURI, String localName, |
|
String rawName, Attributes attrs ) |
|
throws SAXException |
|
{ |
|
startElement( rawName == null ? localName : rawName, null ); |
|
} |
|
|
|
|
|
public void endElement( String namespaceURI, String localName, |
|
String rawName ) |
|
throws SAXException |
|
{ |
|
endElement( rawName == null ? localName : rawName ); |
|
} |
|
|
|
|
|
//------------------------------------------// |
|
// SAX document handler serializing methods // |
|
//------------------------------000---------// |
|
|
|
|
|
public void startElement( String tagName, AttributeList attrs ) |
|
throws SAXException |
|
{ |
|
boolean preserveSpace; |
|
ElementState state; |
|
|
|
try { |
|
state = getElementState(); |
|
if ( isDocumentState() ) { |
|
// 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 |
|
|
|
if ( ! _started ) |
|
startDocument( tagName ); |
|
} |
|
// For any other element, if first in parent, then |
|
|
|
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. |
|
|
|
state = enterElementState( null, null, tagName, preserveSpace ); |
|
} catch ( IOException except ) { |
|
throw new SAXException( except ); |
|
} |
|
} |
|
|
|
|
|
public void endElement( String tagName ) |
|
throws SAXException |
|
{ |
|
try { |
|
endElementIO( tagName ); |
|
} catch ( IOException except ) { |
|
throw new SAXException( except ); |
|
} |
|
} |
|
|
|
|
|
public void endElementIO( String tagName ) |
|
throws IOException |
|
{ |
|
ElementState state; |
|
|
|
// Works much like content() with additions for closing |
|
// an element. Note the different checks for the closed |
|
|
|
state = getElementState(); |
|
// Leave the element state and update that of the parent |
|
|
|
state = leaveElementState(); |
|
state.afterElement = true; |
|
state.empty = false; |
|
if ( isDocumentState() ) |
|
_printer.flush(); |
|
} |
|
|
|
|
|
public void processingInstructionIO( String target, String code ) throws IOException |
|
{ |
|
} |
|
|
|
|
|
public void comment( String text ) |
|
{ |
|
} |
|
|
|
|
|
public void comment( char[] chars, int start, int length ) |
|
{ |
|
} |
|
|
|
|
|
public void characters( char[] chars, int start, int length ) |
|
throws SAXException |
|
{ |
|
ElementState state; |
|
|
|
try { |
|
state = content(); |
|
state.doCData = state.inCData = false; |
|
printText( chars, start, length, true, true ); |
|
} catch ( IOException except ) { |
|
throw new SAXException( except ); |
|
} |
|
} |
|
|
|
|
|
protected void characters( String text, boolean unescaped ) |
|
throws IOException |
|
{ |
|
ElementState state; |
|
|
|
state = content(); |
|
state.doCData = state.inCData = false; |
|
printText( text, true, true ); |
|
} |
|
|
|
|
|
//------------------------------------------// |
|
// Generic node serializing methods methods // |
|
//------------------------------------------// |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected void startDocument( String rootTagName ) |
|
throws IOException |
|
{ |
|
// Required to stop processing the DTD, even though the DTD |
|
|
|
_printer.leaveDTD(); |
|
|
|
_started = true; |
|
|
|
serializePreRoot(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected void serializeElement( Element elem ) |
|
throws IOException |
|
{ |
|
Node child; |
|
ElementState state; |
|
boolean preserveSpace; |
|
String tagName; |
|
|
|
tagName = elem.getTagName(); |
|
state = getElementState(); |
|
if ( isDocumentState() ) { |
|
// 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 |
|
|
|
if ( ! _started ) |
|
startDocument( tagName ); |
|
} |
|
// For any other element, if first in parent, then |
|
|
|
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 |
|
|
|
if ( elem.hasChildNodes() ) { |
|
// Enter an element state, and serialize the children |
|
|
|
state = enterElementState( null, null, tagName, preserveSpace ); |
|
child = elem.getFirstChild(); |
|
while ( child != null ) { |
|
serializeNode( child ); |
|
child = child.getNextSibling(); |
|
} |
|
endElementIO( tagName ); |
|
} else { |
|
if ( ! isDocumentState() ) { |
|
|
|
state.afterElement = true; |
|
state.empty = false; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected void serializeNode( Node node ) |
|
throws IOException |
|
{ |
|
// Based on the node type call the suitable SAX handler. |
|
// Only comments entities and documents which are not |
|
|
|
switch ( node.getNodeType() ) { |
|
case Node.TEXT_NODE : { |
|
String text; |
|
|
|
text = node.getNodeValue(); |
|
if ( text != null ) |
|
characters( node.getNodeValue(), true ); |
|
break; |
|
} |
|
|
|
case Node.CDATA_SECTION_NODE : { |
|
String text; |
|
|
|
text = node.getNodeValue(); |
|
if ( text != null ) |
|
characters( node.getNodeValue(), true ); |
|
break; |
|
} |
|
|
|
case Node.COMMENT_NODE : |
|
break; |
|
|
|
case Node.ENTITY_REFERENCE_NODE : |
|
|
|
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 |
|
|
|
child = node.getFirstChild(); |
|
while ( child != null ) { |
|
serializeNode( child ); |
|
child = child.getNextSibling(); |
|
} |
|
break; |
|
} |
|
|
|
default: |
|
break; |
|
} |
|
} |
|
|
|
|
|
protected ElementState content() |
|
{ |
|
ElementState state; |
|
|
|
state = getElementState(); |
|
if ( ! isDocumentState() ) { |
|
// If this is the first content in the element, |
|
|
|
if ( state.empty ) |
|
state.empty = false; |
|
// Except for one content type, all of them |
|
// are not last element. That one content |
|
|
|
state.afterElement = false; |
|
} |
|
return state; |
|
} |
|
|
|
|
|
protected String getEntityRef( int ch ) |
|
{ |
|
return null; |
|
} |
|
|
|
|
|
} |