Java 如何在不使用显式架构文件的情况下在 jaxb 中解组并享受架构验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2603778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:45:12  来源:igfitidea点击:

how can i unmarshall in jaxb and enjoy the schema validation without using an explicit schema file

javaxsdjaxb

提问by ekeren

I am using jaxb for my application configurations

我在我的应用程序配置中使用 jaxb

I feel like I am doing something really crooked and I am looking for a way to not need an actual file or this transaction.

我觉得我在做一些非常错误的事情,我正在寻找一种不需要实际文件或此交易的方法。

As you can see in code I:

正如您在代码中看到的,我:

1.create a schema into a file from my JaxbContext (from my class annotation actually) 2.set this schema file in order to allow true validation when I unmarshal

1.从我的 JaxbContext(实际上来自我的类​​注释)创建一个模式到一个文件中 2.设置这个模式文件以便在我解组时允许真正的验证

JAXBContext context = JAXBContext.newInstance(clazz);
Schema mySchema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile);
jaxbContext.generateSchema(new MySchemaOutputResolver()); // ultimately creates schemaFile   
Unmarshaller u = m_context.createUnmarshaller();
u.setSchema(mySchema);
u.unmarshal(...);

do any of you know how I can validate jaxb without needing to create a schema file that sits in my computer?

你们中有人知道如何验证 jaxb 而无需创建位于我的计算机中的架构文件吗?

Do I need to create a schema for validation, it looks redundant when I get it by JaxbContect.generateSchema ?

我是否需要创建一个用于验证的模式,当我通过 JaxbContect.generateSchema 获取它时它看起来是多余的?

How do you do this?

你怎么做到这一点?

采纳答案by seanf

Regarding ekeren's solution above, it's not a good idea to use PipedOutputStream/PipedInputStream in a single thread, lest you overflow the buffer and cause a deadlock. ByteArrayOutputStream/ByteArrayInputStream works, but if your JAXB classes generate multiple schemas (in different namespaces) you need multiple StreamSources.

关于上面ekeren的解决方案,在单个线程中使用PipedOutputStream/PipedInputStream不是一个好主意,以免溢出缓冲区并导致死锁。ByteArrayOutputStream/ByteArrayInputStream 有效,但如果您的 JAXB 类生成多个模式(在不同的命名空间中),您需要多个 StreamSource。

I ended up with this:

我结束了这个:

JAXBContext jc = JAXBContext.newInstance(Something.class);
final List<ByteArrayOutputStream> outs = new ArrayList<ByteArrayOutputStream>();
jc.generateSchema(new SchemaOutputResolver(){
    @Override
    public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        outs.add(out);
        StreamResult streamResult = new StreamResult(out);
        streamResult.setSystemId("");
        return streamResult;
    }});
StreamSource[] sources = new StreamSource[outs.size()];
for (int i=0; i<outs.size(); i++) {
    ByteArrayOutputStream out = outs.get(i);
    // to examine schema: System.out.append(new String(out.toByteArray()));
    sources[i] = new StreamSource(new ByteArrayInputStream(out.toByteArray()),"");
}
SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
m.setSchema(sf.newSchema(sources));
m.marshal(docs, new DefaultHandler());  // performs the schema validation

回答by Jason Day

I believe you just need to set a ValidationEventHandleron your unmarshaller. Something like this:

我相信你只需要ValidationEventHandler在你的解组器上设置一个。像这样的东西:

public class JAXBValidator extends ValidationEventCollector {
    @Override
    public boolean handleEvent(ValidationEvent event) {
        if (event.getSeverity() == event.ERROR ||
            event.getSeverity() == event.FATAL_ERROR)
        {
            ValidationEventLocator locator = event.getLocator();
            // change RuntimeException to something more appropriate
            throw new RuntimeException("XML Validation Exception:  " +
                event.getMessage() + " at row: " + locator.getLineNumber() +
                " column: " + locator.getColumnNumber());
        }

        return true;
    }
}

And in your code:

在你的代码中:

Unmarshaller u = m_context.createUnmarshaller();
u.setEventHandler(new JAXBValidator());
u.unmarshal(...);

回答by dolbysurnd

I had the exact issue and found a solution in the Apache Axis 2 source code:

我遇到了确切的问题,并在 Apache Axis 2 源代码中找到了解决方案:

protected List<DOMResult> generateJaxbSchemas(JAXBContext context) throws IOException {
    final List<DOMResult> results = new ArrayList<DOMResult>();
    context.generateSchema(new SchemaOutputResolver() {
        @Override
        public Result createOutput(String ns, String file) throws IOException {
            DOMResult result = new DOMResult();
            result.setSystemId(file);
            results.add(result);
            return result;
        }
     });
    return results;
}

and after you've acquired your list of DOMResults that represent the schemas, you will need to transform them into DOMSource objects before you can feed them into a schema generator. This second step might look something like this:

在获得代表模式的 DOMResults 列表后,您需要将它们转换为 DOMSource 对象,然后才能将它们提供给模式生成器。第二步可能看起来像这样:

Unmarshaller u = myJAXBContext.createUnmarshaller();
List<DOMSource> dsList = new ArrayList<DOMSource>();   
for(DOMResult domresult : myDomList){
    dsList.add(new DOMSource(domresult.getNode()));
}
String schemaLang = "http://www.w3.org/2001/XMLSchema";
SchemaFactory sFactory = SchemaFactory.newInstance(schemaLang);
Schema schema = sFactory.newSchema((DOMSource[]) dsList.toArray(new DOMSource[0]));
u.setSchema(schema);            

回答by Alexander

If you use maven using jaxb2-maven-plugin can help you. It generates schemas in generate-resources phase.

如果您使用 maven 使用 jaxb2-maven-plugin 可以帮助您。它在生成资源阶段生成模式。