package org.plenix.jsp;

import org.w3c.dom.*;
import com.kav.xsl.Formatter;
import java.io.PrintWriter;

/*
ATTRIBUTE_NODE: 2
CDATA_SECTION_NODE: 4
COMMENT_NODE: 8
DOCUMENT_FRAGMENT_NODE: 11
DOCUMENT_NODE: 9
DOCUMENT_TYPE_NODE: 10
ELEMENT_NODE: 1
ENTITY_NODE: 6
ENTITY_REFERENCE_NODE: 5
NOTATION_NODE: 12
PROCESSING_INSTRUCTION_NODE: 7
TEXT_NODE: 3
*/

class XSLFormatter extends Formatter
{
  public void process(NodeList nodeList, PrintWriter printWriter)
  {
    for (int i = 0; i < nodeList.getLength(); i++)
    {
      print(nodeList.item(i), printWriter);
    }

    printWriter.flush();
  }

  private void print(Node node, PrintWriter printWriter)
  {
    if (node == null)
    {
      return;
    }

    switch (node.getNodeType())
    {
      case Node.ELEMENT_NODE:
        Element element = (Element)node;
        NodeList nodeList = element.getChildNodes();

        for (int i = 0; i < nodeList.getLength(); i++)
	{
          print(nodeList.item(i), printWriter);
	}

        break;
      case Node.TEXT_NODE:
      case Node.CDATA_SECTION_NODE:
        printWriter.print(((Text) node).getData());
        break;
    }
  }
}
