SDO之创建DataObject

SDO之创建DataObject

初学SDO ,在网上下载SDO的Sample就调试,结果走了不少弯路,就是出现IllegalArgumentException,不知是出现 什么问题,心想我也是照着例子搞的怎么就是通不过呢?于是乎跟踪tuscany的源码,发现Type为空。为什么呢?可能是在运行这前要把你想要创建的 DataObject的命名空间与类型的名字在文件中得注册。于是又仔细跟踪看到了这么一段: 

    XSDHelper xsdHelper = scope.getXSDHelper();
    InputStream is 
= null;
    
try {
         URL url 
= getClass().getResource("/"+fileName);
          is 
= url.openStream();
          xsdHelper.define(is, url.toString());
     } 
catch (Exception e) {
           somethingUnexpectedHasHappened(e);
     } 
finally {
       
try {
         is.close();
       } 
catch (Exception e) {
         somethingUnexpectedHasHappened(e);
       }
   }

猛然明白了。所以把例子中的xsd文件拷到项目下。再运行通过,高兴呀,哈哈。下面我把例子贴出,可能也有网友跟我一样吧,也希望其它初学SDO的网友别走我弯路。

public   class  SdoDataObject {
    
private   static   final  String COMPANY_GENERATED_XML  =   " companyGenerated.xml " ;

    
/**
     * 创建类型范围的上下文
     * 
     * 
@return
     
*/
    
private  HelperContext createScopeForType() {

        
return  SDOUtil.createHelperContext();
    }

    
/**
     * 从XML计划文件中加载类型。
     * 
     * 
@param  filename
     
*/
    
private   void  loadTypesFromXMLSchemaFile(HelperContext scope, String filename) {
        XSDHelper xsdHelper 
=  scope.getXSDHelper();

        URL url 
=  getClass().getResource( " / "   +  filename);
        InputStream is 
=   null ;
        
try  {
            is 
=  url.openStream();
            xsdHelper.define(is, url.toString());
            is.close();
        } 
catch  (IOException e) {
            e.printStackTrace();
        }
    }

    
/**
     * 组装数据图
     * 
     * 
@param  company
     
*/
    
private   void  populateDataGraph(DataObject company) {
        company.setString(
" name " " aaaa " );
        company.setString(
" employeeOfTheMonth " " D001 " );

        DataObject depts 
=  company.createDataObject( " departments " );

        depts.setString(
" name " " Advanced Technologies " );
        depts.setString(
" location " " NY " );
        depts.setString(
" number " " 123 " );

        System.out.println(
" Creating an employee: John Jones " );
        DataObject johnJones 
=  depts.createDataObject( " employees " );
        johnJones.setString(
" name " " John Jones " );

        johnJones.setString(
" SN " " E0001 " );

        System.out.println(
" Creating an employee: Jane Doe " );
        DataObject janeDoe 
=  depts.createDataObject( " employees " );
        janeDoe.setString(
" name " " Jane Doe " );
        janeDoe.setString(
" SN " " E0003 " );

        System.out.println(
" Creating a manager: Fred Bloggs " );
        DataObject fVarone 
=  depts.createDataObject( " employees " );
        fVarone.setString(
" name " " Fred Bloggs " );
        fVarone.setString(
" SN " " E0004 " );
        fVarone.setString(
" manager " " true " );
        System.out.println(
" DataObject creation completed " );
        System.out.println();
        System.out.println(
" create DataObject susseccfully " );
    }

    
public   void  run() {
        HelperContext scope 
=  createScopeForType();
        loadTypesFromXMLSchemaFile(scope, 
" company.xsd " );
        
        DataObject company 
=  scope.getDataFactory().create( " company.xsd " ,
                
" CompanyType " );
        populateDataGraph(company);

        
try  {
            FileOutputStream fos 
=   new  FileOutputStream(COMPANY_GENERATED_XML);
            
try  {
                scope.getXMLHelper().save(company, 
" company.xsd " " company " ,
                        fos);
            } 
catch  (IOException e) {
                e.printStackTrace();
            }
            String xml 
=  scope.getXMLHelper().save(company,  " company.xsd " ,
                    
" company " );
            System.out.println(xml);
        } 
catch  (FileNotFoundException e) {
            
//  TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    
public   static   void  main(String[] args) {
        SdoDataObject sdoDataObject 
=   new  SdoDataObject();
        sdoDataObject.run();
    }

}

你可能感兴趣的:(SDO之创建DataObject)