1
を表す文字列からJavaクラスを作成します。は、プログラム私はXSDファイルからクラスを作成するために管理XSDスキーマ
私は以下の私のコードを貼り付け:
- を
main_working
ファイルからクラスを生成し、main_not_working
が例外をスローしながら - 正常に動作します。
これはコードです:
import java.io.File;
import java.io.StringReader;
import org.xml.sax.InputSource;
import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
public class XsdMain {
private static File out = new File("out");
private static String xsd =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<xs:schema version=\"1.0\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">" +
"<xs:element name=\"author\" type=\"author\"/>" +
"<xs:element name=\"book\" type=\"book\"/>" +
"<xs:complexType name=\"author\">" +
"<xs:sequence>" +
"<xs:element name=\"firstName\" type=\"xs:string\" minOccurs=\"0\"/><xs:element name=\"lastName\" type=\"xs:string\" minOccurs=\"0\"/>" +
"</xs:sequence>" +
"</xs:complexType>" +
"<xs:complexType name=\"book\">" +
"<xs:sequence>" +
"<xs:element ref=\"author\" minOccurs=\"0\"/>" +
"<xs:element name=\"pages\" type=\"xs:int\"/>" +
"<xs:element name=\"publicationDate\" type=\"xs:dateTime\" minOccurs=\"0\"/>" +
"<xs:element name=\"title\" type=\"xs:string\" minOccurs=\"0\"/>" +
"</xs:sequence>" +
"</xs:complexType>" +
"</xs:schema>";
public static void main(String[] args) throws Exception {
main_working(); // this works
main_not_working(); // this doesn't work
}
public static void main_working() throws Exception {
// Setup schema compiler
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.forcePackageName("com.xyz.schema");
// Setup SAX InputSource
File schemaFile = new File("in/test.xsd");
InputSource is = new InputSource(schemaFile.toURI().toString());
// Parse & build
sc.parseSchema(is);
S2JJAXBModel model = sc.bind();
JCodeModel jCodeModel = model.generateCode(null, null);
jCodeModel.build(out);
}
public static void main_not_working() throws Exception {
// Setup schema compiler
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.forcePackageName("com.xyz.schema");
// Setup SAX InputSource
InputSource is = new InputSource(new StringReader(xsd));
// Parse & build
sc.parseSchema(is);
S2JJAXBModel model = sc.bind();
JCodeModel jCodeModel = model.generateCode(null, null);
jCodeModel.build(out);
}
}
そしてスローexeptionは、このいずれかになります。
Exception in thread "main" java.lang.NullPointerException
at java.net.URI$Parser.parse(URI.java:3035)
at java.net.URI.<init>(URI.java:607)
at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.checkAbsoluteness(SchemaCompilerImpl.java:163)
at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.parseSchema(SchemaCompilerImpl.java:130)
at com.madx.XsdMain.main_not_working(XsdMain.java:71)
at com.madx.XsdMain.main(XsdMain.java:42)
'main_not_working'の中の' InputSource'初期化の直後に 'is.setSystemId(" foo ");という行を追加しました。 – madx