org.chromium.debug.core/src/org/chromium/debug/core/model/VmResourceId.java
changeset 355 8726e95bcbba
equal deleted inserted replaced
354:0bceeb415e7f 355:8726e95bcbba
       
     1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style license that can be
       
     3 // found in the LICENSE file.
       
     4 
       
     5 package org.chromium.debug.core.model;
       
     6 
       
     7 import org.chromium.sdk.Breakpoint;
       
     8 import org.chromium.sdk.JavascriptVm;
       
     9 import org.chromium.sdk.Script;
       
    10 
       
    11 /**
       
    12  * Id of resources loaded in V8 VM. We only know that they may have name (typically filename or
       
    13  * URL) or numerical id instead. This class reflects this.
       
    14  * The class also contains several utility methods that probably should be separated in the future.
       
    15  */
       
    16 public class VmResourceId {
       
    17 
       
    18   public static VmResourceId forName(String scriptName) {
       
    19     return new VmResourceId(scriptName);
       
    20   }
       
    21 
       
    22   public static VmResourceId forId(long scriptId) {
       
    23     return new VmResourceId(Long.valueOf(scriptId));
       
    24   }
       
    25 
       
    26   public static VmResourceId forScript(Script script) {
       
    27     if (script.getName() != null) {
       
    28       return forName(script.getName());
       
    29     } else {
       
    30       return forId(script.getId());
       
    31     }
       
    32   }
       
    33 
       
    34   private final Object value;
       
    35 
       
    36   private VmResourceId(Object value) {
       
    37     if (value == null) {
       
    38       throw new IllegalArgumentException("Null id value"); //$NON-NLS-1$
       
    39     }
       
    40     this.value = value;
       
    41   }
       
    42 
       
    43   @Override
       
    44   public int hashCode() {
       
    45     return value.hashCode();
       
    46   }
       
    47 
       
    48   @Override
       
    49   public boolean equals(Object obj) {
       
    50     if (obj instanceof VmResourceId == false) {
       
    51       return false;
       
    52     }
       
    53     VmResourceId other = (VmResourceId) obj;
       
    54     return this.value.equals(other.value);
       
    55   }
       
    56 
       
    57   /**
       
    58    * @return parameter for {@link JavascriptVm#setBreakpoint} method.
       
    59    */
       
    60   public Breakpoint.Type getTypeForBreakpoint() {
       
    61     if (value instanceof String) {
       
    62       return Breakpoint.Type.SCRIPT_NAME;
       
    63     } else {
       
    64       return Breakpoint.Type.SCRIPT_ID;
       
    65     }
       
    66   }
       
    67 
       
    68   /**
       
    69    * @return parameter for {@link JavascriptVm#setBreakpoint} method.
       
    70    */
       
    71   public String getTargetForBreakpoint() {
       
    72     return value.toString();
       
    73   }
       
    74 
       
    75   String createFileNameTemplate(boolean isEval) {
       
    76     if (value instanceof String) {
       
    77       return value.toString();
       
    78     } else {
       
    79       if (isEval) {
       
    80         return "<eval #" + value + ">"; //$NON-NLS-1$ //$NON-NLS-2$
       
    81       } else {
       
    82         return "<no name #" + value + ">"; //$NON-NLS-1$ //$NON-NLS-2$
       
    83       }
       
    84     }
       
    85 
       
    86   }
       
    87 
       
    88   @Override
       
    89   public String toString() {
       
    90     return getEclipseSourceName();
       
    91   }
       
    92 
       
    93   /**
       
    94    * @return source name that is suitable for Eclipse debug source lookup.
       
    95    */
       
    96   public String getEclipseSourceName() {
       
    97     if (value instanceof String) {
       
    98       String stringValue = (String) value;
       
    99       if (stringValue.startsWith("#")) {
       
   100         // Quote it.
       
   101         stringValue = "#" + stringValue;
       
   102       }
       
   103       return stringValue;
       
   104     } else {
       
   105       return "#" + value;
       
   106     }
       
   107   }
       
   108 
       
   109   public static VmResourceId parseString(String name) {
       
   110     if (name.startsWith("##")) {
       
   111       return VmResourceId.forName(name.substring(1));
       
   112     } else if (name.startsWith("#")) {
       
   113       return VmResourceId.forId(Long.parseLong(name.substring(1)));
       
   114     } else {
       
   115       return VmResourceId.forName(name);
       
   116     }
       
   117   }
       
   118 }