summaryrefslogtreecommitdiff
path: root/src/library/jvm/scala/xml/include/parsing/TokenTests.scala
blob: d56b9bb7d7943740635f63c98a72530fcda054b4 (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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2006, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.xml.parsing;


/**
 * Helper functions for parsing XML fragments
 */
trait TokenTests {

  /** (#x20 | #x9 | #xD | #xA) */
  final def isSpace( ch:Char ): Boolean = ch match {
    case '\u0009' | '\u000A' | '\u000D' | '\u0020' => true
    case _                                         => false;
  }

  /** (#x20 | #x9 | #xD | #xA)+ */
  final def isSpace(cs: Seq[Char]): Boolean = {
    val it = cs.elements;
    it.hasNext && it.forall { isSpace };
  }

  /** NameChar ::= Letter | Digit | '.' | '-' | '_' | ':'
   *             | CombiningChar | Extender
   *
   * see [4] and Appendix B of XML 1.0 specification
  */
  def isNameChar(ch: Char) = isNameStart(ch) || (ch match {
    case '.' | '-' | ':' => true;
    case _ => java.lang.Character.getType( ch ).asInstanceOf[Byte] match {
      case java.lang.Character.COMBINING_SPACING_MARK => true; // Mc
      case java.lang.Character.ENCLOSING_MARK => true;         // Me
      case java.lang.Character.NON_SPACING_MARK => true;       // Mn
      case java.lang.Character.MODIFIER_LETTER => true;        // Lm
      case java.lang.Character.DECIMAL_DIGIT_NUMBER => true;   // Nd
      case _ => false;
    }
  });

  /** NameStart ::= ( Letter | '_' )
   *  where Letter means in one of the Unicode general
   *  categories { Ll, Lu, Lo, Lt, Nl }
   *
   *  We do not allow a name to start with ':'.
   *  see [3] and Appendix B of XML 1.0 specification
   */
  def isNameStart(ch: Char) =
    java.lang.Character.getType(ch).asInstanceOf[Byte] match {
      case java.lang.Character.LOWERCASE_LETTER => true;
      case java.lang.Character.UPPERCASE_LETTER => true;
      case java.lang.Character.OTHER_LETTER     => true;
      case java.lang.Character.TITLECASE_LETTER => true;
      case java.lang.Character.LETTER_NUMBER    => true;
      case _ => ch match {
        case '_' => true
        case _ => false;
      }
    }

  /** Name ::= ( Letter | '_' ) (NameChar)*
   *
   *  see  [5] of XML 1.0 specification
   */
  def isName(s: String): Boolean = {
    if( s.length() > 0 ) {
      val y           = s.elements;
      if (isNameStart(y.next)) {
        while (y.hasNext && isNameChar(y.next)) {};
        !y.hasNext
      } else false;
    } else false;
  }

  def isPubIDChar(ch: Char): Boolean = {
    //Console.println("char: '" + ch + "'");
    ch match {
      case '\u0020' | '\u000D' | '\u000A' => true;
      case _ if
        (('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'z') ||
         ('A' <= ch && ch <= 'Z')) => true;
      case '-' | '\''| '(' | ')' | '+' | ',' | '.' |
           '/' | ':' | '=' | '?' | ';' | '!' | '*' |
           '#' | '@' | '$' | '_' | '%' => true
      case _ =>
        //Console.println("false: '" + ch + "'");
        false;
    }
  }

  /**
   * Returns true if the encoding name is a valid IANA encoding.
   * This method does not verify that there is a decoder available
   * for this encoding, only that the characters are valid for an
   * IANA encoding name.
   *
   * @param ianaEncoding The IANA encoding name.
   */
  def isValidIANAEncoding(ianaEncoding: Seq[Char]): Boolean = {
    val it = ianaEncoding.elements;
    if (!it.hasNext)
      return false;

    var c = it.next;
    if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
      while (it.hasNext) {
        c = it.next;
        if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') &&
            (c < '0' || c > '9') && c != '.' && c != '_' &&
            c != '-') {
              return false;
            }
      }
      return true;
    } else
      return false;
  } // isValidIANAEncoding(String): Boolean

  def checkSysID( s:String ): Boolean = {
    s.indexOf('"'.asInstanceOf[Int]) == -1 || s.indexOf('\''.asInstanceOf[Int]) == -1
  }

  def checkPubID(s: String): Boolean = {
    //Console.println("checkPubID of \""+s+"\"");
    if (s.length() > 0) {
      val y = s.elements;
      var c = ' ';
      while (y.hasNext && isPubIDChar(c)) {
        //Console.println(c);
        c = y.next
      };
      !y.hasNext
    }
    else
      true
  }

}