Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Created March 5, 2012 03:25
Show Gist options
  • Save j5ik2o/1976303 to your computer and use it in GitHub Desktop.
Save j5ik2o/1976303 to your computer and use it in GitHub Desktop.
複数の名前空間に定義されるスキーマを別パッケージのオブジェクトとして生成したい

やりたいこと

複数の名前空間に定義されるスキーマを別パッケージのオブジェクトとして生成したい。sbtで。

build.sbt

packageName in scalaxb in Compile := "api.model"

sourceGenerators in Compile <+= scalaxb in Compile

xsd

XMLの仕様は変更不可能な前提。

src/main/xsd/api1.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">
    <xs:element name="api_response">
        <xs:element name="code" type="xs:string"/>
        <!--- ... -->
    </xs:xs:element>
</xs:schema>

src/main/xsd/api2.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">
    <xs:element name="api_response">
        <xs:element name="name" type="xs:string"/>
        <!--- ... -->
    </xs:xs:element>
</xs:schema>

generated sources

次のようなソースコードを生成したい。

target/scala-2.9.1/src_managed/main/api/model/api1.scala

package api.model.api1

case class Api_response(code: String)

target/scala-2.9.1/src_managed/main/api/model/api2.scala

package api.model.api2

case class Api_response(name: String)

どうすればいいか

scalaxbを複数使う場合の例は記述されていたので、これを使うとよいのかもしれない。

試しにsbt-0.11.2でやってみたが、

not found: value buildSettings
not found: value xsdSource
not found: value packageName

とか発生した。

http://scalaxb.org/ja/sbt-scalaxb

import sbt._
import Keys._

object MyBuild extends Build {

    val Xsd = config("xsd") extend(Compile)
    val Wsdl = config("wsdl") extend(Compile)

    lazy val appSettings = buildSettings ++
        inConfig(Xsd)(baseScalaxbSettings ++ inTask(scalaxb)(customScalaxbSettings("xmlschema"))) ++
        inConfig(Wsdl)(baseScalaxbSettings ++ inTask(scalaxb)(customScalaxbSettings("wsdl11")))

    def customScalaxbSettings(base: String): Seq[Project.Setting[_]] = Seq(
        sources <<= xsdSource map { xsd => Seq(xsd / (base + ".xsd")) },
        packageName := base
    )

}
@eed3si9n
Copy link

eed3si9n commented Mar 6, 2012

パッケージ名の指定を上記の方法でやる場合は名前空間がついてることが前提ですね。
名前空間無しの場合はやっぱり configuration を作って複数走らせるしかないと思います。
それに関しては、今から解説の記事を書いて scalaxb.org に載せる予定です。

protocolPackageName はオプショナルですが、それを使って普段使ってないパッケージに implicit が定義されてる場合は
import foo.binding._ でまとめて import しちゃうのが普通だと思います。

@eed3si9n
Copy link

eed3si9n commented Mar 6, 2012

一応ここにもリンク貼っておきます: http://scalaxb.org/ja/multiple-configs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment