首页 \ 问答 \ 我应该在哪里放置一个XSD文件用于JAXB代码生成和XML验证(Where should I place an XSD file to use for JAXB code gen and for XML validation)

我应该在哪里放置一个XSD文件用于JAXB代码生成和XML验证(Where should I place an XSD file to use for JAXB code gen and for XML validation)

我在我的Java项目中创建了一个XSD文件,用于定义用户可编辑的输入文件(例如,假设XSD名为userinput.xsd,用户可编辑文件为userinput.xml)。 当程序运行时,它使用JAXB来验证用户是否在XML文件中没有出现任何错误,因为它将文件解组到DOM中。

我使用Maven标准目录布局构建了我的项目,并使用xjc生成了JAXB对象工厂和其他类,将它们放在名为/ src / main / java / my / name / space / generated / userinput的目录中(以匹配名称)的XSD)。

我已将XSD文件放在/ src / main / resources中。

当我打包我的jar文件时,该文件位于jar的根目录中,我可以在Java代码中引用它,如下所示(特别注意第4行中提到的资源):

JAXBContext jaxbContext = JAXBContext.newInstance("my.name.space.generated.userinput");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(getClass().getResource("/userinput.xsd"));
unmarshaller.setSchema(schema);
JAXBElement<?> userinputType = (JAXBElement<?>) unmarshaller.unmarshal(new FileInputStream("userinput.xml"));

这可行,但它似乎不正确,因为它意味着如果我想扩展到多个输入文件,我可能最终在我的源代码管理的资源目录和jar文件的根目录中有许多.xsd文件。

此外,当我从IDE而不是从打包的jar运行程序时,我必须将第四个语句更改为:

Schema schema = schemaFactory.newSchema(getClass().getResource("src/main/resources/userinput.xsd"));

我应该将XSD文件(a)放在我的源代码控制(即Maven结构)和(b)我的jar文件中? [注意:通过我的IDE运行xjc将它放在... / generated / userinput目录中,但Maven在打包时会忽略它。

我正在寻找一个答案,表明存在某种共同的方法,所以如果可能的话,我想要一个参考。 如果这是当前未经指定的选择留给开发人员,那么请说明(最好引用),因为我理解stackoverflow不是收集意见的地方。


I have created an XSD file in my Java project that defines a user-editable input file (for illustration, let's say the XSD is called userinput.xsd and the user-editable file is userinput.xml). When the program runs, it uses JAXB to validate that the user has not made any mistakes in the XML file as it unmarshalls the file into a DOM.

I have structured my project using the Maven Standard Directory Layout and generated the JAXB object factory and other classes using xjc, placing them in a directory called /src/main/java/my/name/space/generated/userinput (to match the name of the XSD).

I have placed the XSD file in /src/main/resources.

When I package my jar file, the file is in the root of the jar and I can refer to it in the Java code as follows (note in particular the resource mentioned in line 4):

JAXBContext jaxbContext = JAXBContext.newInstance("my.name.space.generated.userinput");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(getClass().getResource("/userinput.xsd"));
unmarshaller.setSchema(schema);
JAXBElement<?> userinputType = (JAXBElement<?>) unmarshaller.unmarshal(new FileInputStream("userinput.xml"));

This works but it does not seem right as it means if I want to scale to multiple input files, I could end up with many .xsd files both in the resources directory in my source control and in the root of the jar file.

Also when I run the program from my IDE rather than from the packaged jar, I have to change the fourth statement to:

Schema schema = schemaFactory.newSchema(getClass().getResource("src/main/resources/userinput.xsd"));

Where should I put the XSD file (a) in my source control (i.e., the Maven structure) and (b) in my jar file? [Note: Running xjc through my IDE puts it in the .../generated/userinput directory but Maven then ignores it when packaging.]

I'm looking for an answer that indicates that there is some sort of common methodology, so would like a reference if possible. If this is currently an unspecified choice left to the developer, then please say so (preferably referenced) as I understand that stackoverflow is not the place to collect opinions.


原文:https://stackoverflow.com/questions/30983219
更新时间:2024-02-18 20:02

最满意答案

$q.defer Anti-Pattern 1有缺陷

.service('TeamworkPeopleSrvc', function($http, $q){
  var deferred = $q.defer();
  var TeamworkPeopleSrvc = this;
  TeamworkPeopleSrvc.getPeople = function(){
    $http.defaults.headers.common['Authorization'] = 'Basic ' + window.btoa('mycustomapikey' + ':' + 'X');
    $http({
      method: 'GET',
      url: 'http://projects.com/people.json',
      params: { 'pageSize':'5'},
    })
    .then(function(response) {
      deferred.resolve(response);
    });
      return deferred.promise;
  };
  return TeamworkPeopleSrvc;
})

代码中的两个服务都使用有缺陷的$q.defer反模式 。 使用在.getPeople函数之外创建的deferred对象,只有第一个解析才会解析promise。 后续解析将被忽略,并且promise始终返回第一个值。 此外,如果XHR有错误,则错误信息将丢失。 如果出现错误,承诺永远不会仅通过第一个履行的XHR解决或解决。

没有$q.defer实现

.service('TeamworkPeopleSrvc', function($http, $q){
    var TeamworkPeopleSrvc = this;
    TeamworkPeopleSrvc.getPeople = function(){
        var authHeader = { Authorization: 'Basic ' +
              window.btoa('mycustomapikey' + ':' + 'X')
        };
        var httpPromise = ($http({
            method: 'GET',
            url: 'http://projects.com/people.json',
            headers: authHeader,
            params: { 'pageSize':'5'},
            })
        );
        return httpPromise;
    };
    return TeamworkPeopleSrvc;
});

要正确实现服务,只需返回$http服务返回的承诺。 每次调用$http函数时都会创建一个新的promise,并且将正确保留错误信息。


Defective $q.defer Anti-Pattern1

.service('TeamworkPeopleSrvc', function($http, $q){
  var deferred = $q.defer();
  var TeamworkPeopleSrvc = this;
  TeamworkPeopleSrvc.getPeople = function(){
    $http.defaults.headers.common['Authorization'] = 'Basic ' + window.btoa('mycustomapikey' + ':' + 'X');
    $http({
      method: 'GET',
      url: 'http://projects.com/people.json',
      params: { 'pageSize':'5'},
    })
    .then(function(response) {
      deferred.resolve(response);
    });
      return deferred.promise;
  };
  return TeamworkPeopleSrvc;
})

Both services in the code use a defective $q.defer Anti-Pattern. With the deferred object created outside the .getPeople function, the promise will only be resolved once with the first resolve. Subsequent resolves are ignored and the promise always returns the first value. In addition if the XHR has an error, the error information is lost. With errors, the promise never resolves or resolves only with the first fulfilled XHR.

Implemented Without $q.defer

.service('TeamworkPeopleSrvc', function($http, $q){
    var TeamworkPeopleSrvc = this;
    TeamworkPeopleSrvc.getPeople = function(){
        var authHeader = { Authorization: 'Basic ' +
              window.btoa('mycustomapikey' + ':' + 'X')
        };
        var httpPromise = ($http({
            method: 'GET',
            url: 'http://projects.com/people.json',
            headers: authHeader,
            params: { 'pageSize':'5'},
            })
        );
        return httpPromise;
    };
    return TeamworkPeopleSrvc;
});

To implement the service correctly, simply return the promise returned by the $http service. A new promise will be created each time the $http function is invoked and error information will be properly retained.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)