2016-02-05 10 views
5

Tôi đọc thành công lược đồ XSD bằng cách sử dụng org.eclipse.xsd.util.XSDResourceImpl và xử lý tất cả các phần tử XSD, loại, thuộc tính, v.v.
Nhưng khi tôi muốn xử lý tham chiếu đến phần tử được khai báo trong giản đồ đã nhập, tôi nhận được null làm loại của nó. Có vẻ như các lược đồ đã nhập không được xử lý bởi XSDResourceImpl. Bất kỳ ý tưởng nào?Đọc XSD bằng cách sử dụng org.eclipse.xsd.util.XSDResourceImpl

final XSDResourceImpl rsrc = new XSDResourceImpl(URI.createFileURI(xsdFileWithPath)); 
    rsrc.load(new HashMap()); 
    final XSDSchema schema = rsrc.getSchema(); 
    ... 
    if (elem.isElementDeclarationReference()){ //element ref 
     elem = elem.getResolvedElementDeclaration(); 
    } 
    XSDTypeDefinition tdef = elem.getType(); //null for element ref 

Cập nhật:
tôi đã XSD nhập khẩu không hợp lệ, nhưng không nhận được ngoại lệ. Nó có nghĩa là nó thực sự không được phân tích cú pháp. Có cách nào để buộc tải XSD nhập khẩu cùng với chính XSD không?

Trả lời

1

Có một thủ thuật quan trọng để xử lý importsincludes tự động. Bạn phải sử dụng ResourceSet để đọc tệp XSD chính.

import org.eclipse.emf.ecore.resource.Resource; 
import org.eclipse.emf.ecore.resource.ResourceSet; 
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; 
import org.eclipse.xsd.util.XSDResourceFactoryImpl; 
import org.eclipse.xsd.util.XSDResourceImpl; 
import org.eclipse.xsd.XSDSchema; 

static ResourceSet resourceSet; 
XSDResourceFactoryImpl rf = new XSDResourceFactoryImpl(); 
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd", rf); 
resourceSet = new ResourceSetImpl(); 
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_LOCATION, Boolean.TRUE); 
XSDResourceImpl rsrc = (XSDResourceImpl)(resourceSet.getResource(uri, true)); 
XSDSchema sch = rsrc.getSchema(); 

Sau đó, trước khi xử lý một yếu tố, một thuộc tính hoặc một nhóm mô hình mà bạn phải sử dụng này:

elem = elem.getResolvedElementDeclaration(); 
attr = attr.getResolvedAttributeDeclaration(); 
grpdef = grpdef.getResolvedModelGroupDefinition(); 
0

Bạn có thể thử một cái gì đó như thế, tự giải quyết loại:

final XSDResourceImpl rsrc = new XSDResourceImpl(URI.createFileURI(xsdFileWithPath)); 
rsrc.load(new HashMap()); 
final XSDSchema schema = rsrc.getSchema(); 
for (Object content : schema.getContents()) 
{ 
    if (content instanceof XSDImport) 
    { 
     XSDImport xsdImport = (XSDImport) content; 
     xsdImport.resolveTypeDefinition(xsdImport.getNamespace(), ""); 
    } 
} 
0

Bạn có thể có một cái nhìn here. Particulary trong phương pháp này:

private static void forceImport(XSDSchemaImpl schema) { 
     if (schema != null) { 
      for (XSDSchemaContent content: schema.getContents()) { 
       if (content instanceof XSDImportImpl) { 
        XSDImportImpl importDirective = (XSDImportImpl)content; 
        schema.resolveSchema(importDirective.getNamespace()); 
       } 
      } 
     } 
    } 
Các vấn đề liên quan