1

オブジェクト階層を表現するために使用される nodeBuilder を想像してください。

class TestBuilder {

static main(args) {

        def builder = new NodeBuilder()
    def ulcDate = new Date(107,0,1)
    def invoices = builder.invoices{
        invoice(date: ulcDate){

            item(count:5){
                product(name:'ULC', dollar:1499)
            }
            item(count:1){
                product(name:'Visual Editor', dollar:499)
            }
        }
        invoice(date: new Date(106,1,2)){
            item(count:4) {
                product(name:'Visual Editor', dollar:499)
            }
        }
    }

}




   class Invoice {
        List items
        Date date
     }

   class LineItem {
        Product product
        int count
        int total() 
        {
            return product.dollar * count
        }
    }

   class Product {
        String name
        def dollar
    }

NodeBuilder によって生成された請求書オブジェクトを、請求書オブジェクトからすべてが構成された Invoice クラスのインスタンスに実際に変換するにはどうすればよいですか? そのためにはおそらく GPath を使用する必要がありますが (?)、そのコードはどのようになりますか?

私がそうする必要がある理由は、他のクラスの他のメソッドがさらに動作するために Invoice クラスのインスタンスを必要とし、私が推測する NodeBuilder 出力を受け入れないからです。

4

2 に答える 2

2

最も簡単な方法は、特定のオブジェクト セットに対してノード トラバーサルを実行することだと思います。

例:

import groovy.util.*

////////////
// build Node tree as asked in original post

def builder = new NodeBuilder()
def ulcDate = new Date(107,0,1)

def invoices = builder.invoices {
    invoice(date: ulcDate) {
        item(count:5) {
            product(name:'ULC', dollar:1499)
        }
        item(count:1) {
            product(name:'Visual Editor', dollar:499)
        }
    }
    invoice(date: new Date(106,1,2)){
        item(count:4) {
            product(name:'Visual Editor', dollar:499)
        }
    }
}

////////////
// define objects. It is easy to have these in Java

class Invoice {
    def date
    def items = []
}

class Item {
    def count
    def product
}

class Product {
    def name
    def dollar
}

////////////
// convert from nodes to objects

def invoiceNodeList = invoices.get("invoice")
def invoiceList = []

invoiceNodeList.each { def invoiceNode ->
    def date = invoiceNode.attribute("date")
    Invoice invoice = new Invoice(date: date)

    invoiceNode.children().each { def itemNode ->
        def count = itemNode.attribute("count")
        Product product = null
        // assume only one Product per Item, but we'll
        // use children() for simplicity
        itemNode.children().each { def productNode ->
            def name = productNode.attribute("name")
            def dollar = productNode.attribute("dollar")
            product = new Product(name: name, dollar: dollar)
        }
        Item item = new Item(count: count, product: product)
        invoice.items << item
    } 
    invoiceList << invoice
}

////////////
// print out objects 

invoiceList.each { Invoice invoice ->
    println "--------"
    println invoice.date
    invoice.items.each { Item item ->
        println item.count
        println item.product.name
        println item.product.dollar
    }
}
于 2013-05-28T04:17:03.527 に答える
1

Giving a slight tweak to your base classes:

class Invoice {
  List lineItems = []
  Date date

  String toString() {
    String ret = "Invoice $date $lineItems"
  }
}

class LineItem {
  Product product
  int count

  int total() {
    product.dollar * count
  }

  String toString() {
    "$product * $count"
  }
}

class Product {
  String name
  int dollar

  String toString() {
    "$name ($dollar)"
  }
}

Means you can easily use ObjectGraphBuilder to build your list:

List invoices = new ObjectGraphBuilder(classLoader: getClass().classLoader).with { 
  [
    invoice( date: new Date( 107, 0, 1 ) ) {
      lineItem( count: 5 ) {
        product( name: 'ULC', dollar: 1499 )
      }
      lineItem(count:1){
        product(name:'Visual Editor', dollar:499)
      }
    },
    invoice(date: new Date(106,1,2)){
      lineItem(count:4) {
        product(name:'Visual Editor', dollar:499)
      }
    }
  ]
}

If it isn't possible to tweak the base classes, you can customise how properties are looked up by setting the resolvers before building your graph

于 2013-05-28T08:26:56.797 に答える