/*
* Copyright (c) 2002, Tayeb Lemlouma
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the Opera Project (INRIA Rhône Alpes) nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.io.*;
import java.lang.String;
import java.net.*;
public class log2XML {
/**
* Replace a char by a string of char in the input string
*
*@param str1 the input string
*@param str2 the tring used to replace the input character
*@param c the character to be replaced
*/
public String replaceString(String str1, String str2, char c){
String str="";
for(int i=0; i\r\n");
}catch(Exception e){
System.err.println("can't write the XML file: "+e);
}
}//end buildXMLData
/**
* Extract the user agent information
*
*@param logLine a line of the server log
*/
public String extractUserAgent(String logLine){
String userAgent="";
int i,j,k,l,m;
for(i=0; logLine.charAt(i)!=' ' && i
*
*
*
* ...
*
*
*
*@param logFileName The file name of the log.
*@param xmlFileName The file name of the output XML file.
*@param serverType The type of the server that determines the format of the log
*/
public log2XML(String logFileName, String xmlFileName, String serverType){
try {
String logLine;
String IP;
String accessDate;
String request;
String statusCode;
String fileSize;
String referrer;
String userAgent;
if(serverType.equals("Apache/1.3.20")){
OutputStreamWriter out;
FileOutputStream fout = new FileOutputStream(xmlFileName);
out = new OutputStreamWriter(fout, "ISO-8859-1");
FileReader fr = new FileReader(logFileName);
BufferedReader br = new BufferedReader(fr);
out.write("\r\n");
out.write("\r\n");
while ((logLine=br.readLine())!=null){
//System.out.println(logLine);
IP = extractIP(logLine);
//System.out.println("IP: '"+IP+"'");
accessDate = extractDate(logLine);
//System.out.println("accessDate: '"+accessDate+"'");
request = extractRequest(logLine);
//System.out.println("request: '"+request+"'");
statusCode = extractStatusCode(logLine);
//System.out.println("statusCode: '"+statusCode+"'");
fileSize = extractFileSize(logLine);
//System.out.println("fileSize: '"+fileSize+"'");
referrer = extractReferrer(logLine);
//System.out.println("referrer: "+referrer);
userAgent = extractUserAgent(logLine);
//System.out.println("userAgent: '"+userAgent+"'");
request = replaceString(request,"&", '&');
referrer = replaceString(referrer, "&", '&');
//System.out.println((InetAddress.getByName(IP)).toString());
buildXMLData(out, IP, accessDate, request,statusCode, fileSize, referrer, userAgent);
}// end while
out.write("\r\n");
out.close();
}else{System.err.println("The server log type: "+serverType+"is not yet supported");}
}catch(Exception e){
}//end catch
}//end public
/**
* Written to test the log2XML method
*/
public static void main(String[] args) throws Exception {
System.out.println("XML file creating...");
new log2XML(args[0],"output.xml", "Apache/1.3.20");
System.out.println("XML file created.");
}//end main
}