Commit 5a949c6c by Amelin Konstantin

change get signature

parent ee6e333a
......@@ -2,7 +2,7 @@ package templates
import scala.collection.mutable
import scala.util.{Success, Try}
import scala.xml.{Node, Text}
import scala.xml.{NodeSeq, Text}
trait Logger {
this: Template =>
......@@ -37,27 +37,28 @@ trait Logger {
m
}
override protected[this] def get(content: Try[Node], attr: String = null): Text = content match {
case Success(v) =>
var res = Text(v.text)
override protected[this] def get(content: NodeSeq, attr: String = null, idx: Int = 0): Text =
Try { content(idx) } match {
case Success(v) =>
var res = Text(v.text)
if (attr != null) v.attribute(attr) match {
case None =>
res = Text(msgNotFound)
case o =>
data(v.hashCode).head._3 -= o.get.hashCode
res = Text(o.get.text)
} else if (data(v.hashCode).nonEmpty) {
if (data(v.hashCode).head._3 != null) {
val e = data(v.hashCode).head.copy(_2 = null)
if (attr != null) v.attribute(attr) match {
case None =>
res = Text(msgNotFound)
case o =>
data(v.hashCode).head._3 -= o.get.hashCode
res = Text(o.get.text)
} else if (data(v.hashCode).nonEmpty) {
if (data(v.hashCode).head._3 != null) {
val e = data(v.hashCode).head.copy(_2 = null)
data(v.hashCode).enqueue(e)
data(v.hashCode).enqueue(e)
}
data(v.hashCode).dequeue()
}
data(v.hashCode).dequeue()
}
res
case _ => Text(msgNotFound)
res
case _ => Text(msgNotFound)
}
def log(): Unit = data.foreach(e => println(e._2))
......
......@@ -9,8 +9,8 @@ class RepoTemplate(source: Node) extends TradeTemplate(source) {
<fpml:tradeHeader>
{ for (s <- source \ "trade" \ "tradeHeader" \ "partyTradeIdentifier") yield {
<fpml:partyTradeIdentifier>
<fpml:partyReference href={ get(Try { (s \ "partyReference").head }, "href") } />
<fpml:tradeId tradeIdScheme={ get(Try { (s \ "tradeId").head }, "tradeIdScheme") }>{ get(Try { (s \ "tradeId").head }) }</fpml:tradeId>
<fpml:partyReference href={ get(s \ "partyReference", "href") }/>
<fpml:tradeId tradeIdScheme={ get(s \ "tradeId", "tradeIdScheme") }>{ get(s \ "tradeId") }</fpml:tradeId>
</fpml:partyTradeIdentifier>
}
}
......@@ -113,38 +113,38 @@ class RepoTemplate(source: Node) extends TradeTemplate(source) {
*/}
</fpml:tradeHeader>
<fpmlext:repo xsi:type="rtsrep:Repo">
<fpml:productId>{ get(Try { (source \ "trade" \ "repo" \ "productType" ).head }) }</fpml:productId>
<fpml:productId productIdScheme={ get(Try { (source \ "trade" \ "repo" \ "productId" ).head }, "productIdScheme") }>{ get(Try { (source \ "trade" \ "repo" \ "productId" ).head }) }</fpml:productId>
<fpml:productId>{ get(source \ "trade" \ "repo" \ "productType") }</fpml:productId>
<fpml:productId productIdScheme={ get(source \ "trade" \ "repo" \ "productId", "productIdScheme") }>{ get(source \ "trade" \ "repo" \ "productId") }</fpml:productId>
<fpmlext:fixedRateSchedule>
<fpml:initialValue>{ get(Try { (source \ "trade" \ "repo" \ "fixedRateSchedule" \ "initialValue").head }) }</fpml:initialValue>
<fpml:initialValue>{ get(source \ "trade" \ "repo" \ "fixedRateSchedule" \ "initialValue") }</fpml:initialValue>
<step>
<stepDate>{ get(Try { (source \ "trade" \ "repo" \ "fixedRateSchedule" \\ "stepDate").head }) }</stepDate>
<stepValue>{ get(Try { (source \ "trade" \ "repo" \ "fixedRateSchedule" \\ "stepValue").head }) }</stepValue>
<stepDate>{ get(source \ "trade" \ "repo" \ "fixedRateSchedule" \\ "stepDate") }</stepDate>
<stepValue>{ get(source \ "trade" \ "repo" \ "fixedRateSchedule" \\ "stepValue") }</stepValue>
</step>
</fpmlext:fixedRateSchedule>
<fpmlext:floatingRateCalculation>
<fpml:floatingRateIndex>{ get(Try { (source \ "trade" \ "repo" \ "floatingRateCalculation" \ "floatingRateIndex" ).head }) }</fpml:floatingRateIndex>
<fpml:floatingRateIndex>{ get(source \ "trade" \ "repo" \ "floatingRateCalculation" \ "floatingRateIndex") }</fpml:floatingRateIndex>
<indexTenor>
<periodMultiplier>{ get(Try { (source \ "trade" \ "repo" \ "floatingRateCalculation" \\ "periodMultiplier" ).head }) }</periodMultiplier>
<period>{ get(Try { (source \ "trade" \ "repo" \ "floatingRateCalculation" \\ "period" ).head }) }</period>
<periodMultiplier>{ get(source \ "trade" \ "repo" \ "floatingRateCalculation" \\ "periodMultiplier") }</periodMultiplier>
<period>{ get(source \ "trade" \ "repo" \ "floatingRateCalculation" \\ "period") }</period>
</indexTenor>
<spreadSchedule>
<initialValue>{ get(Try { (source \ "trade" \ "repo" \ "floatingRateCalculation" \\ "initialValue" ).head }) }</initialValue>
<initialValue>{ get(source \ "trade" \ "repo" \ "floatingRateCalculation" \\ "initialValue") }</initialValue>
</spreadSchedule>
<initialRate>{ get(Try { (source \ "trade" \ "repo" \ "floatingRateCalculation" \\ "initialRate" ).head }) }</initialRate>
<initialRate>{ get(source \ "trade" \ "repo" \ "floatingRateCalculation" \\ "initialRate") }</initialRate>
</fpmlext:floatingRateCalculation>
<fpmlext:dayCountFraction>{ get(Try { (source \ "trade" \ "repo" \ "dayCountFraction" ).head }) }</fpmlext:dayCountFraction>
<fpmlext:dayCountFraction>{ get(source \ "trade" \ "repo" \ "dayCountFraction") }</fpmlext:dayCountFraction>
<fpmlext:spotLeg xsi:type="rtsrep:RepoTransactionLeg">
<fpml:buyerPartyReference href={ get(Try { (source \ "trade" \ "repo" \ "spotLeg" \ "buyerPartyReference").head }, "href") } />
<fpml:sellerPartyReference href={ get(Try { (source \ "trade" \ "repo" \ "spotLeg" \ "sellerPartyReference").head }, "href") } />
<fpml:buyerPartyReference href={ get(source \ "trade" \ "repo" \ "spotLeg" \ "buyerPartyReference", "href") } />
<fpml:sellerPartyReference href={ get(source \ "trade" \ "repo" \ "spotLeg" \ "sellerPartyReference", "href") } />
<fpmlext:settlementDate>
<fpml:adjustableDate>
<fpml:unadjustedDate>{ get(Try { (source \ "trade" \ "repo" \ "spotLeg" \\ "unadjustedDate").head }) }</fpml:unadjustedDate>
<fpml:unadjustedDate>{ get(source \ "trade" \ "repo" \ "spotLeg" \\ "unadjustedDate") }</fpml:unadjustedDate>
</fpml:adjustableDate>
</fpmlext:settlementDate>
<fpml:settlementAmount>
<fpml:currency>{ get(Try { (source \ "trade" \ "repo" \ "spotLeg" \\ "currency").head }) }</fpml:currency>
<fpml:amount>{ get(Try { (source \ "trade" \ "repo" \ "spotLeg" \\ "amount").head }) }</fpml:amount>
<fpml:currency>{ get(source \ "trade" \ "repo" \ "spotLeg" \\ "currency") }</fpml:currency>
<fpml:amount>{ get(source \ "trade" \ "repo" \ "spotLeg" \\ "amount") }</fpml:amount>
</fpml:settlementAmount>
{/*
<fpmlext:collateral xsi:type="rtsrep:CollateralValuation">
......@@ -161,16 +161,16 @@ class RepoTemplate(source: Node) extends TradeTemplate(source) {
*/}
</fpmlext:spotLeg>
<fpmlext:forwardLeg xsi:type="rtsrep:ForwardRepoTransactionLeg">
<fpml:buyerPartyReference href={ get(Try { (source \ "trade" \ "repo" \ "forwardLeg" \ "buyerPartyReference").head }, "href") } />
<fpml:sellerPartyReference href={ get(Try { (source \ "trade" \ "repo" \ "forwardLeg" \ "sellerPartyReference").head }, "href") } />
<fpml:buyerPartyReference href={ get(source \ "trade" \ "repo" \ "forwardLeg" \ "buyerPartyReference", "href") } />
<fpml:sellerPartyReference href={ get(source \ "trade" \ "repo" \ "forwardLeg" \ "sellerPartyReference", "href") } />
<fpmlext:settlementDate>
<fpml:adjustableDate>
<fpml:unadjustedDate>{ get(Try { (source \ "trade" \ "repo" \ "forwardLeg" \ "settlementDate" \\ "unadjustedDate").head }) }</fpml:unadjustedDate>
<fpml:unadjustedDate>{ get(source \ "trade" \ "repo" \ "forwardLeg" \ "settlementDate" \\ "unadjustedDate") }</fpml:unadjustedDate>
</fpml:adjustableDate>
</fpmlext:settlementDate>
<fpml:settlementAmount>
<fpml:currency>{ get(Try { (source \ "trade" \ "repo" \ "forwardLeg" \\ "currency").head }) }</fpml:currency>
<fpml:amount>{ get(Try { (source \ "trade" \ "repo" \ "forwardLeg" \\ "amount").head }) }</fpml:amount>
<fpml:currency>{ get(source \ "trade" \ "repo" \ "forwardLeg" \\ "currency") }</fpml:currency>
<fpml:amount>{ get(source \ "trade" \ "repo" \ "forwardLeg" \\ "amount") }</fpml:amount>
</fpml:settlementAmount>
{/*
<rtsrep:deliveryDate>
......@@ -180,16 +180,16 @@ class RepoTemplate(source: Node) extends TradeTemplate(source) {
</rtsrep:deliveryDate>
*/}
</fpmlext:forwardLeg>
<fpml:equity id={ get(Try { (source \ "trade" \ "repo" \ "equity").head }, "id") }>
<fpml:instrumentId instrumentIdScheme={ get(Try { (source \ "trade" \ "repo" \ "equity" \ "instrumentId").head }, "instrumentIdScheme") }>{ get(Try { (source \ "trade" \ "repo" \ "equity" \ "instrumentId").head }) }</fpml:instrumentId>
<fpml:equity id={ get(source \ "trade" \ "repo" \ "equity", "id") }>
<fpml:instrumentId instrumentIdScheme={ get(source \ "trade" \ "repo" \ "equity" \ "instrumentId", "instrumentIdScheme") }>{ get(source \ "trade" \ "repo" \ "equity" \ "instrumentId") }</fpml:instrumentId>
</fpml:equity>
<fpml:bond id={ get(Try { (source \ "trade" \ "repo" \ "bond").head }, "id") }>
<fpml:instrumentId instrumentIdScheme={ get(Try { (source \ "trade" \ "repo" \ "bond" \ "instrumentId").head }, "instrumentIdScheme") }>{ get(Try { (source \ "trade" \ "repo" \ "bond" \ "instrumentId").head }) }</fpml:instrumentId>
<fpml:bond id={ get(source \ "trade" \ "repo" \ "bond", "id") }>
<fpml:instrumentId instrumentIdScheme={ get(source \ "trade" \ "repo" \ "bond" \ "instrumentId", "instrumentIdScheme") }>{ get(source \ "trade" \ "repo" \ "bond" \ "instrumentId") }</fpml:instrumentId>
</fpml:bond>
</fpmlext:repo>
<fpml:documentation>
<fpml:masterAgreement>
<fpml:masterAgreementId>{ get(Try { (source \ "trade" \ "tradeHeader" \ "partyTradeIdentifier" \ "linkId").head }) }</fpml:masterAgreementId>
<fpml:masterAgreementId>{ get(source \ "trade" \ "tradeHeader" \ "partyTradeIdentifier" \ "linkId") }</fpml:masterAgreementId>
</fpml:masterAgreement>
</fpml:documentation>
</fpml:trade>
......
......@@ -7,13 +7,14 @@ import scala.xml._
abstract class Template(protected[this] val source: Node) {
protected[this] val msgNotFound = "Not found"
protected[this] def get(content: Try[Node], attr: String = null): Text = content match {
case Success(v) if attr != null => v.attribute(attr) match {
case None => Text(msgNotFound)
case o => Text(o.get.text)
}
case Success(v) => Text(v.text)
case _ => Text(msgNotFound)
protected[this] def get(content: NodeSeq, attr: String = null, idx: Int = 0): Text =
Try { content(idx) } match {
case Success(v) if attr != null => v.attribute(attr) match {
case None => Text(msgNotFound)
case o => Text(o.get.text)
}
case Success(v) => Text(v.text)
case _ => Text(msgNotFound)
}
def template: Node
......
......@@ -13,23 +13,23 @@ abstract class TradeTemplate(source: Node) extends Template(source) {
xmlns:fpmlext="http://www.fpml.org/FpML-5/ext" xmlns:rtsrep="http://www.fpml.ru/repository"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" fpmlVersion="5-7">
<fpml:header>
<fpml:messageId>{ get(Try { (source \ "header" \"messageId").head }) }</fpml:messageId>
<fpml:sentBy>{ get(Try { (source \ "header" \ "sentBy").head }) }</fpml:sentBy>
<fpml:sendTo>{ get(Try { (source \ "header" \ "sendTo").head }) }</fpml:sendTo>
<fpml:creationTimestamp>{ get(Try { (source \ "header" \ "creationTimestamp").head }) }</fpml:creationTimestamp>
<fpml:messageId>{ get(source \ "header" \"messageId") }</fpml:messageId>
<fpml:sentBy>{ get(source \ "header" \ "sentBy") }</fpml:sentBy>
<fpml:sendTo>{ get(source \ "header" \ "sendTo") }</fpml:sendTo>
<fpml:creationTimestamp>{ get(source \ "header" \ "creationTimestamp") }</fpml:creationTimestamp>
<fpml:implementationSpecification>
<fpml:version>2</fpml:version>
</fpml:implementationSpecification>
</fpml:header>
<fpml:isCorrection>{ get(Try { (source \ "isCorrection").head }) }</fpml:isCorrection>
<fpml:correlationId correlationIdScheme="">{get(Try { (source \ "correlationId").head }) }</fpml:correlationId>
<fpml:isCorrection>{ get(source \ "isCorrection") }</fpml:isCorrection>
<fpml:correlationId correlationIdScheme="">{ get(source \ "correlationId") }</fpml:correlationId>
{ templateTrade }
{ for (s <- source \ "party") yield {
<fpml:party id={ get(Try { s.head }, "id") }>
<fpml:partyId partyIdScheme={ get(Try { (s \ "partyId").head }, "partyIdScheme") }>{ get(Try { (s \ "partyId").head }) }</fpml:partyId>
<fpml:partyName>{ get(Try { (s \ "partyName").head }) }</fpml:partyName>
<fpml:country>{ get(Try { (s \ "country").head }) }</fpml:country>
<fpml:organizationType>{ get(Try { (s \ "classification").head }) }</fpml:organizationType>
<fpml:party id={ get(s, "id") }>
<fpml:partyId partyIdScheme={ get(s \ "partyId", "partyIdScheme") }>{ get(s \ "partyId") }</fpml:partyId>
<fpml:partyName>{ get(s \ "partyName") }</fpml:partyName>
<fpml:country>{ get(s \ "country") }</fpml:country>
<fpml:organizationType>{ get(s \ "classification") }</fpml:organizationType>
</fpml:party>
}
}
......
......@@ -3,7 +3,6 @@ package templates
import org.scalatest._
import xmlvalid._
import scala.util.Try
import scala.xml.{Elem, Node, XML}
class TemplateSpec extends WordSpec {
......@@ -49,30 +48,30 @@ class TemplateSpec extends WordSpec {
xsi:schemaLocation="https://www.w3schools.com test_valid.xsd">
<nonpublicExecutionReport>
<trade>
<tradeHeader>{ get(Try { (source \\ "tradeHeader").head }) }</tradeHeader>
<tradeHeader>{ get(source \\ "tradeHeader") }</tradeHeader>
<repo>
<title>{ get(Try { (source \\ "repo" \ "title").head }) }</title>
<from href={ get(Try { (source \\ "repo" \ "from").head }, "href") }>
{ get(Try { (source \\ "repo" \ "from").head }) }
<title>{ get(source \\ "repo" \ "title") }</title>
<from href={ get(source \\ "repo" \ "from", "href") }>
{ get(source \\ "repo" \ "from") }
</from>
<to href={ get(Try { (source \\ "repo" \ "to").head }, "href") }>
{ get(Try { (source \\ "repo" \ "notExist").head }) }
<to href={ get(source \\ "repo" \ "to", "href") }>
{ get(source \\ "repo" \ "notExist") }
</to>
</repo>
<notFoundpart>{ get(Try { (source \\ "notFoundpart").head }) }</notFoundpart>
<notFoundpart>{ get(source \\ "notFoundpart") }</notFoundpart>
<notFoundpartNested1>
<noElem>{ get(Try { (source \\ "notFoundpartNested1" \ "noElem").head }) }</noElem>
<noAttr href={ get(Try { (source \\ "notFoundpartNested1" \ "noAttr").head }, "href") } />
<noAttrAndElem href={ get(Try { (source \\ "notFoundpartNested1" \ "noAttrAndElem").head }, "href") }>
{ get(Try { (source \\ "notFoundpartNested1" \ "noAttrAndElem").head }) }
<noElem>{ get(source \\ "notFoundpartNested1" \ "noElem") }</noElem>
<noAttr href={ get(source \\ "notFoundpartNested1" \ "noAttr", "href") } />
<noAttrAndElem href={ get(source \\ "notFoundpartNested1" \ "noAttrAndElem", "href") }>
{ get(source \\ "notFoundpartNested1" \ "noAttrAndElem") }
</noAttrAndElem>
</notFoundpartNested1>
<notFoundpartNested2>
<notFoundPartNested21>
<noElem>{ get(Try { (source \\ "notFoundpartNested21" \ "noElem").head }) }</noElem>
<noAttr href={ get(Try { (source \\ "notFoundpartNested21" \ "noAttr").head }, "href") } />
<noAttrAndElem href={ get(Try { (source \\ "notFoundpartNested21" \ "noAttrAndElem").head }, "href") }>
{ get(Try { (source \\ "notFoundpartNested21" \ "noAttrAndElem").head }) }
<noElem>{ get(source \\ "notFoundpartNested21" \ "noElem") }</noElem>
<noAttr href={ get(source \\ "notFoundpartNested21" \ "noAttr", "href") } />
<noAttrAndElem href={ get(source \\ "notFoundpartNested21" \ "noAttrAndElem", "href") }>
{ get(source \\ "notFoundpartNested21" \ "noAttrAndElem") }
</noAttrAndElem>
</notFoundPartNested21>
</notFoundpartNested2>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment