2016-02-05 3 views
5

Я успешно прочитал схему XSD с помощью org.eclipse.xsd.util.XSDResourceImpl и процесс всех содержащихся XSD элементов, типов, атрибутов и т.д.
Но когда я хочу, чтобы обработать ссылку на элемент, объявленном в импортированная схема, я получаю ее null. Кажется, импортированные схемы не обрабатываются XSDResourceImpl. Любая идея?Чтение XSD с помощью 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 

Update:
Я сделал импортированные XSD недействительным, но не получаю исключение. Это означает, что он действительно не разбирается. Есть ли способ принудительно загрузить импортированный XSD вместе с основным?

ответ

1

Существует один важный трюк для обработки imports и includes автоматически. Вы должны использовать ResourceSet, чтобы прочитать основной файл XSD.

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(); 

Затем перед обработкой элемента, атрибута или модельной группы, которую вы должны использовать это:

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

Не могли бы вы попробовать что-то вроде этого, вручную разрешить тип:

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

Вы можете посмотреть here. Particulary в этом методе:

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()); 
       } 
      } 
     } 
    } 
Смежные вопросы