/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed 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
*
*
* 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.
*/
/*
* $Id: ToXMLStream.java,v 1.2.4.2 2005/09/15 12:01:25 suresh_emailid Exp $
*/
/**
* This class converts SAX or SAX-like calls to a
* serialized xml document. The xsl:output method is "xml".
*
* This class is used explicitly in code generated by XSLTC,
* so it is "public", but it should
* be viewed as internal or package private, this is not an API.
*
* @xsl.usage internal
*/
{
/**
* remembers if we need to write out "]]>" to close the CDATA
*/
boolean m_cdataTagOpen = false;
/**
* Map that tells which XML characters should have special treatment, and it
* provides character to entity name lookup.
*/
// new CharInfo(CharInfo.XML_ENTITIES_RESOURCE);
/**
* Default constructor.
*/
public ToXMLStream()
{
initCDATA();
// initialize namespaces
m_prefixMap = new NamespaceMappings();
}
/**
* Copy properties from another SerializerToXML.
*
* @param xmlListener non-null reference to a SerializerToXML object.
*/
{
// m_outputStream = xmlListener.m_outputStream;
}
/**
* Receive notification of the beginning of a document.
*
* @throws org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
*
* @throws org.xml.sax.SAXException
*/
{
{
super.startDocumentInternal();
m_needToCallStartDocument = false;
if (m_inEntityRef)
return;
m_needToOutputDocTypeDecl = true;
m_startNewLine = false;
/* The call to getXMLVersion() might emit an error message
* and we should emit this message regardless of if we are
* writing out an XML header or not.
*/
if (getOmitXMLDeclaration() == false)
{
version = "1.0";
{
}
else
{
standalone = "";
}
try
{
if (m_doIndent) {
|| getDoctypePublic() != null
|| getDoctypeSystem() != null
|| m_isStandalone) {
// We almost never put a newline after the XML
// header because this XML could be used as
// an extenal general parsed entity
// and we don't know the context into which it
// will be used in the future. Only when
// standalone, or a doctype system or public is
// specified are we free to insert a new line
// after the header. Is it even worth bothering
// in these rare cases?
}
}
}
catch(IOException e)
{
throw new SAXException(e);
}
}
}
}
/**
* Receive notification of the end of a document.
*
* @throws org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
*
* @throws org.xml.sax.SAXException
*/
{
flushPending();
if (m_doIndent && !m_isprevtext)
{
try
{
}
catch(IOException e)
{
throw new SAXException(e);
}
}
flushWriter();
super.fireEndDoc();
}
/**
* Starts a whitespace preserving section. All characters printed
* within a preserving section are printed without indentation and
* without consolidating multiple spaces. This is equivalent to
* the <tt>xml:space="preserve"</tt> attribute. Only XML
* and HTML serializers need to support this method.
* <p>
* The contents of the whitespace preserving section will be delivered
* through the regular <tt>characters</tt> event.
*
* @throws org.xml.sax.SAXException
*/
{
// Not sure this is really what we want. -sb
m_preserves.push(true);
m_ispreserve = true;
}
/**
* Ends a whitespace preserving section.
*
* @see #startPreserving
*
* @throws org.xml.sax.SAXException
*/
{
// Not sure this is really what we want. -sb
}
/**
* Receive notification of a processing instruction.
*
* @param target The processing instruction target.
* @param data The processing instruction data, or null if
* none was supplied.
* @throws org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
*
* @throws org.xml.sax.SAXException
*/
{
if (m_inEntityRef)
return;
flushPending();
{
}
{
}
else
{
try
{
{
m_elemContext.m_startTagOpen = false;
}
else if (m_needToCallStartDocument)
if (shouldIndent())
indent();
if (indexOfQLT >= 0)
{
// See XSLT spec on error recovery of "?>" in PIs.
if (indexOfQLT > 0)
{
}
{
}
}
else
{
}
/**
* Before Xalan 1497, a newline char was printed out if not inside of an
* element. The whitespace is not significant is the output is standalone
*/
/*
* Don't write out any indentation whitespace now,
* because there may be non-whitespace text after this.
*
* Simply mark that at this point if we do decide
* to indent that we should
* add a newline on the end of the current line before
* the indentation at the start of the next line.
*/
m_startNewLine = true;
}
catch(IOException e)
{
throw new SAXException(e);
}
}
}
/**
* Receive notivication of a entityReference.
*
* @param name The name of the entity.
*
* @throws org.xml.sax.SAXException
*/
{
{
m_elemContext.m_startTagOpen = false;
}
try
{
if (shouldIndent())
indent();
}
catch(IOException e)
{
throw new SAXException(e);
}
super.fireEntityReference(name);
}
/**
* This method is used to add an attribute to the currently open element.
* The caller has guaranted that this attribute is unique, which means that it
* not been seen before and will not be seen again.
*
* @param name the qualified name of the attribute
* @param value the value of the attribute which can contain only
* ASCII printable characters characters in the range 32 to 127 inclusive.
* @param flags the bit values of this integer give optimization information.
*/
throws SAXException
{
{
try
{
{
// "flags" has indicated that the characters
// '>' '<' '&' and '"' are not in the value and
// m_htmlcharInfo has recorded that there are no other
// entities in the range 32 to 127 so we write out the
// value directly
}
else
{
}
} catch (IOException e) {
throw new SAXException(e);
}
}
}
/**
* Add an attribute to the current element.
* @param uri the URI associated with the element name
* @param localName local part of the attribute name
* @param rawName prefix:localName
* @param type
* @param value the value of the attribute
* @param xslAttribute true if this attribute is from an xsl:attribute,
* false if declared within the elements opening tag.
* @throws SAXException
*/
public void addAttribute(
boolean xslAttribute)
throws SAXException
{
{
/*
* We don't run this block of code if:
* 1. The attribute value was only replaced (was_added is false).
* 2. The attribute is from an xsl:attribute element (that is handled
* in the addAttributeAlways() call just above.
* 3. The name starts with "xmlns", i.e. it is a namespace declaration.
*/
{
uri,
rawName);
if (prefixUsed != null
{
// use a different raw name, with the prefix used in the
// generated namespace declaration
}
}
}
else
{
/*
* The startTag is closed, yet we are adding an attribute?
*
* Section: 7.1.3 Creating Attributes Adding an attribute to an
* element after a PI (for example) has been added to it is an
* error. The attributes can be ignored. The spec doesn't explicitly
* say this is disallowed, as it does for child elements, but it
* makes sense to have the same treatment.
*
* We choose to ignore the attribute which is added too late.
*/
// Generate a warning of the ignored attributes
// Create the warning message
try {
// Prepare to issue the warning message
// Issue the warning message
else
}
catch (Exception e){}
}
}
/**
* @see ExtendedContentHandler#endElement(String)
*/
{
}
/**
* This method is used to notify the serializer of a namespace mapping (or node)
* that applies to the current element whose startElement() call has already been seen.
* The official SAX startPrefixMapping(prefix,uri) is to define a mapping for a child
* element that is soon to be seen with a startElement() call. The official SAX call
* does not apply to the current element, hence the reason for this method.
*/
public void namespaceAfterStartElement(
throws SAXException
{
// hack for XSLTC with finding URI for default namespace
{
{
// the elements URI is not known yet, and it
// doesn't have a prefix, and we are currently
// setting the uri for prefix "", so we have
// the uri for the element... lets remember it
}
}
return;
}
/**
* From XSLTC
* Declare a prefix to point to a namespace URI. Inform SAX handler
* if this is a new prefix mapping.
*/
{
try
{
if (m_prefixMap.pushNamespace(
{
return true;
}
}
catch (SAXException e)
{
// falls through
}
return false;
}
/**
* Try's to reset the super class and reset this class for
* re-use, so that you don't need to create a new serializer
* (mostly for performance reasons).
*
* @return true if the class was successfuly reset.
*/
public boolean reset()
{
boolean wasReset = false;
if (super.reset())
{
wasReset = true;
}
return wasReset;
}
/**
* Reset all of the fields owned by ToStream class
*
*/
private void resetToXMLStream()
{
this.m_cdataTagOpen = false;
}
/**
* This method checks for the XML version of output document.
* If XML version of output document is not specified, then output
* document is of version XML 1.0.
* If XML version of output doucment is specified, but it is not either
* XML 1.0 or XML 1.1, a warning message is generated, the XML Version of
* output document is set to XML 1.0 and processing continues.
* @return string (XML version)
*/
{
{
}
{
}
else
{
try
{
// Prepare to issue the warning message
// Issue the warning message
else
}
catch (Exception e){}
}
return xmlVersion;
}
}