Skip to main content
summaryrefslogtreecommitdiffstats
blob: c4882c9b4816482404ac7ea8984c5ea4dfb8cb40 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*******************************************************************************
 *  Copyright (c) 2011  Oracle. All rights reserved.
 *  This program and the accompanying materials are made available under the
 *  terms of the Eclipse Public License v1.0, which accompanies this distribution
 *  and is available at http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors:
 *  	IBM Corporation - initial API and implementation
 *  		(copied mainly from org.eclipse.wst.xsd.contentmodel.internal.XSDImpl)
 *  	Oracle - extensions and modifications
 *******************************************************************************/
package org.eclipse.jpt.jaxb.core.xsd;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.notify.impl.AdapterFactoryImpl;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.jaxb.core.JptJaxbCorePlugin;
import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolverPlugin;
import org.eclipse.wst.xml.core.internal.XMLCorePlugin;
import org.eclipse.wst.xml.core.internal.catalog.provisional.ICatalog;
import org.eclipse.wst.xsd.contentmodel.internal.util.XSDSchemaLocatorAdapterFactory;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDSchemaContent;
import org.eclipse.xsd.XSDSimpleTypeDefinition;
import org.eclipse.xsd.impl.XSDImportImpl;
import org.eclipse.xsd.impl.XSDSchemaImpl;
import org.eclipse.xsd.util.XSDConstants;
import org.eclipse.xsd.util.XSDResourceImpl;
import org.eclipse.xsd.util.XSDSwitch;

/**
 * Utility class for building XSD model and its extensions for JAXB
 */
public class XsdUtil {
	
	static final XsdAdapterFactoryImpl adapterFactory = new XsdAdapterFactoryImpl();
	
	public static String getResolvedUri(String namespace, String location) {
		String resolvedUri = null;
		
		ICatalog catalog = XMLCorePlugin.getDefault().getDefaultXMLCatalog();
		
		if (! StringTools.stringIsEmpty(location)) {
			try {
				resolvedUri = catalog.resolveSystem(location);
				if (resolvedUri == null) {
					resolvedUri = catalog.resolveURI(location);
				}
			}
			catch (MalformedURLException me) {
				JptJaxbCorePlugin.log(me);
				resolvedUri = null;
			}
			catch (IOException ie) {
				JptJaxbCorePlugin.log(ie);
				resolvedUri = null;
			}
		}
		
		if (resolvedUri == null && namespace != null) {
			if ( ! (location != null && location.endsWith(".xsd"))) { //$NON-NLS-1$ 
				try {
					resolvedUri = catalog.resolvePublic(namespace, location);
					if (resolvedUri == null) {
						resolvedUri = catalog.resolveURI(namespace);
					}
				}
				catch (MalformedURLException me) {
					JptJaxbCorePlugin.log(me);
					resolvedUri = null;
				}
				catch (IOException ie) {
            		JptJaxbCorePlugin.log(ie);
					resolvedUri = null;
				}
			}
		}
		
		// if resolvedUri is null, assume the location is resolved
		return (resolvedUri != null) ? resolvedUri : location;
	}
	
	
	/**
	 * Given uri for an XML Schema document, parse the document and build
	 * corresponding EMF object.
	 */
	public static XSDSchema buildXSDModel(String uriString) {
		XSDSchema xsdSchema = null;
		
		try {
			// if XML Schema for Schema is requested, get it through schema model 
			if (uriString.endsWith("2001/XMLSchema.xsd")) {
				xsdSchema = XSDSchemaImpl.getSchemaForSchema(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);			
			}
			else { 	
				ResourceSet resourceSet = new ResourceSetImpl();
				resourceSet.getAdapterFactories().add(new XSDSchemaLocatorAdapterFactory());
				
				URI uri = URI.createURI(uriString);   
				
				// CS : bug 113537 ensure we perform physical resolution before opening a stream for the resource
				String physicalLocation = URIResolverPlugin.createResolver().resolvePhysicalLocation("", "", uriString);       
				InputStream inputStream = resourceSet.getURIConverter().createInputStream(URI.createURI(physicalLocation));
				XSDResourceImpl resource = (XSDResourceImpl)resourceSet.createResource(URI.createURI("*.xsd"));
				resource.setURI(uri);
				resource.load(inputStream, null);         
				xsdSchema = resource.getSchema();      
			}
			handleImports(xsdSchema);
		}
		catch (Exception e) {
			JptJaxbCorePlugin.log(e);
		}
		return xsdSchema;
	}
	
	private static void handleImports(XSDSchema xsdSchema) {
		if (xsdSchema != null) {
			for (XSDSchemaContent content : xsdSchema.getContents()) {
				if (content instanceof XSDImportImpl) {
					XSDImportImpl anImport = (XSDImportImpl) content;
					try {
						if (anImport.getSchemaLocation() != null) {
							anImport.importSchema();
						}
					}
					catch (Exception e) {
						JptJaxbCorePlugin.log(e);
					}
				}
			}
		}
	}
	
	public static Object getAdapter(Notifier notifier) {
		return adapterFactory.adapt(notifier);
	}
	
	
	/**
	 * The Factory for the XSD adapter model. It provides a create method for each
	 * non-abstract class of the model.
	 */
	public static class XsdAdapterFactoryImpl
			extends AdapterFactoryImpl {
		
		@Override
		public Adapter createAdapter(Notifier target) {
			XSDSwitch xsdSwitch = new XSDSwitch() {
				@Override
				public Object caseXSDSchema(XSDSchema object) {
					return new XsdSchema(object);
				}
				
				@Override
				public Object caseXSDSimpleTypeDefinition(XSDSimpleTypeDefinition object) {
					return new XsdSimpleTypeDefinition(object);
				}
				
				@Override
				public Object caseXSDComplexTypeDefinition(XSDComplexTypeDefinition object) {
					return new XsdComplexTypeDefinition(object);
				}
				
				@Override
				public Object caseXSDElementDeclaration(XSDElementDeclaration object) {
					return new XsdElementDeclaration(object);
				}
			};
			
			Object o = xsdSwitch.doSwitch((EObject) target);
			Adapter result = null;
			if (o instanceof Adapter) {
				result = (Adapter) o;
			}
			return result;
		}
		
		public Adapter adapt(Notifier target) {
			return adapt(target, this);
		}
	}
}

Back to the top