alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (PluginDescription.scala)

This example Scala source code file (PluginDescription.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Scala source code examples by using tags.

All credit for the original source code belongs to scala-lang.org; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Scala tags/keywords

bad, compiler, nsc, plugin, plugindescription, reflection, runtimeexception, string, utilities

The PluginDescription.scala Scala example source code

/* NSC -- new Scala compiler
 * Copyright 2007-2013 LAMP/EPFL
 * @author Lex Spoon
 */

package scala.tools.nsc
package plugins

import scala.reflect.internal.util.StringContextStripMarginOps

/** 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
 * @author Adriaan Moors
 * @version 2.0, 2013
 * @param name A short name of the plugin, used to identify it in
 *   various contexts. The phase defined by the plugin
 *   should have the same name.
 * @param classname The name of the main Plugin class.
 */
case class PluginDescription(name: String, classname: String) {
  /** An XML representation of this description.
   *  It should be stored inside the jar archive file.
   */
  def toXML: String =
    sm"""<plugin>
         | <name>${name}</name>
         | <classname>${classname}</classname>
         |</plugin>"""
}

/** Utilities for the PluginDescription class.
 *
 * @author Lex Spoon
 * @version 1.0, 2007-5-21
 * @author Adriaan Moors
 * @version 2.0, 2013
 */
object PluginDescription {
  private def text(ns: org.w3c.dom.NodeList): String =
    if (ns.getLength == 1) ns.item(0).getTextContent.trim
    else throw new RuntimeException("Bad plugin descriptor.")

  def fromXML(xml: java.io.InputStream): PluginDescription = {
    import javax.xml.parsers.DocumentBuilderFactory
    val root = DocumentBuilderFactory.newInstance.newDocumentBuilder.parse(xml).getDocumentElement
    root.normalize()
    if (root.getNodeName != "plugin")
      throw new RuntimeException("Plugin descriptor root element must be <plugin>.")

    PluginDescription(text(root.getElementsByTagName("name")), text(root.getElementsByTagName("classname")))
  }
}

Other Scala source code examples

Here is a short list of links related to this Scala PluginDescription.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.