Skip to content

Commit 31b4fe1

Browse files
paulpadriaanm
authored andcommitted
Another big XML commit.
and using as a testbed for default arguments.
1 parent fc79c58 commit 31b4fe1

22 files changed

+193
-386
lines changed

src/library/scala/xml/Comment.scala

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,17 @@ package scala.xml
1616
* @author Burak Emir
1717
* @param text the text contained in this node, may not contain "--"
1818
*/
19-
case class Comment(commentText: String) extends SpecialNode {
20-
21-
final override def collectNamespacesAndDontTransform = false
22-
23-
if (commentText.indexOf("--") != -1)
24-
throw new IllegalArgumentException("text containts \"--\"")
25-
26-
/** structural equality */
27-
override def equals(x: Any): Boolean = x match {
28-
case Comment(x) => x.equals(commentText)
29-
case _ => false
30-
}
31-
32-
/** the constant "#REM" */
19+
case class Comment(commentText: String) extends SpecialNode
20+
{
3321
def label = "#REM"
34-
35-
/** hashcode for this Comment */
36-
override def hashCode() = commentText.hashCode()
37-
3822
override def text = ""
23+
final override def collectNamespacesAndDontTransform = false
24+
25+
if (commentText contains "--")
26+
throw new IllegalArgumentException("text contains \"--\"")
3927

4028
/** Appends &quot;<!-- text -->&quot; to this string buffer.
4129
*/
4230
override def buildString(sb: StringBuilder) =
43-
sb.append("<!--").append(commentText).append("-->")
31+
sb append ("<!--" + commentText + "-->")
4432
}

src/library/scala/xml/MetaData.scala

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
package scala.xml
1313

1414
import Utility.sbToString
15+
import annotation.tailrec
1516

1617
/**
1718
* Copyright 2008 Google Inc. All Rights Reserved.
@@ -25,11 +26,10 @@ object MetaData {
2526
* the attributes in new_tail, but does not guarantee to preserve the relative order of attribs.
2627
* Duplicates can be removed with normalize.
2728
*/
29+
@tailrec
2830
def concatenate(attribs: MetaData, new_tail: MetaData): MetaData =
29-
if (attribs eq Null)
30-
new_tail
31-
else
32-
concatenate(attribs.next, attribs.copy(new_tail)) // tail-recursive
31+
if (attribs eq Null) new_tail
32+
else concatenate(attribs.next, attribs.copy(new_tail))
3333

3434
/**
3535
* returns normalized MetaData, with all duplicates removed and namespace prefixes resolved to
@@ -81,29 +81,17 @@ object MetaData {
8181
* @author Burak Emir <[email protected]>
8282
*/
8383
@serializable
84-
abstract class MetaData extends Iterable[MetaData] {
85-
86-
/** updates this MetaData with the MetaData given as argument. All attributes that occur in updates
87-
* are part of the resulting MetaData. If an unprefixed attribute occurs in both this instance and
88-
* updates, only the one in updates is part of the result (avoiding duplicates). However, for
89-
* prefixed attributes no duplicate-detection is attempted, the method
90-
* append(updates: MetaData, scope:NamespaceBinding) should be used instead.
91-
*
92-
* @param updates MetaData with new attributes and updated attributes
93-
* @return a new MetaData instance that contains the combined attributes of this and updates
94-
*/
95-
def append(updates: MetaData): MetaData =
96-
MetaData.update(this, TopScope, updates)
97-
98-
/** updates this MetaData with the MetaData given as argument. All attributes that occur in updates
84+
abstract class MetaData extends Iterable[MetaData]
85+
{
86+
/** Updates this MetaData with the MetaData given as argument. All attributes that occur in updates
9987
* are part of the resulting MetaData. If an attribute occurs in both this instance and
10088
* updates, only the one in updates is part of the result (avoiding duplicates). For prefixed
101-
* attributes, namespaces are resolved using the given scope.
89+
* attributes, namespaces are resolved using the given scope, which defaults to TopScope.
10290
*
10391
* @param updates MetaData with new and updated attributes
10492
* @return a new MetaData instance that contains old, new and updated attributes
10593
*/
106-
def append(updates: MetaData, scope: NamespaceBinding): MetaData =
94+
def append(updates: MetaData, scope: NamespaceBinding = TopScope): MetaData =
10795
MetaData.update(this, scope, updates)
10896

10997
/**
@@ -160,16 +148,13 @@ abstract class MetaData extends Iterable[MetaData] {
160148
def isPrefixed: Boolean
161149

162150
/** deep equals method */
163-
override def equals(that: Any) =
164-
that match {
165-
case m: MetaData =>
166-
var res = (this.length == m.length) && (this.hashCode() == m.hashCode())
167-
val it = this.iterator
168-
while (res && it.hasNext) { res = it.next.containedIn1(m) }
169-
res
170-
case _ =>
171-
false
172-
}
151+
override def equals(that: Any) = that match {
152+
case m: MetaData =>
153+
(this.length == m.length) &&
154+
(this.hashCode == m.hashCode) &&
155+
(this forall (_ containedIn1 m))
156+
case _ => false
157+
}
173158

174159
/** returns an iterator on attributes */
175160
def iterator: Iterator[MetaData] = new Iterator[MetaData] {
@@ -238,8 +223,8 @@ abstract class MetaData extends Iterable[MetaData] {
238223

239224
def toString1(): String = sbToString(toString1)
240225

241-
//appends string representations of single attribute to StringBuilder
242-
def toString1(sb:StringBuilder): Unit
226+
// appends string representations of single attribute to StringBuilder
227+
def toString1(sb: StringBuilder): Unit
243228

244229
override def toString(): String = sbToString(buildString)
245230

@@ -259,7 +244,7 @@ abstract class MetaData extends Iterable[MetaData] {
259244
* @param key ...
260245
* @return ...
261246
*/
262-
def remove(key:String): MetaData
247+
def remove(key: String): MetaData
263248

264249
/**
265250
* @param namespace ...

src/library/scala/xml/Node.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ abstract class Node extends NodeSeq {
174174
* @param stripComment if true, strips comment nodes from result
175175
* @return ...
176176
*/
177-
def buildString(stripComment: Boolean): String =
178-
Utility.toXML(this, stripComment)
177+
def buildString(stripComments: Boolean): String =
178+
Utility.toXML(this, stripComments = stripComments).toString
179179

180180
/**
181181
* Same as <code>toString(false)</code>.

src/library/scala/xml/NodeBuffer.scala

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,12 @@ class NodeBuffer extends scala.collection.mutable.ArrayBuffer[Node] {
3939
*/
4040
def &+(o: Any): NodeBuffer = {
4141
o match {
42-
case null | _:Unit | Text("")=>
43-
// ignore
44-
45-
case it:Iterator[_] =>
46-
while (it.hasNext)
47-
this &+ it.next
48-
49-
case n:Node =>
50-
super.+=(n)
51-
52-
case ns:Iterable[_] =>
53-
this &+ ns.iterator
54-
55-
case d =>
56-
super.+=(new Atom(d))
42+
case null | _: Unit | Text("") => // ignore
43+
case it: Iterator[_] => it foreach (this &+ _)
44+
case n: Node => super.+=(n)
45+
case ns: Iterable[_] => this &+ ns.iterator
46+
case d => super.+=(new Atom(d))
5747
}
5848
this
5949
}
60-
6150
}

src/library/scala/xml/NodeSeq.scala

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import collection.mutable.ListBuffer
2121
* @version 1.0
2222
*/
2323
object NodeSeq {
24-
final val Empty = new NodeSeq { def theSeq = Nil }
24+
final val Empty = fromSeq(Nil)
2525
def fromSeq(s: Seq[Node]): NodeSeq = new NodeSeq {
2626
def theSeq = s
2727
}
@@ -46,16 +46,16 @@ abstract class NodeSeq extends immutable.Sequence[Node] with SequenceTemplate[No
4646
def theSeq: Seq[Node]
4747
def length = theSeq.length
4848
override def iterator = theSeq.iterator
49-
def apply(i: Int): Node = theSeq.apply(i)
5049

50+
def apply(i: Int): Node = theSeq(i)
5151
def apply(f: Node => Boolean): NodeSeq = filter(f)
5252

5353
/** structural equality */
5454
override def equals(x: Any): Boolean = x match {
5555
case z:Node => (length == 1) && z == apply(0)
5656
case z:Seq[_] => sameElements(z)
5757
case z:String => text == z
58-
case _ => false;
58+
case _ => false
5959
}
6060

6161
/** Projection function. Similar to XPath, use <code>this \ "foo"</code>
@@ -128,29 +128,6 @@ abstract class NodeSeq extends immutable.Sequence[Node] with SequenceTemplate[No
128128
}
129129
}
130130

131-
override def toString(): String = theSeq.iterator.foldLeft ("") {
132-
(s: String, x: Node) => s + x.toString()
133-
}
134-
/*
135-
def map(f: Node => NodeSeq): NodeSeq = flatMap(f)
136-
137-
def flatMap(f: Node => NodeSeq): NodeSeq = {
138-
val y = toList flatMap { x => f(x).toList }
139-
y
140-
}
141-
142-
override def filter(f: Node => Boolean): NodeSeq = {
143-
val x = toList filter f
144-
x
145-
}
146-
*/
147-
148-
def text: String = {
149-
val sb = new StringBuilder()
150-
val it = this.iterator
151-
while (it.hasNext) {
152-
sb.append(it.next.text)
153-
}
154-
sb.toString()
155-
}
131+
override def toString(): String = theSeq.mkString
132+
def text: String = this map (_.text) mkString
156133
}

src/library/scala/xml/Null.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ package scala.xml
1414
case object Null extends MetaData {
1515

1616
/** appends given MetaData items to this MetaData list */
17-
override def append(m: MetaData): MetaData = m
17+
override def append(m: MetaData, scope: NamespaceBinding = TopScope): MetaData = m
1818

1919
override def containedIn1(m: MetaData): Boolean = false
2020

src/library/scala/xml/PCData.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ case class PCData(_data: String) extends Atom[String](_data) {
2323
* @param sb ...
2424
* @return ...
2525
*/
26-
override def buildString(sb: StringBuilder) = {
27-
sb.append("<![CDATA[")
28-
sb.append(data)
29-
sb.append("]]>")
30-
}
26+
override def buildString(sb: StringBuilder) =
27+
sb append "<![CDATA[%s]]>".format(data)
3128
}

src/library/scala/xml/Parsing.scala

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,26 @@ object Parsing {
2929
}
3030

3131
/** <pre>(#x20 | #x9 | #xD | #xA)+</pre> */
32-
final def isSpace(cs: Seq[Char]): Boolean = {
33-
val it = cs.iterator
34-
it.hasNext && it.forall { isSpace }
35-
}
32+
final def isSpace(cs: Seq[Char]): Boolean = cs forall isSpace
3633

3734
/** <pre>NameChar ::= Letter | Digit | '.' | '-' | '_' | ':'
3835
* | CombiningChar | Extender</pre>
3936
*
4037
* see [4] and Appendix B of XML 1.0 specification
4138
*/
42-
def isNameChar(ch: Char) = isNameStart(ch) || (ch match {
43-
case '.' | '-' | ':' => true
44-
case _ => java.lang.Character.getType(ch).asInstanceOf[Byte] match {
45-
case java.lang.Character.COMBINING_SPACING_MARK => true // Mc
46-
case java.lang.Character.ENCLOSING_MARK => true // Me
47-
case java.lang.Character.NON_SPACING_MARK => true // Mn
48-
case java.lang.Character.MODIFIER_LETTER => true // Lm
49-
case java.lang.Character.DECIMAL_DIGIT_NUMBER => true // Nd
50-
case _ => false
51-
}
52-
});
39+
private val nameCharTypeList = {
40+
import java.lang.Character._
41+
List(
42+
COMBINING_SPACING_MARK, // Mc
43+
ENCLOSING_MARK, // Me
44+
NON_SPACING_MARK, // Mn
45+
MODIFIER_LETTER, // Lm
46+
DECIMAL_DIGIT_NUMBER // Nd
47+
)
48+
}
49+
def isNameChar(ch: Char) =
50+
isNameStart(ch) || List('.', '-', ':').contains(ch) ||
51+
nameCharTypeList.contains(Character.getType(ch).asInstanceOf[Byte])
5352

5453
/** <pre>NameStart ::= ( Letter | '_' )</pre>
5554
* where Letter means in one of the Unicode general
@@ -94,12 +93,5 @@ object Parsing {
9493
def checkSysID(s: String): Boolean =
9594
s.indexOf('"') == -1 || s.indexOf('\'') == -1
9695

97-
def checkPubID(s: String): Boolean =
98-
if (s.length() > 0) {
99-
val z:Seq[Char] = s
100-
val y = z.iterator
101-
while (y.hasNext && isPubIDChar(y.next)) {}
102-
!y.hasNext
103-
} else true
104-
96+
def checkPubID(s: String): Boolean = s forall isPubIDChar
10597
}

src/library/scala/xml/ProcInstr.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ package scala.xml
1717
* @param target target name of this PI
1818
* @param text text contained in this node, may not contain "?>"
1919
*/
20-
case class ProcInstr(target:String, proctext:String) extends SpecialNode {
20+
case class ProcInstr(target:String, proctext:String) extends SpecialNode
21+
{
2122

2223
if (!Utility.isName(target))
2324
throw new IllegalArgumentException(target+" must be an XML Name")
24-
else if (text.indexOf("?>") != -1)
25+
if (text.indexOf("?>") != -1)
2526
throw new IllegalArgumentException(proctext+" may not contain \"?>\"")
2627

2728
final override def collectNamespacesAndDontTransform = false

src/library/scala/xml/QNode.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,5 @@ package scala.xml
1818
* @version 1.0
1919
*/
2020
object QNode {
21-
22-
def unapplySeq(n:Node) = Some (Tuple4(n.scope.getURI(n.prefix), n.label, n.attributes, n.child))
23-
21+
def unapplySeq(n: Node) = Some((n.scope.getURI(n.prefix), n.label, n.attributes, n.child))
2422
}

src/library/scala/xml/SpecialNode.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ package scala.xml
2121
*
2222
* @author Burak Emir
2323
*/
24-
abstract class SpecialNode extends Node with pull.XMLEvent {
25-
24+
abstract class SpecialNode extends Node with pull.XMLEvent
25+
{
2626
/** always empty */
2727
final override def attributes = Null
2828

@@ -34,5 +34,4 @@ abstract class SpecialNode extends Node with pull.XMLEvent {
3434

3535
/** append string representation to the given stringbuffer */
3636
def buildString(sb: StringBuilder): StringBuilder
37-
3837
}

src/library/scala/xml/Text.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ package scala.xml
1919
*
2020
* @param text the text contained in this node, may not be null.
2121
*/
22-
case class Text(_data: String) extends Atom[String](_data) {
23-
22+
case class Text(_data: String) extends Atom[String](_data)
23+
{
2424
if (null == data)
25-
throw new java.lang.NullPointerException("tried to construct Text with null")
25+
throw new IllegalArgumentException("tried to construct Text with null")
2626

2727
final override def equals(x: Any) = x match {
2828
case s:String => s == data

src/library/scala/xml/Unparsed.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ package scala.xml
1515
* are off regarding wellformedness etc.
1616
*
1717
* @author Burak Emir
18-
* @param _data content in this node, may not be null.
18+
* @param data content in this node, may not be null.
1919
*/
20-
case class Unparsed(_data: String) extends Atom[String](_data) {
21-
20+
class Unparsed(data: String) extends Atom[String](data)
21+
{
2222
if (null == data)
23-
throw new java.lang.NullPointerException("tried to construct Unparsed with null")
23+
throw new IllegalArgumentException("tried to construct Unparsed with null")
2424

2525
final override def equals(x: Any) = x match {
2626
case s:String => s == data
@@ -32,5 +32,9 @@ case class Unparsed(_data: String) extends Atom[String](_data) {
3232

3333
/** returns text, with some characters escaped according to XML spec */
3434
override def buildString(sb: StringBuilder) = sb append data
35-
3635
}
36+
37+
object Unparsed {
38+
def apply(data: String) = new Unparsed(data)
39+
def unapply(x: Unparsed) = Some(x.data)
40+
}

0 commit comments

Comments
 (0)