批评我的 JAXB 对象包装器

发布于 2024-08-08 17:44:18 字数 1745 浏览 1 评论 0原文

我有一个关于包装 jaxb 创建的类的问题,并且真的很想听听您的意见。

我的 xsd 看起来有点像:

<ComplexService>
    <ComplexObject1>
        <Element1></Element1>
       <Parameter></Parameter>
    </ComplexObject1>

   <ComplexObject2>
        <Element2> </Element2>
        <Parameter> </Parameter>
   </ComplexObject2>

   ...

  <ComplexObject10>
        <Element10> </Element10>
        <Parameter> </Parameter>
  </ComplexObjec10>

通过 xjc 运行上述 xsd 后创建的类看起来有点像:

public class ComplexService{

ComplexObject1 object1;
ComplexObject2 object2;
...
ComplexObject10 object10;

public static class ComplexObject1{
//Accessors and mutators on ComplexObject1
}

public static class ComplexObject2{
//Accessors and mutators on ComplexObject1
}

...

public static class ComplexObject10{
//Accessors and mutators on ComplexObject1
}

}

现在我想围绕这些 CompleObjects 以及 ComplexService 类创建一个包装器。

public class WrappedComplexObject1{

private final ComplexObject1;

public WrappedComplexObject1(){
complexObject1 = new ComplexObject1();
}

//Delegate calls to the underlying ComplexObject1
public String getServiceName(){
return complexObject1.getServiceName();
}

}

我的问题是:

  1. 上述方式是结束课程的首选方式吗?我的目标是不弄乱 xjc 创建的底层类;提供更好的命名 api(类和方法名称)。

  2. 我还想验证这些对象中的数据。因此我正在考虑使用 装饰器模式进一步包装 WrappedComplexObject1。这是推荐的方法吗?

  3. 最后,xsd 包含元素“Parameter”,其结构相同(仅包含一个值字段)。然而,当 xjc 创建 ComplexService 类时,为每个 ComplexObject 创建了一个新的 Parameter 类。

我应该担心只有一个“参数”包装类,还是应该为每个 ComplexObject 创建一个参数包装类。

任何建议、想法、代码示例都会非常有帮助。

谢谢

I have a question on wrapping a jaxb created class and would really like to hear your inputs.

My xsd looks a bit like:

<ComplexService>
    <ComplexObject1>
        <Element1></Element1>
       <Parameter></Parameter>
    </ComplexObject1>

   <ComplexObject2>
        <Element2> </Element2>
        <Parameter> </Parameter>
   </ComplexObject2>

   ...

  <ComplexObject10>
        <Element10> </Element10>
        <Parameter> </Parameter>
  </ComplexObjec10>

The class created after running the above xsd through xjc looks a bit like :

public class ComplexService{

ComplexObject1 object1;
ComplexObject2 object2;
...
ComplexObject10 object10;

public static class ComplexObject1{
//Accessors and mutators on ComplexObject1
}

public static class ComplexObject2{
//Accessors and mutators on ComplexObject1
}

...

public static class ComplexObject10{
//Accessors and mutators on ComplexObject1
}

}

Now I want to create a wrapper around these CompleObjects as well the ComplexService class.

public class WrappedComplexObject1{

private final ComplexObject1;

public WrappedComplexObject1(){
complexObject1 = new ComplexObject1();
}

//Delegate calls to the underlying ComplexObject1
public String getServiceName(){
return complexObject1.getServiceName();
}

}

My questions are these:

  1. Would the above way be the preferred way to wrap the class? My objectives are to not mess with the underlying classes created by xjc; to provide a better named api (Class as well as method names).

  2. I also want to validate the data in these objects. Therefore I am thinking of using the
    decorator pattern to further wrap WrappedComplexObject1. Would this be a recommended approach?

  3. Lastly, the xsd contains the element "Parameter" which is structurally the same (just contains one value field). However, when xjc created the ComplexService class, for every ComplexObject a new Parameter class was created.

Should I worry about just having one wrapper class for "Parameter" or should I simply create one Parameter wrapper classes per ComplexObject.

Any suggestions, ideas, code samples would be most helpful.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2024-08-15 17:44:18

正如您所看到的,没有任何回应,所以我不确定我的方式是否是“首选”方式。然而,这就是我最终所做的:

//Defined a Wrapper Super class。我封装的所有课程都将由此衍生。

public abstract class WrappedSuperComplexObject{

    protected boolean isValid;
    protected String name;

    public boolean isValid(){
        return isValid;
    }

    public String getName(){
        return name;    
    }

}

//包装产品类

 public class WrappedComplexBondObject extends WrappedSuperComplexObject{ 

    //ComplexObject is an internal object created by JAXB 
    private final JAXBElement<Product> productElement; 

    public WrappedComplexBondObject(JAXBElement<Product> productElement) { 
           this.isValid = true;
           this.name = ProductEnum.BOND;
           this.productElement= productElement; 
    } 

    //Delegate all get/set class to the internal object
    public String getElement1() { 
           return productElement.getElement1(); 
    } 

    public String getParameter1() { 
           return productElement.getParameter1(); 
    } 

    }

// 然后有一个工厂类来验证并创建特定的产品实例。

public WrappedSuperComplexObject createWrappedInstance(JAXBElement<DataDocument> jaxbElement) throws WrappedException{

    DataDocument document = jaxbElement.getValue();
    WrappedValidationResult result = WrappedValidator.validate(document);

    if ( !result.isValid() ){
        throw new WrappedException(result.getMessage());
    }

    JAXBElement<Product> productElem = (JAXBElement<Product>) trade.getProduct();   
    String productName = productElem.getName().getLocalPart().toUpperCase();

    WrappedSuperComplexObject product = null;

     switch( WrappedProductEnum.valueOf(productName) ){

        case BOND:
            product = new WrappedComplexBondObject(productElem);
        break;

        default:
            product = new UnsupportedProduct("Product is not Supported.");
        break;

    }

    return product;     
}

对于问题 3,我最终从主 xsd 中提取了“Parameter”元素,并为参数创建了一个新的 xsd。然后我引用了我的主 xjc 中的“参数”元素。

希望这有帮助。

Well as you can see there were no responses, so I am unsure if my way is the "preferred" way. However, this is what I ended up doing:

//Defined a Wrapper Super class. All the classes I wrapped would descend from this.

public abstract class WrappedSuperComplexObject{

    protected boolean isValid;
    protected String name;

    public boolean isValid(){
        return isValid;
    }

    public String getName(){
        return name;    
    }

}

//Wrapper product class

 public class WrappedComplexBondObject extends WrappedSuperComplexObject{ 

    //ComplexObject is an internal object created by JAXB 
    private final JAXBElement<Product> productElement; 

    public WrappedComplexBondObject(JAXBElement<Product> productElement) { 
           this.isValid = true;
           this.name = ProductEnum.BOND;
           this.productElement= productElement; 
    } 

    //Delegate all get/set class to the internal object
    public String getElement1() { 
           return productElement.getElement1(); 
    } 

    public String getParameter1() { 
           return productElement.getParameter1(); 
    } 

    }

// And then have a factory class that validates and creates the specific product instance.

public WrappedSuperComplexObject createWrappedInstance(JAXBElement<DataDocument> jaxbElement) throws WrappedException{

    DataDocument document = jaxbElement.getValue();
    WrappedValidationResult result = WrappedValidator.validate(document);

    if ( !result.isValid() ){
        throw new WrappedException(result.getMessage());
    }

    JAXBElement<Product> productElem = (JAXBElement<Product>) trade.getProduct();   
    String productName = productElem.getName().getLocalPart().toUpperCase();

    WrappedSuperComplexObject product = null;

     switch( WrappedProductEnum.valueOf(productName) ){

        case BOND:
            product = new WrappedComplexBondObject(productElem);
        break;

        default:
            product = new UnsupportedProduct("Product is not Supported.");
        break;

    }

    return product;     
}

For question 3, I ended up extracting the "Parameter" element from my main xsd and created a new xsd just for Parameter. I then referenced the "Parameter" element from my main xjc.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文