jakarta-watchdog-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rame...@locus.apache.org
Subject cvs commit: jakarta-watchdog-4.0/src/tools/org/apache/jspxml XmlViewGenerator.java
Date Sat, 16 Dec 2000 01:43:07 GMT
rameshm     00/12/15 17:43:07

  Added:       src/tools/org/apache/jspxml XmlViewGenerator.java
  Log:
  Tool to directly run and generate XMLView for JSP files. You can give directory parameter
  and this tool will traverse through all contents and generate XML View for the jsp file
by appending "XMLView.jsp" to the jsp file name part before ".jsp" extension.
   You can also use this as
  "java org.apache.jspxml.XmlViewGenerator <inputJspFile> <outputXMLViewFile>"
  
    For directory contents you need to use
  "java org.apache.jspxml.XmlViewGenerator <inputDir>"
  
  -Ramesh
  
  Revision  Changes    Path
  1.1                  jakarta-watchdog-4.0/src/tools/org/apache/jspxml/XmlViewGenerator.java
  
  Index: XmlViewGenerator.java
  ===================================================================
  package org.apache.jspxml;
  
  /** @File : XmlViewGenerator.java
  	@Author: Ramesh.Mandava
  	@Description: This program modifies a general JSP sysntax file ( with well formed HTML
tags )
          to a XML view of the file.
  	Different Uses
  	1. java XmlViewGenerator <inputJspFile> <outputJspWithXmlView>
  	  Using this way we can covert one JSp syntax file to XmlView.
  
  	2. java XmlViewGenerator <directoryName>
  	    In this way we can mention the directory name and will create XML view of each
  	    file whose name not ends with "XMLView.jsp" and ends with ".jsp" . For any file
  	    which ends with ".jsp" and not ending with "XMLView.jsp" this will create file whose
  	    name ends with "XMLView.jsp" 
  
  	
     
  */
  	
  import java.io.*;
  
  public class  XmlViewGenerator  implements FilenameFilter {
  
  	public static void main ( String[ ] args )
  	{
  		if  ( args.length == 0 )
  		{	
  			usage( );
  			System.exit(0);
  		}
  		else if ( args.length == 1 )
  		{
  			System.out.println("Have one argument, which is assumed as directory");
  			File passedDir = new File( args[0] );
  			if ( !passedDir.exists( ) )
  			{
  				System.out.println( args[0] + ": doesn't exist");
  				System.exit(0);
  			}
  			else if ( !passedDir.isDirectory() )
  			{
  				System.out.println("ERROR: Directory expected" );
  				System.out.println( args[0] + " : is not a directory" );
  			}
  			else
  			{
  				XmlViewGenerator myGenerator = new XmlViewGenerator();
  				myGenerator.generateXmlViewForDirectoryContents( passedDir );
  							
                    }
  	    	}
  		else if ( args.length == 2 )
  		{
  			System.out.println("Got two arguments: first is assumed as input file and second is OUTPUTFILE");
  			File INPUTFILE = new File( args[0] );
  			if ( !INPUTFILE.exists( ) )
  			{
  				System.out.println( args[0] + ": doesn't exist");
  				System.exit(0);
  			}
  			else
  			{
  				XmlViewGenerator myGenerator = new XmlViewGenerator();
  				myGenerator.generateXMLView(  INPUTFILE, args[1] );
  			}	
  
  		}
  	
  
  	}
  
  	public void generateXmlViewForDirectoryContents( File passedDir )
  	{
  		String[] fileList = passedDir.list(this );
  		System.out.println("In generateXml.. fileList -> " + fileList );
  		System.out.println("\nNumber -> " + fileList.length );
  		for ( int i=0; i< fileList.length; i++ )
  		{
  			System.out.println("File[" + i + "] -> " + fileList[i] );
  			File presentFile = new File ( passedDir.getAbsolutePath() + File.separator + fileList[i]);
  			System.out.println("Absolute Path -> " + presentFile.getAbsolutePath() );
  
  			if ( presentFile.isDirectory( ) )
  			{
  				System.out.println(" Generating XML view for directory contents : " + presentFile.getName()
);
  				generateXmlViewForDirectoryContents( presentFile );
  			}
  			else
  			{
  				generateXMLView( presentFile );
  			}
  		}
  	}
  
  	public boolean accept( File dir, String name )
  	{
  		File myFile = new File( dir.getAbsolutePath() + File.separator + name );
  		return ( ( myFile.isDirectory()) || (( 	name.endsWith(".jsp") ) && ( !name.endsWith("XMLView.jsp")
) ));
  	}
  
  	public static void usage( )
  	{
  		System.out.println("USAGE: java XmlViewGenerator <inputJspFile> <outputXmlViewJsp>"
);
  		System.out.println(" (OR) \n java XmlViewGenerator <inputDirectory> " );
  
  	}
  
  	public void generateXMLView ( File currentFile )
  	{
  		String fileName = currentFile.getAbsolutePath();
  		String outFileName = fileName.substring( 0,fileName.indexOf(".jsp")) +  "XMLView.jsp";
  		System.out.println("Input Name -> " + fileName );
  
  		System.out.println("Output Name -> " + outFileName );
  		generateXMLView( currentFile, outFileName );
  	}
  
  		
          public void generateXMLView( File currentFile, String outFileName )
  	{
  		jsp2XML jspConverter = new jsp2XML( currentFile.getAbsolutePath( ) );
  		String xmlView = jspConverter.ConvertJsp2XML();
  		try 
  	        {
  		  	FileWriter fw = new FileWriter(outFileName);
  			fw.write(xmlView);
  			fw.close();
  		}
  		catch(IOException ioe)
  		{
  			System.err.println("Error writing to file" + outFileName );
  		}
  
  	
  	}
  
  
         /*
  	public void generateXMLView( File currentFile, String outFileName )
  	{
  		try 
  		{
  			String tempLine= null;
  			String modifiedLine = null;
  			int tempEndIndex = -1;
  			int tempIndex = -1;
  
  
  			FileOutputStream fout = new FileOutputStream( currentFile.getParent() + File.separator
+ outFileName );
  			
  			System.out.println("Response File Abs Path -> " + currentFile.getParent() + File.separator
+ outFileName  );
  			fout.write("<!-- This file is generated by XmlViewGenerator.java -->\n".getBytes());
  			fout.write("<!-- Issues/Comments to <A HREF='mailto:ramesh.mandava@eng.sun.com'>Ramesh.Mandava</A>
-->\n\n".getBytes() );
  
  
  			fout.write("<! DOCTYPE root PUBLIC \"-//Sun Microsystems Inc. //DTD Java Server Pages
Version 1.1//EN\" \"http://java.sun.com/products/jsp/dtd/jspcore_1_2.dtd\" >".getBytes());
  			fout.write("\n".getBytes() );
  			fout.write("<jsp:root xmlns:jsp=\"http://java.sun.com/products/jsp/dtd/jsp_1_2.dtd\">".getBytes()
);
  			fout.write("\n".getBytes() );
  
  			
  			BufferedReader fin = new BufferedReader( new FileReader ( currentFile ) );
  			String prLine = null;
  			while ( ( prLine = fin.readLine()) != null )
  			{
  				boolean requestTimeExpressionPossible = false;
  				if ( ( prLine.indexOf("jsp:setProperty") > -1 ) || ( prLine.indexOf("jsp:useBean")
> -1 ) || ( prLine.indexOf("jsp:include") > -1 ) || ( prLine.indexOf("jsp:forward")
> -1 ) || ( prLine.indexOf("jsp:param") > -1 ) )
  				{
  					System.out.println("re	uestTime Expression Possible is true");
  	 				requestTimeExpressionPossible = true;
  
  					if ( isCDATAText == true )
  					{
  						fout.write("]]></jsp:cdata>".getBytes());
  						isCDATAText = false;
  					}
  				}
  
  				int directiveIndex=-1;
  				if ( ( directiveIndex = prLine.indexOf( "<%@")) > -1 )
  				{
  					System.out.println("directive exist");
  					int directiveWordIndex = -1;
  					if ( ( directiveWordIndex = prLine.indexOf("include")) > -1 )
  					{
  						System.out.println("include directive exist");
  					}
  					else 	if ( ( directiveWordIndex = prLine.indexOf("page")) > -1 )
  					{
  						System.out.println("page directive exist");
  					}
  
  					else if ( ( directiveWordIndex = prLine.indexOf("taglib")) > -1 )
  					{
  						System.out.println("taglib directive exist");
  					}
  					String newLine=null;
  					
  					if ( isCDATAText == true )
  					{
  						fout.write( prLine.prLine.substring(0, directiveWordIndex ).getBytes() );
  						fout.write("]]></jsp:cdata>".getBytes());
  						isCDATAText = false;
  					}
  
  					String newLine = "<jsp:directive." + prLine.substring( directiveWordIndex );
  					int directiveEndIndex = -1;
  					if ( (  directiveEndIndex = newLine.indexOf( "%>" ) ) > -1 )
  					{
  						newLine = newLine.substring( 0, directiveEndIndex) + "/>" + newLine.substring(
 directiveEndIndex + 2) ;
  						fout.write( newLine.getBytes());
  						fout.write( "\n".getBytes() );
  					}
  					else
  					{ 
  						fout.write( newLine.getBytes());
  						fout.write( "\n".getBytes() );
  						String myLine=null;
  						while ((  ( myLine = fin.readLine()) != null ) && ( ( directiveEndIndex =
myLine.indexOf( "%>") ) == -1 ) )
  						{
  							fout.write( myLine.getBytes() );
  							fout.write("\n".getBytes() );
  						}
  						if ( directiveEndIndex > -1 )
  						{
  							newLine= myLine.substring( 0, directiveEndIndex ) + "/>" + myLine.substring( 
directiveEndIndex + 2 )  ;
  							fout.write( newLine.getBytes());
  							fout.write( "\n".getBytes() );
  						}
  					}
  
  				}
  				else if ( ( tempIndex = prLine.indexOf( "<%!" )) > -1 )
  				{
  					System.out.println("declaration exist");
  					if ( isCDATAText == true )
  					{
  						fout.write( prLine.substring(0, tempIndex ).getBytes() );
  						fout.write("]]></jsp:cdata>".getBytes());
  						isCDATAText = false;
  						modifiedLine =  "<jsp:declaration> <![CDATA[ " + prLine.substring( tempIndex
+ 3 );
  
  					}
  					else
  					{
  						modifiedLine = prLine.substring(0, tempIndex ) + "<jsp:declaration> <![CDATA[
" + prLine.substring( tempIndex + 3 );
  	
  					}
  				
  					if ( (  tempEndIndex = modifiedLine.indexOf( "%>" ) ) > -1 )
  					{
  						modifiedLine = modifiedLine.substring( 0, tempEndIndex) + "]]> </jsp:declaration>"
+ modifiedLine.substring(  tempEndIndex + 2) ;
  						fout.write( modifiedLine.getBytes());
  						fout.write( "\n".getBytes() );
  					}
  					else
  					{ 
  						fout.write( modifiedLine.getBytes());
  						fout.write( "\n".getBytes() );
  					
  						while ((  ( tempLine = fin.readLine()) != null ) && ( ( tempEndIndex = tempLine.indexOf(
"%>") ) == -1 ) )
  						{
  							fout.write( tempLine.getBytes() );
  							fout.write("\n".getBytes() );
  						}
  						if ( tempEndIndex > -1 )
  						{
  							modifiedLine= tempLine.substring( 0, tempEndIndex ) + "]]> </jsp:declaration>"
+ tempLine.substring(  tempEndIndex + 2 ) ;						
  							fout.write( modifiedLine.getBytes());
  							fout.write( "\n".getBytes() );
  						}
  					}
  
  				}
  				else if ( ( tempIndex = prLine.indexOf( "<%=" )) > -1 )
  				{
  					System.out.println("expression exist");
  					if ( ( requestTimeExpressionPossible == true ) && ( ( prLine.indexOf("value")
> -1 ) || ( prLine.indexOf("beanName") > -1 ) || ( prLine.indexOf("page") > -1 )
 ) )
  					{
   						modifiedLine = prLine.substring( 0, tempIndex ) + "%=" + prLine.substring( tempIndex
+ 3 );
  						System.out.println("After converting <%= to %= :: modifiedLine -> " + modifiedLine
);
  
  					}
  					else
  					{
  						modifiedLine = prLine.substring( 0, tempIndex ) + "<jsp:expression> <![CDATA[
" + prLine.substring( tempIndex + 3 );
  
  					}
  
  				
  					if ( (  tempEndIndex = modifiedLine.indexOf( "%>" ) ) > -1 )
  					{
  
  						if ( ( requestTimeExpressionPossible == true ) && ( ( prLine.indexOf("value")
> -1 ) || ( prLine.indexOf("beanName") > -1 ) || ( prLine.indexOf("page") > -1 )
 ) )
  						{
   							modifiedLine = modifiedLine.substring( 0, tempEndIndex ) + "%" + modifiedLine.substring(
tempEndIndex + 2 );
  						      System.out.println("When setting requestTimeExpression as false : mod Line ->
" + modifiedLine );
  							requestTimeExpressionPossible = false; 
  						}
  						else
  						{
  							modifiedLine = modifiedLine.substring( 0, tempEndIndex) + "]]> </jsp:expression>"
+ modifiedLine.substring(  tempEndIndex+ 2) ;
  						}
  
  						fout.write( modifiedLine.getBytes());
  						fout.write( "\n".getBytes() );
  					}
  					else
  					{ 
  						fout.write( modifiedLine.getBytes());
  						fout.write( "\n".getBytes() );
  					
  						while ((  ( tempLine = fin.readLine()) != null ) && ( ( tempEndIndex = tempLine.indexOf(
"%>") ) == -1 ) )
  						{
  							fout.write( tempLine.getBytes() );
  							fout.write("\n".getBytes() );
  						}
  						if ( tempEndIndex > -1 )
  						{
  							if ( ( requestTimeExpressionPossible == true ) && ( ( prLine.indexOf("value")
> -1 ) || ( prLine.indexOf("beanName") > -1 ) || ( prLine.indexOf("page") > -1 )
 ) )
  							{
   								modifiedLine = modifiedLine.substring( 0, tempEndIndex ) + "%" + modifiedLine.substring(
tempEndIndex + 2 );
  								System.out.println("When setting( 2)requestTimeExpression as false : mod Line ->
" + modifiedLine );
  		
  								requestTimeExpressionPossible = false; 
  							}
  							else
  							{
  
  								modifiedLine = modifiedLine.substring( 0, tempEndIndex) + "]]> </jsp:expression>"
+ modifiedLine.substring(  tempEndIndex+ 2) ;
  							}
  
  
  							fout.write( modifiedLine.getBytes());
  							fout.write( "\n".getBytes() );
  						}
  					}
  
  				}
  				else if ( ( tempIndex = prLine.indexOf( "<%" )) > -1 )
  				{
  					System.out.println("scriptlet exist");
  					modifiedLine = prLine.substring( 0, tempIndex ) + "<jsp:scriptlet> <![CDATA[
" + prLine.substring( tempIndex + 2 );
  					
  				
  					if ( (  tempEndIndex = modifiedLine.indexOf( "%>" ) ) > -1 )
  					{
  						modifiedLine = modifiedLine.substring( 0, tempEndIndex) + "]]> </jsp:scriptlet>"
+ modifiedLine.substring(  tempEndIndex + 2) ;
  						fout.write( modifiedLine.getBytes());
  						fout.write( "\n".getBytes() );
  					}
  					else
  					{ 
  						fout.write( modifiedLine.getBytes());
  						fout.write( "\n".getBytes() );
  					
  						while ((  ( tempLine = fin.readLine()) != null ) && ( ( tempEndIndex = tempLine.indexOf(
"%>") ) == -1 ) )
  						{
  							fout.write( tempLine.getBytes() );
  							fout.write("\n".getBytes() );
  						}
  						if ( tempEndIndex > -1 )
  						{
  							modifiedLine= tempLine.substring( 0, tempEndIndex ) + "]]> </jsp:scriptlet>"
+ tempLine.substring(  tempEndIndex + 2) ;						
  							fout.write( modifiedLine.getBytes());
  							fout.write( "\n".getBytes() );
  						}
  					}
  
  				}
  				else
  				{
  					if ( ( isCDATAText == false ) && ( 	requestTimeExpressionPossible == false
) )
  					{	
  						fout.write("<jsp:cdata><![CDATA[".getBytes() );
  						isCDATAText = true;
  					}
  					
  					fout.write( prLine.getBytes() );
  					fout.write("\n".getBytes() );
  				}
  				
  			
  			}
  	
  			fout.write("</jsp:root>".getBytes() );
  		}
  		catch ( Exception e )
  		{
  			System.out.println("Exception -> " + e );
  		}
  	}
  
  	*/
  
  }
  
  
  

Mime
View raw message