summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-09-23 14:13:55 +0000
committerPaul Phillips <paulp@improving.org>2009-09-23 14:13:55 +0000
commit0d86d977a3eb153f46c59f348638426b821e006c (patch)
treee3f063a2fb6e5d0b6e122e1e8178175e433ccc75
parentdc65ebea9e2e5953a23a11c4df87d0fd7c81f19b (diff)
downloadscala-0d86d977a3eb153f46c59f348638426b821e006c.tar.gz
scala-0d86d977a3eb153f46c59f348638426b821e006c.tar.bz2
scala-0d86d977a3eb153f46c59f348638426b821e006c.zip
Some more XML reworking.
-rw-r--r--src/library/scala/collection/Iterator.scala9
-rw-r--r--src/library/scala/xml/dtd/ElementValidator.scala89
2 files changed, 39 insertions, 59 deletions
diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala
index bbb6d3d690..d3bab1aff2 100644
--- a/src/library/scala/collection/Iterator.scala
+++ b/src/library/scala/collection/Iterator.scala
@@ -366,6 +366,15 @@ trait Iterator[+A] { self =>
}
}
+ /** Returns an iterator over all the elements of this iterator which
+ * do not satisfy the predicate <code>p</code>.
+ *
+ * @param p the predicate used to filter.
+ * @return the elements of this iterator not satisfying <code>p</code>.
+ */
+
+ def filterNot(p: A => Boolean): Iterator[A] = filter(!p(_))
+
/** Returns a new iterator based on the partial function <code>pf</code>,
* containing <code>pf(x)</code> for all the elements which are defined on pf.
* The order of the elements is preserved.
diff --git a/src/library/scala/xml/dtd/ElementValidator.scala b/src/library/scala/xml/dtd/ElementValidator.scala
index cb64c20f8d..8c375ca1c8 100644
--- a/src/library/scala/xml/dtd/ElementValidator.scala
+++ b/src/library/scala/xml/dtd/ElementValidator.scala
@@ -12,6 +12,7 @@
package scala.xml
package dtd
+import PartialFunction._
import ContentModel.ElemName
import scala.util.automata._
@@ -42,24 +43,17 @@ class ElementValidator() extends Function1[Node,Boolean] {
/** set meta data, enabling attribute validation */
def setMetaData(adecls: List[AttrDecl]) { this.adecls = adecls }
- def getIterator(nodes: Seq[Node], skipPCDATA: Boolean): Iterator[ElemName] =
- nodes.toList
- .filter { x => x match {
- case y:SpecialNode => y match {
+ def getIterator(nodes: Seq[Node], skipPCDATA: Boolean): Iterator[ElemName] = {
+ def isAllWhitespace(a: Atom[_]) = cond(a.data) { case s: String if s.trim.isEmpty => true }
- case a:Atom[_] if (a.data.isInstanceOf[String] &&
- a.data.asInstanceOf[String].trim.length == 0 ) =>
- false; // always skip all-whitespace nodes
-
- case _ =>
- !skipPCDATA
-
- }
- case _ =>
- x.namespace eq null
- }}
- .map { x => ElemName(x.label) }
- .iterator;
+ nodes.filter {
+ case y: SpecialNode => y match {
+ case a: Atom[_] if isAllWhitespace(a) => false // always skip all-whitespace nodes
+ case _ => !skipPCDATA
+ }
+ case x => x.namespace eq null
+ } . map (x => ElemName(x.label)) iterator
+ }
/** check attributes, return true if md corresponds to attribute declarations in adecls.
*/
@@ -116,60 +110,37 @@ class ElementValidator() extends Function1[Node,Boolean] {
* @pre contentModel != null
*/
def check(nodes: Seq[Node]): Boolean = contentModel match {
- case ANY =>
- true
-
- case EMPTY =>
- !getIterator(nodes, false).hasNext
-
- case PCDATA =>
- !getIterator(nodes, true).hasNext
-
- case MIXED(ContentModel.Alt(branches @ _*)) => //@todo
+ case ANY => true
+ case EMPTY => !getIterator(nodes, false).hasNext
+ case PCDATA => !getIterator(nodes, true).hasNext
+ case MIXED(ContentModel.Alt(branches @ _*)) => // @todo
val j = exc.length
- def find(Key: String): Boolean = {
- var res = false
- val jt = branches.iterator
- while (jt.hasNext && !res)
- jt.next match { // !!! check for match translation problem
- case ContentModel.Letter(ElemName(Key)) => res = true;
- case _ =>
- }
- res
- }
+ def find(Key: String): Boolean =
+ branches exists { case ContentModel.Letter(ElemName(Key)) => true ; case _ => false }
- var it = getIterator(nodes, true); while(it.hasNext) {
- var label = it.next.name;
- if (!find(label)) {
- exc = MakeValidationException.fromUndefinedElement(label) :: exc;
- }
+ getIterator(nodes, true) map (_.name) filterNot find foreach {
+ exc ::= MakeValidationException fromUndefinedElement _
}
+ (exc.length == j) // - true if no new exception
- (exc.length == j) //- true if no new exception
-
- case _:ELEMENTS =>
+ case _: ELEMENTS =>
var q = 0
- val it = getIterator(nodes, false)
- while (it.hasNext) {
- val e = it.next
- dfa.delta(q).get(e) match {
- case Some(p) => q = p
- case _ => throw ValidationException("element "+e+" not allowed here")
+ getIterator(nodes, false) foreach { e =>
+ (dfa delta q get e) match {
+ case Some(p) => q = p
+ case _ => throw ValidationException("element %s not allowed here" format e)
}
}
- dfa.isFinal(q) //- true if arrived in final state
+
+ dfa isFinal q // - true if arrived in final state
}
/** applies various validations - accumulates error messages in exc
* @todo: fail on first error, ignore other errors (rearranging conditions)
*/
- def apply(n: Node): Boolean = {
+ def apply(n: Node): Boolean =
//- ? check children
- var res = (null == contentModel) || check( n.child );
-
+ ((contentModel == null) || check(n.child)) &&
//- ? check attributes
- res = ((null == adecls) || check( n.attributes )) && res;
-
- res
- }
+ ((adecls == null) || check(n.attributes))
}