XsltcTransformerBase.java :  » Web-Framework » argun » biz » hammurapi » web » xslt » Java Open Source

Java Open Source » Web Framework » argun 
argun » biz » hammurapi » web » xslt » XsltcTransformerBase.java
/*
 * argun 1.0
 * Web 2.0 delivery framework 
 * Copyright (C) 2007  Hammurapi Group
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * URL: http://www.hammurapi.biz
 * e-Mail: support@hammurapi.biz
 */
package biz.hammurapi.web.xslt;

import java.io.InputStream;

import javax.xml.transform.Source;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Logger;
import org.apache.xalan.xsltc.trax.TransformerFactoryImpl;

import biz.hammurapi.web.HammurapiWebException;

/**
 * @author Pavel Vlasov
 * @version $Revision: 1.1 $
 */
public class XsltcTransformerBase extends AbstractXsltTransformer {
  private static Logger logger=Logger.getLogger(XsltcTransformerBase.class);
  
  private String transletName;
  private String transletPackage;
  
  public void setTransletName(String transletName) {
    if (transletName==null) {    
      this.transletName=null;
      this.transletPackage=null;
      logger.debug(this.getClass().getName()+": transletName -> null");
    } else {
      int idx=transletName.lastIndexOf('.');
      if (idx==-1) {
        this.transletName=transletName;
      } else {
        this.transletName=transletName.substring(idx+1);
        this.transletPackage=transletName.substring(0,idx);
      }
      logger.debug(this.getClass().getName()+": transletName -> "+this.transletName);
      logger.debug(this.getClass().getName()+": transletPackage -> "+this.transletPackage);      
    }
  }

  protected Source getSource() throws HammurapiWebException {
    if (transletName==null) {
      InputStream is=getStyleSheetAsStream();
      if (is!=null) {
        throw new UnsupportedOperationException("Runtime compilation is not supported");
//          XSLTC xsltc = new XSLTC();  
//          xsltc.init();
//          if (xsltc.compile(is, getClass().getName().replace('.','_')+"_Translet_"+String.valueOf(counter++))) {
//            transletName=xsltc.getClassName();
//          } else {            
//            throw new HammurapiWebException("Stylesheet compilation failed");
//          }
      }      
    }
    
    return transletName==null ? null : new StreamSource(transletName);
  }

  protected TransformerFactory createTransformerFactory() {
    // On one hand it is bad to instantiate TransformerFactory
    // in this way, on the other hand I hate to modify 
    // system-wide properties even temporarily. 
    TransformerFactory tf=new TransformerFactoryImpl();
        tf.setAttribute("use-classpath", Boolean.TRUE);
        if (transletPackage!=null) {
          tf.setAttribute("package-name", transletPackage);
        }
    return tf ;
  }  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.