Advertisement
Guest User

Untitled

a guest
Dec 5th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.10 KB | None | 0 0
  1. private def parseObject(obj: Node, currentPath: String): Either[ParseError,Seq[SensorData]] = {
  2.  
  3.     val eitherInfoVals: Seq[Either[ParseError, SensorData]] = for {
  4.       infoitem <- obj \ "InfoItem"
  5.  
  6.       objId = obj \ "id"
  7.       objName = infoitem \ "@name"
  8.       objValue = infoitem \ "value"
  9.  
  10.       sensorDataEither <- for {
  11.         path: String <- currentPath + "/" + (
  12.           objId.headOption match {
  13.             case Some( path_ ) => Right(path_.text)
  14.             case None          =>
  15.               Left( new ParseError( "Broken odf format. No id." ) )
  16.           }
  17.         ) + "/" +  objName.text
  18.  
  19.         value: String <- objValue.headOption match {
  20.           case Some( value_ ) => Right(value_.text)
  21.           case None           =>
  22.             Left( new ParseError( "No value element found in infoitem." ) )
  23.         }  
  24.  
  25.         dateTimeAttr = infoitem \ "value" \ "@dateTime"
  26.         dateTime: String <- dateTimeAttr.headOption match { // or Right(...getOrElse(""))
  27.           case Some( value_ ) => Right(value_.text)
  28.           case None           => Right("")
  29.         }  
  30.  
  31.         typeAttr = infoitem \ "value" \ "@type"
  32.         typeOfInfo: String <- typeAttr.headOption match {
  33.           case Some( value_ ) => Right(value_.text)
  34.           case None           => Right("")
  35.         }  
  36.       } yield new SensorData( path, value, dateTime )
  37.  
  38.  
  39.     } yield sensorDataEither
  40.  
  41.     val obObjects = obj \ "Object"
  42.  
  43.     val eitherDeeperVals =
  44.       for {
  45.         subobj <- obObjects
  46.  
  47.         objId = obj \ "id"
  48.         nextPath = currentPath + "/" + (
  49.           objId.headOption match {
  50.             case Some( path_ ) => Right(path_.text)
  51.             case None          =>
  52.               return Left( new ParseError( "Broken odf format. No id." ) )
  53.           }
  54.         )
  55.        
  56.       } yield parseObject(subobj, nextPath)
  57.    
  58.  
  59.     val allData = eitherInfoVals ++ eitherDeeperVals.flatten
  60.  
  61.     val rightData = allData.map(
  62.       _ match {
  63.         case Left(err) => return Left(err)
  64.         case Right(r)  => r
  65.       }
  66.     )
  67.  
  68.     return Right(rightData)
  69.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement