Re: JFor in a servlet

"[email protected]" <[email protected]> Mon, 23 Sep 2002 06:20:16 -0400
Newsgroups gmane.text.xml.jfor.general
Message-ID <[email protected]>
Hi,
i have developed a servlet for jfor. It works fine. I am using tomcat
server. The problem i am facing is the inputfile ie... fo file , its
neither taking relative path nor any other path. So i have to hardcode the
whole path. Can anyone help me on this. ( I am including the whole code
here..)

**************************************************************************


import javax.servlet.ServletException;
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Properties;
import org.xml.sax.*;
import org.jfor.jfor.converter.Converter;
import java.util.StringTokenizer;


public class RTFServlet extends HttpServlet
{
	public void service(HttpServletRequest _request, HttpServletResponse
_response) throws ServletException, IOException
	{


		String name = _request.getParameter("fo");
		System.out.println("File Name :>>>> "+name);
		StringTokenizer st = new StringTokenizer(name,"\\");
		int iTokens = st.countTokens();
		String sFileName="";
		String sName="";

		try{
			while(st.hasMoreTokens()){
			sFileName = st.nextToken();
			System.out.println("value token :>>>> "+sFileName);
			}
		}catch(Exception e){
			System.out.println("Exception in tokenizer "+e.toString());
		}
		System.out.println("File Name :>>>> "+sFileName);
        sName = getFileName(sFileName);
		String sContextPath = _request.getRealPath("/");
		System.out.println("Context Path : "+sContextPath);
		//sFileName = "foFiles" + "/" + sFileName;
sFileName =
"/Personal/jakarta-tomcat-3.2.4/webapps/examples/html2fo/foFiles/"+sFileName
;
		String tryURL = "/";
		
		//name = "file:///d:/samples/"+name;
		String outFile = sContextPath+ "rtfFiles\\"+sName+".rtf";
		//System.out.println("req uri "+name);
		System.out.println("Real Path :
"+getServletConfig().getServletContext().getRealPath(tryURL));
		String realPath =
getServletConfig().getServletContext().getRealPath(tryURL);
		//name = realPath + name;
		System.out.println("Real Path : "+sFileName);
	//	System.out.println("Real Path :
"+_request.getRealPath(_request.getRequestURI()));
		System.out.println("Servlet Path : "+_request.getServletPath());
		System.out.println("Before ConvertRTF method");
		try{
			ConvertRTF(sFileName,outFile);
			System.out.println("Before redirect");
			_response.sendRedirect(outFile);
			System.out.println("after redirect");	
			}catch(FileNotFoundException ex){
			System.out.println("Exception while converting : "+ex.toString());
			}catch(Exception e){
				System.out.println("Exception while converting : "+e.toString());
			}
	}	
		
        
		


public void ConvertRTF(String inFile,String outFile) throws
FileNotFoundException{

        try {
			
			// Used by relative image path.
			String userDir = System.getProperty ("user.dir");
            System.out.println("user Directory "+userDir);
			final String xslInput = inFile;
			System.out.println("Path :::: "+xslInput);
			
			final String rtfOutput =  outFile;

			System.out.println("Creating input and output objects...");
			final InputSource input = new InputSource(xslInput);
			final Writer output = new BufferedWriter(new FileWriter(outFile));

			// Used by relative image path.
			setUserDir (new File (xslInput).getCanonicalFile().getParent ());
            System.out.println("Path User By Images is : "+new File
(xslInput).getCanonicalFile().getParent());

			System.out.println("Converting XSL:FO file '" + xslInput + "' to RTF
output '" + rtfOutput + "'...");
			new Converter(input,output,Converter.createConverterOption ());
			System.out.println("Done writing RTF output '" + rtfOutput + "'.");
			// Used by relative image path.
			setUserDir (userDir);
                        
		} catch (Exception e) {
			System.out.println("Message : "+e.toString());
			throw new FileNotFoundException();
			
		}

	}

	/** Convert from a filename to a file URL. */
	private static String fileToUrl(String filename)
	throws MalformedURLException
	{
		return toURL (new File (filename)).toString ();
	}

	/** System.setProperty function (java 1.1 compatibility) */
	private static void setUserDir (String dir)
	{
		if (dir == null)
		{
			System.out.println ("relative image path is not supported");
			return;
		}
		Properties p = System.getProperties ();
		p.put ("user.dir", dir);
		System.setProperties (p);
	}

	/**
	 * Converts this abstract pathname into a <code>file:</code> URL.  The
	 * exact form of the URL is system-dependent.  If it can be determined that
	 * the file denoted by this abstract pathname is a directory, then the
	 * resulting URL will end with a slash.
	 *
	 * @return a URL object representing the equivalent file URL.
	 * @throws MalformedURLException if the path cannot be parsed as a URL.
	 * @see     java.net.URL
	 * @since   1.2
	 */
	private static URL toURL (File file) throws MalformedURLException {
		String path = file.getAbsolutePath();
		if (File.separatorChar != '/') {
			path = path.replace(File.separatorChar, '/');
		}
		if (!path.startsWith("/")) {
			path = "/" + path;
		}
		if (!path.endsWith("/") && file.isDirectory()) {
			path = path + "/";
		}
		return new URL("file", "", path);
	}

	public static String getFileName(String sFileName){
		int i = 0;
		try{
			i  = sFileName.lastIndexOf(".");
		}catch(Exception e){
			System.out.println("Exception in File Retrieval Utility :
"+e.toString());
		}
		return (sFileName.substring(0,i));
	}
	public static String getFileType(String sFileName){
		int i = 0;
		try{
			i = sFileName.lastIndexOf(".");
		}catch(Exception e){
			System.out.println("Exception in File Retrieval Utility :
"+e.toString());
		}
		return (sFileName.substring(i+1));
	}
}

	
**************************************************************************

Regards,
sanjeev


Original Message:
-----------------
From: Bertrand Delacretaz [email protected]
Date: Mon, 23 Sep 2002 11:10:36 +0200
To: [email protected], [email protected],
[email protected]
Subject: Re: [jfor-users] JFor in a servlet


On Thursday 19 September 2002 08:58, [email protected] wrote:
> Do we get the servlet implementation of Jfor ie fo to rtf conversion?? 

The current jfor code base does not include a servlet implementation that 
runs jfor.

If you want to contribute one (which would be nice), we (through the 
jfor-devel mailing list) would be happy to help.

-- 
 Bertrand Delacrétaz (codeconsult.ch, jfor.org)

 buzzwords: XML, java, XSLT, cocoon, mentoring/teaching/coding.
 disclaimer: eternity is very long. mostly towards the end. get ready.






--------------------------------------------------------------------
mail2web - Check your email from the web at
http://mail2web.com/ .




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf