KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > xmi > impl > GenericXMLResourceImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: GenericXMLResourceImpl.java,v 1.2 2005/06/10 20:59:30 emerks Exp $
16  */

17 package org.eclipse.emf.ecore.xmi.impl;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Map JavaDoc;
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 JavaDoc;
37
38
39 public class GenericXMLResourceImpl extends XMLResourceImpl
40 {
41   protected static final Class JavaDoc xsdEcoreBuilderClass;
42   protected static final Constructor JavaDoc xsdEcoreBuilderConstructor;
43   protected static final Method JavaDoc xsdEcoreBuilderGenerateResourcesMethod;
44     
45   static
46   {
47     Class JavaDoc theXSDEcoreBuilderClass = null;
48     Constructor JavaDoc theXSDEcoreBuilderConstructor = null;
49     Method JavaDoc theXSDEcoreBuilderGenerateResourcesMethod = null;
50     
51     try
52     {
53       theXSDEcoreBuilderClass = CommonPlugin.loadClass("org.eclipse.xsd", "org.eclipse.xsd.ecore.XSDEcoreBuilder");
54       theXSDEcoreBuilderConstructor = theXSDEcoreBuilderClass.getConstructor(new Class JavaDoc [] { ExtendedMetaData.class });
55       theXSDEcoreBuilderGenerateResourcesMethod = theXSDEcoreBuilderClass.getMethod("generateResources", new Class JavaDoc [] { Collection JavaDoc.class });
56     }
57     catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc xsdEcoreBuilder;
89     protected Collection JavaDoc generatedResources;
90     
91     protected GenericSAXXMLHandler(XMLResource xmlResource, XMLHelper helper, Map JavaDoc options)
92     {
93       super(xmlResource, helper, options);
94     }
95
96     protected void handleTopLocations(String JavaDoc prefix, String JavaDoc name)
97     {
98       if (urisToLocations != null && xsdEcoreBuilderConstructor != null && xsdEcoreBuilderGenerateResourcesMethod != null)
99       {
100         try
101         {
102           xsdEcoreBuilder = xsdEcoreBuilderConstructor.newInstance(new Object JavaDoc [] { extendedMetaData });
103           generatedResources =
104               (Collection JavaDoc)xsdEcoreBuilderGenerateResourcesMethod.invoke(xsdEcoreBuilder, new Object JavaDoc [] { urisToLocations.values() });
105           // xmlResource.getResourceSet().getResources().addAll(generatedResources);
106
}
107         catch (Exception JavaDoc exception)
108         {
109           XMIPlugin.INSTANCE.log(exception);
110         }
111       }
112
113       // Ensure that anything can be handled, even if it's not recognized.
114
//
115
String JavaDoc 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 JavaDoc 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