1 17 package org.eclipse.emf.ecore.xmi.impl; 18 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.Method ; 21 import java.util.Collection ; 22 import java.util.Map ; 23 24 import org.eclipse.emf.common.CommonPlugin; 25 import org.eclipse.emf.common.util.URI; 26 import org.eclipse.emf.ecore.EPackage; 27 import org.eclipse.emf.ecore.util.ExtendedMetaData; 28 import org.eclipse.emf.ecore.xmi.XMIPlugin; 29 import org.eclipse.emf.ecore.xmi.XMLHelper; 30 import org.eclipse.emf.ecore.xmi.XMLLoad; 31 import org.eclipse.emf.ecore.xmi.XMLResource; 32 import org.eclipse.emf.ecore.xmi.impl.SAXWrapper; 33 import org.eclipse.emf.ecore.xmi.impl.SAXXMLHandler; 34 import org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl; 35 import org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl; 36 import org.xml.sax.helpers.DefaultHandler ; 37 38 39 public class GenericXMLResourceImpl extends XMLResourceImpl 40 { 41 protected static final Class xsdEcoreBuilderClass; 42 protected static final Constructor xsdEcoreBuilderConstructor; 43 protected static final Method xsdEcoreBuilderGenerateResourcesMethod; 44 45 static 46 { 47 Class theXSDEcoreBuilderClass = null; 48 Constructor theXSDEcoreBuilderConstructor = null; 49 Method theXSDEcoreBuilderGenerateResourcesMethod = null; 50 51 try 52 { 53 theXSDEcoreBuilderClass = CommonPlugin.loadClass("org.eclipse.xsd", "org.eclipse.xsd.ecore.XSDEcoreBuilder"); 54 theXSDEcoreBuilderConstructor = theXSDEcoreBuilderClass.getConstructor(new Class [] { ExtendedMetaData.class }); 55 theXSDEcoreBuilderGenerateResourcesMethod = theXSDEcoreBuilderClass.getMethod("generateResources", new Class [] { Collection .class }); 56 } 57 catch (Exception exception) 58 { 59 XMIPlugin.INSTANCE.log(exception); 60 exception.printStackTrace(); 61 } 62 63 xsdEcoreBuilderClass = theXSDEcoreBuilderClass; 64 xsdEcoreBuilderConstructor = theXSDEcoreBuilderConstructor; 65 xsdEcoreBuilderGenerateResourcesMethod = theXSDEcoreBuilderGenerateResourcesMethod; 66 } 67 68 public GenericXMLResourceImpl(URI uri) 69 { 70 super(uri); 71 } 72 73 protected static class GenericXMLLoadImpl extends XMLLoadImpl 74 { 75 protected GenericXMLLoadImpl(XMLHelper helper) 76 { 77 super(helper); 78 } 79 80 protected DefaultHandler makeDefaultHandler() 81 { 82 return new SAXWrapper(new GenericSAXXMLHandler(resource, helper, options)); 83 } 84 } 85 86 protected static class GenericSAXXMLHandler extends SAXXMLHandler 87 { 88 protected Object xsdEcoreBuilder; 89 protected Collection generatedResources; 90 91 protected GenericSAXXMLHandler(XMLResource xmlResource, XMLHelper helper, Map options) 92 { 93 super(xmlResource, helper, options); 94 } 95 96 protected void handleTopLocations(String prefix, String name) 97 { 98 if (urisToLocations != null && xsdEcoreBuilderConstructor != null && xsdEcoreBuilderGenerateResourcesMethod != null) 99 { 100 try 101 { 102 xsdEcoreBuilder = xsdEcoreBuilderConstructor.newInstance(new Object [] { extendedMetaData }); 103 generatedResources = 104 (Collection )xsdEcoreBuilderGenerateResourcesMethod.invoke(xsdEcoreBuilder, new Object [] { urisToLocations.values() }); 105 } 107 catch (Exception exception) 108 { 109 XMIPlugin.INSTANCE.log(exception); 110 } 111 } 112 113 String namespaceURI = helper.getURI(prefix); 116 if (extendedMetaData.getPackage(namespaceURI) == null) 117 { 118 extendedMetaData.demandFeature(namespaceURI, name, true); 119 } 120 } 121 122 protected EPackage handleMissingPackage(String uriString) 123 { 124 return 125 objects.isEmpty() ? 126 extendedMetaData.demandPackage(uriString) : 127 super.handleMissingPackage(uriString); 128 } 129 } 130 131 protected XMLLoad createXMLLoad() 132 { 133 return new GenericXMLLoadImpl(createXMLHelper()); 134 } 135 } | Popular Tags |