From 08b33c99f5eaa65410c785ab5e523bf9b4149dab Mon Sep 17 00:00:00 2001 From: pfullbright Date: Tue, 11 Jan 2011 22:48:40 +0000 Subject: Added basic Xsd adapter model - schema and types --- .../jaxb/core/xsd/XsdComplexTypeDefinition.java | 21 ++++ .../org/eclipse/jpt/jaxb/core/xsd/XsdSchema.java | 46 ++++++++ .../jpt/jaxb/core/xsd/XsdSimpleTypeDefinition.java | 21 ++++ .../jpt/jaxb/core/xsd/XsdTypeDefinition.java | 25 +++++ .../src/org/eclipse/jpt/jaxb/core/xsd/XsdUtil.java | 116 +++++++++++++++++++++ 5 files changed, 229 insertions(+) create mode 100644 jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdComplexTypeDefinition.java create mode 100644 jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdSchema.java create mode 100644 jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdSimpleTypeDefinition.java create mode 100644 jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdTypeDefinition.java create mode 100644 jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdUtil.java diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdComplexTypeDefinition.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdComplexTypeDefinition.java new file mode 100644 index 0000000000..53b457aa34 --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdComplexTypeDefinition.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * 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: + * Oracle - initial API and implementation + *******************************************************************************/ +package org.eclipse.jpt.jaxb.core.xsd; + +import org.eclipse.xsd.XSDComplexTypeDefinition; + + +public class XsdComplexTypeDefinition + extends XsdTypeDefinition { + + XsdComplexTypeDefinition(XSDComplexTypeDefinition xsdComplexTypeDefinition) { + super(xsdComplexTypeDefinition); + } +} diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdSchema.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdSchema.java new file mode 100644 index 0000000000..b179ec0d68 --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdSchema.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * 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: + * Oracle - initial API and implementation + *******************************************************************************/ +package org.eclipse.jpt.jaxb.core.xsd; + +import org.eclipse.emf.common.notify.impl.AdapterImpl; +import org.eclipse.jpt.utility.internal.iterables.TransformationIterable; +import org.eclipse.xsd.XSDSchema; +import org.eclipse.xsd.XSDTypeDefinition; + +public class XsdSchema + extends AdapterImpl { + + protected final XSDSchema xsdSchema; + + + + XsdSchema(XSDSchema xsdSchema) { + this.xsdSchema = xsdSchema; + } + + + public Iterable getTypeDefinitions() { + return new TransformationIterable(this.xsdSchema.getTypeDefinitions()) { + @Override + protected XsdTypeDefinition transform(XSDTypeDefinition o) { + return (XsdTypeDefinition) XsdUtil.getAdapter(o); + } + }; + } + + public XsdTypeDefinition getTypeDefinition(String namespace, String name) { + for (XSDTypeDefinition typeDefinition : this.xsdSchema.getTypeDefinitions()) { + if (typeDefinition.getTargetNamespace().equals(namespace) && typeDefinition.getName().equals(name)) { + return (XsdTypeDefinition) XsdUtil.getAdapter(typeDefinition); + } + } + return null; + } +} \ No newline at end of file diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdSimpleTypeDefinition.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdSimpleTypeDefinition.java new file mode 100644 index 0000000000..c50310c85f --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdSimpleTypeDefinition.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * 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: + * Oracle - initial API and implementation + *******************************************************************************/ +package org.eclipse.jpt.jaxb.core.xsd; + +import org.eclipse.xsd.XSDSimpleTypeDefinition; + + +public class XsdSimpleTypeDefinition + extends XsdTypeDefinition { + + XsdSimpleTypeDefinition(XSDSimpleTypeDefinition xsdSimpleTypeDefinition) { + super(xsdSimpleTypeDefinition); + } +} diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdTypeDefinition.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdTypeDefinition.java new file mode 100644 index 0000000000..3aabfd7d65 --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdTypeDefinition.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * 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: + * Oracle - initial API and implementation + *******************************************************************************/ +package org.eclipse.jpt.jaxb.core.xsd; + +import org.eclipse.emf.common.notify.impl.AdapterImpl; +import org.eclipse.xsd.XSDTypeDefinition; + + +public abstract class XsdTypeDefinition + extends AdapterImpl { + + protected final XSDTypeDefinition xsdTypeDefinition; + + + protected XsdTypeDefinition(XSDTypeDefinition xsdTypeDefinition) { + this.xsdTypeDefinition = xsdTypeDefinition; + } +} diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdUtil.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdUtil.java new file mode 100644 index 0000000000..d85a73af5e --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/xsd/XsdUtil.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * 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.InputStream; +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.jaxb.core.JptJaxbCorePlugin; +import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolverPlugin; +import org.eclipse.wst.xsd.contentmodel.internal.util.XSDSchemaLocatorAdapterFactory; +import org.eclipse.xsd.XSDComplexTypeDefinition; +import org.eclipse.xsd.XSDSchema; +import org.eclipse.xsd.XSDSimpleTypeDefinition; +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 { + + protected static final XsdAdapterFactoryImpl adapterFactory = new XsdAdapterFactoryImpl(); + + /** + * 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(); + } + } + catch (Exception e) { + JptJaxbCorePlugin.log(e); + } + return xsdSchema; + } + + 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); + } + }; + + 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); + } + } +} -- cgit v1.2.3