protected void printDocument(final SAXTarget out, final FormatStack fstack,
			final NamespaceStack nstack, final Document document)
			throws SAXException {
		if (document == null) {
			return;
		}

		// contentHandler.startDocument()
		out.getContentHandler().startDocument();

		// Fire DTD events
		if (out.isReportDTDEvents()) {
			printDocType(out, fstack, document.getDocType());
		}

		// Handle root element, as well as any root level
		// processing instructions and comments
		// ignore DocType, if any.
		final int sz = document.getContentSize();
		
		if (sz > 0) {
			for (int i = 0; i < sz; i++) {
				final Content c = document.getContent(i);
				out.getLocator().setNode(c);
				switch (c.getCType()) {
					case Comment :
						printComment(out, fstack, (Comment) c);
						break;
					case DocType :
						// cannot simply add a DocType to a SAX stream
						// it is added when the Stream is created.
						break;
					case Element :
						printElement(out, fstack, nstack, (Element)c);
						break;
					case ProcessingInstruction :
						printProcessingInstruction(out, fstack,  
								(ProcessingInstruction)c);
						break;
					default :
						// do nothing.
				}
			}
		}
		
		// contentHandler.endDocument()
		out.getContentHandler().endDocument();

	}