summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/plugins/PluginDescription.scala
blob: 9ecc098687bc601e0e3d82a06887acca2268a571 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
/* NSC -- new Scala compiler
 * Copyright 2007-2013 LAMP/EPFL
 * @author Lex Spoon
 */

package scala.tools.nsc
package plugins

import scala.xml.Node

/** A description of a compiler plugin, suitable for serialization
 *  to XML for inclusion in the plugin's .jar file.
 *
 * @author Lex Spoon
 * @version 1.0, 2007-5-21
 */
abstract class PluginDescription {

  /** A short name of the compiler, used to identify it in
   *  various contexts. The phase defined by the plugin
   *  should have the same name.
   */
  val name: String

  /** The name of the main class for the plugin */
  val classname: String

  /** An XML representation of this description.  It can be
   *  read back using <code>PluginDescription.fromXML</code>.
   *  It should be stored inside the jar archive file.
   */
  def toXML: Node = {
    <plugin>
      <name>{name}</name>
      <classname>{classname}</classname>
    </plugin>
  }
}

/** Utilities for the PluginDescription class.
 *
 *  @author Lex Spoon
 *  @version 1.0, 2007-5-21
 */
object PluginDescription {

  def fromXML(xml: Node): Option[PluginDescription] = {
    // check the top-level tag
    xml match {
      case <plugin>{_*}</plugin>  => ()
      case _ => return None
    }
    // extract one field
    def getField(field: String): Option[String] = {
      val text = (xml \\ field).text.trim
      if (text == "") None else Some(text)
    }

    // extract the required fields
    val name1 = getField("name") match {
      case None => return None
      case Some(str) => str
    }
    val classname1 = getField("classname") match {
      case None => return None
      case Some(str) => str
    }

    Some(new PluginDescription {
      val name = name1
      val classname = classname1
    })
  }

}