alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Elem.scala)

This example Scala source code file (Elem.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

elem, elem, illegalargumentexception, metadata, metadata, namespacebinding, node, node, none, seq, serializable, specialnode, string, string

The Scala Elem.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2002-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */


package scala.xml

/** This singleton object contains the apply and unapplySeq methods for convenient construction and
 *  deconstruction. It is possible to deconstruct any Node instance (that is not a SpecialNode or
 *  a Group) using the syntax
 * <code> case Elem(prefix, label, attribs, scope, child @ _*) => ... 
 *
 * Copyright 2008 Google Inc. All Rights Reserved.
 * @author Burak Emir <bqe@google.com>
 */
object Elem {
  def apply(prefix: String,label: String, attributes: MetaData, scope: NamespaceBinding, child: Node*) = 
    new Elem(prefix,label,attributes,scope,child:_*)

  def unapplySeq(n: Node) = n match {
    case _: SpecialNode | _: Group  => None
    case _                          => Some((n.prefix, n.label, n.attributes, n.scope, n.child))
  }
}

/** The case class <code>Elem extends the Node class,
 *  providing an immutable data object representing an XML element.
 *
 *  @param prefix namespace prefix (may be null, but not the empty string)
 *  @param label the element name
 *  @param attribute the attribute map
 *  @param scope the scope containing the namespace bindings
 *  @param child the children of this node
 *
 * Copyright 2008 Google Inc. All Rights Reserved.
 * @author Burak Emir <bqe@google.com>
 */
class Elem(
  override val prefix: String,
  val label: String,
  override val attributes: MetaData,
  override val scope: NamespaceBinding,
  val child: Node*)
extends Node with Serializable
{
  final override def doCollectNamespaces = true
  final override def doTransform         = true
    
  if (prefix == "")
    throw new IllegalArgumentException("prefix of zero length, use null instead")

  if (scope == null)
    throw new IllegalArgumentException("scope is null, use xml.TopScope for empty scope")

  //@todo: copy the children, 
  //  setting namespace scope if necessary
  //  cleaning adjacent text nodes if necessary

  override def basisForHashCode: Seq[Any] = prefix :: label :: attributes :: child.toList

  /** Returns a new element with updated attributes, resolving namespace uris from this element's scope.
   *  See MetaData.update for details.
   *  @param  updates MetaData with new and updated attributes
   *  @return a new symbol with updated attributes
   */
  final def %(updates: MetaData): Elem =
    copy(attributes = MetaData.update(attributes, scope, updates))
  
  /** Returns a copy of this element with any supplied arguments replacing
   *  this element's value for that field.
   *
   *  @return a new symbol with updated attributes
   */
  def copy(
    prefix: String = this.prefix,
    label: String = this.label,
    attributes: MetaData = this.attributes,
    scope: NamespaceBinding = this.scope,
    child: Seq[Node] = this.child.toSeq
  ): Elem = Elem(prefix, label, attributes, scope, child: _*)

  /** Returns concatenation of <code>text(n) for each child
   *  <code>n.
   */
  override def text = child map (_.text) mkString
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Elem.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.