htmlunit-2.7

roperties(Window window, String... propertiesToDelete)
  {
    for (String property : propertiesToDelete) {
      window.delete(property);
    }
  }
  
  private void removePrototypeProperties(Window window, String className, String... properties)
  {
    ScriptableObject prototype = (ScriptableObject)ScriptableObject.getClassPrototype(window, className);
    for (String property : properties) {
      prototype.delete(property);
    }
  }
  
  private ScriptableObject configureClass(ClassConfiguration config, Scriptable window)
    throws InstantiationException, IllegalAccessException
  {
    Class<?> jsHostClass = config.getLinkedClass();
    ScriptableObject prototype = (ScriptableObject)jsHostClass.newInstance();
    prototype.setParentScope(window);
    
    configureConstantsPropertiesAndFunctions(config, prototype);
    
    return prototype;
  }
  
  private void configureConstantsPropertiesAndFunctions(ClassConfiguration config, ScriptableObject scriptable)
  {
    configureConstants(config, scriptable);
    for (String propertyName : config.propertyKeys())
    {
      Method readMethod = config.getPropertyReadMethod(propertyName);
      Method writeMethod = config.getPropertyWriteMethod(propertyName);
      scriptable.defineProperty(propertyName, null, readMethod, writeMethod, 0);
    }
    int attributes = 0;
    if (webClient_.getBrowserVersion().isIE()) {
      attributes = 2;
    }
    for (String functionName : config.functionKeys())
    {
      Method method = config.getFunctionMethod(functionName);
      FunctionObject functionObject = new FunctionObject(functionName, method, scriptable);
      scriptable.defineProperty(functionName, functionObject, attributes);
    }
  }
  
  private void configureConstants(ClassConfiguration config, ScriptableObject scriptable)
  {
    for (String constant : config.constants())
    {
      Class<?> linkedClass = config.getLinkedClass();
      try
      {
        Object value = linkedClass.getField(constant).get(null);
        scriptable.defineProperty(constant, value, 0);
      }
      catch (Exception e)
      {
        throw Context.reportRuntimeError("Cannot get field '" + constant + "' for type: " + config.getClassName());
      }
    }
  }
  
  public Script compile(HtmlPage htmlPage, String sourceCode, final String sourceName, final int startLine)
  {
    WebAssert.notNull("sourceCode", sourceCode);
    
    Scriptable scope = getScope(htmlPage, null);
    final String source = sourceCode;
    ContextAction action = new HtmlUnitContextAction(scope, htmlPage, source)
    {
      public Object doRun(Context cx)
      {
        return cx.compileString(source, sourceName, startLine, null);
      }
      
      protected String getSourceCode(Context cx)
      {
        return source;
      }
    };
    return (Script)getContextFactory().call(action);
  }
  
  public Object execute(HtmlPage htmlPage, String sourceCode, String sourceName, int startLine)
  {
    Script script = compile(htmlPage, sourceCode, sourceName, startLine);
    return execute(htmlPage, script);
  }
  
  public Object execute(HtmlPage htmlPage, final Script script)
  {
    final Scriptable scope = getScope(htmlPage, null);
    
    ContextAction action = new HtmlUnitContextAction(scope, htmlPage, script)
    {
      public Object doRun(Context cx)
      {
        return script.exec(cx, scope);
      }
      
      protected String getSourceCode(Context cx)
      {
        return null;
      }
    };
    return getContextFactory().call(action);
  }
  
  public Object callFunction(final HtmlPage htmlPage, Object javaScriptFunction, final Object thisObject, final Object[] args, DomNode htmlElement)
  {
    final Scriptable scope = getScope(htmlPage, htmlElement);
    
    final Function function = (Function)javaScriptFunction;
    ContextAction action = new HtmlUnitContextAction(scope, htmlPage, htmlPage)
    {
      public Object doRun(Context cx)
      {
        return callFunction(htmlPage, function, cx, scope, (Scriptable)thisObject, args);
      }
      
      protected String getSourceCode(Context cx)
      {
        return cx.decompileFunction(function, 2);
      }
    };
    return getContextFactory().call(action);
  }
  
  private Scriptable getScope(HtmlPage htmlPage, DomNode htmlElement)
  {
    Scriptable scope;
    Scriptable scope;
    if (htmlElement != null) {
      scope = htmlElement.getScriptObject();
    } else {
      scope = (Window)htmlPage.getEnclosingWindow().getScriptObject();
    }
    return scope;
  }
  
  public Object callFunction(HtmlPage htmlPage, Function function, Context context, Scriptable scope, Scriptable thisObject, Object[] args)
  {
    synchronized (htmlPage)
    {
      Object result = function.call(context, scope, thisObject, args);
      doProcessPostponedActions();
      return result;
    }
  }
  
  public boolean isScriptRunning()
  {
    return Boolean.TRUE.equals(javaScriptRunning_.get());
  }
  
  private abstract class HtmlUnitContextAction
    implements ContextAction
  {
    private final Scriptable scope_;
    private final HtmlPage htmlPage_;
    
    public HtmlUnitContextAction(Scriptable scope, HtmlPage htmlPage)
    {
      scope_ = scope;
      htmlPage_ = htmlPage;
    }
    
    /* Error */
    public final Object run(Context cx)
    {
      // Byte code:
      //   0: aload_0
      //   1: getfield 1	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:this$0	Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;
      //   4: invokestatic 5	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine:access$300	(Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;)Ljava/lang/ThreadLocal;
      //   7: invokevirtual 6	java/lang/ThreadLocal:get	()Ljava/lang/Object;
      //   10: checkcast 7	java/lang/Boolean
      //   13: astore_2
      //   14: aload_0
      //   15: getfield 1	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:this$0	Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;
      //   18: invokestatic 5	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine:access$300	(Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;)Ljava/lang/ThreadLocal;
      //   21: getstatic 8	java/lang/Boolean:TRUE	Ljava/lang/Boolean;
      //   24: invokevirtual 9	java/lang/ThreadLocal:set	(Ljava/lang/Object;)V
      //   27: aload_1
      //   28: ldc 10
      //   30: aload_0
      //   31: getfield 3	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:scope_	Lnet/sourceforge/htmlunit/corejs/javascript/Scriptable;
      //   34: invokevirtual 11	net/sourceforge/htmlunit/corejs/javascript/Context:putThreadLocal	(Ljava/lang/Object;Ljava/lang/Object;)V
      //   37: aload_1
      //   38: ldc 12
      //   40: aload_0
      //   41: getfield 4	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:htmlPage_	Lcom/gargoylesoftware/htmlunit/html/HtmlPage;
      //   44: invokevirtual 11	net/sourceforge/htmlunit/corejs/javascript/Context:putThreadLocal	(Ljava/lang/Object;Ljava/lang/Object;)V
      //   47: aload_0
      //   48: getfield 4	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:htmlPage_	Lcom/gargoylesoftware/htmlunit/html/HtmlPage;
      //   51: dup
      //   52: astore_3
      //   53: monitorenter
      //   54: aload_0
      //   55: aload_1
      //   56: invokevirtual 13	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:doRun	(Lnet/sourceforge/htmlunit/corejs/javascript/Context;)Ljava/lang/Object;
      //   59: astore 4
      //   61: aload_0
      //   62: getfield 1	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:this$0	Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;
      //   65: invokestatic 14	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine:access$400	(Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;)V
      //   68: aload 4
      //   70: astore 5
      //   72: aload_3
      //   73: monitorexit
      //   74: aload_0
      //   75: getfield 1	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:this$0	Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;
      //   78: invokestatic 5	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine:access$300	(Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;)Ljava/lang/ThreadLocal;
      //   81: aload_2
      //   82: invokevirtual 9	java/lang/ThreadLocal:set	(Ljava/lang/Object;)V
      //   85: aload 5
      //   87: areturn
      //   88: astore 6
      //   90: aload_3
      //   91: monitorexit
      //   92: aload 6
      //   94: athrow
      //   95: astore_3
      //   96: aload_0
      //   97: getfield 1	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:this$0	Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;
      //   100: new 16	com/gargoylesoftware/htmlunit/ScriptException
      //   103: dup
      //   104: aload_0
      //   105: getfield 4	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:htmlPage_	Lcom/gargoylesoftware/htmlunit/html/HtmlPage;
      //   108: aload_3
      //   109: aload_0
      //   110: aload_1
      //   111: invokevirtual 17	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:getSourceCode	(Lnet/sourceforge/htmlunit/corejs/javascript/Context;)Ljava/lang/String;
      //   114: invokespecial 18	com/gargoylesoftware/htmlunit/ScriptException:<init>	(Lcom/gargoylesoftware/htmlunit/html/HtmlPage;Ljava/lang/Throwable;Ljava/lang/String;)V
      //   117: invokevirtual 19	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine:handleJavaScriptException	(Lcom/gargoylesoftware/htmlunit/ScriptException;)V
      //   120: aconst_null
      //   121: astore 4
      //   123: aload_0
      //   124: getfield 1	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:this$0	Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;
      //   127: invokestatic 5	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine:access$300	(Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;)Ljava/lang/ThreadLocal;
      //   130: aload_2
      //   131: invokevirtual 9	java/lang/ThreadLocal:set	(Ljava/lang/Object;)V
      //   134: aload 4
      //   136: areturn
      //   137: astore_3
      //   138: aload_0
      //   139: getfield 1	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:this$0	Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;
      //   142: invokevirtual 21	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine:getWebClient	()Lcom/gargoylesoftware/htmlunit/WebClient;
      //   145: invokevirtual 22	com/gargoylesoftware/htmlunit/WebClient:isThrowExceptionOnScriptError	()Z
      //   148: ifeq +12 -> 160
      //   151: new 23	java/lang/RuntimeException
      //   154: dup
      //   155: aload_3
      //   156: invokespecial 24	java/lang/RuntimeException:<init>	(Ljava/lang/Throwable;)V
      //   159: athrow
      //   160: invokestatic 25	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine:access$100	()Lorg/apache/commons/logging/Log;
      //   163: ldc 26
      //   165: aload_3
      //   166: invokeinterface 27 3 0
      //   171: aconst_null
      //   172: astore 4
      //   174: aload_0
      //   175: getfield 1	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:this$0	Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;
      //   178: invokestatic 5	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine:access$300	(Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;)Ljava/lang/ThreadLocal;
      //   181: aload_2
      //   182: invokevirtual 9	java/lang/ThreadLocal:set	(Ljava/lang/Object;)V
      //   185: aload 4
      //   187: areturn
      //   188: astore 7
      //   190: aload_0
      //   191: getfield 1	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine$HtmlUnitContextAction:this$0	Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;
      //   194: invokestatic 5	com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine:access$300	(Lcom/gargoylesoftware/htmlunit/javascript/JavaScriptEngine;)Ljava/lang/ThreadLocal;
      //   197: aload_2
      //   198: invokevirtual 9	java/lang/ThreadLocal:set	(Ljava/lang/Object;)V
      //   201: aload 7
      //   203: athrow
      // Line number table:
      //   Java source line #514	-> byte code offset #0
      //   Java source line #515	-> byte code offset #14
      //   Java source line #518	-> byte code offset #27
      //   Java source line #519	-> byte code offset #37
      //   Java source line #520	-> byte code offset #47
      //   Java source line #521	-> byte code offset #54
      //   Java source line #522	-> byte code offset #61
      //   Java source line #523	-> byte code offset #68
      //   Java source line #538	-> byte code offset #74
      //   Java source line #524	-> byte code offset #88
      //   Java source line #526	-> byte code offset #95
      //   Java source line #527	-> byte code offset #96
      //   Java source line #528	-> byte code offset #120
      //   Java source line #538	-> byte code offset #123
      //   Java source line #530	-> byte code offset #137
      //   Java source line #531	-> byte code offset #138
      //   Java source line #532	-> byte code offset #151
      //   Java source line #534	-> byte code offset #160
      //   Java source line #535	-> byte code offset #171
      //   Java source line #538	-> byte code offset #174
      // Local variable table:
      //   start	length	slot	name	signature
      //   0	204	0	this	HtmlUnitContextAction
      //   0	204	1	cx	Context
      //   13	185	2	javaScriptAlreadyRunning	Boolean
      //   95	14	3	e	Exception
      //   137	29	3	e	TimeoutError
      //   59	127	4	response	Object
      //   70	16	5	localObject1	Object
      //   88	5	6	localObject2	Object
      //   188	14	7	localObject3	Object
      // Exception table:
      //   from	to	target	type
      //   54	74	88	finally
      //   88	92	88	finally
      //   27	74	95	java/lang/Exception
      //   88	95	95	java/lang/Exception
      //   27	74	137	com/gargoylesoftware/htmlunit/javascript/TimeoutError
      //   88	95	137	com/gargoylesoftware/htmlunit/javascript/TimeoutError
      //   27	74	188	finally
      //   88	123	188	finally
      //   137	174	188	finally
      //   188	190	188	finally
    }
    
    protected abstract Object doRun(Context paramContext);
    
    protected abstract String getSourceCode(Context paramContext);
  }
  
  private void doProcessPostponedActions()
  {
    if (Boolean.TRUE.equals(holdPostponedActions_.get())) {
      return;
    }
    try
    {
      getWebClient().loadDownloadedResponses();
    }
    catch (RuntimeException e)
    {
      throw e;
    }
    catch (Exception e)
    {
      throw new RuntimeException(e);
    }
    List<PostponedAction> actions = (List)postponedActions_.get();
    postponedActions_.set(null);
    if (actions != null) {
      try
      {
        for (PostponedAction action : actions)
        {
          Page owningPage = action.getOwningPage();
          if ((owningPage != null) && (owningPage == owningPage.getEnclosingWindow().getEnclosedPage())) {
            action.execute();
          }
        }
      }
      catch (Exception e)
      {
        Context.throwAsScriptRuntimeEx(e);
      }
    }
  }
  
  public void addPostponedAction(PostponedAction action)
  {
    List<PostponedAction> actions = (List)postponedActions_.get();
    if (actions == null)
    {
      actions = new ArrayList();
      postponedActions_.set(actions);
    }
    actions.add(action);
  }
  
  protected void handleJavaScriptException(ScriptException scriptException)
  {
    HtmlPage page = scriptException.getPage();
    if (page != null)
    {
      WebWindow window = page.getEnclosingWindow();
      if (window != null)
      {
        Window w = (Window)window.getScriptObject();
        if (w != null) {
          w.triggerOnError(scriptException);
        }
      }
    }
    if (getWebClient().isThrowExceptionOnScriptError()) {
      throw scriptException;
    }
    LOG.info("Caught script exception", scriptException);
  }
  
  public void holdPosponedActions()
  {
    holdPostponedActions_.set(Boolean.TRUE);
  }
  
  public void processPostponedActions()
  {
    holdPostponedActions_.set(Boolean.FALSE);
    doProcessPostponedActions();
  }
  
  private void readObject(ObjectInputStream in)
    throws IOException, ClassNotFoundException
  {
    in.defaultReadObject();
    initTransientFields();
  }
  
  private void initTransientFields()
  {
    javaScriptRunning_ = new ThreadLocal();
    postponedActions_ = new ThreadLocal();
    holdPostponedActions_ = new ThreadLocal();
  }
  
  private static class FallbackCaller
    extends ScriptableObject
  {
    private static final long serialVersionUID = -7124423159070941606L;
    
    public Object get(String name, Scriptable start)
    {
      if ((start instanceof ScriptableWithFallbackGetter)) {
        return ((ScriptableWithFallbackGetter)start).getWithFallback(name);
      }
      return NOT_FOUND;
    }
    
    public String getClassName()
    {
      return "htmlUnitHelper-fallbackCaller";
    }
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.javascript;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ScriptPreProcessor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import net.sourceforge.htmlunit.corejs.javascript.Context;
import net.sourceforge.htmlunit.corejs.javascript.ContextFactory;
import net.sourceforge.htmlunit.corejs.javascript.ErrorReporter;
import net.sourceforge.htmlunit.corejs.javascript.Evaluator;
import net.sourceforge.htmlunit.corejs.javascript.Script;

class HtmlUnitContextFactory$TimeoutContext
  extends Context
{
  private long startTime_;
  
  protected HtmlUnitContextFactory$TimeoutContext(HtmlUnitContextFactory paramHtmlUnitContextFactory, ContextFactory factory)
  {
    super(factory);
  }
  
  public void startClock()
  {
    startTime_ = System.currentTimeMillis();
  }
  
  public void terminateScriptIfNecessary()
  {
    if (HtmlUnitContextFactory.access$000(this$0) > 0L)
    {
      long currentTime = System.currentTimeMillis();
      if (currentTime - startTime_ > HtmlUnitContextFactory.access$000(this$0)) {
        throw new TimeoutError(HtmlUnitContextFactory.access$000(this$0), currentTime - startTime_);
      }
    }
  }
  
  protected Script compileString(String source, Evaluator compiler, ErrorReporter compilationErrorReporter, String sourceName, int lineno, Object securityDomain)
  {
    boolean isWindowEval = compiler != null;
    if (!isWindowEval)
    {
      String sourceCodeTrimmed = source.trim();
      if (sourceCodeTrimmed.startsWith("<!--")) {
        source = source.replaceFirst("<!--", "// <!--");
      }
      if ((HtmlUnitContextFactory.access$100(this$0).isIE()) && (sourceCodeTrimmed.endsWith("-->")))
      {
        int lastDoubleSlash = source.lastIndexOf("//");
        int lastNewLine = Math.max(source.lastIndexOf('\n'), source.lastIndexOf('\r'));
        if (lastNewLine > lastDoubleSlash) {
          source = source.substring(0, lastNewLine);
        }
      }
    }
    HtmlPage page = (HtmlPage)Context.getCurrentContext().getThreadLocal("startingPage");
    
    source = this$0.preProcess(page, source, sourceName, null);
    
    source = new StringScriptPreProcessor(this$0, lineno).preProcess(page, source, sourceName, null);
    if (HtmlUnitContextFactory.access$100(this$0).isIE())
    {
      ScriptPreProcessor ieCCPreProcessor = new IEConditionalCompilationScriptPreProcessor();
      source = ieCCPreProcessor.preProcess(page, source, sourceName, null);
    }
    return super.compileString(source, compiler, compilationErrorReporter, sourceName, lineno, securityDomain);
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.TimeoutContext
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.javascript;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.BrowserVersionFeatures;
import com.gargoylesoftware.htmlunit.ScriptPreProcessor;
import com.gargoylesoftware.htmlunit.WebAssert;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.regexp.HtmlUnitRegExpProxy;
import net.sourceforge.htmlunit.corejs.javascript.Callable;
import net.sourceforge.htmlunit.corejs.javascript.Context;
import net.sourceforge.htmlunit.corejs.javascript.ContextFactory;
import net.sourceforge.htmlunit.corejs.javascript.ErrorReporter;
import net.sourceforge.htmlunit.corejs.javascript.Evaluator;
import net.sourceforge.htmlunit.corejs.javascript.Script;
import net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime;
import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
import net.sourceforge.htmlunit.corejs.javascript.WrapFactory;
import net.sourceforge.htmlunit.corejs.javascript.debug.Debugger;

public class HtmlUnitContextFactory
  extends ContextFactory
{
  private static final long serialVersionUID = -1282169475857079041L;
  private static final int INSTRUCTION_COUNT_THRESHOLD = 10000;
  private final BrowserVersion browserVersion_;
  private final WebClient webClient_;
  private long timeout_;
  private Debugger debugger_;
  private final ErrorReporter errorReporter_;
  private final WrapFactory wrapFactory_ = new HtmlUnitWrapFactory();
  
  public HtmlUnitContextFactory(WebClient webClient)
  {
    WebAssert.notNull("webClient", webClient);
    webClient_ = webClient;
    browserVersion_ = webClient.getBrowserVersion();
    errorReporter_ = new StrictErrorReporter();
  }
  
  public void setTimeout(long timeout)
  {
    timeout_ = timeout;
  }
  
  public long getTimeout()
  {
    return timeout_;
  }
  
  public void setDebugger(Debugger debugger)
  {
    debugger_ = debugger;
  }
  
  public Debugger getDebugger()
  {
    return debugger_;
  }
  
  private class TimeoutContext
    extends Context
  {
    private long startTime_;
    
    protected TimeoutContext(ContextFactory factory)
    {
      super();
    }
    
    public void startClock()
    {
      startTime_ = System.currentTimeMillis();
    }
    
    public void terminateScriptIfNecessary()
    {
      if (timeout_ > 0L)
      {
        long currentTime = System.currentTimeMillis();
        if (currentTime - startTime_ > timeout_) {
          throw new TimeoutError(timeout_, currentTime - startTime_);
        }
      }
    }
    
    protected Script compileString(String source, Evaluator compiler, ErrorReporter compilationErrorReporter, String sourceName, int lineno, Object securityDomain)
    {
      boolean isWindowEval = compiler != null;
      if (!isWindowEval)
      {
        String sourceCodeTrimmed = source.trim();
        if (sourceCodeTrimmed.startsWith("<!--")) {
          source = source.replaceFirst("<!--", "// <!--");
        }
        if ((browserVersion_.isIE()) && (sourceCodeTrimmed.endsWith("-->")))
        {
          int lastDoubleSlash = source.lastIndexOf("//");
          int lastNewLine = Math.max(source.lastIndexOf('\n'), source.lastIndexOf('\r'));
          if (lastNewLine > lastDoubleSlash) {
            source = source.substring(0, lastNewLine);
          }
        }
      }
      HtmlPage page = (HtmlPage)Context.getCurrentContext().getThreadLocal("startingPage");
      
      source = preProcess(page, source, sourceName, null);
      
      source = new StringScriptPreProcessor(HtmlUnitContextFactory.this, lineno).preProcess(page, source, sourceName, null);
      if (browserVersion_.isIE())
      {
        ScriptPreProcessor ieCCPreProcessor = new IEConditionalCompilationScriptPreProcessor();
        source = ieCCPreProcessor.preProcess(page, source, sourceName, null);
      }
      return super.compileString(source, compiler, compilationErrorReporter, sourceName, lineno, securityDomain);
    }
  }
  
  protected String preProcess(HtmlPage htmlPage, String sourceCode, String sourceName, HtmlElement htmlElement)
  {
    String newSourceCode = sourceCode;
    ScriptPreProcessor preProcessor = webClient_.getScriptPreProcessor();
    if (preProcessor != null)
    {
      newSourceCode = preProcessor.preProcess(htmlPage, sourceCode, sourceName, htmlElement);
      if (newSourceCode == null) {
        newSourceCode = "";
      }
    }
    return newSourceCode;
  }
  
  protected Context makeContext()
  {
    TimeoutContext cx = new TimeoutContext(this);
    
    cx.setOptimizationLevel(-1);
    
    cx.setInstructionObserverThreshold(10000);
    
    configureErrorReporter(cx);
    cx.setWrapFactory(wrapFactory_);
    if (debugger_ != null) {
      cx.setDebugger(debugger_, null);
    }
    ScriptRuntime.setRegExpProxy(cx, new HtmlUnitRegExpProxy(ScriptRuntime.getRegExpProxy(cx)));
    
    return cx;
  }
  
  protected void configureErrorReporter(Context context)
  {
    context.setErrorReporter(errorReporter_);
  }
  
  protected void observeInstructionCount(Context cx, int instructionCount)
  {
    TimeoutContext tcx = (TimeoutContext)cx;
    tcx.terminateScriptIfNecessary();
  }
  
  protected Object doTopCall(Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args)
  {
    TimeoutContext tcx = (TimeoutContext)cx;
    tcx.startClock();
    return super.doTopCall(callable, cx, scope, thisObj, args);
  }
  
  protected boolean hasFeature(Context cx, int featureIndex)
  {
    switch (featureIndex)
    {
    case 3: 
      return true;
    case 5: 
      return !browserVersion_.isIE();
    case 1: 
      return browserVersion_.isIE();
    case 14: 
      return browserVersion_.hasFeature(BrowserVersionFeatures.SET_READONLY_PROPERTIES);
    case 15: 
      return false;
    }
    return super.hasFeature(cx, featureIndex);
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.javascript;

import com.gargoylesoftware.htmlunit.Page;
import java.lang.ref.WeakReference;

public abstract class PostponedAction
{
  private final WeakReference<Page> owningPageRef_;
  
  public PostponedAction(Page owningPage)
  {
    owningPageRef_ = new WeakReference(owningPage);
  }
  
  Page getOwningPage()
  {
    return (Page)owningPageRef_.get();
  }
  
  public abstract void execute()
    throws Exception;
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.javascript.PostponedAction
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.javascript;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.SgmlPage;
import com.gargoylesoftware.htmlunit.html.DomAttr;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.javascript.host.Attr;
import com.gargoylesoftware.htmlunit.javascript.host.Node;
import net.sourceforge.htmlunit.corejs.javascript.Context;
import net.sourceforge.htmlunit.corejs.javascript.Scriptable;

public class NamedNodeMap
  extends SimpleScriptable
  implements ScriptableWithFallbackGetter
{
  private static final long serialVersionUID = -1910087049570242560L;
  private final org.w3c.dom.NamedNodeMap attributes_;
  
  public NamedNodeMap()
  {
    attributes_ = null;
  }
  
  public NamedNodeMap(DomElement element)
  {
    setParentScope(element.getScriptObject());
    setPrototype(getPrototype(getClass()));
    
    attributes_ = element.getAttributes();
    setDomNode(element, false);
  }
  
  public final Object get(int index, Scriptable start)
  {
    NamedNodeMap startMap = (NamedNodeMap)start;
    Object response = startMap.jsxFunction_item(index);
    if (response != null) {
      return response;
    }
    return NOT_FOUND;
  }
  
  public Object getWithFallback(String name)
  {
    Object response = jsxFunction_getNamedItem(name);
    if (response != null) {
      return response;
    }
    if ((useRecursiveAttributeForIE()) && (isRecursiveAttribute(name))) {
      return getUnspecifiedAttributeNode(name);
    }
    return NOT_FOUND;
  }
  
  public Object jsxFunction_getNamedItem(String name)
  {
    DomNode attr = (DomNode)attributes_.getNamedItem(name);
    if (attr != null) {
      return attr.getScriptObject();
    }
    if ((!name.equals("className")) && (useRecursiveAttributeForIE()) && (isRecursiveAttribute(name))) {
      return getUnspecifiedAttributeNode(name);
    }
    return null;
  }
  
  public void jsxFunction_setNamedItem(Node node)
  {
    attributes_.setNamedItem(node.getDomNodeOrDie());
  }
  
  public Object jsxFunction_item(int index)
  {
    DomNode attr = (DomNode)attributes_.item(index);
    if (attr != null) {
      return attr.getScriptObject();
    }
    if (useRecursiveAttributeForIE())
    {
      index -= attributes_.getLength();
      String name = getRecusiveAttributeNameAt(index);
      if (name != null) {
        return getUnspecifiedAttributeNode(name);
      }
    }
    return null;
  }
  
  private boolean useRecursiveAttributeForIE()
  {
    return (getBrowserVersion().isIE()) && ((getDomNodeOrDie() instanceof HtmlElement));
  }
  
  private Attr getUnspecifiedAttributeNode(String attrName)
  {
    HtmlElement domNode = (HtmlElement)getDomNodeOrDie();
    
    DomAttr attr = domNode.getPage().createAttribute(attrName);
    domNode.setAttributeNode(attr);
    return (Attr)attr.getScriptObject();
  }
  
  public int jsxGet_length()
  {
    int length = attributes_.getLength();
    if (useRecursiveAttributeForIE()) {
      length += getRecursiveAttributesLength();
    }
    return length;
  }
  
  private boolean isRecursiveAttribute(String name)
  {
    for (Scriptable object = getDomNodeOrDie().getScriptObject(); object != null; object = object.getPrototype()) {
      for (Object id : object.getIds()) {
        if (name.equals(Context.toString(id))) {
          return true;
        }
      }
    }
    return false;
  }
  
  private int getRecursiveAttributesLength()
  {
    int length = 0;
    for (Scriptable object = getDomNodeOrDie().getScriptObject(); object != null; object = object.getPrototype()) {
      length += object.getIds().length;
    }
    return length;
  }
  
  private String getRecusiveAttributeNameAt(int index)
  {
    int i = 0;
    for (Scriptable object = getDomNodeOrDie().getScriptObject(); object != null; object = object.getPrototype()) {
      for (Object id : object.getIds())
      {
        if (i == index) {
          return Context.toString(id);
        }
        i++;
      }
    }
    return null;
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.javascript.NamedNodeMap
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.javascript;

import net.sourceforge.htmlunit.corejs.javascript.Context;
import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
import net.sourceforge.htmlunit.corejs.javascript.debug.DebugFrame;

public class DebugFrameAdapter
  implements DebugFrame
{
  public void onDebuggerStatement(Context cx) {}
  
  public void onEnter(Context cx, Scriptable activation, Scriptable thisObj, Object[] args) {}
  
  public void onExceptionThrown(Context cx, Throwable ex) {}
  
  public void onExit(Context cx, boolean byThrow, Object resultOrException) {}
  
  public void onLineChange(Context cx, int lineNumber) {}
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.javascript.DebugFrameAdapter
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.javascript.regexp;

import net.sourceforge.htmlunit.corejs.javascript.regexp.NativeRegExp;
import org.apache.commons.lang.StringUtils;

class HtmlUnitRegExpProxy$RegExpData
{
  private final String jsSource_;
  private final String jsFlags_;
  
  HtmlUnitRegExpProxy$RegExpData(NativeRegExp re)
  {
    String str = re.toString();
    jsSource_ = StringUtils.substringBeforeLast(str.substring(1), "/");
    jsFlags_ = StringUtils.substringAfterLast(str, "/");
  }
  
  public HtmlUnitRegExpProxy$RegExpData(String string)
  {
    jsSource_ = string;
    jsFlags_ = "";
  }
  
  public int getJavaFlags()
  {
    int flags = 0;
    if (jsFlags_.contains("i")) {
      flags |= 0x2;
    }
    if (jsFlags_.contains("m")) {
      flags |= 0x8;
    }
    return flags;
  }
  
  public String getJavaPattern()
  {
    return HtmlUnitRegExpProxy.jsRegExpToJavaRegExp(jsSource_);
  }
  
  boolean hasFlag(char c)
  {
    return jsFlags_.indexOf(c) != -1;
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.javascript.regexp.HtmlUnitRegExpProxy.RegExpData
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.javascript.regexp;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import net.sourceforge.htmlunit.corejs.javascript.Context;
import net.sourceforge.htmlunit.corejs.javascript.RegExpProxy;
import net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime;
import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
import net.sourceforge.htmlunit.corejs.javascript.regexp.NativeRegExp;
import net.sourceforge.htmlunit.corejs.javascript.regexp.RegExpImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HtmlUnitRegExpProxy
  extends RegExpImpl
{
  private static final Log LOG = LogFactory.getLog(HtmlUnitRegExpProxy.class);
  private final RegExpProxy wrapped_;
  
  public HtmlUnitRegExpProxy(RegExpProxy wrapped)
  {
    wrapped_ = wrapped;
  }
  
  public Object action(Context cx, Scriptable scope, Scriptable thisObj, Object[] args, int actionType)
  {
    try
    {
      return doAction(cx, scope, thisObj, args, actionType);
    }
    catch (StackOverflowError e)
    {
      LOG.warn(e.getMessage(), e);
    }
    return wrapped_.action(cx, scope, thisObj, args, actionType);
  }
  
  private Object doAction(Context cx, Scriptable scope, Scriptable thisObj, Object[] args, int actionType)
  {
    if ((2 == actionType) && (args.length == 2) && ((args[1] instanceof String)))
    {
      String thisString = Context.toString(thisObj);
      String replacement = (String)args[1];
      Object arg0 = args[0];
      if ((arg0 instanceof String))
      {
        replacement = replacement.replaceAll("\\$\\$", "\\$");
        
        return StringUtils.replaceOnce(thisString, (String)arg0, replacement);
      }
      if ((arg0 instanceof NativeRegExp)) {
        try
        {
          NativeRegExp regexp = (NativeRegExp)arg0;
          RegExpData reData = new RegExpData(regexp);
          String regex = reData.getJavaPattern();
          int flags = reData.getJavaFlags();
          Pattern pattern = Pattern.compile(regex, flags);
          Matcher matcher = pattern.matcher(thisString);
          if (reData.hasFlag('g')) {
            return doReplacement(thisString, replacement, matcher, true);
          }
          return doReplacement(thisString, replacement, matcher, false);
        }
        catch (PatternSyntaxException e)
        {
          LOG.warn(e.getMessage(), e);
        }
      }
    }
    else if (1 == actionType)
    {
      if (args.length == 0) {
        return null;
      }
      Object arg0 = args[0];
      String thisString = Context.toString(thisObj);
      RegExpData reData;
      RegExpData reData;
      if ((arg0 instanceof NativeRegExp)) {
        reData = new RegExpData((NativeRegExp)arg0);
      } else {
        reData = new RegExpData(Context.toString(arg0));
      }
      Pattern pattern = Pattern.compile(reData.getJavaPattern(), reData.getJavaFlags());
      Matcher matcher = pattern.matcher(thisString);
      if (!matcher.find()) {
        return null;
      }
      int index = matcher.start(0);
      List<Object> groups = new ArrayList();
      if (reData.hasFlag('g'))
      {
        groups.add(matcher.group(0));
        while (matcher.find()) {
          groups.add(matcher.group(0));
        }
      }
      for (int i = 0; i <= matcher.groupCount(); i++)
      {
        Object group = matcher.group(i);
        if (group == null) {
          group = Context.getUndefinedValue();
        }
        groups.add(group);
      }
      Scriptable response = cx.newArray(scope, groups.toArray());
      
      response.put("index", response, new Integer(index));
      response.put("input", response, thisString);
      return response;
    }
    return wrappedAction(cx, scope, thisObj, args, actionType);
  }
  
  private String doReplacement(String originalString, String replacement, Matcher matcher, boolean replaceAll)
  {
    StringBuffer sb = new StringBuffer();
    int previousIndex = 0;
    while (matcher.find())
    {
      sb.append(originalString.substring(previousIndex, matcher.start()));
      String localReplacement = replacement;
      if (replacement.contains("$")) {
        localReplacement = computeReplacementValue(replacement, originalString, matcher);
      }
      sb.append(localReplacement);
      previousIndex = matcher.end();
      if (!replaceAll) {
        break;
      }
    }
    sb.append(originalString.substring(previousIndex));
    return sb.toString();
  }
  
  static String computeReplacementValue(String replacement, String originalString, Matcher matcher)
  {
    int lastIndex = 0;
    StringBuilder result = new StringBuilder();
    int i;
    while ((i = replacement.indexOf('$', lastIndex)) > -1)
    {
      if (i > 0) {
        result.append(replacement.substring(lastIndex, i));
      }
      String ss = null;
      if ((i < replacement.length() - 1) && ((i == lastIndex) || (replacement.charAt(i - 1) != '$')))
      {
        char next = replacement.charAt(i + 1);
        if ((next >= '1') && (next <= '9'))
        {
          int num = next - '0';
          if (num <= matcher.groupCount()) {
            ss = matcher.group(num);
          }
        }
        else
        {
          switch (next)
          {
          case '&': 
            ss = matcher.group();
            break;
          case '`': 
            ss = originalString.substring(0, matcher.start());
            break;
          case '\'': 
            ss = originalString.substring(matcher.end());
            break;
          case '$': 
            ss = "$";
            break;
          }
        }
      }
      if (ss != null)
      {
        result.append(ss);
        lastIndex = i + 2;
      }
      else
      {
        result.append('$');
        lastIndex = i + 1;
      }
    }
    result.append(replacement.substring(lastIndex));
    
    return result.toString();
  }
  
  static boolean isEscaped(String characters, int position)
  {
    int p = position;
    int nbBackslash = 0;
    while ((p > 0) && (characters.charAt(--p) == '\\')) {
      nbBackslash++;
    }
    return nbBackslash % 2 == 1;
  }
  
  private Object wrappedAction(Context cx, Scriptable scope, Scriptable thisObj, Object[] args, int actionType)
  {
    try
    {
      ScriptRuntime.setRegExpProxy(cx, wrapped_);
      return wrapped_.action(cx, scope, thisObj, args, actionType);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd