/*
** Luxor - XML User Interface Language (XUL) Toolkit
** Copyright (c) 2001, 2002 by Gerald Bauer
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/

package luxor.core.xslt;

import java.io.*;
import java.util.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.transform.*;
import luxor.core.*;
import luxor.status.*;
import luxor.*;

public class StylesheetDef extends XulNode
{
   static Logger T = Logger.getLogger( StylesheetDef.class );

   public StylesheetDef( Element element )
   {
      super( element );
   }

   public Element getContent()
   {
      // todo: make this method more efficient
      //   use SAX instead of reparsing strings constantly

      Element result = new Element( "result" );

      // returns transformed content

      // get data (aka xml input/source)

      String datasource = getAttribute( Xul.Attribute.DATASOURCE );
      if( datasource == null )
      {
         Xul.error( "datasource attribute required for stylesheet inside xul document" );
         return result;
      }

      InputStream inStream = XulManager.getXulManager().getResourceAsStream( datasource );

      if( inStream == null )
      {
         Xul.error( "datasource '" + datasource + "' not found" );
         return result;
      }

      // remove datasource attribute from stylesheet otherwise xsl/t stumbles over our own custom attribute
      getElement().removeAttribute( Xul.Attribute.DATASOURCE );

      try
      {
         // get xsl/t stylesheet as a string
         StringWriter stylesheetBuffer = new StringWriter();
         XMLOutputter outputter = new XMLOutputter( "", false );
         outputter.output( getElement(), stylesheetBuffer );

         T.debug( "stylesheet=" + stylesheetBuffer.toString() );

         // setup jaxp transformer (aka xsl/t engine)
         StreamSource inSource = new StreamSource( inStream, datasource );
         StreamSource xsltSource = new StreamSource( new StringReader( stylesheetBuffer.toString() ) );

         /*
          *  StringWriter resultBuffer = new StringWriter();
          *  StreamResult outResult = new StreamResult( resultBuffer );
          */
         JDOMResult outResult = new JDOMResult();

         TransformerFactory tf = TransformerFactory.newInstance();
         Transformer trans = tf.newTransformer( xsltSource );
         trans.transform( inSource, outResult );

         // todo: check if parser supports fragements
         //  as xml fragments can have more than one root element

         /*
          *  SAXBuilder builder = new SAXBuilder();
          *  Document doc = builder.build( new StringReader( resultBuffer.toString() ));
          *  result.addContent( doc.getRootElement().detach() ) ;
          */
         result.addContent( outResult.getDocument().getRootElement().detach() );
      }
      /*
       *  catch( JDOMException jex )
       *  {
       *  Xul.error( "failed to parse transformed xul fragement: " + jex.toString() );
       *  }
       */
      catch( TransformerConfigurationException tcex )
      {
         Xul.error( "failed to setup xsl/t processor: " + tcex.toString() );
      }
      catch( TransformerException tex )
      {
         Xul.error( "failed to process xsl/t stylesheet: " + tex.toString() );
      }
      catch( IOException ioex )
      {
         Xul.error( "failed to process xsl/t stylesheet: " + ioex.toString() );
      }

      // todo: add datasource attribute again?
      return result;
   }
}