jsoup-1.6.3

16:38:13.716 INFO  jd.cli.Main - Decompiling jsoup-1.6.3.jar
package org.jsoup;

import java.net.URL;
import java.util.Map;

public abstract interface Connection$Base<T extends Base>
{
  public abstract URL url();
  
  public abstract T url(URL paramURL);
  
  public abstract Connection.Method method();
  
  public abstract T method(Connection.Method paramMethod);
  
  public abstract String header(String paramString);
  
  public abstract T header(String paramString1, String paramString2);
  
  public abstract boolean hasHeader(String paramString);
  
  public abstract T removeHeader(String paramString);
  
  public abstract Map<String, String> headers();
  
  public abstract String cookie(String paramString);
  
  public abstract T cookie(String paramString1, String paramString2);
  
  public abstract boolean hasCookie(String paramString);
  
  public abstract T removeCookie(String paramString);
  
  public abstract Map<String, String> cookies();
}

/* Location:
 * Qualified Name:     org.jsoup.Connection.Base
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup;

public abstract interface Connection$KeyVal
{
  public abstract KeyVal key(String paramString);
  
  public abstract String key();
  
  public abstract KeyVal value(String paramString);
  
  public abstract String value();
}

/* Location:
 * Qualified Name:     org.jsoup.Connection.KeyVal
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup;

public enum Connection$Method
{
  GET,  POST;
  
  private Connection$Method() {}
}

/* Location:
 * Qualified Name:     org.jsoup.Connection.Method
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup;

import java.util.Collection;
import org.jsoup.parser.Parser;

public abstract interface Connection$Request
  extends Connection.Base<Request>
{
  public abstract int timeout();
  
  public abstract Request timeout(int paramInt);
  
  public abstract boolean followRedirects();
  
  public abstract Request followRedirects(boolean paramBoolean);
  
  public abstract boolean ignoreHttpErrors();
  
  public abstract Request ignoreHttpErrors(boolean paramBoolean);
  
  public abstract boolean ignoreContentType();
  
  public abstract Request ignoreContentType(boolean paramBoolean);
  
  public abstract Request data(Connection.KeyVal paramKeyVal);
  
  public abstract Collection<Connection.KeyVal> data();
  
  public abstract Request parser(Parser paramParser);
  
  public abstract Parser parser();
}

/* Location:
 * Qualified Name:     org.jsoup.Connection.Request
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup;

import java.io.IOException;
import org.jsoup.nodes.Document;

public abstract interface Connection$Response
  extends Connection.Base<Response>
{
  public abstract int statusCode();
  
  public abstract String statusMessage();
  
  public abstract String charset();
  
  public abstract String contentType();
  
  public abstract Document parse()
    throws IOException;
  
  public abstract String body();
  
  public abstract byte[] bodyAsBytes();
}

/* Location:
 * Qualified Name:     org.jsoup.Connection.Response
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup;

import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Map;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;

public abstract interface Connection
{
  public abstract Connection url(URL paramURL);
  
  public abstract Connection url(String paramString);
  
  public abstract Connection userAgent(String paramString);
  
  public abstract Connection timeout(int paramInt);
  
  public abstract Connection referrer(String paramString);
  
  public abstract Connection followRedirects(boolean paramBoolean);
  
  public abstract Connection method(Method paramMethod);
  
  public abstract Connection ignoreHttpErrors(boolean paramBoolean);
  
  public abstract Connection ignoreContentType(boolean paramBoolean);
  
  public abstract Connection data(String paramString1, String paramString2);
  
  public abstract Connection data(Map<String, String> paramMap);
  
  public abstract Connection data(String... paramVarArgs);
  
  public abstract Connection header(String paramString1, String paramString2);
  
  public abstract Connection cookie(String paramString1, String paramString2);
  
  public abstract Connection cookies(Map<String, String> paramMap);
  
  public abstract Connection parser(Parser paramParser);
  
  public abstract Document get()
    throws IOException;
  
  public abstract Document post()
    throws IOException;
  
  public abstract Response execute()
    throws IOException;
  
  public abstract Request request();
  
  public abstract Connection request(Request paramRequest);
  
  public abstract Response response();
  
  public abstract Connection response(Response paramResponse);
  
  public static abstract interface KeyVal
  {
    public abstract KeyVal key(String paramString);
    
    public abstract String key();
    
    public abstract KeyVal value(String paramString);
    
    public abstract String value();
  }
  
  public static abstract interface Response
    extends Connection.Base<Response>
  {
    public abstract int statusCode();
    
    public abstract String statusMessage();
    
    public abstract String charset();
    
    public abstract String contentType();
    
    public abstract Document parse()
      throws IOException;
    
    public abstract String body();
    
    public abstract byte[] bodyAsBytes();
  }
  
  public static abstract interface Request
    extends Connection.Base<Request>
  {
    public abstract int timeout();
    
    public abstract Request timeout(int paramInt);
    
    public abstract boolean followRedirects();
    
    public abstract Request followRedirects(boolean paramBoolean);
    
    public abstract boolean ignoreHttpErrors();
    
    public abstract Request ignoreHttpErrors(boolean paramBoolean);
    
    public abstract boolean ignoreContentType();
    
    public abstract Request ignoreContentType(boolean paramBoolean);
    
    public abstract Request data(Connection.KeyVal paramKeyVal);
    
    public abstract Collection<Connection.KeyVal> data();
    
    public abstract Request parser(Parser paramParser);
    
    public abstract Parser parser();
  }
  
  public static abstract interface Base<T extends Base>
  {
    public abstract URL url();
    
    public abstract T url(URL paramURL);
    
    public abstract Connection.Method method();
    
    public abstract T method(Connection.Method paramMethod);
    
    public abstract String header(String paramString);
    
    public abstract T header(String paramString1, String paramString2);
    
    public abstract boolean hasHeader(String paramString);
    
    public abstract T removeHeader(String paramString);
    
    public abstract Map<String, String> headers();
    
    public abstract String cookie(String paramString);
    
    public abstract T cookie(String paramString1, String paramString2);
    
    public abstract boolean hasCookie(String paramString);
    
    public abstract T removeCookie(String paramString);
    
    public abstract Map<String, String> cookies();
  }
  
  public static enum Method
  {
    GET,  POST;
    
    private Method() {}
  }
}

/* Location:
 * Qualified Name:     org.jsoup.Connection
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.examples;

class HtmlToPlainText$1 {}

/* Location:
 * Qualified Name:     org.jsoup.examples.HtmlToPlainText.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.examples;

import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.NodeVisitor;

class HtmlToPlainText$FormattingVisitor
  implements NodeVisitor
{
  private static final int maxWidth = 80;
  private int width = 0;
  private StringBuilder accum = new StringBuilder();
  
  private HtmlToPlainText$FormattingVisitor(HtmlToPlainText paramHtmlToPlainText) {}
  
  public void head(Node node, int depth)
  {
    String name = node.nodeName();
    if ((node instanceof TextNode)) {
      append(((TextNode)node).text());
    } else if (name.equals("li")) {
      append("\n * ");
    }
  }
  
  public void tail(Node node, int depth)
  {
    String name = node.nodeName();
    if (name.equals("br")) {
      append("\n");
    } else if (StringUtil.in(name, new String[] { "p", "h1", "h2", "h3", "h4", "h5" })) {
      append("\n\n");
    } else if (name.equals("a")) {
      append(String.format(" <%s>", new Object[] { node.absUrl("href") }));
    }
  }
  
  private void append(String text)
  {
    if (text.startsWith("\n")) {
      width = 0;
    }
    if (text.equals(" ")) {
      if (accum.length() != 0)
      {
        if (!StringUtil.in(accum.substring(accum.length() - 1), new String[] { " ", "\n" })) {}
      }
      else {
        return;
      }
    }
    if (text.length() + width > 80)
    {
      String[] words = text.split("\\s+");
      for (int i = 0; i < words.length; i++)
      {
        String word = words[i];
        boolean last = i == words.length - 1;
        if (!last) {
          word = word + " ";
        }
        if (word.length() + width > 80)
        {
          accum.append("\n").append(word);
          width = word.length();
        }
        else
        {
          accum.append(word);
          width += word.length();
        }
      }
    }
    else
    {
      accum.append(text);
      width += text.length();
    }
  }
  
  public String toString()
  {
    return accum.toString();
  }
}

/* Location:
 * Qualified Name:     org.jsoup.examples.HtmlToPlainText.FormattingVisitor
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.examples;

import java.io.IOException;
import java.io.PrintStream;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.NodeTraversor;
import org.jsoup.select.NodeVisitor;

public class HtmlToPlainText
{
  public static void main(String... args)
    throws IOException
  {
    Validate.isTrue(args.length == 1, "usage: supply url to fetch");
    String url = args[0];
    
    Document doc = Jsoup.connect(url).get();
    
    HtmlToPlainText formatter = new HtmlToPlainText();
    String plainText = formatter.getPlainText(doc);
    System.out.println(plainText);
  }
  
  public String getPlainText(Element element)
  {
    FormattingVisitor formatter = new FormattingVisitor(null);
    NodeTraversor traversor = new NodeTraversor(formatter);
    traversor.traverse(element);
    
    return formatter.toString();
  }
  
  private class FormattingVisitor
    implements NodeVisitor
  {
    private static final int maxWidth = 80;
    private int width = 0;
    private StringBuilder accum = new StringBuilder();
    
    private FormattingVisitor() {}
    
    public void head(Node node, int depth)
    {
      String name = node.nodeName();
      if ((node instanceof TextNode)) {
        append(((TextNode)node).text());
      } else if (name.equals("li")) {
        append("\n * ");
      }
    }
    
    public void tail(Node node, int depth)
    {
      String name = node.nodeName();
      if (name.equals("br")) {
        append("\n");
      } else if (StringUtil.in(name, new String[] { "p", "h1", "h2", "h3", "h4", "h5" })) {
        append("\n\n");
      } else if (name.equals("a")) {
        append(String.format(" <%s>", new Object[] { node.absUrl("href") }));
      }
    }
    
    private void append(String text)
    {
      if (text.startsWith("\n")) {
        width = 0;
      }
      if (text.equals(" ")) {
        if (accum.length() != 0)
        {
          if (!StringUtil.in(accum.substring(accum.length() - 1), new String[] { " ", "\n" })) {}
        }
        else {
          return;
        }
      }
      if (text.length() + width > 80)
      {
        String[] words = text.split("\\s+");
        for (int i = 0; i < words.length; i++)
        {
          String word = words[i];
          boolean last = i == words.length - 1;
          if (!last) {
            word = word + " ";
          }
          if (word.length() + width > 80)
          {
            accum.append("\n").append(word);
            width = word.length();
          }
          else
          {
            accum.append(word);
            width += word.length();
          }
        }
      }
      else
      {
        accum.append(text);
        width += text.length();
      }
    }
    
    public String toString()
    {
      return accum.toString();
    }
  }
}

/* Location:
 * Qualified Name:     org.jsoup.examples.HtmlToPlainText
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.examples;

import java.io.IOException;
import java.io.PrintStream;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ListLinks
{
  public static void main(String[] args)
    throws IOException
  {
    Validate.isTrue(args.length == 1, "usage: supply url to fetch");
    String url = args[0];
    print("Fetching %s...", new Object[] { url });
    
    Document doc = Jsoup.connect(url).get();
    Elements links = doc.select("a[href]");
    Elements media = doc.select("[src]");
    Elements imports = doc.select("link[href]");
    
    print("\nMedia: (%d)", new Object[] { Integer.valueOf(media.size()) });
    for (Element src : media) {
      if (src.tagName().equals("img")) {
        print(" * %s: <%s> %sx%s (%s)", new Object[] { src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"), trim(src.attr("alt"), 20) });
      } else {
        print(" * %s: <%s>", new Object[] { src.tagName(), src.attr("abs:src") });
      }
    }
    print("\nImports: (%d)", new Object[] { Integer.valueOf(imports.size()) });
    for (Element link : imports) {
      print(" * %s <%s> (%s)", new Object[] { link.tagName(), link.attr("abs:href"), link.attr("rel") });
    }
    print("\nLinks: (%d)", new Object[] { Integer.valueOf(links.size()) });
    for (Element link : links) {
      print(" * a: <%s>  (%s)", new Object[] { link.attr("abs:href"), trim(link.text(), 35) });
    }
  }
  
  private static void print(String msg, Object... args)
  {
    System.out.println(String.format(msg, args));
  }
  
  private static String trim(String s, int width)
  {
    if (s.length() > width) {
      return s.substring(0, width - 1) + ".";
    }
    return s;
  }
}

/* Location:
 * Qualified Name:     org.jsoup.examples.ListLinks
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.helper;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Document.OutputSettings;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;

public class DataUtil
{
  private static final Pattern charsetPattern = Pattern.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"]*)");
  static final String defaultCharset = "UTF-8";
  private static final int bufferSize = 131072;
  
  public static Document load(File in, String charsetName, String baseUri)
    throws IOException
  {
    FileInputStream inStream = null;
    try
    {
      inStream = new FileInputStream(in);
      ByteBuffer byteData = readToByteBuffer(inStream);
      return parseByteData(byteData, charsetName, baseUri, Parser.htmlParser());
    }
    finally
    {
      if (inStream != null) {
        inStream.close();
      }
    }
  }
  
  public static Document load(InputStream in, String charsetName, String baseUri)
    throws IOException
  {
    ByteBuffer byteData = readToByteBuffer(in);
    return parseByteData(byteData, charsetName, baseUri, Parser.htmlParser());
  }
  
  public static Document load(InputStream in, String charsetName, String baseUri, Parser parser)
    throws IOException
  {
    ByteBuffer byteData = readToByteBuffer(in);
    return parseByteData(byteData, charsetName, baseUri, parser);
  }
  
  static Document parseByteData(ByteBuffer byteData, String charsetName, String baseUri, Parser parser)
  {
    Document doc = null;
    String docData;
    if (charsetName == null)
    {
      String docData = Charset.forName("UTF-8").decode(byteData).toString();
      doc = parser.parseInput(docData, baseUri);
      Element meta = doc.select("meta[http-equiv=content-type], meta[charset]").first();
      if (meta != null)
      {
        String foundCharset = meta.hasAttr("http-equiv") ? getCharsetFromContentType(meta.attr("content")) : meta.attr("charset");
        if ((foundCharset != null) && (foundCharset.length() != 0) && (!foundCharset.equals("UTF-8")))
        {
          charsetName = foundCharset;
          byteData.rewind();
          docData = Charset.forName(foundCharset).decode(byteData).toString();
          doc = null;
        }
      }
    }
    else
    {
      Validate.notEmpty(charsetName, "Must set charset arg to character set of file to parse. Set to null to attempt to detect from HTML");
      docData = Charset.forName(charsetName).decode(byteData).toString();
    }
    if (doc == null)
    {
      if (docData.charAt(0) == 65279) {
        docData = docData.substring(1);
      }
      doc = parser.parseInput(docData, baseUri);
      doc.outputSettings().charset(charsetName);
    }
    return doc;
  }
  
  static ByteBuffer readToByteBuffer(InputStream inStream)
    throws IOException
  {
    byte[] buffer = new byte[131072];
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(131072);
    for (;;)
    {
      int read = inStream.read(buffer);
      if (read == -1) {
        break;
      }
      outStream.write(buffer, 0, read);
    }
    ByteBuffer byteData = ByteBuffer.wrap(outStream.toByteArray());
    return byteData;
  }
  
  static String getCharsetFromContentType(String contentType)
  {
    if (contentType == null) {
      return null;
    }
    Matcher m = charsetPattern.matcher(contentType);
    if (m.find()) {
      return m.group(1).trim().toUpperCase();
    }
    return null;
  }
}

/* Location:
 * Qualified Name:     org.jsoup.helper.DataUtil
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.helper;

class DescendableLinkedList$1 {}

/* Location:
 * Qualified Name:     org.jsoup.helper.DescendableLinkedList.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.helper;

import java.util.Iterator;
import java.util.ListIterator;

class DescendableLinkedList$DescendingIterator<E>
  implements Iterator<E>
{
  private final ListIterator<E> iter;
  
  private DescendableLinkedList$DescendingIterator(DescendableLinkedList paramDescendableLinkedList, int index)
  {
    iter = paramDescendableLinkedList.listIterator(index);
  }
  
  public boolean hasNext()
  {
    return iter.hasPrevious();
  }
  
  public E next()
  {
    return (E)iter.previous();
  }
  
  public void remove()
  {
    iter.remove();
  }
}

/* Location:
 * Qualified Name:     org.jsoup.helper.DescendableLinkedList.DescendingIterator
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.helper;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class DescendableLinkedList<E>
  extends LinkedList<E>
{
  public void push(E e)
  {
    addFirst(e);
  }
  
  public E peekLast()
  {
    return size() == 0 ? null : getLast();
  }
  
  public E pollLast()
  {
    return size() == 0 ? null : removeLast();
  }
  
  public Iterator<E> descendingIterator()
  {
    return new DescendingIterator(size(), null);
  }
  
  private class DescendingIterator<E>
    implements Iterator<E>
  {
    private final ListIterator<E> iter;
    
    private DescendingIterator(int index)
    {
      iter = listIterator(index);
    }
    
    public boolean hasNext()
    {
      return iter.hasPrevious();
    }
    
    public E next()
    {
      return (E)iter.previous();
    }
    
    public void remove()
    {
      iter.remove();
    }
  }
}

/* Location:
 * Qualified Name:     org.jsoup.helper.DescendableLinkedList
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.helper;

class HttpConnection$1 {}

/* Location:
 * Qualified Name:     org.jsoup.helper.HttpConnection.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.helper;

import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.jsoup.Connection.Base;
import org.jsoup.Connection.Method;

abstract class HttpConnection$Base<T extends Connection.Base>
  implements Connection.Base<T>
{
  URL url;
  Connection.Method method;
  Map<String, String> headers;
  Map<String, String> cookies;
  
  private HttpConnection$Base()
  {
    headers = new LinkedHashMap();
    cookies = new LinkedHashMap();
  }
  
  public URL url()
  {
    return url;
  }
  
  public T url(URL url)
  {
    Validate.notNull(url, "URL must not be null");
    this.url = url;
    return this;
  }
  
  public Connection.Method method()
  {
    return method;
  }
  
  public T method(Connection.Method method)
  {
    Validate.notNull(method, "Method must not be null");
    this.method = method;
    return this;
  }
  
  public String header(String name)
  {
    Validate.notNull(name, "Header name must not be null");
    return getHeaderCaseInsensitive(name);
  }
  
  public T header(String name, String value)
  {
    Validate.notEmpty(name, "Header name must not be empty");
    Validate.notNull(value, "Header value must not be null");
    removeHeader(name);
    headers.put(name, value);
    return this;
  }
  
  public boolean hasHeader(String name)
  {
    Validate.notEmpty(name, "Header name must not be empty");
    return getHeaderCaseInsensitive(name) != null;
  }
  
  public T removeHeader(String name)
  {
    Validate.notEmpty(name, "Header name must not be empty");
    Map.Entry<String, String> entry = scanHeaders(name);
    if (entry != null) {
      headers.remove(entry.getKey());
    }
    return this;
  }
  
  public Map<String, String> headers()
  {
    return headers;
  }
  
  private String getHeaderCaseInsensitive(String name)
  {
    Validate.notNull(name, "Header name must not be null");
    
    String value = (String)headers.get(name);
    if (value == null) {
      value = (String)headers.get(name.toLowerCase());
    }
    if (value == null)
    {
      Map.Entry<String, String> entry = scanHeaders(name);
      if (entry != null) {
        value = (String)entry.getValue();
      }
    }
    return value;
  }
  
  private Map.Entry<String, String> scanHeaders(String name)
  {
    String lc = name.toLowerCase();
    for (Map.Entry<String, String> entry : headers.entrySet()) {
      if (((String)entry.getKey()).toLowerCase().equals(lc)) {
        return entry;
      }
    }
    return null;
  }
  
  public String cookie(String name)
  {
    Validate.notNull(name, "Cookie name must not be null");
    return (String)cookies.get(name);
  }
  
  public T cookie(String name, String value)
  {
    Validate.notEmpty(name, "Cookie name must not be empty");
    Validate.notNull(value, "Cookie value must not be null");
    cookies.put(name, value);
    return this;
  }
  
  public boolean hasCookie(String name)
  {
    Validate.notEmpty("Cookie name must not be empty");
    return cookies.containsKey(name);
  }
  
  public T removeCookie(String name)
  {
    Validate.notEmpty("Cookie name must not be empty");
    cookies.remove(name);
    return this;
  }
  
  public Map<String, String> cookies()
  {
    return cookies;
  }
}

/* Location:
 * Qualified Name:     org.jsoup.helper.HttpConnection.Base
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.helper;

import org.jsoup.Connection.KeyVal;

public class HttpConnection$KeyVal
  implements Connection.KeyVal
{
  private String key;
  private String value;
  
  public static KeyVal create(String key, String value)
  {
    Validate.notEmpty(key, "Data key must not be empty");
    Validate.notNull(value, "Data value must not be null");
    return new KeyVal(key, value);
  }
  
  private HttpConnection$KeyVal(String key, String value)
  {
    this.key = key;
    this.value = value;
  }
  
  public KeyVal key(String key)
  {
    Validate.notEmpty(key, "Data key must not be empty");
    this.key = key;
    return this;
  }
  
  public String key()
  {
    return key;
  }
  
  public KeyVal value(String value)
  {
    Validate.notNull(value, "Data value must not be null");
    this.value = value;
    return this;
  }
  
  public String value()
  {
    return value;
  }
  
  public String toString()
  {
    return key + "=" + value;
  }
}

/* Location:
 * Qualified Name:     org.jsoup.helper.HttpConnection.KeyVal
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.helper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import org.jsoup.Connection.KeyVal;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Request;
import org.jsoup.parser.Parser;

public class HttpConnection$Request
  extends HttpConnection.Base<Connection.Request>
  implements Connection.Request
{
  private int timeoutMilliseconds;
  private boolean followRedirects;
  private Collection<Connection.KeyVal> data;
  private boolean ignoreHttpErrors = false;
  private boolean ignoreContentType = false;
  private Parser parser;
  
  private HttpConnection$Request()
  {
    super(null);
    timeoutMilliseconds = 3000;
    followRedirects = true;
    data = new ArrayList();
    method = Connection.Method.GET;
    headers.put("Accept-Encoding", "gzip");
    parser = Parser.htmlParser();
  }
  
  public int timeout()
  {
    return timeoutMilliseconds;
  }
  
  public Request timeout(int millis)
  {
    Validate.isTrue(millis >= 0, "Timeout milliseconds must be 0 (infinite) or greater");
    timeoutMilliseconds = millis;
    return this;
  }
  
  public boolean followRedirects()
  {
    return followRedirects;
  }
  
  public Connection.Request followRedirects(boolean followRedirects)
  {
    this.followRedirects = followRedirects;
    return this;
  }
  
  public boolean ignoreHttpErrors()
  {
    return ignoreHttpErrors;
  }
  
  public Connection.Request ignoreHttpErrors(boolean ignoreHttpErrors)
  {
    this.ignoreHttpErrors = ignoreHttpErrors;
    return this;
  }
  
  public boolean ignoreContentType()
  {
    return ignoreContentType;
  }
  
  public Connection.Request ignoreContentType(boolean ignoreContentType)
  {
    this.ignoreContentType = ignoreContentType;
    return this;
  }
  
  public Request data(Connection.KeyVal keyval)
  {
    Validate.notNull(keyval, "Key val must not be null");
    data.add(keyval);
    return this;
  }
  
  public Collection<Connection.KeyVal> data()
  {
    return data;
  }
  
  public Request parser(Parser parser)
  {
    this.parser = parser;
    return this;
  }
  
  public Parser parser()
  {
    return parser;
  }
}

/* Location:
 * Qualified Name:     org.jsoup.helper.HttpConnection.Request
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.helper;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream;
import org.jsoup.Connection.KeyVal;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Request;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Document.OutputSettings;
import org.jsoup.parser.TokenQueue;

public class HttpConnection$Response
  extends HttpConnection.Base<Connection.Response>
  implements Connection.Response
{
  private static final int MAX_REDIRECTS = 20;
  private int statusCode;
  private String statusMessage;
  private ByteBuffer byteData;
  private String charset;
  private String contentType;
  private boolean executed = false;
  private int numRedirects = 0;
  private Connection.Request req;
  
  HttpConnection$Response()
  {
    super(null);
  }
  
  private HttpConnection$Response(Response previousResponse)
    throws IOException
  {
    super(null);
    if (previousResponse != null)
    {
      numRedirects += 1;
      if (numRedirects >= 20) {
        throw new IOException(String.format("Too many redirects occurred trying to load URL %s", new Object[] { previousResponse.url() }));
      }
    }
  }
  
  static Response execute(Connection.Request req)
    throws IOException
  {
    return execute(req, null);
  }
  
  static Response execute(Connection.Request req, Response previousResponse)
    throws IOException
  {
    Validate.notNull(req, "Request must not be null");
    String protocol = req.url().getProtocol();
    Validate.isTrue((protocol.equals("http")) || (protocol.equals("https")), "Only http & https protocols supported");
    if ((req.method() == Connection.Method.GET) && (req.data().size() > 0)) {
      serialiseRequestUrl(req);
    }
    HttpURLConnection conn = createConnection(req);
    conn.connect();
    if (req.method() == Connection.Method.POST) {
      writePost(req.data(), conn.getOutputStream());
    }
    int status = conn.getResponseCode();
    boolean needsRedirect = false;
    if (status != 200) {
      if ((status == 302) || (status == 301) || (status == 303)) {
        needsRedirect = true;
      } else if (!req.ignoreHttpErrors()) {
        throw new IOException(status + " error loading URL " + req.url().toString());
      }
    }
    Response res = new Response(previousResponse);
    res.setupFromConnection(conn, previousResponse);
    if ((needsRedirect) && (req.followRedirects()))
    {
      req.method(Connection.Method.GET);
      req.data().clear();
      req.url(new URL(req.url(), res.header("Location")));
      for (Map.Entry<String, String> cookie : cookies.entrySet()) {
        req.cookie((String)cookie.getKey(), (String)cookie.getValue());
      }
      return execute(req, res);
    }
    req = req;
    
    InputStream bodyStream = null;
    InputStream dataStream = null;
    try
    {
      dataStream = conn.getErrorStream() != null ? conn.getErrorStream() : conn.getInputStream();
      bodyStream = (res.hasHeader("Content-Encoding")) && (res.header("Content-Encoding").equalsIgnoreCase("gzip")) ? new BufferedInputStream(new GZIPInputStream(dataStream)) : new BufferedInputStream(dataStream);
      
      byteData = DataUtil.readToByteBuffer(bodyStream);
      charset = DataUtil.getCharsetFromContentType(contentType);
    }
    finally
    {
      if (bodyStream != null) {
        bodyStream.close();
      }
      if (dataStream != null) {
        dataStream.close();
      }
    }
    executed = true;
    return res;
  }
  
  public int statusCode()
  {
    return statusCode;
  }
  
  public String statusMessage()
  {
    return statusMessage;
  }
  
  public String charset()
  {
    return charset;
  }
  
  public String contentType()
  {
    return contentType;
  }
  
  public Document parse()
    throws IOException
  {
    Validate.isTrue(executed, "Request must be executed (with .execute(), .get(), or .post() before parsing response");
    if ((!req.ignoreContentType()) && ((contentType == null) || ((!contentType.startsWith("text/")) && (!contentType.startsWith("application/xml")) && (!contentType.startsWith("application/xhtml+xml"))))) {
      throw new IOException(String.format("Unhandled content type \"%s\" on URL %s. Must be text/*, application/xml, or application/xhtml+xml", new Object[] { contentType, url.toString() }));
    }
    Document doc = DataUtil.parseByteData(byteData, charset, url.toExternalForm(), req.parser());
    byteData.rewind();
    charset = doc.outputSettings().charset().name();
    return doc;
  }
  
  public String body()
  {
    Validate.isTrue(executed, "Request must be executed (with .execute(), .get(), or .post() before getting response body");
    String body;
    String body;
    if (charset == null) {
      body = Charset.forName("UTF-8").decode(byteData).toString();
    } else {
      body = Charset.forName(charset).decode(byteData).toString();
    }
    byteData.rewind();
    return body;
  }
  
  public byte[] bodyAsBytes()
  {
    Validate.isTrue(executed, "Request must be executed (with .execute(), .get(), or .post() before getting response body");
    return byteData.array();
  }
  
  private static HttpURLConnection createConnection(Connection.Request req)
    throws IOException
  {
    HttpURLConnection conn = (HttpURLConnection)req.url().openConnection();
    conn.setRequestMethod(req.method().name());
    conn.setInstanceFollowRedirects(false);
    conn.setConnectTimeout(req.timeout());
    conn.setReadTimeout(req.timeout());
    if (req.method() == Connection.Method.POST) {
      conn.setDoOutput(true);
    }
    if (req.cookies().size() > 0) {
      conn.addRequestProperty("Cookie", getRequestCookieString(req));
    }
    for (Map.Entry<String, String> header : req.headers().entrySet()) {
      conn.addRequestProperty((String)header.getKey(), (String)header.getValue());
    }
    return conn;
  }
  
  private void setupFromConnection(HttpURLConnection conn, Connection.Response previousResponse)
    throws IOException
  {
    method = Connection.Method.valueOf(conn.getRequestMethod());
    url = conn.getURL();
    statusCode = conn.getResponseCode();
    statusMessage = conn.getResponseMessage();
    contentType = conn.getContentType();
    
    Map<String, List<String>> resHeaders = conn.getHeaderFields();
    processResponseHeaders(resHeaders);
    if (previousResponse != null) {
      for (Map.Entry<String, String> prevCookie : previousResponse.cookies().entrySet()) {
        if (!hasCookie((String)prevCookie.getKey())) {
          cookie((String)prevCookie.getKey(), (String)prevCookie.getValue());
        }
      }
    }
  }
  
  void processResponseHeaders(Map<String, List<String>> resHeaders)
  {
    for (Map.Entry<String, List<String>> entry : resHeaders.entrySet())
    {
      String name = (String)entry.getKey();
      if (name != null)
      {
        List<String> values = (List)entry.getValue();
        if (name.equalsIgnoreCase("Set-Cookie")) {
          for (String value : values) {
            if (value != null)
            {
              TokenQueue cd = new TokenQueue(value);
              String cookieName = cd.chompTo("=").trim();
              String cookieVal = cd.consumeTo(";").trim();
              if (cookieVal == null) {
                cookieVal = "";
              }
              if ((cookieName != null) && (cookieName.length() > 0)) {
                cookie(cookieName, cookieVal);
              }
            }
          }
        } else if (!values.isEmpty()) {
          header(name, (String)values.get(0));
        }
      }
    }
  }
  
  private static void writePost(Collection<Connection.KeyVal> data, OutputStream outputStream)
    throws IOException
  {
    OutputStreamWriter w = new OutputStreamWriter(outputStream, "UTF-8");
    boolean first = true;
    for (Connection.KeyVal keyVal : data)
    {
      if (!first) {
        w.append('&');
      } else {
        first = false;
      }
      w.write(URLEncoder.encode(keyVal.key(), "UTF-8"));
      w.write(61);
      w.write(URLEncoder.encode(keyVal.value(), "UTF-8"));
    }
    w.close();
  }
  
  private static String getRequestCookieString(Connection.Request req)
  {
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (Map.Entry<String, String> cookie : req.cookies().entrySet())
    {
      if (!first) {
        sb.append("; ");
      } else {
        first = false;
      }
      sb.append((String)cookie.getKey()).append('=').append((String)cookie.getValue());
    }
    return sb.toString();
  }
  
  private static void serialiseRequestUrl(Connection.Request req)
    throws IOException
  {
    URL in = req.url();
    StringBuilder url = new StringBuilder();
    boolean first = true;
    
    url.append(in.getProtocol()).append("://").append(in.getAuthority()).append(in.getPath()).append("?");
    if (in.getQuery() != null)
    {
      url.append(in.getQuery());
      first = false;
    }
    for (Connection.KeyVal keyVal : req.data())
    {
      if (!first) {
        url.append('&');
      } else {
        first = false;
      }
      url.append(URLEncoder.encode(keyVal.key(), "UTF-8")).append('=').append(URLEncoder.encode(keyVal.value(), "UTF-8"));
    }
    req.url(new URL(url.toString()));
    req.data().clear();
  }
}

/* Location:
 * Qualified Name:     org.jsoup.helper.HttpConnection.Response
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.jsoup.helper;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream;
import org.jsoup.Connection;
import org.jsoup.Connection.Base;
import org.jsoup.Connection.KeyVal;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Request;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Document.OutputSettings;
import org.jsoup.parser.Parser;
import org.jsoup.parser.TokenQueue;

public class HttpConnection
  implements Connection
{
  private Connection.Request req;
  private Connection.Response res;
  
  public static Connection connect(String url)
  {
    Connection con = new HttpConnection();
    con.url(url);
    return con;
  }
  
  public static Connection connect(URL url)
  {
    Connection con = new HttpConnection();
    con.url(url);
    return con;
  }
  
  private HttpConnection()
  {
    req = new Request(null);
    res = new Response();
  }
  
  public Connection url(URL url)
  {
    req.url(url);
    return this;
  }
  
  public Connection url(String url)
  {
    Validate.notEmpty(url, "Must supply a valid URL");
    try
    {
      req.url(new URL(url));
    }
    catch (MalformedURLException e)
    {
      throw new IllegalArgumentException("Malformed URL: " + url, e);
    }
    return this;
  }
  
  public Connection userAgent(String userAgent)
  {
    Validate.notNull(userAgent, "User agent must not be null");
    req.header("User-Agent", userAgent);
    return this;
  }
  
  public Connection timeout(int millis)
  {
    req.timeout(millis);
    return this;
  }
  
  public Connection followRedirects(boolean followRedirects)
  {
    req.followRedirects(followRedirects);
    return this;
  }
  
  public Connection referrer(String referrer)
  {
    Validate.notNull(referrer, "Referrer must not be nu
1 2 3 4 5 6 7 8 9 10

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