htmlunit-2.7

ires_ = null;
    }
  }
  
  public String getName()
  {
    return name_;
  }
  
  public String getValue()
  {
    return value_;
  }
  
  public String getDomain()
  {
    return domain_;
  }
  
  public String getPath()
  {
    return path_;
  }
  
  public Date getExpires()
  {
    return expires_;
  }
  
  public boolean isSecure()
  {
    return secure_;
  }
  
  public String toString()
  {
    return name_ + "=" + value_ + (domain_ != null ? ";domain=" + domain_ : "") + (path_ != null ? ";path=" + path_ : "") + (expires_ != null ? ";expires=" + expires_ : "") + (secure_ ? ";secure" : "");
  }
  
  public boolean equals(Object o)
  {
    if (!(o instanceof Cookie)) {
      return false;
    }
    Cookie other = (Cookie)o;
    return new EqualsBuilder().append(name_, name_).append(domain_, domain_).append(path_, path_).isEquals();
  }
  
  public int hashCode()
  {
    return new HashCodeBuilder().append(name_).append(domain_).append(path_).toHashCode();
  }
  
  public org.apache.commons.httpclient.Cookie toHttpClient()
  {
    return new org.apache.commons.httpclient.Cookie(domain_, name_, value_, path_, expires_, secure_);
  }
  
  public static org.apache.commons.httpclient.Cookie[] toHttpClient(Collection<Cookie> cookies)
  {
    org.apache.commons.httpclient.Cookie[] array = new org.apache.commons.httpclient.Cookie[cookies.size()];
    Iterator<Cookie> it = cookies.iterator();
    for (int i = 0; i < cookies.size(); i++) {
      array[i] = ((Cookie)it.next()).toHttpClient();
    }
    return array;
  }
  
  public static List<Cookie> fromHttpClient(org.apache.commons.httpclient.Cookie[] cookies)
  {
    List<Cookie> list = new ArrayList(cookies.length);
    for (int i = 0; i < cookies.length; i++)
    {
      org.apache.commons.httpclient.Cookie c = cookies[i];
      Cookie cookie = new Cookie(c.getDomain(), c.getName(), c.getValue(), c.getPath(), c.getExpiryDate(), c.getSecure());
      
      list.add(cookie);
    }
    return list;
  }
}

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

import java.io.File;

public class KeyDataPair
  extends NameValuePair
{
  private static final long serialVersionUID = -1129314696176851675L;
  private final File fileObject_;
  private final String contentType_;
  private final String charset_;
  private byte[] data_;
  
  public KeyDataPair(String key, File file, String contentType, String charset)
  {
    super(key, file.getName());
    if (file.exists()) {
      fileObject_ = file;
    } else {
      fileObject_ = null;
    }
    contentType_ = contentType;
    charset_ = charset;
  }
  
  public File getFile()
  {
    return fileObject_;
  }
  
  public String getCharset()
  {
    return charset_;
  }
  
  public String getContentType()
  {
    return contentType_;
  }
  
  public byte[] getData()
  {
    return data_;
  }
  
  public void setData(byte[] data)
  {
    data_ = data;
  }
}

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

import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.WebWindow;
import java.net.URL;
import net.sourceforge.htmlunit.corejs.javascript.debug.DebuggableScript;
import net.sourceforge.htmlunit.corejs.javascript.tools.debugger.SourceProvider;
import org.apache.commons.lang.StringUtils;

final class WebClientUtils$1
  implements SourceProvider
{
  WebClientUtils$1(WebClient paramWebClient) {}
  
  public String getSource(DebuggableScript script)
  {
    String sourceName = script.getSourceName();
    if (sourceName.startsWith("script in "))
    {
      sourceName = StringUtils.substringBetween(sourceName, "script in ", " from");
      for (WebWindow ww : val$client.getWebWindows())
      {
        WebResponse wr = ww.getEnclosedPage().getWebResponse();
        if (sourceName.equals(wr.getRequestSettings().getUrl().toString())) {
          return wr.getContentAsString();
        }
      }
    }
    return null;
  }
}

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

import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine;
import java.net.URL;
import net.sourceforge.htmlunit.corejs.javascript.debug.DebuggableScript;
import net.sourceforge.htmlunit.corejs.javascript.tools.debugger.Main;
import net.sourceforge.htmlunit.corejs.javascript.tools.debugger.ScopeProvider;
import net.sourceforge.htmlunit.corejs.javascript.tools.debugger.SourceProvider;
import org.apache.commons.lang.StringUtils;

public final class WebClientUtils
{
  public static void attachVisualDebugger(WebClient client)
  {
    ScopeProvider sp = null;
    HtmlUnitContextFactory cf = client.getJavaScriptEngine().getContextFactory();
    Main main = Main.mainEmbedded(cf, sp, "HtmlUnit JavaScript Debugger");
    
    SourceProvider sourceProvider = new SourceProvider()
    {
      public String getSource(DebuggableScript script)
      {
        String sourceName = script.getSourceName();
        if (sourceName.startsWith("script in "))
        {
          sourceName = StringUtils.substringBetween(sourceName, "script in ", " from");
          for (WebWindow ww : val$client.getWebWindows())
          {
            WebResponse wr = ww.getEnclosedPage().getWebResponse();
            if (sourceName.equals(wr.getRequestSettings().getUrl().toString())) {
              return wr.getContentAsString();
            }
          }
        }
        return null;
      }
    };
    main.setSourceProvider(sourceProvider);
  }
}

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

import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.WebResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

public class WebResponseWrapper
  implements WebResponse
{
  private static final long serialVersionUID = -5167730179562144482L;
  private final WebResponse wrappedWebResponse_;
  
  public WebResponseWrapper(WebResponse webResponse)
    throws IllegalArgumentException
  {
    if (webResponse == null) {
      throw new IllegalArgumentException("Wrapped WebResponse can't be null");
    }
    wrappedWebResponse_ = webResponse;
  }
  
  public InputStream getContentAsStream()
    throws IOException
  {
    return wrappedWebResponse_.getContentAsStream();
  }
  
  public String getContentAsString()
  {
    return wrappedWebResponse_.getContentAsString();
  }
  
  public String getContentAsString(String encoding)
  {
    return wrappedWebResponse_.getContentAsString(encoding);
  }
  
  public byte[] getContentAsBytes()
  {
    return wrappedWebResponse_.getContentAsBytes();
  }
  
  @Deprecated
  public String getContentCharSet()
  {
    return wrappedWebResponse_.getContentCharSet();
  }
  
  public String getContentCharsetOrNull()
  {
    return wrappedWebResponse_.getContentCharsetOrNull();
  }
  
  public String getContentCharset()
  {
    return wrappedWebResponse_.getContentCharset();
  }
  
  public String getContentType()
  {
    return wrappedWebResponse_.getContentType();
  }
  
  public long getLoadTime()
  {
    return wrappedWebResponse_.getLoadTime();
  }
  
  @Deprecated
  public HttpMethod getRequestMethod()
  {
    return wrappedWebResponse_.getRequestMethod();
  }
  
  public List<NameValuePair> getResponseHeaders()
  {
    return wrappedWebResponse_.getResponseHeaders();
  }
  
  public String getResponseHeaderValue(String headerName)
  {
    return wrappedWebResponse_.getResponseHeaderValue(headerName);
  }
  
  public int getStatusCode()
  {
    return wrappedWebResponse_.getStatusCode();
  }
  
  public String getStatusMessage()
  {
    return wrappedWebResponse_.getStatusMessage();
  }
  
  @Deprecated
  public URL getRequestUrl()
  {
    return wrappedWebResponse_.getRequestUrl();
  }
  
  public WebRequestSettings getRequestSettings()
  {
    return wrappedWebResponse_.getRequestSettings();
  }
}

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

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.WebResponse;
import java.io.IOException;

public class WebConnectionWrapper
  implements WebConnection
{
  private final WebConnection wrappedWebConnection_;
  
  public WebConnectionWrapper(WebConnection webConnection)
    throws IllegalArgumentException
  {
    if (webConnection == null) {
      throw new IllegalArgumentException("Wrapped connection can't be null");
    }
    wrappedWebConnection_ = webConnection;
  }
  
  public WebConnectionWrapper(WebClient webClient)
    throws IllegalArgumentException
  {
    if (webClient == null) {
      throw new IllegalArgumentException("WebClient can't be null");
    }
    wrappedWebConnection_ = webClient.getWebConnection();
    webClient.setWebConnection(this);
  }
  
  public WebResponse getResponse(WebRequestSettings settings)
    throws IOException
  {
    return wrappedWebConnection_.getResponse(settings);
  }
}

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

import net.sourceforge.htmlunit.corejs.javascript.Context;
import net.sourceforge.htmlunit.corejs.javascript.ContextAction;
import net.sourceforge.htmlunit.corejs.javascript.Script;

class DebuggingWebConnection$1
  implements ContextAction
{
  DebuggingWebConnection$1(DebuggingWebConnection paramDebuggingWebConnection, String paramString1, String paramString2) {}
  
  public Object run(Context cx)
  {
    cx.setOptimizationLevel(-1);
    Script script = cx.compileString(val$scriptSource, val$scriptName, 0, null);
    return cx.decompileScript(script, 4);
  }
}

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

import com.gargoylesoftware.htmlunit.FormEncodingType;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.WebResponseData;
import com.gargoylesoftware.htmlunit.WebResponseImpl;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import net.sourceforge.htmlunit.corejs.javascript.Context;
import net.sourceforge.htmlunit.corejs.javascript.ContextAction;
import net.sourceforge.htmlunit.corejs.javascript.ContextFactory;
import net.sourceforge.htmlunit.corejs.javascript.Script;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DebuggingWebConnection
  extends WebConnectionWrapper
{
  private static final Log LOG = LogFactory.getLog(DebuggingWebConnection.class);
  private int counter_;
  private final WebConnection wrappedWebConnection_;
  private final File javaScriptFile_;
  private final File reportFolder_;
  private boolean uncompressJavaScript_ = true;
  
  public DebuggingWebConnection(WebConnection webConnection, String dirName)
    throws IOException
  {
    super(webConnection);
    
    wrappedWebConnection_ = webConnection;
    File tmpDir = new File(System.getProperty("java.io.tmpdir"));
    reportFolder_ = new File(tmpDir, dirName);
    if (reportFolder_.exists()) {
      FileUtils.forceDelete(reportFolder_);
    }
    FileUtils.forceMkdir(reportFolder_);
    javaScriptFile_ = new File(reportFolder_, "hu.js");
    createOverview();
  }
  
  public WebResponse getResponse(WebRequestSettings settings)
    throws IOException
  {
    WebResponse response = wrappedWebConnection_.getResponse(settings);
    if ((isUncompressJavaScript()) && (isJavaScript(response))) {
      response = uncompressJavaScript(response);
    }
    saveResponse(response, settings);
    return response;
  }
  
  protected WebResponse uncompressJavaScript(WebResponse response)
  {
    WebRequestSettings requestSettings = response.getRequestSettings();
    final String scriptName = requestSettings.getUrl().toString();
    final String scriptSource = response.getContentAsString();
    
    ContextFactory factory = new ContextFactory();
    ContextAction action = new ContextAction()
    {
      public Object run(Context cx)
      {
        cx.setOptimizationLevel(-1);
        Script script = cx.compileString(scriptSource, scriptName, 0, null);
        return cx.decompileScript(script, 4);
      }
    };
    try
    {
      String decompileScript = (String)factory.call(action);
      WebResponseData wrd = new WebResponseData(decompileScript.getBytes(), response.getStatusCode(), response.getStatusMessage(), response.getResponseHeaders());
      
      return new WebResponseImpl(wrd, response.getRequestSettings().getUrl(), response.getRequestSettings().getHttpMethod(), response.getLoadTime());
    }
    catch (Exception e)
    {
      LOG.warn("Failed to decompress JavaScript response. Delivering as it.", e);
    }
    return response;
  }
  
  public void addMark(String mark)
    throws IOException
  {
    appendToJSFile("tab[tab.length] = \"" + mark + "\";\n");
    LOG.info("--- " + mark + " ---");
  }
  
  protected void saveResponse(WebResponse response, WebRequestSettings settings)
    throws IOException
  {
    counter_ += 1;
    String extension;
    String extension;
    if (isJavaScript(response))
    {
      extension = ".js";
    }
    else
    {
      String extension;
      if ("text/html".equals(response.getContentType())) {
        extension = ".html";
      } else {
        extension = ".txt";
      }
    }
    File f = createFile(settings.getUrl(), extension);
    String content = response.getContentAsString();
    URL url = response.getRequestSettings().getUrl();
    FileUtils.writeStringToFile(f, content, response.getContentCharset());
    LOG.info("Created file " + f.getAbsolutePath() + " for response " + counter_ + ": " + url);
    
    StringBuilder buffer = new StringBuilder();
    buffer.append("tab[tab.length] = {code: " + response.getStatusCode() + ", ");
    buffer.append("fileName: '" + f.getName() + "', ");
    buffer.append("contentType: '" + response.getContentType() + "', ");
    buffer.append("method: '" + settings.getHttpMethod().name() + "', ");
    if ((settings.getHttpMethod() == HttpMethod.POST) && (settings.getEncodingType() == FormEncodingType.URL_ENCODED)) {
      buffer.append("postParameters: " + nameValueListToJsMap(settings.getRequestParameters()) + ", ");
    }
    buffer.append("url: '" + url + "', ");
    buffer.append("loadTime: " + response.getLoadTime() + ", ");
    byte[] bytes = response.getContentAsBytes();
    buffer.append("responseSize: " + (bytes == null ? 0 : bytes.length) + ", ");
    buffer.append("responseHeaders: " + nameValueListToJsMap(response.getResponseHeaders()));
    buffer.append("};\n");
    appendToJSFile(buffer.toString());
  }
  
  protected boolean isJavaScript(WebResponse response)
  {
    String contentType = response.getContentType();
    return (contentType.contains("javascript")) || (contentType.contains("ecmascript")) || ((contentType.startsWith("text/")) && (contentType.endsWith("js")));
  }
  
  public boolean isUncompressJavaScript()
  {
    return uncompressJavaScript_;
  }
  
  public void setUncompressJavaScript(boolean decompress)
  {
    uncompressJavaScript_ = decompress;
  }
  
  private void appendToJSFile(String str)
    throws IOException
  {
    FileWriter jsFileWriter = new FileWriter(javaScriptFile_, true);
    jsFileWriter.write(str);
    
    jsFileWriter.close();
  }
  
  private File createFile(URL url, String extension)
    throws IOException
  {
    String name = url.getPath().replaceFirst("/$", "").replaceAll(".*/", "");
    name = StringUtils.substringBefore(name, "?");
    name = StringUtils.substringBefore(name, ";");
    if (!name.endsWith(extension)) {
      name = name + extension;
    }
    int counter = 0;
    for (;;)
    {
      String fileName;
      String fileName;
      if (counter != 0) {
        fileName = StringUtils.substringBeforeLast(name, ".") + "_" + counter + "." + StringUtils.substringAfterLast(name, ".");
      } else {
        fileName = name;
      }
      File f = new File(reportFolder_, fileName);
      if (f.createNewFile()) {
        return f;
      }
      counter++;
    }
  }
  
  static String nameValueListToJsMap(List<NameValuePair> headers)
  {
    if ((headers == null) || (headers.isEmpty())) {
      return "{}";
    }
    StringBuilder buffer = new StringBuilder("{");
    for (NameValuePair header : headers) {
      buffer.append("'" + header.getName() + "': '" + header.getValue().replaceAll("'", "\\'") + "', ");
    }
    buffer.delete(buffer.length() - 2, buffer.length());
    buffer.append("}");
    return buffer.toString();
  }
  
  private void createOverview()
    throws IOException
  {
    FileUtils.writeStringToFile(javaScriptFile_, "var tab = [];\n", "ISO-8859-1");
    
    URL indexResource = DebuggingWebConnection.class.getResource("DebuggingWebConnection.index.html");
    if (indexResource == null) {
      throw new RuntimeException("Missing dependency DebuggingWebConnection.index.html");
    }
    File summary = new File(reportFolder_, "index.html");
    FileUtils.copyURLToFile(indexResource, summary);
    
    LOG.info("Summary will be in " + summary.getAbsolutePath());
  }
}

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

class EncodingSniffer$Attribute
{
  private final String name_;
  private final String value_;
  private final int updatedIndex_;
  
  EncodingSniffer$Attribute(String name, String value, int updatedIndex)
  {
    name_ = name;
    value_ = value;
    updatedIndex_ = updatedIndex;
  }
  
  String getName()
  {
    return name_;
  }
  
  String getValue()
  {
    return value_;
  }
  
  int getUpdatedIndex()
  {
    return updatedIndex_;
  }
}

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

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class EncodingSniffer
{
  private static final Log LOG = LogFactory.getLog(EncodingSniffer.class);
  static final String UTF16_LE = "UTF-16LE";
  static final String UTF16_BE = "UTF-16BE";
  static final String UTF8 = "UTF-8";
  private static final byte[][] COMMENT_START = { { 60 }, { 33 }, { 45 }, { 45 } };
  private static final byte[][] META_START = { { 60 }, { 109, 77 }, { 101, 69 }, { 116, 84 }, { 97, 65 }, { 9, 10, 12, 13, 32, 47 } };
  private static final byte[][] OTHER_START = { { 60 }, { 33, 47, 63 } };
  private static final byte[][] CHARSET_START = { { 99, 67 }, { 104, 72 }, { 97, 65 }, { 114, 82 }, { 115, 83 }, { 101, 69 }, { 116, 84 } };
  private static final int SIZE_OF_HTML_CONTENT_SNIFFED = 4096;
  private static final int SIZE_OF_XML_CONTENT_SNIFFED = 512;
  
  public static String sniffEncoding(List<NameValuePair> headers, InputStream content)
    throws IOException
  {
    if (isHtml(headers)) {
      return sniffHtmlEncoding(headers, content);
    }
    if (isXml(headers)) {
      return sniffXmlEncoding(headers, content);
    }
    return sniffUnknownContentTypeEncoding(headers, content);
  }
  
  static boolean isHtml(List<NameValuePair> headers)
  {
    return contentTypeEndsWith(headers, new String[] { "text/html" });
  }
  
  static boolean isXml(List<NameValuePair> headers)
  {
    return contentTypeEndsWith(headers, new String[] { "text/xml", "application/xml", "text/vnd.wap.wml", "+xml" });
  }
  
  static boolean contentTypeEndsWith(List<NameValuePair> headers, String... contentTypeEndings)
  {
    for (NameValuePair pair : headers)
    {
      String name = pair.getName();
      if ("content-type".equalsIgnoreCase(name))
      {
        String value = pair.getValue();
        int i = value.indexOf(';');
        if (i != -1) {
          value = value.substring(0, i);
        }
        value = value.trim();
        boolean found = false;
        for (String ending : contentTypeEndings) {
          if (value.toLowerCase().endsWith(ending.toLowerCase()))
          {
            found = true;
            break;
          }
        }
        return found;
      }
    }
    return false;
  }
  
  public static String sniffHtmlEncoding(List<NameValuePair> headers, InputStream content)
    throws IOException
  {
    String encoding = sniffEncodingFromHttpHeaders(headers);
    if ((encoding != null) || (content == null)) {
      return encoding;
    }
    byte[] bytes = read(content, 3);
    encoding = sniffEncodingFromUnicodeBom(bytes);
    if (encoding != null) {
      return encoding;
    }
    bytes = readAndPrepend(content, 4096, bytes);
    encoding = sniffEncodingFromMetaTag(bytes);
    return encoding;
  }
  
  public static String sniffXmlEncoding(List<NameValuePair> headers, InputStream content)
    throws IOException
  {
    String encoding = sniffEncodingFromHttpHeaders(headers);
    if ((encoding != null) || (content == null)) {
      return encoding;
    }
    byte[] bytes = read(content, 3);
    encoding = sniffEncodingFromUnicodeBom(bytes);
    if (encoding != null) {
      return encoding;
    }
    bytes = readAndPrepend(content, 512, bytes);
    encoding = sniffEncodingFromXmlDeclaration(bytes);
    return encoding;
  }
  
  public static String sniffUnknownContentTypeEncoding(List<NameValuePair> headers, InputStream content)
    throws IOException
  {
    String encoding = sniffEncodingFromHttpHeaders(headers);
    if ((encoding != null) || (content == null)) {
      return encoding;
    }
    byte[] bytes = read(content, 3);
    encoding = sniffEncodingFromUnicodeBom(bytes);
    return encoding;
  }
  
  static String sniffEncodingFromHttpHeaders(List<NameValuePair> headers)
  {
    String encoding = null;
    for (NameValuePair pair : headers)
    {
      String name = pair.getName();
      if ("content-type".equalsIgnoreCase(name))
      {
        String value = pair.getValue();
        encoding = extractEncodingFromContentType(value);
        if (encoding != null) {
          break;
        }
      }
    }
    if ((encoding != null) && (LOG.isDebugEnabled())) {
      LOG.debug("Encoding found in HTTP headers: '" + encoding + "'.");
    }
    return encoding;
  }
  
  static String sniffEncodingFromUnicodeBom(byte[] bytes)
  {
    String encoding = null;
    byte[] markerUTF8 = { -17, -69, -65 };
    byte[] markerUTF16BE = { -2, -1 };
    byte[] markerUTF16LE = { -1, -2 };
    if ((bytes != null) && (ArrayUtils.isEquals(markerUTF8, ArrayUtils.subarray(bytes, 0, 3)))) {
      encoding = "UTF-8";
    } else if ((bytes != null) && (ArrayUtils.isEquals(markerUTF16BE, ArrayUtils.subarray(bytes, 0, 2)))) {
      encoding = "UTF-16BE";
    } else if ((bytes != null) && (ArrayUtils.isEquals(markerUTF16LE, ArrayUtils.subarray(bytes, 0, 2)))) {
      encoding = "UTF-16LE";
    }
    if ((encoding != null) && (LOG.isDebugEnabled())) {
      LOG.debug("Encoding found in Unicode Byte Order Mark: '" + encoding + "'.");
    }
    return encoding;
  }
  
  static String sniffEncodingFromMetaTag(byte[] bytes)
  {
    for (int i = 0; i < bytes.length; i++) {
      if (matches(bytes, i, COMMENT_START))
      {
        i = indexOfSubArray(bytes, new byte[] { 45, 45, 62 }, i);
        if (i == -1) {
          break;
        }
        i += 2;
      }
      else if (matches(bytes, i, META_START))
      {
        i += META_START.length;
        for (Attribute att = getAttribute(bytes, i); att != null; att = getAttribute(bytes, i))
        {
          i = att.getUpdatedIndex();
          String name = att.getName();
          String value = att.getValue();
          if (("charset".equals(name)) || ("content".equals(name)))
          {
            String charset = null;
            if ("charset".equals(name))
            {
              charset = value;
            }
            else if ("content".equals(name))
            {
              charset = extractEncodingFromContentType(value);
              if (charset == null) {
                continue;
              }
            }
            if (("UTF-16BE".equalsIgnoreCase(charset)) || ("UTF-16LE".equalsIgnoreCase(charset))) {
              charset = "UTF-8";
            }
            if (isSupportedCharset(charset))
            {
              if (LOG.isDebugEnabled()) {
                LOG.debug("Encoding found in meta tag: '" + charset + "'.");
              }
              return charset;
            }
          }
        }
      }
      else if ((i + 1 < bytes.length) && (bytes[i] == 60) && (Character.isLetter(bytes[(i + 1)])))
      {
        i = skipToAnyOf(bytes, i, new byte[] { 9, 10, 12, 13, 32, 62 });
        if (i == -1) {
          break;
        }
        Attribute att;
        while ((att = getAttribute(bytes, i)) != null) {
          i = att.getUpdatedIndex();
        }
      }
      else if ((i + 2 < bytes.length) && (bytes[i] == 60) && (bytes[(i + 1)] == 47) && (Character.isLetter(bytes[(i + 2)])))
      {
        i = skipToAnyOf(bytes, i, new byte[] { 9, 10, 12, 13, 32, 62 });
        if (i == -1) {
          break;
        }
        Attribute attribute;
        while ((attribute = getAttribute(bytes, i)) != null) {
          i = attribute.getUpdatedIndex();
        }
      }
      else if (matches(bytes, i, OTHER_START))
      {
        i = skipToAnyOf(bytes, i, new byte[] { 62 });
        if (i == -1) {
          break;
        }
      }
    }
    return null;
  }
  
  static Attribute getAttribute(byte[] bytes, int i)
  {
    if (i >= bytes.length) {
      return null;
    }
    while ((bytes[i] == 9) || (bytes[i] == 10) || (bytes[i] == 12) || (bytes[i] == 13) || (bytes[i] == 32) || (bytes[i] == 47))
    {
      i++;
      if (i >= bytes.length) {
        return null;
      }
    }
    if (bytes[i] == 62) {
      return null;
    }
    String name = "";
    String value = "";
    for (;; i++)
    {
      if (i >= bytes.length) {
        return new Attribute(name, value, i);
      }
      if ((bytes[i] == 61) && (name.length() > 0))
      {
        i++;
        break;
      }
      if ((bytes[i] == 9) || (bytes[i] == 10) || (bytes[i] == 12) || (bytes[i] == 13) || (bytes[i] == 32))
      {
        while ((bytes[i] == 9) || (bytes[i] == 10) || (bytes[i] == 12) || (bytes[i] == 13) || (bytes[i] == 32))
        {
          i++;
          if (i >= bytes.length) {
            return new Attribute(name, value, i);
          }
        }
        if (bytes[i] != 61) {
          return new Attribute(name, value, i);
        }
        i++;
        break;
      }
      if ((bytes[i] == 47) || (bytes[i] == 62)) {
        return new Attribute(name, value, i);
      }
      name = name + (char)bytes[i];
    }
    if (i >= bytes.length) {
      return new Attribute(name, value, i);
    }
    while ((bytes[i] == 9) || (bytes[i] == 10) || (bytes[i] == 12) || (bytes[i] == 13) || (bytes[i] == 32))
    {
      i++;
      if (i >= bytes.length) {
        return new Attribute(name, value, i);
      }
    }
    if ((bytes[i] == 34) || (bytes[i] == 39))
    {
      byte b = bytes[i];
      for (i++; i < bytes.length; i++)
      {
        if (bytes[i] == b)
        {
          i++;
          return new Attribute(name, value, i);
        }
        if ((bytes[i] >= 65) && (bytes[i] <= 90))
        {
          byte b2 = (byte)(bytes[i] + 32);
          value = value + (char)b2;
        }
        else
        {
          value = value + (char)bytes[i];
        }
      }
      return new Attribute(name, value, i);
    }
    if (bytes[i] == 62) {
      return new Attribute(name, value, i);
    }
    if ((bytes[i] >= 65) && (bytes[i] <= 90))
    {
      byte b = (byte)(bytes[i] + 32);
      value = value + (char)b;
      i++;
    }
    else
    {
      value = value + (char)bytes[i];
      i++;
    }
    for (; i < bytes.length; i++)
    {
      if ((bytes[i] == 9) || (bytes[i] == 10) || (bytes[i] == 12) || (bytes[i] == 13) || (bytes[i] == 32) || (bytes[i] == 62)) {
        return new Attribute(name, value, i);
      }
      if ((bytes[i] >= 65) && (bytes[i] <= 90))
      {
        byte b = (byte)(bytes[i] + 32);
        value = value + (char)b;
      }
      else
      {
        value = value + (char)bytes[i];
      }
    }
    return new Attribute(name, value, i);
  }
  
  static String extractEncodingFromContentType(String s)
  {
    if (s == null) {
      return null;
    }
    byte[] bytes = s.getBytes();
    for (int i = 0; i < bytes.length; i++) {
      if (matches(bytes, i, CHARSET_START))
      {
        i += CHARSET_START.length;
        break;
      }
    }
    if (i == bytes.length) {
      return null;
    }
    while ((bytes[i] == 9) || (bytes[i] == 10) || (bytes[i] == 12) || (bytes[i] == 13) || (bytes[i] == 32))
    {
      i++;
      if (i == bytes.length) {
        return null;
      }
    }
    if (bytes[i] != 61) {
      return null;
    }
    i++;
    if (i == bytes.length) {
      return null;
    }
    while ((bytes[i] == 9) || (bytes[i] == 10) || (bytes[i] == 12) || (bytes[i] == 13) || (bytes[i] == 32))
    {
      i++;
      if (i == bytes.length) {
        return null;
      }
    }
    if (bytes[i] == 34)
    {
      if (bytes.length <= i + 1) {
        return null;
      }
      int index = ArrayUtils.indexOf(bytes, (byte)34, i + 1);
      if (index == -1) {
        return null;
      }
      String charset = new String(ArrayUtils.subarray(bytes, i + 1, index));
      return isSupportedCharset(charset) ? charset : null;
    }
    if (bytes[i] == 39)
    {
      if (bytes.length <= i + 1) {
        return null;
      }
      int index = ArrayUtils.indexOf(bytes, (byte)39, i + 1);
      if (index == -1) {
        return null;
      }
      String charset = new String(ArrayUtils.subarray(bytes, i + 1, index));
      return isSupportedCharset(charset) ? charset : null;
    }
    int end = skipToAnyOf(bytes, i, new byte[] { 9, 10, 12, 13, 32, 59 });
    if (end == -1) {
      end = bytes.length;
    }
    String charset = new String(ArrayUtils.subarray(bytes, i, end));
    return isSupportedCharset(charset) ? charset : null;
  }
  
  static String sniffEncodingFromXmlDeclaration(byte[] bytes)
  {
    String encoding = null;
    byte[] declarationPrefix = "<?xml ".getBytes();
    if (ArrayUtils.isEquals(declarationPrefix, ArrayUtils.subarray(bytes, 0, declarationPrefix.length)))
    {
      int index = ArrayUtils.indexOf(bytes, (byte)63, 2);
      if ((index + 1 < bytes.length) && (bytes[(index + 1)] == 62))
      {
        String declaration = new String(bytes, 0, index + 2);
        int start = declaration.indexOf("encoding");
        if (start != -1)
        {
          start += 8;
          char delimiter;
          for (;;)
          {
            switch (declaration.charAt(start))
            {
            case '"': 
            case '\'': 
              delimiter = declaration.charAt(start);
              start += 1;
              break;
            default: 
              start++;
            }
          }
          int end = declaration.indexOf(delimiter, start);
          encoding = declaration.substring(start, end);
        }
      }
    }
    if ((encoding != null) && (!isSupportedCharset(encoding))) {
      encoding = null;
    }
    if ((encoding != null) && (LOG.isDebugEnabled())) {
      LOG.debug("Encoding found in XML declaration: '" + encoding + "'.");
    }
    return encoding;
  }
  
  static boolean isSupportedCharset(String charset)
  {
    try
    {
      return Charset.isSupported(charset);
    }
    catch (IllegalCharsetNameException e) {}
    return false;
  }
  
  static boolean matches(byte[] bytes, int i, byte[][] sought)
  {
    if (i + sought.length > bytes.length) {
      return false;
    }
    for (int x = 0; x < sought.length; x++)
    {
      byte[] possibilities = sought[x];
      boolean match = false;
      for (int y = 0; y < possibilities.length; y++) {
        if (bytes[(i + x)] == possibilities[y])
        {
          match = true;
          break;
        }
      }
      if (!match) {
        return false;
      }
    }
    return true;
  }
  
  static int skipToAnyOf(byte[] bytes, int i, byte[] targets)
  {
    for (; i < bytes.length; i++) {
      if (ArrayUtils.contains(targets, bytes[i])) {
        break;
      }
    }
    if (i == bytes.length) {
      i = -1;
    }
    return i;
  }
  
  static int indexOfSubArray(byte[] array, byte[] subarray, int startIndex)
  {
    for (int i = startIndex; i < array.length; i++)
    {
      boolean found = true;
      if (i + subarray.length > array.length) {
        break;
      }
      for (int j = 0; j < subarray.length; j++)
      {
        byte a = array[(i + j)];
        byte b = subarray[j];
        if (a != b)
        {
          found = false;
          break;
        }
      }
      if (found) {
        return i;
      }
    }
    return -1;
  }
  
  static byte[] read(InputStream content, int size)
    throws IOException
  {
    byte[] bytes = new byte[size];
    int count = content.read(bytes);
    if (count == -1)
    {
      bytes = new byte[0];
    }
    else if (count < size)
    {
      byte[] smaller = new byte[count];
      System.arraycopy(bytes, 0, smaller, 0, count);
      bytes = smaller;
    }
    return bytes;
  }
  
  static byte[] readAndPrepend(InputStream content, int size, byte[] prefix)
    throws IOException
  {
    byte[] bytes = read(content, size);
    byte[] joined = new byte[prefix.length + bytes.length];
    System.arraycopy(prefix, 0, joined, 0, prefix.length);
    System.arraycopy(bytes, 0, joined, prefix.length, bytes.length);
    return joined;
  }
  
  static class Attribute
  {
    private final String name_;
    private final String value_;
    private final int updatedIndex_;
    
    Attribute(String name, String value, int updatedIndex)
    {
      name_ = name;
      value_ = value;
      updatedIndex_ = updatedIndex;
    }
    
    String getName()
    {
      return name_;
    }
    
    String getValue()
    {
      return value_;
    }
    
    int getUpdatedIndex()
    {
      return updatedIndex_;
    }
  }
}

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

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.WebResponseData;
import com.gargoylesoftware.htmlunit.WebResponseImpl;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public abstract class FalsifyingWebConnection
  extends WebConnectionWrapper
{
  public FalsifyingWebConnection(WebConnection webConnection)
    throws IllegalArgumentException
  {
    super(webConnection);
  }
  
  public FalsifyingWebConnection(WebClient webClient)
    throws IllegalArgumentException
  {
    super(webClient);
  }
  
  protected WebResponse deliverFromAlternateUrl(WebRequestSettings webRequestSettings, URL url)
    throws IOException
  {
    URL originalUrl = webRequestSettings.getUrl();
    webRequestSettings.setUrl(url);
    WebResponse resp = super.getResponse(webRequestSettings);
    resp.getRequestSettings().setUrl(originalUrl);
    return resp;
  }
  
  protected WebResponse replaceContent(WebResponse wr, String newContent)
    throws IOException
  {
    byte[] body = newContent.getBytes(wr.getContentCharset());
    WebResponseData wrd = new WebResponseData(body, wr.getStatusCode(), wr.getStatusMessage(), wr.getResponseHeaders());
    
    return new WebResponseImpl(wrd, wr.getRequestSettings().getUrl(), wr.getRequestSettings().getHttpMethod(), wr.getLoadTime());
  }
  
  protected WebResponse createWebResponse(WebRequestSettings wr, String content, String contentType)
    throws IOException
  {
    return createWebResponse(wr, content, contentType, 200, "OK");
  }
  
  protected WebResponse createWebResponse(WebRequestSettings wr, String content, String contentType, int responseCode, String responseMessage)
    throws IOException
  {
    List<NameValuePair> headers = new ArrayList();
    String encoding = "UTF-8";
    headers.add(new NameValuePair("content-type", contentType + "; charset=" + "UTF-8"));
    byte[] body = content.getBytes("UTF-8");
    WebResponseData wrd = new WebResponseData(body, responseCode, responseMessage, headers);
    return new WebResponseImpl(wrd, wr.getUrl(), wr.getHttpMethod(), 0L);
  }
}

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

import java.util.Iterator;

class DomNode$1
  implements Iterable<DomNode>
{
  DomNode$1(DomNode paramDomNode) {}
  
  public Iterator<DomNode> iterator()
  {
    return new DomNode.ChildIterator(this$0);
  }
}

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

import java.util.Iterator;
import java.util.NoSuchElementException;

public class DomNode$ChildIterator
  implements Iterator<DomNode>
{
  private DomNode nextNode_ = DomNode.access$000(this$0);
  private DomNode currentNode_ = null;
  
  protected DomNode$ChildIterator(DomNode paramDomNode) {}
  
  public boolean hasNext()
  {
    return nextNode_ != null;
  }
  
  public DomNode next()
  {
    if (nextNode_ != null)
    {
      currentNode_ = nextNode_;
      nextNode_ = DomNode.access$100(nextNode_);
      return currentNode_;
    }
    throw new NoSuchElementException();
  }
  
  public void remove()
  {
    if (currentNode_ == null) {
      throw new IllegalStateException();
    }
    currentNode_.remove();
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.html.DomNode.ChildIterator
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoyl
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