summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/io/AladdinReader.scala
blob: e8dd056312dc552ef27f43003f636b43b4b5f258 (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package scala.tools.nsc.io

import java.io.{File, FileInputStream, InputStream, IOException}
import java.net.{ContentHandler, ContentHandlerFactory, URL, URLConnection}
import java.nio.charset.CharsetDecoder

class AladdinReader(decoder: CharsetDecoder) extends SourceReader(decoder) {

  final val ALADDIN_URL = "http://scala-webapps.epfl.ch/bugtracking/bugs/json/"

  object AladdinHandler extends ContentHandler {
    def getContent(con: URLConnection) = {
      var state = 0
      val is = con.getInputStream
      val src = scala.io.Source.fromInputStream(is)
      while(state < 6 && src.hasNext) {
        state = (state,src.next) match {
          case (0,'\"') => 1
          case (1, 'c') => 2
          case (2, 'o') => 3
          case (3, 'd') => 4
          case (4, 'e') => 5
          case (5,'\"') => 6
          case _        => 0
        }
      }
      while(src.next != ':') {}
      while(src.next != '\"') {}
      val sb = new StringBuilder()
      var c = ' '
      while(c != '\"' && src.hasNext) { /*Console.print("["+c+"]"); */sb.append(c); c = src.next;}
      is.close
      //Console.println("!!"+sb.toString+"!!") // DEBUG if you want to see what you are downloading
      sb.toString.replace("\\u0022","\"").replace("\\uu005c","\\").toCharArray
    }
  }

  java.net.URLConnection.setContentHandlerFactory(new ContentHandlerFactory { def createContentHandler(s:String)   = AladdinHandler })

  private def wellformed(str: String): Boolean = try {
    str.charAt(0) == '#' && { Integer.parseInt(str.substring(1)); true }
  } catch {
    case _ => false
  }

  /** Reads the specified file. */
  @throws(classOf[java.net.MalformedURLException])
  override def read(file: File): Array[Char] = {

    val content = new String(super.read(file)).trim()
    if(!wellformed(content))
      throw FatalError("content should just be a bug identifier, like \"#1023\"")

    var bugURL  = ALADDIN_URL + new String(content).trim().substring(1)
    try {
      new java.net.URL(bugURL).getContent().asInstanceOf[Array[Char]]
    } catch {
      case e => throw FatalError("while getting content of URL, "+e.getClass()+" saying "+e.getMessage)
    }
  }

}