You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by sa...@apache.org on 2002/05/23 18:13:39 UTC

cvs commit: xml-xalan/java/src/org/apache/xalan/xsltc/runtime/output StreamOutput.java StreamXMLOutput.java

santiagopg    02/05/23 09:13:38

  Modified:    java/src/org/apache/xalan/xsltc/runtime/output
                        StreamOutput.java StreamXMLOutput.java
  Log:
  
  
  Revision  Changes    Path
  1.5       +7 -1      xml-xalan/java/src/org/apache/xalan/xsltc/runtime/output/StreamOutput.java
  
  Index: StreamOutput.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/runtime/output/StreamOutput.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- StreamOutput.java	21 May 2002 20:18:19 -0000	1.4
  +++ StreamOutput.java	23 May 2002 16:13:38 -0000	1.5
  @@ -1,5 +1,5 @@
   /*
  - * @(#)$Id: StreamOutput.java,v 1.4 2002/05/21 20:18:19 santiagopg Exp $
  + * @(#)$Id: StreamOutput.java,v 1.5 2002/05/23 16:13:38 santiagopg Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -91,6 +91,8 @@
   
       protected boolean _indent = false;
       protected boolean _omitHeader = false;
  +    protected String  _standalone = null;
  +    protected String  _version    = "1.0";
   
       protected boolean _lineFeedNextStartTag = false;
       protected boolean _linefeedNextEndTag = false;
  @@ -119,6 +121,10 @@
   
       public void omitHeader(boolean value) {
           _omitHeader = value;
  +    }
  +
  +    public void setStandalone(String standalone) {
  +	_standalone = standalone;
       }
   
       protected void appendDTD(String name) {
  
  
  
  1.3       +122 -31   xml-xalan/java/src/org/apache/xalan/xsltc/runtime/output/StreamXMLOutput.java
  
  Index: StreamXMLOutput.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/runtime/output/StreamXMLOutput.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- StreamXMLOutput.java	22 May 2002 18:09:34 -0000	1.2
  +++ StreamXMLOutput.java	23 May 2002 16:13:38 -0000	1.3
  @@ -1,5 +1,5 @@
   /*
  - * @(#)$Id: StreamXMLOutput.java,v 1.2 2002/05/22 18:09:34 santiagopg Exp $
  + * @(#)$Id: StreamXMLOutput.java,v 1.3 2002/05/23 16:13:38 santiagopg Exp $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -79,6 +79,14 @@
   
   public class StreamXMLOutput extends StreamOutput implements Constants {
   
  +    private static final String BEGCDATA = "<![CDATA[";
  +    private static final String ENDCDATA = "]]>";
  +    private static final String CNTCDATA = "]]]]><![CDATA[>";
  +    private static final String BEGCOMM  = "<!--";
  +    private static final String ENDCOMM  = "-->";
  +    private static final String CDATA_ESC_START = "]]>&#";
  +    private static final String CDATA_ESC_END   = ";<![CDATA[";
  +
       /**
        * Holds the current tree depth.
        */
  @@ -111,6 +119,8 @@
        */
       private Stack _cdataStack;
   
  +    private boolean _cdataTagOpen = false;
  +
       private HashSet _attributes = new HashSet();
   
       static class Attribute {
  @@ -162,17 +172,44 @@
   	// CDATA stack
   	_cdataStack = new Stack();
   	_cdataStack.push(new Integer(-1)); 	// push dummy value
  -	initNamespaces();
  +
  +	// Namespaces
  +	_namespaces = new Hashtable();
  +	_nodeStack = new Stack();
  +	_prefixStack = new Stack();
  +
  +	// Define the default namespace (initially maps to "" uri)
  +	Stack stack;
  +	_namespaces.put(EMPTYSTRING, stack = new Stack());
  +	stack.push(EMPTYSTRING);
  +	_prefixStack.push(EMPTYSTRING);
  +
  +	_namespaces.put(XML_PREFIX, stack = new Stack());
  +	stack.push("http://www.w3.org/XML/1998/namespace");
  +	_prefixStack.push(XML_PREFIX);
  +
  +	_nodeStack.push(new Integer(-1));
  +	_depth = 0;
       }
   
       public void startDocument() throws TransletException { 
  -	// empty
  +	if (!_omitHeader) {
  +	    _buffer.append("<?xml version=\"").append(_version)
  +	           .append("\" encoding=\"").append(_encoding);
  +	    if (_standalone != null) {
  +		_buffer.append("\" standalone=\"").append(_standalone);
  +	    }
  +	    _buffer.append("\"?>\n");
  +	}
       }
   
       public void endDocument() throws TransletException { 
   	if (_startTagOpen) {
   	    _buffer.append("/>");
   	}
  +	else if (_cdataTagOpen) {
  +	    closeCDATA();
  +	}
   
   	try {
   	    int n = 0;
  @@ -199,6 +236,9 @@
   	if (_startTagOpen) {
   	    _buffer.append('>');
   	}
  +	else if (_cdataTagOpen) {
  +	    closeCDATA();
  +	}
   
   	// Handle document type declaration (for first element only)
   	if (_firstElement) {
  @@ -208,6 +248,10 @@
   	    _firstElement = false;
   	}
   
  +	if (_cdata != null && _cdata.containsKey(elementName)) {
  +	    _cdataStack.push(new Integer(_depth));
  +	}
  +
   	if (_indent) {
   	    indent(_lineFeedNextStartTag);
   	    _lineFeedNextStartTag = true;
  @@ -228,6 +272,7 @@
   	    _startTagOpen = false;
   	    _buffer.append("/>");
   	    _indentLevel--;
  +	    _indentNextEndTag = true;
   	}
   	else {
   	    if (_indent) {
  @@ -245,17 +290,26 @@
   	_depth--;
       }
   
  -    public void characters(String characters)
  -	throws TransletException 
  -    { 
  +    public void characters(String characters) throws TransletException { 
   	if (_startTagOpen) {
   	    _buffer.append('>');
   	    _startTagOpen = false;
   	}
   
  -	if (_escaping) {
  -	    escapeCharacters(characters.toCharArray(), 0, characters.length());
  -	}
  +	final Integer I = (Integer) _cdataStack.peek();
  +	if (I.intValue() == _depth && !_cdataTagOpen) {
  +	    startCDATA(characters.toCharArray(), 0, characters.length());
  +	} 
  +	else if (_escaping) {
  +	    if (_cdataTagOpen) {
  +		escapeCDATA(characters.toCharArray(), 0, 
  +			    characters.length());
  +	    } 
  +	    else {
  +		escapeCharacters(characters.toCharArray(), 0, 
  +			         characters.length());
  +	    }
  +	} 
   	else {
   	    _buffer.append(characters);
   	}
  @@ -347,28 +401,6 @@
       }
   
       /**
  -     * Initialize namespace stacks
  -     */
  -    private void initNamespaces() {
  -	_namespaces = new Hashtable();
  -	_nodeStack = new Stack();
  -	_prefixStack = new Stack();
  -
  -	// Define the default namespace (initially maps to "" uri)
  -	Stack stack;
  -	_namespaces.put(EMPTYSTRING, stack = new Stack());
  -	stack.push(EMPTYSTRING);
  -	_prefixStack.push(EMPTYSTRING);
  -
  -	_namespaces.put(XML_PREFIX, stack = new Stack());
  -	stack.push("http://www.w3.org/XML/1998/namespace");
  -	_prefixStack.push(XML_PREFIX);
  -
  -	_nodeStack.push(new Integer(-1));
  -	_depth = 0;
  -    }
  -
  -    /**
        * Declare a prefix to point to a namespace URI
        */
       private boolean pushNamespace(String prefix, String uri) {
  @@ -419,6 +451,65 @@
   	    if (i.intValue() != _depth) return;
   	    _nodeStack.pop();
   	    popNamespace((String)_prefixStack.pop());
  +	}
  +    }
  +
  +    /**
  +     * Utility method - pass a whole charactes as CDATA to SAX handler
  +     */
  +    private void startCDATA(char[] ch, int off, int len) {
  +	final int limit = off + len;
  +	int offset = off;
  +
  +	// Output start bracket - "<![CDATA["
  +	_buffer.append(BEGCDATA);
  +
  +	// Detect any occurence of "]]>" in the character array
  +	for (int i = offset; i < limit - 2; i++) {
  +	    if (ch[i] == ']' && ch[i+1] == ']' && ch[i+2] == '>') {
  +		_buffer.append(ch, offset, i - offset);
  +		_buffer.append(CNTCDATA);
  +		offset = i + 3;
  +		i = i + 2; 	// Skip next chars ']' and '>'.
  +	    }
  +	}
  +
  +	// Output the remaining characters
  +	if (offset < limit) {
  +	    _buffer.append(ch, offset, limit - offset);
  +	}
  +	_cdataTagOpen = true;
  +    }
  +
  +    private void closeCDATA() {
  +	_buffer.append(ENDCDATA);
  +	_cdataTagOpen = false;
  +    }
  +
  +    /**
  +     * Utility method - escape special characters and pass to SAX handler
  +     */
  +    private void escapeCDATA(char[] ch, int off, int len) {
  +	int limit = off + len;
  +	int offset = off;
  +
  +	if (limit > ch.length) {
  +	    limit = ch.length;
  +	}
  +
  +	// Step through characters and escape all special characters
  +	for (int i = off; i < limit; i++) {
  +	    if (ch[i] > '\u00ff') { 	// encoding??
  +		_buffer.append(ch, offset, i - offset)
  +		       .append(CDATA_ESC_START)
  +		       .append(Integer.toString((int) ch[i]))
  +		       .append(CDATA_ESC_END);
  +		offset = i + 1;
  +	    }
  +	}
  +	// Output remaining characters 
  +	if (offset < limit) {
  +	    _buffer.append(ch, offset, limit - offset);
   	}
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org