/*
* This software and supporting documentation were developed by
*
* Siemens Corporate Technology
* Competence Center Knowledge Management and Business Transformation
* D-81730 Munich, Germany
*
* Authors (representing a really great team ;-) )
* Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
* This software is Open Source under GNU General Public License (GPL).
* Read the text of this license in LICENSE.TXT
* or look at www.opensource.org/licenses/
*
* Once more we emphasize, that:
* THIS SOFTWARE IS MADE AVAILABLE, AS IS, WITHOUT ANY WARRANTY
* REGARDING THE SOFTWARE, ITS PERFORMANCE OR
* FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
* ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
* PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/
// ReplaceCommentFileWorker
// ************ package ******************************************************
package KFM.preprocessor;
// ************ imports ******************************************************
// OROMatcher packages
import com.oroinc.text.regex.*;
import java.io.*;
import java.util.*;
import KFM.Exceptions.*;
import KFM.DirNavigator.*;
import KFM.File.*;
import KFM.*;
import KFM.log.*;
/** ReplaceCommentFileWorker is a FileWorker that replaces JS-Comments in
* HTML- or JS-Sources with JSP-Comments and renames the File to <filename>.jsp.
*
* @see KFM.FileWorker
* @see KFM.FileService
*/
public class ReplaceCommentFileWorker implements FileWorker {
// ************ attributes ******************************************************
File mFile = null;
/**
* The ending of the created jsp file
*/
static final String cJSPEnder = ".jsp";
/**
* JSP start of comment.
*/
static final String sStartComment = "<%-- ";
/**
* JSP end of comment.
*/
static final String sEndComment = " --%>";
static final String tRegSIPF = "SIPF";
static final String cSIPF = "<sip:sipf/>";
static final String tTagPrefix = "<%@ taglib uri=\"http://portal.siemens.net/PortalTaglib\" prefix=\"sip\"%>";
// ****************reg exp +++++++++++++++++++***********************************
/** Match '//...' but not '//-->'. each line*/
static final String cRElinecomment =
"("
+ "\\s+((//)/*"
+ "(?!-->)" // "(?!...)" is a negative lookahead assertion.
+ "(.*?)))\n"; // Group cGroupLineComment
/** Match comments that start with '/*'.
* . matches also \n here because Oromatcher runs in SINGLELINE_MODE.
*/
static final String cRElongcomment =
"(\\s+((/\\*)(.*?)\\*/))";
static final String cRegExpFollowComment = "((--%>)\\s*(<%--))";
// ************************************************************
// ************ methods ******************************************************
public ReplaceCommentFileWorker()
{
}
/**
* Executes the file if it is a '.html', '.htm' or '.js' file.
* It reads the content and then calls 'replaceComments'.
*/
public void workFile (File aFile)
{
mFile = aFile;
String tName = mFile.getName();
String tPath = mFile.getAbsolutePath();
System.out.println("Replace file " + tPath);
try {
if ((mFile.isFile()) && (mFile.exists())) {
//translate only html or js files
if ((tPath.endsWith(".html"))||
(tPath.endsWith(".htm"))||
(tPath.endsWith(".js"))||
(tPath.endsWith(".css"))){
String tContent = FileUtils.readWholeTextFile (tPath);
tContent = "\n" + tContent;
tContent = replaceComments (tContent);
tContent = tTagPrefix + "\n" + tContent;
tPath += cJSPEnder;
try {
BufferedWriter tWriter = null;
//if file is already a jsp-file, you shouldn't overwrite it!
//It is already converted!
//So only create the file if it not already exist!
//A file isn't created on hard disk with the constructor "new File()".
//So you can test if a file exists with file.exists().
File tFile = new File (tPath);
try {
tWriter = new BufferedWriter (new FileWriter (tPath));
tWriter.write( tContent);
}
finally {
if (tWriter != null)
tWriter.close();
}
} catch (IOException ioe){
System.out.println("Can't write file: "+tPath+ " .Maybe file is write protected");
}
if (aFile.isFile())
aFile.delete();
}
}
else {
System.out.println ("File " + aFile.getAbsolutePath() +" isn't a file or doesn' t exist!");
}
}
catch (Exception e){
e.printStackTrace ();
}
}
/**
* Parses a string with a comment regular expression and replaces it with
* jsp commands.
*
* @param aContent String to parse
* @param aRegExp regular expression to parse
* @param isSingleLine indicates if a single line lookahead is wanted or not
*/
private String matchExpression (
String aContent,
String aRegExp,
boolean isSingleLine)
{
MatchResult tMatcher = null;
int tBeginOffset = 0;
int tEndOffset = 0;
int tSipBegOffset = -1;
int tBegOff = 0;
int tEndOff = 0;
boolean isInComment = false;
String tContent = new String("");
//first replace only line comments
while (true)
{
tMatcher = RegExp.match(aRegExp, aContent, false, isSingleLine, tBeginOffset);
tSipBegOffset = aContent.indexOf(tRegSIPF, tBeginOffset);
if (tMatcher != null){
tBegOff = tMatcher.beginOffset(2);
tEndOff = tMatcher.endOffset(2);
}
else {
tBegOff = tBeginOffset;
}
if (((tSipBegOffset <= tBegOff) || (tMatcher == null))&& (tSipBegOffset != -1))
{
tContent += aContent.substring(tBeginOffset, tSipBegOffset);
tContent += cSIPF;
tBeginOffset = tSipBegOffset + tRegSIPF.length();
tEndOffset = tSipBegOffset + tRegSIPF.length();
}
else if (tMatcher != null){
//fetch only the content
String tComment = tMatcher.group(2);
int tSubCommentIndex = 0;
//if an subcomment is found, delete the surrounding jsp comments because it is commented
// by the outer jsp comment tags
while ((tSubCommentIndex = tComment.indexOf(sStartComment))>=0){
String tStr = tComment.substring(0, tSubCommentIndex);
tStr += tComment.substring((tSubCommentIndex + sStartComment.length()), tComment.length());
tSubCommentIndex = tStr.indexOf(sEndComment);
tStr = tStr.substring(0, tSubCommentIndex) + tStr.substring((tSubCommentIndex + sEndComment.length()), tStr.length());
tComment = tStr;
}
if (aContent.substring ((tEndOff - 1), tEndOff).equals ("\n"))
tEndOff = tEndOff - 1;
if ((tComment.startsWith("//")) || (tComment.startsWith("/*")))
tComment = tComment.substring(2, tComment.length());
if (tComment.endsWith("*/"))
tComment = tComment.substring(0, tComment.length() - 2);
tComment = sStartComment + tComment +sEndComment;
String temp = aContent.substring (tBeginOffset, tBegOff);
temp += tComment;
tContent += temp;
tBeginOffset = tEndOff;
tEndOffset = tEndOff;
}
else break;
}
tContent += aContent.substring(tEndOffset, aContent.length());
return tContent;
}
/**
* ReplaceComments replaces in the parsed file all jsp or html comments through jsp comments.
* It uses the Oromatcher
*/
public String replaceComments (
String aContent)
throws IOException
{
String tContent = matchExpression (aContent, cRElinecomment, false);
tContent = matchExpression (tContent, cRElongcomment, true);
//last concat neighbour comments
MatchResult tMatcher = null;
while ((tMatcher = RegExp.match(cRegExpFollowComment, tContent, false, true, 0 ))!= null){
int tEndOff = tMatcher.endOffset(0);
int tBegOff = tMatcher.beginOffset(0);
String temp = tContent.substring (0, tBegOff);
temp += tContent.substring (tMatcher.endOffset(2), tMatcher.beginOffset(3));
temp += tContent.substring (tEndOff, tContent.length());
tContent = temp;
}
return tContent;
}
public static void main (String [] args)
{
if (args.length == 0){
System.out.println("You have to insert a filepath as parameter : ReplaceCommentFileWorker <aFilePath>");
}
String tStartDir = args[0];
FileWorker tFileWorker = new ReplaceCommentFileWorker();
DirNavigator tNavigator = new DirNavigator (tStartDir, tFileWorker);
tNavigator.traverse ();
}
}