2

以下の回答に基づいて、この投稿を最新の例で更新します。


MOXY を使用して、GML 2.1.2 スキーマ (xlink をインポートする) を使用する単純なスキーマを使用して DynamicJAXBContext を作成しようとしています。moxy で GML スキーマ (または GML によってインポートされた xlink スキーマ) のインポートに問題があるようです。DynamicJAXBContextFactory に jaxb バインディング ファイルを提供しようとしましたが、うまくいきません。

私は eclipselink 2.4.2 ( 2/20 nightly ) と jaxb-xjc 2.1.12 を使用しています。生成される例外は次のとおりです。

例外 [EclipseLink-0] (Eclipse Persistence Services - 2.4.2.v20130220-bf58d47): org.eclipse.persistence.exceptions.IntegrityException Descriptor

例外:

例外 [EclipseLink-217] (Eclipse Persistence Services - 2.4.2.v20130220-bf58d47): org.eclipse.persistence.exceptions.DescriptorException 例外の説明: XMLDirectMapping/XMLCompositeDirectCollectionMapping の無効な XPath。XPath には、属性の @ 記号を含めるか、テキスト ノードの /text() で終了する必要があります。例: 「@name」または「name/text()」 マッピング: org.eclipse.persistence.oxm.mappings.XMLDirectMapping[title-->ns2:title] 記述子: XMLDescriptor(org.w3._1999.xlink.Extended --> [])

例外 [EclipseLink-217] (Eclipse Persistence Services - 2.4.2.v20130220-bf58d47): org.eclipse.persistence.exceptions.DescriptorException 例外の説明: XMLDirectMapping/XMLCompositeDirectCollectionMapping の無効な XPath。XPath には、属性の @ 記号を含めるか、テキスト ノードの /text() で終了する必要があります。例: 「@name」または「name/text()」 マッピング: org.eclipse.persistence.oxm.mappings.XMLDirectMapping[xlinkTitle1-->ns2:title] 記述子: XMLDescriptor(org.w3._1999.xlink.LocatorType --> [データベーステーブル(ns2:ロケータ)])

例外 [EclipseLink-217] (Eclipse Persistence Services - 2.4.2.v20130220-bf58d47): org.eclipse.persistence.exceptions.DescriptorException 例外の説明: XMLDirectMapping/XMLCompositeDirectCollectionMapping の無効な XPath。XPath には、属性の @ 記号を含めるか、テキスト ノードの /text() で終了する必要があります。例: 「@name」または「name/text()」 マッピング: org.eclipse.persistence.oxm.mappings.XMLDirectMapping[xlinkTitle2-->ns2:title] 記述子: XMLDescriptor(org.w3._1999.xlink.ArcType --> [データベーステーブル(ns2:arc)])

ランタイム例外:

org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:638) で org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:574) で org.eclipse.persistence.internal org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:777) の .sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:533) org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl) .java:735) org.eclipse.persistence.oxm.XMLContext$XMLContextState.(XMLContext.java:940) org.eclipse.persistence.oxm.XMLContext$XMLContextState.(XMLContext.java:915) org.eclipse. persistence.oxm.XMLContext.(XMLContext.java:194) org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext$SchemaContextInput.createContextState(DynamicJAXBContext.java:332) at org.eclipse.persistence.jaxb.JAXBContext.(JAXBContext.java:169) at org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext.( org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory.createContextFromXSD(DynamicJAXBContextFactory.java:309) の DynamicJAXBContext.java:70) ogc.catalog.moxy.TestMoxy.testMoxy(TestMoxy.java:45) のjava:309) ogc.catalog.moxy.TestMoxy.testMoxy(TestMoxy.java:45) でjava:309) ogc.catalog.moxy.TestMoxy.testMoxy(TestMoxy.java:45) で

これが私のテストmoxyクラスです:

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
import org.junit.Test;

public class TestMoxy {

    private static final String SCHEMA = "example-feature.xsd";

    @Test
    public void testMoxy() throws JAXBException {

        System.setProperty("javax.xml.bind.context.factory",
                "org.eclipse.persistence.jaxb.DynamicJAXBContextFactory");

        System.setProperty("com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.noCorrectnessCheck",
                "true");

        InputStream schema = this.getClass().getClassLoader().getResourceAsStream(SCHEMA);

        Source schemaSource = new StreamSource(schema);
        schemaSource.setSystemId(SCHEMA);

        InputStream xjbStream = this.getClass().getClassLoader()
                .getResourceAsStream("xlink-bindings.xjb");
        Source xjbSource = new StreamSource(xjbStream);
        xjbSource.setSystemId(SCHEMA);

        Map<String, Object> props = new HashMap<String, Object>();
        props.put(DynamicJAXBContextFactory.EXTERNAL_BINDINGS_KEY, xjbSource);

        try {
            DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(
                    schemaSource, null, null, props);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

example-feature.xsd は次のとおりです。

<?xml version='1.0' encoding='UTF-8'?>
<xsd:schema elementFormDefault='qualified' targetNamespace='http://example.org' 
    xmlns:example='http://example.org' 
    xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
    xmlns:gml='http://www.opengis.net/gml'>
    <xsd:import namespace='http://www.opengis.net/gml' schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
    <xsd:complexType name='example_featureType'>
        <xsd:complexContent>
            <xsd:extension base='gml:AbstractFeatureType'>
                <xsd:sequence>
                    <xsd:element maxOccurs='1' minOccurs='1' name='id' nillable='false' type='xsd:long'/>
                    <xsd:element maxOccurs='1' minOccurs='1' name='version' nillable='false' type='xsd:long'/>
                    <xsd:element maxOccurs='1' minOccurs='0' name='access_date' nillable='true' type='xsd:dateTime'/>
                    <xsd:element maxOccurs='1' minOccurs='0' name='ground_geom' nillable='true' type='gml:PolygonPropertyType'/>
                </xsd:sequence>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
    <xsd:element name='exampleFeature' substitutionGroup='gml:_Feature' type='example:example_featureType'/>
</xsd:schema>

そして xlink-bindings.xjb:

<jaxb:bindings version="1.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc">
    <jaxb:bindings schemaLocation="http://www.w3.org/1999/xlink.xsd">
        <jaxb:bindings node="//xs:attributeGroup[@name='locatorAttrs']//xs:attribute[@ref='xlink:title']">
            <jaxb:property name="xlink:title1" />
        </jaxb:bindings>
        <jaxb:bindings node="//xs:attributeGroup[@name='arcAttrs']//xs:attribute[@ref='xlink:title']">
            <jaxb:property name="xlink:title2" />
        </jaxb:bindings>
    </jaxb:bindings>

    <jaxb:bindings schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"
        node="/xs:schema">
        <jaxb:globalBindings fixedAttributeAsConstantProperty="false"
            typesafeEnumBase="xs:string" typesafeEnumMemberName="generateName" generateIsSetMethod="true"
            generateMixedExtensions="true">
            <xjc:noValidator />
            <xjc:noValidatingUnmarshaller />
        </jaxb:globalBindings>

        <jaxb:schemaBindings>
            <jaxb:package name="ogc.schema.opengis.gml.v_2_1_2" />
            <jaxb:nameXmlTransform>
                <jaxb:elementName suffix="Element" />
            </jaxb:nameXmlTransform>
        </jaxb:schemaBindings>
        <jaxb:bindings node="xs:element[@name='_geometryProperty']">
            <jaxb:class name="AbstractGeometryProperty" />
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

xjc が私のために働いていることに注意してください: xjc -extension example-feature.xsd -b xlink-bindings.xjb

xjc バージョン "JDK 6 の JAXB 2.1.10" *Java バージョン "1.6.0_38"*

4

2 に答える 2

3

このバインディング ファイルを使用して、xjc v2.1.10 コンパイラを動作させることができました。

<jaxb:bindings version="1.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
jaxb:extensionBindingPrefixes="xjc">

<jaxb:bindings schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"
    node="/xs:schema">
    <jaxb:schemaBindings>
        <jaxb:package name="ogc.schema.opengis.gml.v_2_1_2" />
        <jaxb:nameXmlTransform>
            <jaxb:elementName suffix="Element" />
        </jaxb:nameXmlTransform>
    </jaxb:schemaBindings>
    <jaxb:bindings node="xs:element[@name='_geometryProperty']">
        <jaxb:class name="AbstractGeometryProperty" />
    </jaxb:bindings>
</jaxb:bindings>

<jaxb:bindings schemaLocation="http://www.w3.org/1999/xlink.xsd">
    <jaxb:bindings
        node="//xs:attributeGroup[@name='locatorAttrs']//xs:attribute[@ref='xlink:title']">
        <jaxb:property name="xlink:title1" />
    </jaxb:bindings>
    <jaxb:bindings
        node="//xs:attributeGroup[@name='arcAttrs']//xs:attribute[@ref='xlink:title']">
        <jaxb:property name="xlink:title2" />
    </jaxb:bindings>
</jaxb:bindings>

しかし、Java テストを実行しようとすると、次のようになります。

java.lang.NullPointerException
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.processXmlSeeAlso(AnnotationsProcessor.java:1201)
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.preBuildTypeInfo(AnnotationsProcessor.java:555)
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.processClassesAndProperties(AnnotationsProcessor.java:267)
at org.eclipse.persistence.jaxb.compiler.Generator.<init>(Generator.java:104)
at org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext$SchemaContextInput.createContextState(DynamicJAXBContext.java:316)
at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:157)
at org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext.<init>(DynamicJAXBContext.java:70)
at org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory.createContextFromXSD(DynamicJAXBContextFactory.java:302)
at ogc.common.schema.test.TestMoxy.testEntityResolver(TestMoxy.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

これが役立つかどうかはわかりません。

-キース

于 2013-02-14T21:36:16.080 に答える
1

私はあなたの問題を見てきましたが、それを機能させることもできませんでした。XJC スキーマ コンパイラを使用してみましたが、スキーマの競合にも問題があり、EclipseLink は動的 JAXB コンテキストの作成を XJC に依存しています。

まず、Eclipse でテスト ケースを実行すると、少し異なるエラーが発生しました。

com.sun.istack.SAXParseException2: Property "Title" is already defined. Use &lt;jaxb:property> to resolve this conflict.

しかし、あなたのバインディング ファイルでは、既にタイトル (小文字) に jaxb:property を使用していることがわかります。また、関連する XSD のいずれにもタイトル (大文字) が見つかりませんでした。

次に、XJC を実行すると、次の結果が得られました。

C:\...\src\omar>xjc example-feature.xsd -b xlink-bindings.xjb
parsing a schema...
compiling a schema...
[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 28 of http://schemas.opengis.net/gml/2.1.2/feature.xsd

[ERROR] (Related to above error) This is the other declaration.
  line 29 of http://schemas.opengis.net/gml/2.1.2/feature.xsd

Failed to produce code.

しかし、feature.xsd の 29 行目は次のようになっているため、これも奇妙です。

<element name="geometryProperty" type="gml:GeometryAssociationType"/>

しかし、geometryProperty との競合は見つかりません。

XJC を機能させるためのバインディング ファイルを取得するには、これで十分な情報だと思います。その時点で、DynamicJAXBContext をブートストラップできるはずです。xjc 実行可能ファイルは、JAVA_HOME\bin ディレクトリにあります。

  • リック
于 2013-02-14T20:45:44.860 に答える