summaryrefslogtreecommitdiff
path: root/src/library/scala/xml/factory/LoggedNodeFactory.scala
blob: cac61acc3989951d67a6c161408f62540d456070 (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
76
77
78
79
80
81
82
83
84
85
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2002-2013, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

package scala.xml
package factory

/** This class logs what the nodefactory is actually doing.
 *  If you want to see what happens during loading, use it like this:
{{{
object testLogged extends App {
  val x = new scala.xml.parsing.NoBindingFactoryAdapter
        with scala.xml.factory.LoggedNodeFactory[scala.xml.Elem]
        with scala.util.logging.ConsoleLogger

  Console.println("Start")
  val doc = x.load(new java.net.URL("http://example.com/file.xml"))
  Console.println("End")
  Console.println(doc)
}
}}}
 *
 *  @author  Burak Emir
 *  @version 1.0
 */
trait LoggedNodeFactory[A <: Node] extends NodeFactory[A] with scala.util.logging.Logged {
  // configuration values
  val logNode      = true
  val logText      = false
  val logComment   = false
  val logProcInstr = false

  final val NONE  = 0
  final val CACHE = 1
  final val FULL  = 2
  /** 0 = no logging, 1 = cache hits, 2 = detail */
  val logCompressLevel  = 1

  // methods of NodeFactory

  /** logged version of makeNode method */
  override def makeNode(pre: String, label: String, attrSeq: MetaData,
                        scope: NamespaceBinding, children: Seq[Node]): A = {
    if (logNode)
      log("[makeNode for "+label+"]");

    val hash = Utility.hashCode(pre, label, attrSeq.##, scope.##, children)

    /*
    if(logCompressLevel >= FULL) {
      log("[hashcode total:"+hash);
      log(" elem name "+uname+" hash "+ ? ));
      log(" attrs     "+attrSeq+" hash "+attrSeq.hashCode());
      log(" children :"+children+" hash "+children.hashCode());
    }
    */
    if (!cache.get( hash ).isEmpty && (logCompressLevel >= CACHE))
      log("[cache hit !]");

    super.makeNode(pre, label, attrSeq, scope, children)
  }

  override def makeText(s: String) = {
    if (logText)
      log("[makeText:\""+s+"\"]");
    super.makeText(s)
  }

  override def makeComment(s: String): Seq[Comment] = {
    if (logComment)
      log("[makeComment:\""+s+"\"]");
    super.makeComment(s)
  }

  override def makeProcInstr(t: String, s: String): Seq[ProcInstr] = {
    if (logProcInstr)
      log("[makeProcInstr:\""+t+" "+ s+"\"]");
    super.makeProcInstr(t, s)
  }

}