You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by ab...@locus.apache.org on 2000/08/15 21:26:04 UTC
cvs commit: xml-xerces/java/src/org/apache/xml/serialize OutputFormat.java BaseMarkupSerializer.java
abagchi 00/08/15 12:26:00
Modified: java/src/org/apache/xml/serialize OutputFormat.java
BaseMarkupSerializer.java
Log:
Added strip comments feature
Revision Changes Path
1.8 +57 -31 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.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- OutputFormat.java 2000/05/30 21:54:08 1.7
+++ OutputFormat.java 2000/08/15 19:25:56 1.8
@@ -2,7 +2,7 @@
* The Apache Software License, Version 1.1
*
*
- * Copyright (c) 1999 The Apache Software Foundation. All rights
+ * Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -10,7 +10,7 @@
* are met:
*
* 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
+ * notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
@@ -18,7 +18,7 @@
* distribution.
*
* 3. The end-user documentation included with the redistribution,
- * if any, must include the following acknowledgment:
+ * if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
@@ -26,7 +26,7 @@
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
- * software without prior written permission. For written
+ * software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
@@ -81,9 +81,9 @@
* compatible with the document type (XML, HTML, Text, etc), encoding and
* indentation
* </ul>
- *
*
- * @version $Revision: 1.7 $ $Date: 2000/05/30 21:54:08 $
+ *
+ * @version $Revision: 1.8 $ $Date: 2000/08/15 19:25:56 $
* @author <a href="mailto:arkin@exoffice.com">Assaf Arkin</a>
* <a href="mailto:visco@exoffice.com">Keith Visco</a>
* @see Serializer
@@ -101,31 +101,31 @@
* 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.
@@ -133,23 +133,23 @@
* @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.
@@ -202,6 +202,12 @@
/**
+ * Ture if the comments should be ommited;
+ */
+ private boolean _stripComments = false;
+
+
+ /**
* True if the document type should be marked as standalone.
*/
private boolean _standalone = false;
@@ -284,7 +290,7 @@
setDoctype( whichDoctypePublic( doc ), whichDoctypeSystem( doc ) );
setMediaType( whichMediaType( getMethod() ) );
}
-
+
/**
* Constructs a new output format with the proper method,
@@ -377,7 +383,7 @@
{
return _indent;
}
-
+
/**
* Returns true if indentation was specified.
@@ -534,6 +540,26 @@
_omitXmlDeclaration = omit;
}
+ /**
+ * Returns true if comments should
+ * be ommited. The default is false.
+ */
+ public boolean getStripComments()
+ {
+ return _stripComments;
+ }
+
+
+ /**
+ * Sets stripping comments on and off.
+ *
+ * @param strip True if comments should be ommited
+ */
+ public void setStripComments( boolean strip )
+ {
+ _stripComments = strip;
+ }
+
/**
* Returns true if the document type is standalone.
@@ -579,7 +605,7 @@
public boolean isCDataElement( String tagName )
{
int i;
-
+
if ( _cdataElements == null )
return false;
for ( i = 0 ; i < _cdataElements.length ; ++i )
@@ -622,7 +648,7 @@
public boolean isNonEscapingElement( String tagName )
{
int i;
-
+
if ( _nonEscapingElements == null )
return false;
for ( i = 0 ; i < _nonEscapingElements.length ; ++i )
@@ -674,8 +700,8 @@
else
_lineSeparator = lineSeparator;
}
-
-
+
+
/**
* Returns true if the default behavior for this format is to
* preserve spaces. All elements that do not specify otherwise
@@ -768,20 +794,20 @@
// 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.
-
+
// 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" ) ) {
+ } else if ( node.getNodeName().equalsIgnoreCase( "root" ) ) {
return Method.FOP;
} else {
return Method.XML;
@@ -801,7 +827,7 @@
// Anything else, the method is xml.
return Method.XML;
}
-
+
/**
* Returns the document type public identifier
@@ -810,7 +836,7 @@
public static String whichDoctypePublic( Document doc )
{
DocumentType doctype;
-
+
/* XXX Delayed until DOM Level 2 is introduced into the code base
doctype = doc.getDoctype();
if ( doctype != null ) {
@@ -834,7 +860,7 @@
public static String whichDoctypeSystem( Document doc )
{
DocumentType doctype;
-
+
/* XXX Delayed until DOM Level 2 is introduced into the code base
doctype = doc.getDoctype();
if ( doctype != null ) {
1.17 +100 -98 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.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- BaseMarkupSerializer.java 2000/05/26 20:20:50 1.16
+++ BaseMarkupSerializer.java 2000/08/15 19:25:56 1.17
@@ -2,7 +2,7 @@
* The Apache Software License, Version 1.1
*
*
- * Copyright (c) 1999 The Apache Software Foundation. All rights
+ * Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -10,7 +10,7 @@
* are met:
*
* 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
+ * notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
@@ -18,7 +18,7 @@
* distribution.
*
* 3. The end-user documentation included with the redistribution,
- * if any, must include the following acknowledgment:
+ * if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
@@ -26,7 +26,7 @@
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
- * software without prior written permission. For written
+ * software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
@@ -116,9 +116,9 @@
* lines. An element is serialized indented when it is the first or
* last child of an element, or immediate following or preceding
* another element.
- *
*
- * @version $Revision: 1.16 $ $Date: 2000/05/26 20:20:50 $
+ *
+ * @version $Revision: 1.17 $ $Date: 2000/08/15 19:25:56 $
* @author <a href="mailto:arkin@exoffice.com">Assaf Arkin</a>
* @see Serializer
* @see DOMSerializer
@@ -244,7 +244,7 @@
protected BaseMarkupSerializer( OutputFormat format )
{
int i;
-
+
_elementStates = new ElementState[ 10 ];
for ( i = 0 ; i < _elementStates.length ; ++i )
_elementStates[ i ] = new ElementState();
@@ -279,7 +279,7 @@
public void setOutputByteStream( OutputStream output )
{
String encoding;
-
+
if ( output == null )
throw new NullPointerException( "SER001 Argument 'output' is null." );
_output = output;
@@ -296,8 +296,8 @@
_output = null;
reset();
}
-
-
+
+
public void setOutputFormat( OutputFormat format )
{
if ( format == null )
@@ -305,8 +305,8 @@
_format = format;
reset();
}
-
-
+
+
public boolean reset()
{
if ( _elementStateCount > 1 )
@@ -421,7 +421,7 @@
if ( _printer.getException() != null )
throw _printer.getException();
}
-
+
/**
* Serializes the DOM document using the previously specified
@@ -447,7 +447,7 @@
if ( _printer.getException() != null )
throw _printer.getException();
}
-
+
//------------------------------------------//
// SAX document handler serializing methods //
@@ -457,15 +457,15 @@
public void characters( char[] chars, int start, int length )
{
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.inCData || state.doCData ) {
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.
@@ -476,9 +476,9 @@
saveIndent = _printer.getNextIndent();
_printer.setNextIndent( 0 );
for ( int index = 0 ; index < length ; ++index ) {
- if ( index + 2 < length && chars[ index ] == ']' &&
+ if ( index + 2 < length && chars[ index ] == ']' &&
chars[ index + 1 ] == ']' && chars[ index + 2 ] == '>' ) {
-
+
printText( chars, start, index + 2, true, true );
_printer.printText( "]]><![CDATA[" );
start += index + 2;
@@ -489,11 +489,11 @@
if ( length > 0 )
printText( chars, start, length, true, true );
_printer.setNextIndent( saveIndent );
-
+
} else {
-
+
int saveIndent;
-
+
if ( state.preserveSpace ) {
// If preserving space then hold of indentation so no
// excessive spaces are printed at line breaks, escape
@@ -508,14 +508,14 @@
}
}
}
-
+
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.
@@ -529,14 +529,14 @@
}
}
}
-
-
+
+
public void processingInstruction( String target, String code )
{
int index;
StringBuffer buffer;
ElementState state;
-
+
state = content();
buffer = new StringBuffer( 40 );
@@ -556,7 +556,7 @@
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 ( isDocumentState() ) {
@@ -569,8 +569,8 @@
_printer.unindent();
}
}
-
-
+
+
public void comment( char[] chars, int start, int length )
{
comment( new String( chars, start, length ) );
@@ -582,7 +582,9 @@
StringBuffer buffer;
int index;
ElementState state;
-
+
+ if (_format.getStripComments()) return;
+
state = content();
buffer = new StringBuffer( 40 );
// Create the processing comment textual representation.
@@ -592,7 +594,7 @@
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 ( isDocumentState() ) {
@@ -610,16 +612,16 @@
public void startCDATA()
{
ElementState state;
-
+
state = getElementState();
state.doCData = true;
}
-
-
+
+
public void endCDATA()
{
ElementState state;
-
+
state = getElementState();
state.doCData = false;
}
@@ -628,7 +630,7 @@
public void startNonEscaping()
{
ElementState state;
-
+
state = getElementState();
state.unescaped = true;
}
@@ -637,7 +639,7 @@
public void endNonEscaping()
{
ElementState state;
-
+
state = getElementState();
state.unescaped = false;
}
@@ -646,7 +648,7 @@
public void startPreserving()
{
ElementState state;
-
+
state = getElementState();
state.preserveSpace = true;
}
@@ -655,7 +657,7 @@
public void endPreserving()
{
ElementState state;
-
+
state = getElementState();
state.preserveSpace = false;
}
@@ -716,8 +718,8 @@
_printer.printText( name );
_printer.printText( ';' );
}
-
-
+
+
public void startPrefixMapping( String prefix, String uri )
throws SAXException
{
@@ -725,13 +727,13 @@
_prefixes = new Hashtable();
_prefixes.put( uri, prefix == null ? "" : prefix );
}
-
-
+
+
public void endPrefixMapping( String prefix )
throws SAXException
{
}
-
+
//------------------------------------------//
// SAX DTD/Decl handler serializing methods //
@@ -744,14 +746,14 @@
_docTypePublicId = publicId;
_docTypeSystemId = systemId;
}
-
-
+
+
public void endDTD()
{
// Nothing to do here, all the magic occurs in startDocument(String).
}
-
-
+
+
public void elementDecl( String name, String model )
{
_printer.enterDTD();
@@ -763,8 +765,8 @@
if ( _indenting )
_printer.breakLine();
}
-
-
+
+
public void attributeDecl( String eName, String aName, String type,
String valueDefault, String value )
{
@@ -782,14 +784,14 @@
if ( value != null ) {
_printer.printText( " \"" );
printEscaped( value );
- _printer.printText( '"' );
+ _printer.printText( '"' );
}
_printer.printText( '>' );
if ( _indenting )
_printer.breakLine();
}
-
-
+
+
public void internalEntityDecl( String name, String value )
{
_printer.enterDTD();
@@ -801,15 +803,15 @@
if ( _indenting )
_printer.breakLine();
}
-
-
+
+
public void externalEntityDecl( String name, String publicId, String systemId )
{
_printer.enterDTD();
unparsedEntityDecl( name, publicId, systemId, null );
}
-
-
+
+
public void unparsedEntityDecl( String name, String publicId,
String systemId, String notationName )
{
@@ -835,8 +837,8 @@
if ( _indenting )
_printer.breakLine();
}
-
-
+
+
public void notationDecl( String name, String publicId, String systemId )
{
_printer.enterDTD();
@@ -859,8 +861,8 @@
if ( _indenting )
_printer.breakLine();
}
-
-
+
+
//------------------------------------------//
// Generic node serializing methods methods //
//------------------------------------------//
@@ -882,16 +884,16 @@
switch ( node.getNodeType() ) {
case Node.TEXT_NODE : {
String text;
-
+
text = node.getNodeValue();
if ( text != null )
characters( node.getNodeValue() );
break;
}
-
+
case Node.CDATA_SECTION_NODE : {
String text;
-
+
text = node.getNodeValue();
if ( text != null ) {
startCDATA();
@@ -900,16 +902,16 @@
}
break;
}
-
+
case Node.COMMENT_NODE : {
String text;
-
+
text = node.getNodeValue();
if ( text != null )
comment( node.getNodeValue() );
break;
}
-
+
case Node.ENTITY_REFERENCE_NODE : {
Node child;
@@ -922,15 +924,15 @@
}
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;
@@ -938,7 +940,7 @@
Entity entity;
Notation notation;
int i;
-
+
// If there is a document type, use the SAX events to
// serialize it.
docType = ( (Document) node ).getDoctype();
@@ -986,7 +988,7 @@
}
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.
@@ -997,13 +999,13 @@
}
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
@@ -1011,11 +1013,11 @@
* state with <tt>empty</tt> and <tt>afterElement</tt> set to false.
*
* @return The current element state
- */
+ */
protected ElementState content()
{
ElementState state;
-
+
state = getElementState();
if ( ! isDocumentState() ) {
// Need to close CData section first
@@ -1052,17 +1054,17 @@
protected void characters( String text )
{
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.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.
@@ -1082,11 +1084,11 @@
_printer.setNextIndent( 0 );
printText( buffer.toString(), true, true );
_printer.setNextIndent( saveIndent );
-
+
} else {
-
+
int saveIndent;
-
+
if ( state.preserveSpace ) {
// If preserving space then hold of indentation so no
// excessive spaces are printed at line breaks, escape
@@ -1112,7 +1114,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
@@ -1134,7 +1136,7 @@
protected void serializePreRoot()
{
int i;
-
+
if ( _preRoot != null ) {
for ( i = 0 ; i < _preRoot.size() ; ++i ) {
printText( (String) _preRoot.elementAt( i ), true, true );
@@ -1168,7 +1170,7 @@
{
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
@@ -1208,7 +1210,7 @@
{
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
@@ -1252,7 +1254,7 @@
protected void printDoctypeURL( String url )
{
int i;
-
+
_printer.printText( '"' );
for( i = 0 ; i < url.length() ; ++i ) {
if ( url.charAt( i ) == '"' || url.charAt( i ) < 0x20 || url.charAt( i ) > 0x7F ) {
@@ -1268,7 +1270,7 @@
protected void printEscaped( char ch )
{
String charRef;
-
+
// If there is a suitable entity reference for this
// character, print it. The list of available entity
// references is almost but not identical between
@@ -1334,10 +1336,10 @@
String rawName, boolean preserveSpace )
{
ElementState state;
-
+
if ( _elementStateCount + 1 == _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 + 10 ];
@@ -1362,8 +1364,8 @@
_prefixes = null;
return state;
}
-
-
+
+
/**
* Leave the current element state and return to the
* state of the parent element. If this was the root
@@ -1393,8 +1395,8 @@
{
return _elementStateCount == 0;
}
-
-
+
+
/**
* Returns the namespace prefix for the specified URI.
* If the URI has been mapped to a prefix, returns the
@@ -1406,7 +1408,7 @@
protected String getPrefix( String namespaceURI )
{
String prefix;
-
+
if ( _prefixes != null ) {
prefix = (String) _prefixes.get( namespaceURI );
if ( prefix != null )