Back to index

lightning-sunbird  0.9+nobinonly
FileLocator.java
Go to the documentation of this file.
00001 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * The contents of this file are subject to the Netscape Public
00004  * License Version 1.1 (the "License"); you may not use this file
00005  * except in compliance with the License. You may obtain a copy of
00006  * the License at http://www.mozilla.org/NPL/
00007  *
00008  * Software distributed under the License is distributed on an "AS
00009  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
00010  * implied. See the License for the specific language governing
00011  * rights and limitations under the License.
00012  *
00013  * The Original Code is Mozilla Communicator client code, released
00014  * March 31, 1998.
00015  *
00016  * The Initial Developer of the Original Code is Netscape
00017  * Communications Corporation.  Portions created by Netscape are
00018  * Copyright (C) 1998 Netscape Communications Corporation. All
00019  * Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Patrick C. Beard <beard@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the
00026  * terms of the GNU Public License (the "GPL"), in which case the
00027  * provisions of the GPL are applicable instead of those above.
00028  * If you wish to allow use of your version of this file only
00029  * under the terms of the GPL and not to allow others to use your
00030  * version of this file under the NPL, indicate your decision by
00031  * deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL.  If you do not delete
00033  * the provisions above, a recipient may use your version of this
00034  * file under either the NPL or the GPL.
00035  */
00036 
00037 import java.io.*;
00038 import java.util.*;
00039 
00040 public class FileLocator {
00041        static boolean USE_BLAME = false;
00042        static boolean ASSIGN_BLAME = false;
00043 
00044        static final String MOZILLA_BASE = "/mozilla/";
00045        static final String LXR_BASE = "http://lxr.mozilla.org/seamonkey/source/";
00046        static final String BONSAI_BASE = "http://bonsai.mozilla.org/cvsblame.cgi?file=";
00047 
00048        static final String NS_BASE = "/ns/";
00049        static final String NS_BONSAI_URL = "http://warp.mcom.com/webtools/bonsai/cvsblame.cgi?root=/m/src&file=";
00050 
00051        static final Hashtable fileTables = new Hashtable();
00052        static final RevisionTable revisionTable = new RevisionTable();
00053        static final BlameTable blameTable = new BlameTable();
00054     static final Hashtable addr2LineTable = new Hashtable();
00055 
00056     public static String getFileLocation(byte[] line) throws IOException {
00057         return getFileLocation(new String(line));
00058     }
00059 
00060        public static String getFileLocation(String line) throws IOException {
00061         // start from the LAST occurence of '[', as C++ symbols can include it.
00062               int leftBracket = line.lastIndexOf('[');
00063               if (leftBracket == -1)
00064                      return line;
00065               int rightBracket = line.indexOf(']', leftBracket + 1);
00066               if (rightBracket == -1)
00067                      return line;
00068         
00069         // generalize for Linux & Mac versions of the Leak detector.
00070         String fullPath;
00071         int lineNumber, lineAnchor;
00072         int comma = line.indexOf(',', leftBracket + 1);
00073         if (comma != -1) {
00074             // Mac format: unmangled_symbol[mac_path,byte_offset]
00075             String macPath = line.substring(leftBracket + 1, comma);
00076             fullPath = "/" + macPath.replace(':', '/');
00077               
00078             // compute the line number in the file.
00079             int offset = 0;
00080             try {
00081                 offset = Integer.parseInt(line.substring(comma + 1, rightBracket));
00082             } catch (NumberFormatException nfe) {
00083                 return line;
00084             } catch (StringIndexOutOfBoundsException sobe) {
00085                 System.err.println("### Error processing line: " + line);
00086                 System.err.println("### comma = " + comma + ", rightBracket = " + rightBracket);
00087                 System.err.flush();
00088                 return line;
00089             }
00090               
00091             FileTable table = (FileTable) fileTables.get(fullPath);
00092             if (table == null) {
00093                 table = new FileTable(fullPath);
00094                 fileTables.put(fullPath, table);
00095             }
00096             lineNumber = 1 + table.getLine(offset);
00097             lineAnchor = lineNumber;
00098         } else {
00099             int space = line.indexOf(' ', leftBracket + 1);
00100             if (space != -1) {
00101                 // Raw Linux format: mangled_symbol[library +address]
00102                 String library = line.substring(leftBracket + 1, space);
00103                 String address = line.substring(space + 1, rightBracket);
00104                 Addr2Line addr2line = (Addr2Line) addr2LineTable.get(library);
00105                 if (addr2line == null) {
00106                     System.out.println("new Addr2Line["+library+"]");
00107                     addr2line = new Addr2Line(library);
00108                     addr2LineTable.put(library, addr2line);
00109                 }
00110                 line = addr2line.getLine(address);
00111                 return getFileLocation(line);
00112             } else {
00113                 // Linux format: unmangled_symbol[unix_path:line_number]
00114                 int colon = line.indexOf(':', leftBracket + 1);
00115                 fullPath = line.substring(leftBracket + 1, colon);
00116                 try {
00117                     lineNumber = Integer.parseInt(line.substring(colon + 1, rightBracket));
00118                 } catch (NumberFormatException nfe) {
00119                     return line;
00120                 }
00121                 lineAnchor = lineNumber;
00122             }
00123         }
00124         
00125               // compute the URL of the file.
00126               int mozillaIndex = fullPath.indexOf(MOZILLA_BASE);
00127               String locationURL = null, blameInfo = "";
00128               if (mozillaIndex > -1) {
00129                      // if using blame, hilite the line number of the call, and include the revision.
00130                      String mozillaPath = fullPath.substring(mozillaIndex + 1);
00131                      String revision = revisionTable.getRevision(fullPath);
00132                      String bonsaiPath = mozillaPath + (revision.length() > 0 ? "&rev=" + revision : "");
00133                      if (USE_BLAME) {
00134                             locationURL = BONSAI_BASE + bonsaiPath + "&mark=" + lineNumber;
00135                             if (lineAnchor > 10)
00136                                    lineAnchor -= 10;
00137                      } else {
00138                             locationURL = LXR_BASE + mozillaPath.substring(MOZILLA_BASE.length());
00139                      }
00140                      if (ASSIGN_BLAME)
00141                             blameInfo = " (" + blameTable.getBlame(bonsaiPath, lineNumber) + ")";
00142               } else {
00143                      int nsIndex = fullPath.indexOf(NS_BASE);
00144                      if (nsIndex > -1 && USE_BLAME) {
00145                             // if using blame, hilite the line number of the call, and include the revision.
00146                             String nsPath = fullPath.substring(nsIndex + 1);
00147                             String revision = revisionTable.getRevision(fullPath);
00148                             String bonsaiPath = nsPath + (revision.length() > 0 ? "&rev=" + revision : "");
00149                             locationURL = NS_BONSAI_URL + bonsaiPath + "&mark=" + lineNumber;
00150                             if (lineAnchor > 10)
00151                                    lineAnchor -= 10;
00152                             if (ASSIGN_BLAME)
00153                                    blameInfo = " (" + blameTable.getBlame(bonsaiPath, lineNumber) + ")";
00154                      } else {
00155                             locationURL = "file://" + fullPath;
00156                      }
00157               }
00158               
00159               return "<A HREF=\"" + locationURL + "#" + lineAnchor + "\"TARGET=\"SOURCE\">" + line.substring(0, leftBracket) + "</A>" + blameInfo;
00160        }
00161 }
00162