All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalakml.io.KmlToXml.scala Maven / Gradle / Ivy

/*
 * Copyright (c) 2013, Ringo Wathelet
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of "scalakml" nor the names of its contributors may
 *   be used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.scalakml.io

import com.scalakml.kml._
import scala.collection.mutable.MutableList
import xml._
import com.scalakml.gx._
import com.scalakml.atom.Author
import com.scalaxal.xAL.AddressDetails
import com.scalaxal.io.XalToXml
import com.scalakml.kml.Document
import scala.language.implicitConversions
import scala.language.postfixOps

/**
 * @author Ringo Wathelet
 *         Date: 12/12/12
 *         Version: 1
 */

/**
 * represents the extraction of an xml node sequence from a kml element
 */
trait XmlExtractor {
  def getXmlFrom[A: KmlToXml](kml: A): NodeSeq
}

trait KmlToXml[A] {
  def toXml(value: A): NodeSeq
}

trait KmlToXmlSeq[A] {
  def toXml(value: A): Seq[NodeSeq]
}

/** Factory to convert kml objects instances to scala xml NodeSeq */
object KmlToXml extends XmlExtractor {

  // ------------------------------------------------------------    
  // -----------------------implicits----------------------------
  // ------------------------------------------------------------  

  implicit def StringToXmlText(valueOption: Option[String]): Option[xml.Text] = {
    valueOption match {
      case Some(value) => Some(Text(value.trim))
      case None => None
    }
  }

  /** this is the crux of getting xml from the kml objects */
  def getXmlFrom[A: KmlToXml](kml: A) = implicitly[KmlToXml[A]].toXml(kml)

  def getXmlSeqFrom[A: KmlToXmlSeq](kml: A) = implicitly[KmlToXmlSeq[A]].toXml(kml)

  def getNodeFromFieldName(name: String, objOption: Option[Any]): NodeSeq = {
    val baseName = if (name.startsWith("gx:")) name.substring(3) else name
    objOption match {
      case Some(obj) =>
        if (!obj.getClass.getDeclaredFields.exists(field => field.getName.equals(baseName)))
          NodeSeq.Empty
        else {
          Some(obj.getClass.getDeclaredField(baseName)) match {
            case Some(field) => {
              field.setAccessible(true)
              val fieldValue = field.get(obj)
              if (fieldValue == null || !fieldValue.isInstanceOf[Option[_]]) NodeSeq.Empty
              else makeXmlNode(name, fieldValue.asInstanceOf[Option[_]])
            }
            case _ => NodeSeq.Empty
          }
        }
      case None => NodeSeq.Empty
    }
  }

  def makeXmlNode[_](name: String, valueOption: Option[_]): NodeSeq = {
    valueOption match {
      case Some(value) => value match {

        case bool: Boolean => 
          {if (bool) "1" else "0"}
        .copy(label = name)

        case vec2: Vec2 =>
          val theNode =  % Attribute(None, "x", Text(vec2.x.toString), Null) % Attribute(None, "y", Text(vec2.y.toString), Null) % Attribute(None, "xunits", Text(vec2.xunits.toString), Null) % Attribute(None, "yunits", Text(vec2.yunits.toString), Null)
          theNode.copy(label = name)


        case hColor: HexColor => 
          {hColor.hexString}
        .copy(label = name)

        case _ => 
          {value}
        .copy(label = name)
      }
      case None => NodeSeq.Empty
    }
  }

  implicit object AddressDetailsToXml extends KmlToXml[Option[AddressDetails]] {
    def toXml(addressDetailsOption: Option[AddressDetails]): NodeSeq = {
      addressDetailsOption match {
        case Some(addressDetails) => XalToXml(addressDetails)
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object UpdateToXml extends KmlToXml[Option[Update]] {
    def toXml(updateOption: Option[Update]): NodeSeq = {
      updateOption match {
        case Some(update) => 
          {getNodeFromFieldName("targetHref", updateOption)}
          {getXmlSeqFrom(Option(update.updateOption))}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object UpdateOptionToXml extends KmlToXml[Option[UpdateOption]] {
    def toXml(updateOptionOption: Option[UpdateOption]): NodeSeq = {
      updateOptionOption match {
        case Some(updateOption) => updateOption match {
          case delete: Delete => getXmlFrom(Option(delete))
          case create: Create => getXmlFrom(Option(create))
          case change: Change => getXmlFrom(Option(change))
          case _ => NodeSeq.Empty
        }
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object DeleteToXml extends KmlToXml[Option[Delete]] {
    def toXml(deleteOption: Option[Delete]): NodeSeq = {
      deleteOption match {
        case Some(delete) =>
          if (delete.featureSet == Nil) NodeSeq.Empty
          else
            
              {for (f <- delete.featureSet) yield getXmlFrom(Option(f))}
            
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object CreateToXml extends KmlToXml[Option[Create]] {
    def toXml(createOption: Option[Create]): NodeSeq = {
      createOption match {
        case Some(create) =>
          if (create.containerSet == Nil) NodeSeq.Empty
          else
            
              {for (f <- create.containerSet) yield getXmlFrom(Option(f))}
            
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object KmlObjectToXml extends KmlToXml[Option[KmlObject]] {
    def toXml(kmlObjectOption: Option[KmlObject]): NodeSeq = {
      kmlObjectOption match {
        case Some(kmlObject) => kmlObject match {
          case resourceMap: ResourceMap => getXmlFrom(Option(resourceMap))
          case alias: Alias => getXmlFrom(Option(alias))
          case viewVolume: ViewVolume => getXmlFrom(Option(viewVolume))
          case imagePyramid: ImagePyramid => getXmlFrom(Option(imagePyramid))
          case pair: Pair => getXmlFrom(Option(pair))
          case data: Data => getXmlFrom(Option(data))
          case schemaData: SchemaData => getXmlFrom(Option(schemaData))
          case timePrimitive: TimePrimitive => getXmlFrom(Option(timePrimitive))
          case region: Region => getXmlFrom(Option(region))
          case latLonAltBox: LatLonAltBox => getXmlFrom(Option(latLonAltBox))
          case latLonBox: LatLonBox => getXmlFrom(Option(latLonBox))
          case lod: Lod => getXmlFrom(Option(lod))
          case icon: Icon => getXmlFrom(Option(icon))
          case link: Link => getXmlFrom(Option(link))
          case location: Location => getXmlFrom(Option(location))
          case orientation: Orientation => getXmlFrom(Option(orientation))
          case scale: Scale => getXmlFrom(Option(scale))
          case geometry: Geometry => getXmlFrom(Option(geometry))
          case iconStyle: IconStyle => getXmlFrom(Option(iconStyle))
          case labelStyle: LabelStyle => getXmlFrom(Option(labelStyle))
          case lineStyle: LineStyle => getXmlFrom(Option(lineStyle))
          case polyStyle: PolyStyle => getXmlFrom(Option(polyStyle))
          case balloonStyle: BalloonStyle => getXmlFrom(Option(balloonStyle))
          case listStyle: ListStyle => getXmlFrom(Option(listStyle))
          case style: Style => getXmlFrom(Option(style))
          case styleMap: StyleMap => getXmlFrom(Option(styleMap))
          case itemIcon: ItemIcon => getXmlFrom(Option(itemIcon))
          case placemark: Placemark => getXmlFrom(Option(placemark))
          case document: Document => getXmlFrom(Option(document))
          case folder: Folder => getXmlFrom(Option(folder))
          case networkLink: NetworkLink => getXmlFrom(Option(networkLink))
          case photoOverlay: PhotoOverlay => getXmlFrom(Option(photoOverlay))
          case screenOverlay: ScreenOverlay => getXmlFrom(Option(screenOverlay))
          case groundOverlay: GroundOverlay => getXmlFrom(Option(groundOverlay))
          case abstractView: AbstractView => getXmlFrom(Option(abstractView))
          case feature: Feature => getXmlFrom(Option(feature))
          case _ => NodeSeq.Empty
        }
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object ChangeToXml extends KmlToXml[Option[Change]] {
    def toXml(changeOption: Option[Change]): NodeSeq = {
      changeOption match {
        case Some(change) =>
          if (change.objectChangeSet == Nil) NodeSeq.Empty
          else
            
              {for (kmlObject <- change.objectChangeSet) yield getXmlFrom(Option(kmlObject.asInstanceOf[KmlObject]))}
            
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object networkLinkControlToXml extends KmlToXml[Option[NetworkLinkControl]] {
    def toXml(networkLinkControlOption: Option[NetworkLinkControl]): NodeSeq = {
      networkLinkControlOption match {
        case Some(networkLinkControl) => 
          {getNodeFromFieldName("minRefreshPeriod", networkLinkControlOption)}
          {getNodeFromFieldName("maxSessionLength", networkLinkControlOption)}
          {getNodeFromFieldName("cookie", networkLinkControlOption)}
          {getNodeFromFieldName("message", networkLinkControlOption)}
          {getNodeFromFieldName("linkName", networkLinkControlOption)}
          {getNodeFromFieldName("linkDescription", networkLinkControlOption)}
          {getNodeFromFieldName("expires", networkLinkControlOption)}
          {if (networkLinkControl.linkSnippet.isDefined)
             0) networkLinkControl.linkSnippet.get.maxLines.toString else null}>
              {if ((networkLinkControl.linkSnippet.get.value != null) && (!networkLinkControl.linkSnippet.get.value.isEmpty)) networkLinkControl.linkSnippet.get.value else null}
            
          else null}
          {getXmlFrom(networkLinkControl.update)}
          {getXmlFrom(networkLinkControl.abstractView)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object placemarkToXml extends KmlToXml[Option[Placemark]] {
    def toXml(placemarkOption: Option[Placemark]): NodeSeq = {
      placemarkOption match {
        case Some(placemark) => 
          {getXmlSeqFrom(Option(placemark.featurePart))}{getXmlFrom(placemark.geometry)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object documentToXml extends KmlToXml[Option[Document]] {
    def toXml(documentOption: Option[Document]): NodeSeq = {
      documentOption match {
        case Some(document) => 
          {getXmlSeqFrom(Option(document.featurePart))}{for (s <- document.schemas) yield getXmlFrom(Option(s))}{for (f <- document.features) yield getXmlFrom(Option(f))}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object SchemaToXml extends KmlToXml[Option[Schema]] {
    def toXml(schemaOption: Option[Schema]): NodeSeq = {
      schemaOption match {
        case Some(schema) => 
          {for (x <- schema.simpleField) yield getXmlFrom(Option(x))}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object SimpleFieldToXml extends KmlToXml[Option[SimpleField]] {
    def toXml(simpleFieldOption: Option[SimpleField]): NodeSeq = {
      simpleFieldOption match {
        case Some(simpleField) => 
          {getNodeFromFieldName("displayName", simpleFieldOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  // TODO ability to make different namespaces
  implicit object kmlToXml extends KmlToXml[Option[Kml]] {
    def toXml(kmlOption: Option[Kml]): NodeSeq = {
      kmlOption match {
        case Some(kml) => 
          {getXmlFrom(kml.networkLinkControl)}{getXmlFrom(kml.feature)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object BalloonStyleToXml extends KmlToXml[Option[BalloonStyle]] {
    def toXml(balloonStyleOption: Option[BalloonStyle]): NodeSeq = {
      balloonStyleOption match {
        case Some(balloonStyle) => 
          {makeXmlNode("bgColor", balloonStyle.bgColor)}
          {makeXmlNode("textColor", balloonStyle.textColor)}
          {getNodeFromFieldName("displayMode", balloonStyleOption)}
          {getNodeFromFieldName("text", balloonStyleOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object IconStyleToXml extends KmlToXml[Option[IconStyle]] {
    def toXml(iconStyleOption: Option[IconStyle]): NodeSeq = {
      iconStyleOption match {
        case Some(iconStyle) => 
          {getNodeFromFieldName("scale", iconStyleOption)}
          {getNodeFromFieldName("heading", iconStyleOption)}
          {getXmlFrom(iconStyle.color)}
          {getNodeFromFieldName("colorMode", iconStyleOption)}
          {getXmlFrom(iconStyle.icon)}
          {makeXmlNode("hotSpot", iconStyle.hotSpot)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object LabelStyleToXml extends KmlToXml[Option[LabelStyle]] {
    def toXml(labelStyleOption: Option[LabelStyle]): NodeSeq = {
      labelStyleOption match {
        case Some(labelStyle) => 
          {getNodeFromFieldName("scale", labelStyleOption)}
          {getXmlFrom(labelStyle.color)}
          {getNodeFromFieldName("colorMode", labelStyleOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object LineStyleToXml extends KmlToXml[Option[LineStyle]] {
    def toXml(lineStyleOption: Option[LineStyle]): NodeSeq = {
      lineStyleOption match {
        case Some(lineStyle) => 
          {getNodeFromFieldName("width", lineStyleOption)}
          {getXmlFrom(lineStyle.color)}
          {getNodeFromFieldName("colorMode", lineStyleOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object ListStyleToXml extends KmlToXml[Option[ListStyle]] {
    def toXml(listStyleOption: Option[ListStyle]): NodeSeq = {
      listStyleOption match {
        case Some(listStyle) => 
          {getNodeFromFieldName("listItemType", listStyleOption)}
          {getXmlSeqFrom(Option(listStyle.itemIcon))}
          {makeXmlNode("bgColor", listStyle.bgColor)}
          {getNodeFromFieldName("maxSnippetLines", listStyleOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object IconStateToXml extends KmlToXml[Option[ItemIconState]] {
    def toXml(iconStateOption: Option[ItemIconState]): NodeSeq = {
      iconStateOption match {
        case Some(iconState) =>  {iconState} 
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object ItemIconToXml extends KmlToXml[Option[ItemIcon]] {
    def toXml(itemIconOption: Option[ItemIcon]): NodeSeq = {
      itemIconOption match {
        case Some(itemIcon) => 
          {getNodeFromFieldName("href", itemIconOption)}
          {if (itemIcon.state != Nil) {  {for (x <- itemIcon.state) yield x.toString + " "}  }}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object PolyStyleToXml extends KmlToXml[Option[PolyStyle]] {
    def toXml(polyStyleOption: Option[PolyStyle]): NodeSeq = {
      polyStyleOption match {
        case Some(polyStyle) => 
          {getNodeFromFieldName("fill", polyStyleOption)}
          {getNodeFromFieldName("outline", polyStyleOption)}
          {getXmlFrom(polyStyle.color)}
          {getNodeFromFieldName("colorMode", polyStyleOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object StyleToXml extends KmlToXml[Option[Style]] {
    def toXml(styleOption: Option[Style]): NodeSeq = {
      styleOption match {
        case Some(style) => 
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object StyleSelectorToXml extends KmlToXml[Option[StyleSelector]] {
    def toXml(styleSelectorOption: Option[StyleSelector]): NodeSeq = {
      styleSelectorOption match {
        case Some(styleSelector) => styleSelector match {
          case style: Style => getXmlFrom(Option(style))
          case styleMap: StyleMap => getXmlFrom(Option(styleMap))
          case _ => NodeSeq.Empty
        }
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object PairToXml extends KmlToXml[Option[Pair]] {
    def toXml(pairOption: Option[Pair]): NodeSeq = {
      pairOption match {
        case Some(pair) => 
          {getNodeFromFieldName("key", pairOption)}{if (pair.styleUrl.isDefined) getNodeFromFieldName("styleUrl", pairOption)
          else getXmlFrom(pair.styleSelector)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object StyleMapToXml extends KmlToXml[Option[StyleMap]] {
    def toXml(styleMapOption: Option[StyleMap]): NodeSeq = {
      styleMapOption match {
        case Some(styleMap) => 
          {getXmlSeqFrom(Option(styleMap.pair))}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object AtomAuthorToXml extends KmlToXml[Option[Author]] {
    def toXml(authorOption: Option[Author]): NodeSeq = {
      authorOption match {
        case Some(author) => 
          
            {if (author.name.isDefined) author.name.get else null}
          
          
            {if (author.uri.isDefined) author.uri.get else null}
          
          
            {if (author.email.isDefined) author.email.get else null}
          
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object AtomLinkToXml extends KmlToXml[Option[com.scalakml.atom.Link]] {
    def toXml(linkOption: Option[com.scalakml.atom.Link]): NodeSeq = {
      linkOption match {
        case Some(link) =>
            
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object AbstractViewToXml extends KmlToXml[Option[AbstractView]] {
    def toXml(abstractViewOption: Option[AbstractView]): NodeSeq = {
      abstractViewOption match {
        case Some(abstractView) => abstractView match {
          case camera: Camera =>
            
              {getNodeFromFieldName("roll", Option(camera))}
              {getNodeFromFieldName("longitude", Option(camera))}
              {getNodeFromFieldName("latitude", Option(camera))}
              {getNodeFromFieldName("altitude", Option(camera))}
              {getNodeFromFieldName("heading", Option(camera))}
              {getNodeFromFieldName("tilt", Option(camera))}
              {getNodeFromFieldName("altitudeMode", Option(camera))}
            
          case lookAt: LookAt =>
            
              {getNodeFromFieldName("range", Option(lookAt))}
              {getNodeFromFieldName("longitude", Option(lookAt))}
              {getNodeFromFieldName("latitude", Option(lookAt))}
              {getNodeFromFieldName("altitude", Option(lookAt))}
              {getNodeFromFieldName("heading", Option(lookAt))}
              {getNodeFromFieldName("tilt", Option(lookAt))}
              {getNodeFromFieldName("altitudeMode", Option(lookAt))}
            
          case _ => NodeSeq.Empty
        }
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object TimePrimitiveToXml extends KmlToXml[Option[TimePrimitive]] {
    def toXml(timePrimitiveOption: Option[TimePrimitive]): NodeSeq = {
      timePrimitiveOption match {
        case Some(timePrimitive) => timePrimitive match {
          case timeStamp: TimeStamp =>
            
              {getNodeFromFieldName("when", Option(timeStamp))}
            
          case timeSpan: TimeSpan =>
            
              {getNodeFromFieldName("begin", Option(timeSpan))}{getNodeFromFieldName("end", Option(timeSpan))}
            
          case _ => NodeSeq.Empty
        }
        case None => NodeSeq.Empty
      }
    }
  }

  // TODO  how to get dataExtension
  implicit object DataToXml extends KmlToXml[Option[Data]] {
    def toXml(dataOption: Option[Data]): NodeSeq = {
      dataOption match {
        case Some(data) => 
          {getNodeFromFieldName("value", dataOption)}{getNodeFromFieldName("displayName", dataOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  // TODO how to write other
  implicit object ExtendedDataToXml extends KmlToXml[Option[ExtendedData]] {
    def toXml(extendedDataOption: Option[ExtendedData]): NodeSeq = {
      extendedDataOption match {
        case Some(extendedData) => 
          {if (extendedData.data != Nil) for (x <- extendedData.data) yield getXmlFrom(Option(x))}
          {if (extendedData.schemaData != Nil) for (x <- extendedData.schemaData) yield getXmlFrom(Option(x))}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object SchemaDataToXml extends KmlToXml[Option[SchemaData]] {
    def toXml(schemaDataOption: Option[SchemaData]): NodeSeq = {
      schemaDataOption match {
        case Some(schemaData) => 
          {if (schemaData.simpleData != Nil) for (x <- schemaData.simpleData) yield getXmlFrom(Option(x))}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object SimpleDataToXml extends KmlToXml[Option[SimpleData]] {
    def toXml(simpleDataOption: Option[SimpleData]): NodeSeq = {
      simpleDataOption match {
        case Some(simpleData) => 
          {if (simpleData.value.isDefined) simpleData.value.get else null}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object FolderToXml extends KmlToXml[Option[Folder]] {
    def toXml(folderOption: Option[Folder]): NodeSeq = {
      folderOption match {
        case Some(folder) => 
          {getXmlSeqFrom(Option(folder.featurePart))}{for (f <- folder.features) yield getXmlFrom(Option(f))}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object LatLonBoxToXml extends KmlToXml[Option[LatLonBox]] {
    def toXml(latLonBoxOption: Option[LatLonBox]): NodeSeq = {
      latLonBoxOption match {
        case Some(latLonBox) => 
          {for (field <- latLonBox.getClass.getDeclaredFields) yield getNodeFromFieldName(field.getName, latLonBoxOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object LatLonAltBoxToXml extends KmlToXml[Option[LatLonAltBox]] {
    def toXml(latLonAltBoxOption: Option[LatLonAltBox]): NodeSeq = {
      latLonAltBoxOption match {
        case Some(latLonAltBox) => 
          {for (field <- latLonAltBox.getClass.getDeclaredFields) yield getNodeFromFieldName(field.getName, latLonAltBoxOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object LatLonQuadToXml extends KmlToXml[Option[LatLonQuad]] {
    def toXml(latLonQuadOption: Option[LatLonQuad]): NodeSeq = {
      latLonQuadOption match {
        case Some(latLonQuad) => 
          {getXmlFrom(latLonQuad.coordinates)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object GroundOverlayToXml extends KmlToXml[Option[GroundOverlay]] {
    def toXml(overlayOption: Option[GroundOverlay]): NodeSeq = {
      overlayOption match {
        case Some(overlay) => 
          {getXmlSeqFrom(Option(overlay.featurePart))}
          {getNodeFromFieldName("altitude", overlayOption)}
          {getNodeFromFieldName("drawOrder", overlayOption)}
          {getNodeFromFieldName("altitudeMode", overlayOption)}
          {getXmlFrom(overlay.latLonBox)}
          {getXmlFrom(overlay.latLonQuad)}
          {getXmlFrom(overlay.color)}
          {getNodeFromFieldName("drawOrder", overlayOption)}
          {getXmlFrom(overlay.icon)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object ViewVolumeToXml extends KmlToXml[Option[ViewVolume]] {
    def toXml(viewVolumeOption: Option[ViewVolume]): NodeSeq = {
      viewVolumeOption match {
        case Some(viewVolume) => 
          {getNodeFromFieldName("leftFov", viewVolumeOption)}{getNodeFromFieldName("rightFov", viewVolumeOption)}{getNodeFromFieldName("bottomFov", viewVolumeOption)}{getNodeFromFieldName("topFov", viewVolumeOption)}{getNodeFromFieldName("near", viewVolumeOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object ImagePyramidToXml extends KmlToXml[Option[ImagePyramid]] {
    def toXml(imagePyramidOption: Option[ImagePyramid]): NodeSeq = {
      imagePyramidOption match {
        case Some(imagePyramid) => 
          {getNodeFromFieldName("tileSize", imagePyramidOption)}{getNodeFromFieldName("maxWidth", imagePyramidOption)}{getNodeFromFieldName("maxHeight", imagePyramidOption)}{getNodeFromFieldName("gridOrigin", imagePyramidOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object PhotoOverlayToXml extends KmlToXml[Option[PhotoOverlay]] {
    def toXml(overlayOption: Option[PhotoOverlay]): NodeSeq = {
      overlayOption match {
        case Some(overlay) => 
          {getXmlSeqFrom(Option(overlay.featurePart))}{getNodeFromFieldName("rotation", overlayOption)}{getXmlFrom(overlay.viewVolume)}{getXmlFrom(overlay.imagePyramid)}{getXmlFrom(overlay.point.asInstanceOf[Option[Geometry]])}{getNodeFromFieldName("shape", overlayOption)}
          {getXmlFrom(overlay.color)}
          {getNodeFromFieldName("drawOrder", overlayOption)}
          {getXmlFrom(overlay.icon)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object ScreenOverlayToXml extends KmlToXml[Option[ScreenOverlay]] {
    def toXml(overlayOption: Option[ScreenOverlay]): NodeSeq = {
      overlayOption match {
        case Some(overlay) => 
          {getXmlSeqFrom(Option(overlay.featurePart))}{getNodeFromFieldName("overlayXY", overlayOption)}{getNodeFromFieldName("screenXY", overlayOption)}{getNodeFromFieldName("rotationXY", overlayOption)}{getNodeFromFieldName("size", overlayOption)}{getNodeFromFieldName("rotation", overlayOption)}
          {getXmlFrom(overlay.color)}
          {getNodeFromFieldName("drawOrder", overlayOption)}
          {getXmlFrom(overlay.icon)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object makeNetworkLinkToXml extends KmlToXml[Option[NetworkLink]] {
    def toXml(networkLinkOption: Option[NetworkLink]): NodeSeq = {
      networkLinkOption match {
        case Some(networkLink) => 
          {getXmlSeqFrom(Option(networkLink.featurePart))}{getNodeFromFieldName("refreshVisibility", networkLinkOption)}{getNodeFromFieldName("flyToView", networkLinkOption)}{getNodeFromFieldName("refreshVisibility", networkLinkOption)}{getXmlFrom(networkLink.link)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object LinkToXml extends KmlToXml[Option[com.scalakml.kml.Link]] {
    def toXml(linkOption: Option[com.scalakml.kml.Link]): NodeSeq = {
      linkOption match {
        case Some(link) => 
          {for (field <- link.getClass.getDeclaredFields) yield getNodeFromFieldName(field.getName, linkOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object IconTypeToXml extends KmlToXml[Option[Icon]] {
    def toXml(linkOption: Option[Icon]): NodeSeq = {
      linkOption match {
        case Some(link) => 
          {for (field <- link.getClass.getDeclaredFields) yield getNodeFromFieldName(field.getName, linkOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object AliasToXml extends KmlToXml[Option[Alias]] {
    def toXml(aliasOption: Option[Alias]): NodeSeq = {
      aliasOption match {
        case Some(alias) => 
          {getNodeFromFieldName("targetHref", aliasOption)}{getNodeFromFieldName("sourceHref", aliasOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object ResourceMapToXml extends KmlToXml[Option[ResourceMap]] {
    def toXml(resourceMapOption: Option[ResourceMap]): NodeSeq = {
      resourceMapOption match {
        case Some(resourceMap) => 
          {for (x <- resourceMap.alias) yield getXmlFrom(Option(x))}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object ScaleToXml extends KmlToXml[Option[Scale]] {
    def toXml(scaleOption: Option[Scale]): NodeSeq = {
      scaleOption match {
        case Some(scale) => 
          {for (field <- scale.getClass.getDeclaredFields) yield getNodeFromFieldName(field.getName, scaleOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object OrientationToXml extends KmlToXml[Option[Orientation]] {
    def toXml(orientationOption: Option[Orientation]): NodeSeq = {
      orientationOption match {
        case Some(orientation) => 
          {for (field <- orientation.getClass.getDeclaredFields) yield getNodeFromFieldName(field.getName, orientationOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object LocationToXml extends KmlToXml[Option[Location]] {
    def toXml(locationOption: Option[Location]): NodeSeq = {
      locationOption match {
        case Some(location) => 
          {for (field <- location.getClass.getDeclaredFields) yield getNodeFromFieldName(field.getName, locationOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object LocationsToXml extends KmlToXml[Option[Seq[Location]]] {
    def toXml(coordsOption: Option[Seq[Location]]): NodeSeq = {
      coordsOption match {
        case Some(coords) =>
          
            {for (x <- coords) yield {
            if (x.longitude.isDefined && x.latitude.isDefined) {
              x.longitude.get.toString + "," + x.latitude.get.toString + (if (x.altitude.isDefined) "," + x.altitude.get.toString else "") + " \n"
            }
          }}
          
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object CoordinatesToXml extends KmlToXml[Option[Seq[Coordinate]]] {
    def toXml(coordsOption: Option[Seq[Coordinate]]): NodeSeq = {
      coordsOption match {
        case Some(coords) =>
          
            {for (x <- coords) yield {
            if (x.longitude.isDefined && x.latitude.isDefined) {
              x.longitude.get.toString + "," + x.latitude.get.toString + (if (x.altitude.isDefined) "," + x.altitude.get.toString else "") + " \n"
            }
          }}
          
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object CoordinateToXml extends KmlToXml[Option[Coordinate]] {
    def toXml(coordsOption: Option[Coordinate]): NodeSeq = {
      coordsOption match {
        case Some(coord) =>
          
            {
            if (coord.longitude.isDefined && coord.latitude.isDefined) {
              coord.longitude.get.toString + "," + coord.latitude.get.toString + (if (coord.altitude.isDefined) "," + coord.altitude.get.toString else "") + " \n"
            }
          }
          
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object HexColorToXml extends KmlToXml[Option[HexColor]] {
    def toXml(colorOption: Option[HexColor]): NodeSeq = {
      colorOption match {
        case Some(color) => 
          {if ((color.hexString != null) && !color.hexString.isEmpty) color.hexString else null}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object SnippetToXml extends KmlToXml[Option[Snippet]] {
    def toXml(snippetOption: Option[Snippet]): NodeSeq = {
      snippetOption match {
        case Some(snippet) => = 0) snippet.maxLines.toString else null}>
          {if ((snippet.value != null) && (!snippet.value.isEmpty)) snippet.value else null}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object LodToXml extends KmlToXml[Option[Lod]] {
    def toXml(lodOption: Option[Lod]): NodeSeq = {
      lodOption match {
        case Some(lod) => 
          {for (field <- lod.getClass.getDeclaredFields) yield getNodeFromFieldName(field.getName, lodOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object RegionToXml extends KmlToXml[Option[Region]] {
    def toXml(regionOption: Option[Region]): NodeSeq = {
      regionOption match {
        case Some(region) => 
          {getXmlFrom(region.latLonAltBox)}{getXmlFrom(region.lod)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object GeometryToXml extends KmlToXml[Option[Geometry]] {
    def toXml(geometryOption: Option[Geometry]): NodeSeq = {
      geometryOption match {
        case Some(geometry) => geometry match {

          case point: Point =>
            
              {getNodeFromFieldName("extrude", Option(point))}{getNodeFromFieldName("altitudeMode", Option(point))}{getXmlFrom(point.coordinates)}
            


          case lineString: LineString =>
            
              {getNodeFromFieldName("extrude", Option(lineString))}{getNodeFromFieldName("tessellate", Option(lineString))}{getNodeFromFieldName("altitudeMode", Option(lineString))}{getXmlFrom(lineString.coordinates)}
            

          case linearRing: LinearRing =>
            
              {getNodeFromFieldName("extrude", Option(linearRing))}{getNodeFromFieldName("tessellate", Option(linearRing))}{getNodeFromFieldName("altitudeMode", Option(linearRing))}{getXmlFrom(linearRing.coordinates)}
            

          case polygon: Polygon =>
            
              {getNodeFromFieldName("extrude", Option(polygon))}{getNodeFromFieldName("tessellate", Option(polygon))}{getNodeFromFieldName("altitudeMode", Option(polygon))}
              {if (polygon.outerBoundaryIs.isDefined)
              
                { if (polygon.outerBoundaryIs.get.linearRing.isDefined)
                getXmlFrom(Option(polygon.outerBoundaryIs.get.linearRing.get.asInstanceOf[Geometry]))
              else null
                }
              }
              {if (polygon.innerBoundaryIs != Nil)
              
              {for (s <- polygon.innerBoundaryIs) yield
                if (s.linearRing.isDefined) getXmlFrom(Option(s.linearRing.get.asInstanceOf[Geometry]))}
              }
            

          case multiGeometry: MultiGeometry =>
            
              {for (x <- multiGeometry.geometries) yield getXmlFrom(Option(x))}
            

          case model: Model =>
            
              {getNodeFromFieldName("altitudeMode", Option(model))}{getXmlFrom(model.location)}{getXmlFrom(model.orientation)}{getXmlFrom(model.scale)}{getXmlFrom(model.link)}{getXmlFrom(model.resourceMap)}
            

          case _ => NodeSeq.Empty
        }
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object FeatureToXml extends KmlToXml[Option[Feature]] {

    import TourToXml._

    def toXml(featureOption: Option[Feature]): NodeSeq = {
      featureOption match {
        case Some(feature) => feature match {
          case placemark: Placemark => getXmlFrom(Option(placemark))
          case networkLink: NetworkLink => getXmlFrom(Option(networkLink))
          case tour: Tour => getXmlFrom(Option(tour))
          case document: Document => getXmlFrom(Option(document))
          case folder: Folder => getXmlFrom(Option(folder))
          case groundOverlay: GroundOverlay => getXmlFrom(Option(groundOverlay))
          case screenOverlay: ScreenOverlay => getXmlFrom(Option(screenOverlay))
          case photoOverlay: PhotoOverlay => getXmlFrom(Option(photoOverlay))
          case _ => NodeSeq.Empty
        }
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object ContainerToXml extends KmlToXml[Option[Container]] {
    def toXml(containerOption: Option[Container]): NodeSeq = {
      containerOption match {
        case Some(container) => container match {
          case document: Document => getXmlFrom(Option(document))
          case folder: Folder => getXmlFrom(Option(folder))
          case _ => NodeSeq.Empty
        }
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object StyleSelectorSeqToXml extends KmlToXmlSeq[Option[Seq[StyleSelector]]] {
    def toXml(styleSet: Option[Seq[StyleSelector]]): Seq[NodeSeq] = {
      styleSet match {
        case Some(styles) => styles collect {
          case x => getXmlFrom(Option(x.asInstanceOf[StyleSelector]))
        } filter (x => (x != null) && (x != None)) toSeq
        case None => Seq.empty
      }
    }
  }

  implicit object PairSeqToXml extends KmlToXmlSeq[Option[Seq[Pair]]] {
    def toXml(pairSet: Option[Seq[Pair]]): Seq[NodeSeq] = {
      pairSet match {
        case Some(pSet) => pSet collect {
          case x => getXmlFrom(Option(x.asInstanceOf[Pair]))
        } filter (x => (x != null) && (x != None)) toSeq
        case None => Seq.empty
      }
    }
  }

  // ------------------------------------------------------------
  // -----------------------def----------------------------------  
  // ------------------------------------------------------------  

  implicit object UpdateOptionSeqToXml extends KmlToXmlSeq[Option[Seq[UpdateOption]]] {
    def toXml(updateOptionSet: Option[Seq[UpdateOption]]): Seq[NodeSeq] = {
      updateOptionSet match {
        case Some(uSet) => (uSet collect {
          case x => getXmlFrom(Option(x.asInstanceOf[UpdateOption]))
        } filter (x => (x != null) && (x != None)) toSeq)
        case None => Seq.empty
      }
    }
  }

  implicit object ItemIconSeqToXml extends KmlToXmlSeq[Option[Seq[ItemIcon]]] {
    def toXml(itemIconSet: Option[Seq[ItemIcon]]): Seq[NodeSeq] = {
      itemIconSet match {
        case Some(iSet) => iSet collect {
          case x => getXmlFrom(Option(x.asInstanceOf[ItemIcon]))
        } filter (x => (x != null) && (x != None)) toSeq
        case None => Seq.empty
      }
    }
  }

  implicit object IconStateSeqToXml extends KmlToXmlSeq[Option[Seq[ItemIconState]]] {
    def toXml(itemIconStateSet: Option[Seq[ItemIconState]]): Seq[NodeSeq] = {
      itemIconStateSet match {
        case Some(iSet) => iSet collect {
          case x => getXmlFrom(Option(x.asInstanceOf[ItemIconState]))
        } filter (x => (x != null) && (x != None)) toSeq
        case None => Seq.empty
      }
    }
  }

  implicit object FeaturePartToXml extends KmlToXmlSeq[Option[FeaturePart]] {
    def toXml(featurePart: Option[FeaturePart]): Seq[NodeSeq] = {
      if (!featurePart.isDefined || (featurePart.get == null)) Seq.empty
      else {
        val list = new MutableList[NodeSeq]()

        list += getNodeFromFieldName("name", featurePart)
        list += getNodeFromFieldName("visibility", featurePart)
        list += getNodeFromFieldName("open", featurePart)
        list += getXmlFrom(featurePart.get.atomAuthor)
        list += getXmlFrom(featurePart.get.atomLink)
        list += getNodeFromFieldName("address", featurePart)
        list += getXmlFrom(featurePart.get.addressDetails)
        list += getNodeFromFieldName("phoneNumber", featurePart)
        list += getNodeFromFieldName("description", featurePart)
        list += getXmlFrom(featurePart.get.extendedData)
        list += getXmlFrom(featurePart.get.snippet)
        list += getXmlFrom(featurePart.get.region)
        list += getXmlFrom(featurePart.get.timePrimitive)
        list += getNodeFromFieldName("styleUrl", featurePart)
        list ++= getXmlSeqFrom(Option(featurePart.get.styleSelector))
        list += getXmlFrom(featurePart.get.abstractView)

        list filter (x => (x != null) && (x != None)) toSeq
      }
    }
  }

  //-----------------------------------------------------------------------------------------------
  //----------------------------------gx-----------------------------------------------------------
  //-----------------------------------------------------------------------------------------------

  // Note: the AlitudeMode currently does not carry the gx: namespace

  implicit object PlaylistToXml extends KmlToXml[Option[Playlist]] {
    def toXml(playlistOption: Option[Playlist]): NodeSeq = {
      import TourPrimitiveToXml._
      playlistOption match {
        case Some(playlist) =>
          if ((!playlist.tourPrimitiveGroup.isDefined) || (playlist.tourPrimitiveGroup.get == Nil)) NodeSeq.Empty
          else
            
              {for (tourPrimitive <- playlist.tourPrimitiveGroup.get) yield getXmlFrom(Option(tourPrimitive))}
            
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object TourToXml extends KmlToXml[Option[Tour]] {
    def toXml(tourOption: Option[Tour]): NodeSeq = {
      tourOption match {
        case Some(tour) => 
          {getXmlFrom(tour.playlist)}{getXmlSeqFrom(Option(tour.featurePart))}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object AnimatedUpdateToXml extends KmlToXml[Option[AnimatedUpdate]] {
    def toXml(animatedUpdateOption: Option[AnimatedUpdate]): NodeSeq = {
      animatedUpdateOption match {
        case Some(animatedUpdate) => 
          {getNodeFromFieldName("gx:duration", animatedUpdateOption)}{getXmlFrom(animatedUpdate.update)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object FlyToToXml extends KmlToXml[Option[FlyTo]] {
    def toXml(flyToOption: Option[FlyTo]): NodeSeq = {
      flyToOption match {
        case Some(flyTo) => 
          {getNodeFromFieldName("gx:duration", flyToOption)}{getNodeFromFieldName("gx:flyToMode", flyToOption)}{getXmlFrom(flyTo.abstractView)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object SoundCueToXml extends KmlToXml[Option[SoundCue]] {
    def toXml(soundCueOption: Option[SoundCue]): NodeSeq = {
      soundCueOption match {
        case Some(soundCue) => 
          {getNodeFromFieldName("href", soundCueOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object TourControlToXml extends KmlToXml[Option[TourControl]] {
    def toXml(tourControlOption: Option[TourControl]): NodeSeq = {
      tourControlOption match {
        case Some(tourControl) => 
          {getNodeFromFieldName("gx:playMode", tourControlOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object WaitToXml extends KmlToXml[Option[Wait]] {
    def toXml(waitOption: Option[Wait]): NodeSeq = {
      waitOption match {
        case Some(waitt) => 
          {getNodeFromFieldName("gx:duration", waitOption)}
        
        case None => NodeSeq.Empty
      }
    }
  }

  implicit object TourPrimitiveToXml extends KmlToXml[Option[TourPrimitive]] {
    def toXml(tourPrimitiveOption: Option[TourPrimitive]): NodeSeq = {
      tourPrimitiveOption match {
        case Some(tourPrimitive) => tourPrimitive match {
          case animatedUpdate: AnimatedUpdate => getXmlFrom(Option(animatedUpdate))
          case flyTo: FlyTo => getXmlFrom(Option(flyTo))
          case soundCue: SoundCue => getXmlFrom(Option(soundCue))
          case waitt: Wait => getXmlFrom(Option(waitt))
          case tourControl: TourControl => getXmlFrom(Option(tourControl))
          case _ => NodeSeq.Empty
        }
        case None => NodeSeq.Empty
      }
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy