package de.infinityloop.upcast.exportfilters;


import java.io.Writer;

import org.xml.sax.Attributes;

import de.infinityloop.common.Constants;

/**
 * This Export Filter writes directly to the passed writer 
 * 
 *
 * @author rC
 * @version 1.0
 */

public class ExportFilter42 extends ExportFilterBase {

//
// Constants
//
  private static final int indentationFactor = 2; // indent 2 spaces per level
  
//
// Local variables
//
  /**
   * The writer output should be written to. This is all setup correctly when we receive it.
   */
  Writer out;
  
  /**
   * a boolean indicating whether prettyPrinting is to be performed.
   */
  boolean prettyPrint = false;
  
  /**
   * counter of element nesting
   */
  int nesting = 0;

//
// Private support methods
//
  /**
   * writes indentation corresponding to the passed level to the passed Writer.
   */
  private void indent( Writer w, int level ) {
    try {
      w.write( System.getProperty( "line.sep", "\n" ) );
      
      for( int i = 0; i < level*indentationFactor; ++i ) {
        w.write( ' ' );
      }
    } catch( Exception e ) {
      logger.debug( Constants.kDebugErrExpected, "error writing indentation: " + e.getMessage(), e );
    }
  }

    
//
// Implementation of "callbacks"
//  

  /**
   * returns a human readable string of the current configuration.
   */
  protected String getHumanReadableConfigurationDescription() {
    return "";
  }

  protected void startDocument( Writer destination, String systemId ) { 
    out = destination;
  }

  protected void endDocument() { 
    ; // nothing to do here
  }

  protected void startElement( String uri, String localName, String qName, Attributes attr ) { 
    try {
      indent( out, nesting );
      out.write( "<" + qName );
      
      for( int i = 0; i < attr.getLength(); ++i ) {
        out.write( " " + attr.getQName( i ) + "=\"" + encodeForXMLAttribute( attr.getValue( i ) ) + "\"" );
      }
      
      out.write(">");
    } catch( Exception e ) {
      logger.error( "Error opening element: " + e.getMessage() );
    }
    ++nesting;
  }

  protected void endElement( String uri, String localName, String qName, Attributes attr ) { 
    --nesting;
    indent( out, nesting );
    
    try {  
      out.write( "</" + qName + ">" );
    } catch( Exception e ) {
      logger.error( "Error closing element: " + e.getMessage() );
    }
      
  }

  protected void characters( String text ) { 
    ++nesting;
    indent( out, nesting );
    
    try {
      out.write( "[#[" + encodeForXMLText( text ) + "]" );
    } catch( Exception e ) {
      logger.error( "Error writing PCDATA: " + e.getMessage() );
    }
    
    --nesting;
  }

  protected void literal( String text ) { 
    ++nesting;
    indent( out, nesting );
    
    try {
      out.write( "[CDATA[" + encodeForXMLText( text ) + "]]" );
    } catch( Exception e ) {
      logger.error( "Error writing PCDATA: " + e.getMessage() );
    }
    
    --nesting;
  }

  /**
   * Module class name
   */
  public String getModuleClassName() { return "Raw Tree Dumper [devtool]"; }


  /**
   * Module identification name
   */
  public String getIdentificationName() { return "RawTreeDumper"; }


  /**
   * Notification that preferences might have changed. Update cache from current values.
   */
  protected void updateConfiguration() {
  } 


  /**
   * Notification that the filter's preferences should be initialized. Called
   * directly after instantiation, e.g.
   */
  protected void initializeConfiguration() {
    setParameter( kTableModelParamName, new String( kHTMLTableModelParamValue ) );
  } 

}

