Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 127 lines (102 sloc) 4.519 kb
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */



package scala.xml
package parsing

import collection.mutable
import mutable.HashMap
import scala.io.Source
import scala.util.logging.Logged
import scala.xml.dtd._

/** class that handles markup - provides callback methods to MarkupParser.
* the default is nonvalidating behaviour
*
* @author Burak Emir
* @version 1.0
*
* @todo can we ignore more entity declarations (i.e. those with extIDs)?
* @todo expanding entity references
*/
abstract class MarkupHandler extends Logged
{
  /** returns true is this markup handler is validating */
  val isValidating: Boolean = false

  var decls: List[Decl] = Nil
  var ent: mutable.Map[String, EntityDecl] = new HashMap[String, EntityDecl]()

  def lookupElemDecl(Label: String): ElemDecl = {
    for (z @ ElemDecl(Label, _) <- decls)
      return z

    null
  }

  def replacementText(entityName: String): Source =
    Source fromString ((ent get entityName) match {
      case Some(ParsedEntityDecl(_, IntDef(value))) => value
      case Some(ParameterEntityDecl(_, IntDef(value))) => " %s " format value
      case Some(_) => "<!-- %s; -->" format entityName
      case None => "<!-- unknown entity %s; -->" format entityName
    })

  def endDTD(n: String): Unit = ()

  /** callback method invoked by MarkupParser after start-tag of element.
*
* @param pos the position in the sourcefile
* @param pre the prefix
* @param label the local name
* @param attrs the attributes (metadata)
*/
  def elemStart(pos: Int, pre: String, label: String, attrs: MetaData, scope: NamespaceBinding): Unit = ()

  /** callback method invoked by MarkupParser after end-tag of element.
*
* @param pos the position in the source file
* @param pre the prefix
* @param label the local name
* @param attrs the attributes (metadata)
*/
  def elemEnd(pos: Int, pre: String, label: String): Unit = ()

  /** callback method invoked by MarkupParser after parsing an element,
* between the elemStart and elemEnd callbacks
*
* @param pos the position in the source file
* @param pre the prefix
* @param label the local name
* @param attrs the attributes (metadata)
* @param args the children of this element
* @return ...
*/
  def elem(pos: Int, pre: String, label: String, attrs: MetaData, scope: NamespaceBinding, args: NodeSeq): NodeSeq

  /** callback method invoked by MarkupParser after parsing PI.
*/
  def procInstr(pos: Int, target: String, txt: String): NodeSeq

  /** callback method invoked by MarkupParser after parsing comment.
*/
  def comment(pos: Int, comment: String): NodeSeq

  /** callback method invoked by MarkupParser after parsing entity ref.
* @todo expanding entity references
*/
  def entityRef(pos: Int, n: String): NodeSeq

  /** callback method invoked by MarkupParser after parsing text.
*/
  def text(pos: Int, txt: String): NodeSeq

  // DTD handler methods

  def elemDecl(n: String, cmstr: String): Unit = ()

  def attListDecl(name: String, attList: List[AttrDecl]): Unit = ()

  private def someEntityDecl(name: String, edef: EntityDef, f: (String, EntityDef) => EntityDecl): Unit =
    edef match {
      case _: ExtDef if !isValidating => // ignore (cf REC-xml 4.4.1)
      case _ =>
        val y = f(name, edef)
        decls ::= y
        ent.update(name, y)
    }

  def parameterEntityDecl(name: String, edef: EntityDef): Unit =
    someEntityDecl(name, edef, ParameterEntityDecl.apply _)

  def parsedEntityDecl(name: String, edef: EntityDef): Unit =
    someEntityDecl(name, edef, ParsedEntityDecl.apply _)

  def peReference(name: String) { decls ::= PEReference(name) }
  def unparsedEntityDecl(name: String, extID: ExternalID, notat: String): Unit = ()
  def notationDecl(notat: String, extID: ExternalID): Unit = ()
  def reportSyntaxError(pos: Int, str: String): Unit
}
Something went wrong with that request. Please try again.