2009-10-22 49 views
1

我有一個關於包裝jaxb創建類的問題,並且很想聽聽你的輸入。評論我的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包含結構相同的元素「參數」(只包含一個值字段)。但是,當xjc創建ComplexService類時,爲每個ComplexObject創建一個新的Parameter類。

我應該擔心只有一個「Parameter」包裝類,或者我應該簡單地爲每個ComplexObject創建一個Parameter包裝類。

任何建議,想法,代碼示例將是最有幫助的。

感謝

+0

我對這個問題很感興趣。你有沒有得到它的任何地方? – Martin 2009-12-07 00:01:32

+0

答案如下。 – CaptainHastings 2009-12-07 17:30:43

回答

1

好吧,你可以看到有沒有反應,所以我不能確定,如果我的方式是「首選」的方式。然而,這是我最終做的:

//定義了一個包裝超級類。我所包裝的所有課程都將從此下降。

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中的「Parameter」元素。

希望這會有所幫助。

+0

感謝您的更新! – Martin 2009-12-10 21:20:40