English 中文(简体)
• 如何控制日本宇宙航空研究开发局在校内制作订单/序列?
原标题:How to control JAXB in-memory schema generation ordering/sequence?

我拿着3×秒的文档,它们相互依赖,以形成我的要素定义。 每个xsd文档都有自己的名称空间。 当我利用日本宇宙航空研究开发机构Xjc制作我的班时,我收到3份相应的包裹。 迄今情况良好。

我的问题是,在我想与无记名者一道进行图谋验证的时候。 为了避免不得不读到xsd文档中,我从有关类别中拿到没有经过翻译的飞机。 然而,由于这一类别取决于其他2个包裹的物体,因此除非我具体说明所有3个包裹,否则无法生成该表。 这已经是一个非常实际的解决办法,因为它要求我提前知道目标等级/依赖性树,并相应地具体说明一揽子清单。

我的最大问题在于,我试图利用SchemaFactory(SchemaFactory.newSchema(Source[])从3种生成的chem体中产生新的图象。 很明显,向化工工厂提供化chem的顺序对于解决依赖性至关重要。 如果阵列中的第一组图谋取决于从阵列最后一项要素中得出的一个类型定义,我就会发现一个定点错误:

org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name  ns1:InCalculationDataType  to a(n)  type definition  component.

如果我修改命令,把第3条图示放在首位,则该命令是毫无错误的。

This makes it nearly impossible to write a method fairly generic, but rather have to code for each XSD case individually.

我能做些什么来缓解这一问题? 是否有办法迫使SchemaFactory首先阅读所有材料,然后才发现错误? 我知道你可以产生一名ErrorHandler,然而,JavaDocs指出,如果它犯了法文本错误,任何进一步处理都是不可靠的。

http://www.ohchr.org。

就我自己的思想和平而言,我试图制造一个错误的手稿,忽略了非致命的错误(假装),但产生的图象是不可靠的,无法适当证实Xml的错误。 因此,这对我没有任何价值。

END EDIT

Any suggestions or thoughts would be appreciated.

感谢!

Eric

最佳回答

经过大量搜查,我最后找到了答案。 希望这将有助于其他人。 在这个问题上,在StackOverflow已经存在其他线索,但没有了解适当的关键词,我没有找到答案。

解决办法是利用LSResourceResolver进行化工工厂。 即:

schemaFactory.setResourceResolver(new LSResourceResolver(){})

在LSResourceResolver()负责退还科学、技术和企业发展司要求的包括/进口资源。

Searching for LSResourceResolver in SO found a few useful threads: https://stackoverflow.com/a/3830649/827480, https://stackoverflow.com/a/2342859/827480

我将努力在稍后时间提出我自己的解决办法,但我将密切关注上述两条联系(地雷使用强硬,而不是流......)中已经提出的建议。

http://www.ohchr.org。

正如承诺的那样,我最后用的是:

        // get the schemas used by this class
        final Map<String, String> schemas = new HashMap<String,String>();
        schemas.putAll(generateSchemas(jc));

        List<StreamSource> sources = new ArrayList<StreamSource>();
        for( String schema : schemas.values() )
            sources.add( new StreamSource( new ByteArrayInputStream(schema.getBytes())));

        SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
        sf.setResourceResolver(new LSResourceResolver() {
            @Override
            public LSInput resolveResource(String type, final String namespaceURI, String publicId, String systemId, String baseURI){
                logger.debug( "Need to resolve Resource: " + namespaceURI );
                return new LSInput(){
                    @Override
                    public String getStringData() {
                        // return the schema if found
                        if( schemas.containsKey(namespaceURI)){
                            if( logger.isTraceEnabled())
                                logger.trace("resourceResolver: Resolving schema for namespace: " + namespaceURI + schemas.get(namespaceURI) );
                            return schemas.get(namespaceURI);
                        }
                        else
                            return null;
                    }
                    @Override
                    public Reader getCharacterStream() {
                        return null;
                    }
                    @Override
                    public void setCharacterStream(Reader paramReader) {
                    }
                    @Override
                    public InputStream getByteStream() {
                        return null;
                    }
                    @Override
                    public void setByteStream(InputStream paramInputStream) {
                    }
                    @Override
                    public void setStringData(String paramString) {
                    }
                    @Override
                    public String getSystemId() {
                        return null;
                    }
                    @Override
                    public void setSystemId(String paramString) {
                    }
                    @Override
                    public String getPublicId() {
                        return null;
                    }
                    @Override
                    public void setPublicId(String paramString) {
                    }
                    @Override
                    public String getBaseURI() {
                        return null;
                    }
                    @Override
                    public void setBaseURI(String paramString) {
                    }
                    @Override
                    public String getEncoding() {
                        return null;
                    }
                    @Override
                    public void setEncoding(String paramString) {
                    }
                    @Override
                    public boolean getCertifiedText() {
                        return false;
                    }
                    @Override
                    public void setCertifiedText(boolean paramBoolean) {
                    }
                };
            }
        });

        // validate the schema
        u.setSchema(sf.newSchema(sources.toArray(new StreamSource[]{})));

and method generateSchemas(jc):

private Map<String, String> generateSchemas (JAXBContext jaxbContext) throws JAXBException{
    // generate the schemas
    final Map<String, ByteArrayOutputStream> schemaStreams = new LinkedHashMap<String,ByteArrayOutputStream>();

    try {
        jaxbContext.generateSchema(new SchemaOutputResolver(){
            @Override
            public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                logger.debug( "GenerateSchemas: adding namespace: " + namespaceUri);
                schemaStreams.put(namespaceUri, out);
                StreamResult streamResult = new StreamResult(out);
                streamResult.setSystemId("");
                return streamResult;
            }});
    } catch (IOException e) {
        // no IO being performed.  Can safely ignore any IO exception.
    }

    // convert to a list of string
    Map<String,String> schemas = new LinkedHashMap<String,String>();
    for( Map.Entry<String, ByteArrayOutputStream> entry : schemaStreams.entrySet() ){
        String schema = entry.getValue().toString();
        String namespace = entry.getKey();
        schemas.put(namespace, schema);
    }

    // done
    return schemas;
}

<>ENDIT

我希望这能够帮助其他人。

感谢

Eric

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签