alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*  $Id: xsl_transform.java,v 1.4 2001/01/17 22:40:03 agarcia3 Exp $ 
    webEditor. The new way in content management
    Copyright (C) 2001  Alfredo Garcia

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    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 General Public License for more details.
*/

package webEditor.util;

import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import org.apache.xalan.xslt.XSLTProcessorFactory;
import org.apache.xalan.xslt.XSLTInputSource;
import org.apache.xalan.xslt.XSLTResultTarget;
import org.apache.xalan.xslt.XSLTProcessor;


/**
 * Direct access to the Xalan XSL formating capabilities
 *
 * @author <a href="mailto:agarcia@mundofree.com">Alfredo Garcia
 */
public class xsl_transform
{
   public xsl_transform () 
   {
   }
   
   /**
    * Transformation method; We expect to improve this method soon.
    * Given a xml stream and an XSL template, it returns a string with 
    * the document transformation;
    * @param input		Content of the XML file
    * @param templateName	Name of the template
    * @return String		Output of the transformation
    */
   public String process (String input, String templateName) 
   throws java.io.IOException,
          java.net.MalformedURLException

   {
   	StringReader readerInput = new StringReader (input);
   	XSLTInputSource XSLinput = new XSLTInputSource (readerInput);

	StringWriter outBuffer = new StringWriter();
	XSLTResultTarget XSLoutput = new XSLTResultTarget(outBuffer);	
try {
	XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
	processor.process(XSLinput, new XSLTInputSource(templateName),
                          XSLoutput);
} catch (Exception e) {
	e.printStackTrace();
}
	String result = XSLoutput.getCharacterStream().toString();
	//System.out.println (result);
   	return (result);
   }

    /**
    * Transformation method; We expect to improve this method soon
    * In this case, the input source is a DOM tree.
    * @param input		DOM tree of the document
    * @param templateName	Name of the template
    * @return String		XSL output
    */
   public String processDOM (Document input, String templateName) 
   throws java.io.IOException,
          java.net.MalformedURLException

   {
   	XSLTInputSource XSLinput = new XSLTInputSource (input);

	StringWriter outBuffer = new StringWriter();
	XSLTResultTarget XSLoutput = new XSLTResultTarget(outBuffer);	
try {
	// Set up the XSLTProcessor to use XercesLiaison.
	XSLTProcessor processor = XSLTProcessorFactory.getProcessor
                                  (new org.apache.xalan.xpath.xdom.XercesLiaison());

	processor.process(XSLinput, new XSLTInputSource(templateName),
                          XSLoutput);
} catch (Exception e) {
	e.printStackTrace();
}
	String result = XSLoutput.getCharacterStream().toString();
   	return (result);
   }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.