package examples;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import org.w3c.dom.*;
import org.w3c.dom.traversal.*;
import org.xml.sax.SAXException;

import oracle.xml.parser.v2.*;

public class XMLViewTag extends TagSupport {

   String showAs;
   JspWriter out;

   public XMLViewTag() {
     resetAttr();
   }

   public void release() {
     resetAttr();
   }

   public void setShowAs(String showAs) {
     this.showAs=showAs.toLowerCase();
   }

   public int doStartTag() throws JspException {
     if (showAs.equals("stream")) 
         displayInputStream();
     if (showAs.equals("tree")) 
         displayTree();
     return EVAL_BODY_INCLUDE;
   }

   public int doEndTag() throws JspException {
     return EVAL_PAGE;
   }

  private void displayInputStream() {
    out = pageContext.getOut();
    try {
      out.print(XMLViewTagTLV.getXMLView());
    } catch (IOException io ) {
      System.err.println("unable to get xmlview");
    }      
   }
 
  private void displayTree() {
    out = pageContext.getOut();
    XMLDocument xdoc = XMLViewTagTLV.getXMLDocument();
     XMLPrintDriver xpd = new XMLPrintDriver(new PrintWriter((Writer)out));
      try {
        xpd.printDocument(xdoc);
      } catch (Exception e) {
         System.err.println("Error: " + e.getMessage());
      }
  }

  private void resetAttr() {
    showAs=null;
  }
   
}

