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

Scala example source code file (Attribute.scala)

This example Scala source code file (Attribute.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

attribute, attribute, boolean, metadata, metadata, namespacebinding, none, prefixedattribute, seq, seq, some, string, string, unprefixedattribute

The Scala Attribute.scala source code

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


package scala.xml

/** Attribute defines the interface shared by both
 *  PrefixedAttribute and UnprefixedAttribute
 */

object Attribute {
  def unapply(x: Attribute) = x match {
    case PrefixedAttribute(_, key, value, next) => Some(key, value, next)
    case UnprefixedAttribute(key, value, next)  => Some(key, value, next)
    case _                                      => None
  }

  /** Convenience functions which choose Un/Prefixedness appropriately */
  def apply(key: String, value: Seq[Node], next: MetaData): Attribute =
    new UnprefixedAttribute(key, value, next)
    
  def apply(pre: String, key: String, value: String, next: MetaData): Attribute =
    if (pre == null || pre == "") new UnprefixedAttribute(key, value, next)
    else new PrefixedAttribute(pre, key, value, next)
    
  def apply(pre: String, key: String, value: Seq[Node], next: MetaData): Attribute =
    if (pre == null || pre == "") new UnprefixedAttribute(key, value, next)
    else new PrefixedAttribute(pre, key, value, next)
    
  def apply(pre: Option[String], key: String, value: Seq[Node], next: MetaData): Attribute =
    pre match {
      case None     => new UnprefixedAttribute(key, value, next)
      case Some(p)  => new PrefixedAttribute(p, key, value, next)
    }  
}

abstract trait Attribute extends MetaData {
  def pre: String        // will be null if unprefixed
  val key: String
  val value: Seq[Node]
  val next: MetaData
  
  def apply(key: String): Seq[Node]
  def apply(namespace: String, scope: NamespaceBinding, key: String): Seq[Node]
  def copy(next: MetaData): Attribute

  def remove(key: String) =
    if (!isPrefixed && this.key == key) next
    else copy(next remove key)
  
  def remove(namespace: String, scope: NamespaceBinding, key: String) =
    if (isPrefixed && this.key == key && (scope getURI pre) == namespace) next
    else next.remove(namespace, scope, key)

  def isPrefixed: Boolean = pre != null
  def getNamespace(owner: Node): String
  def wellformed(scope: NamespaceBinding): Boolean = {
    val arg = if (isPrefixed) scope getURI pre else null    
    (next(arg, scope, key) == null) && (next wellformed scope)
  }
  
  /** Appends string representation of only this attribute to stringbuffer.
   */
  def toString1(sb: StringBuilder) {
    if (value == null)
      return
    if (isPrefixed)
      sb append pre append ':'
    
    sb append key append '='
    val sb2 = new StringBuilder()
    Utility.sequenceToXML(value, TopScope, sb2, true)    
    Utility.appendQuoted(sb2.toString(), sb)
  }
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.