Search in sources :

Example 1 with AdapterFactoryImpl

use of org.eclipse.emf.common.notify.impl.AdapterFactoryImpl in project tmdm-studio-se by Talend.

the class Util method getXSDSchema.

private static XSDSchema getXSDSchema(String namespaceURI, String rawData, final List<XSDImport> imports, final TreeObject treeObj, boolean uri, final List<Exception> exceptions, final Map<String, Integer> schemaMonitor) throws Exception {
    FileInputStream fin = null;
    try {
        // $NON-NLS-1$//$NON-NLS-2$
        final String xsdFileName = System.getProperty("user.dir") + "/.xsdModel.xml";
        URI fileURI = URI.createFileURI(xsdFileName);
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setValidating(false);
        DocumentBuilder documentBuilder;
        XSDSchema schema = null;
        InputSource source = null;
        Document document = null;
        String schemaLocation = rawData;
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        if (rawData == null) {
            // $NON-NLS-1$
            return XSDSchemaImpl.getSchemaForSchema("http://www.w3.org/2001/XMLSchema");
        }
        if (namespaceURI == null && rawData.endsWith(".xsd") && rawData.indexOf(File.separator) > 0) {
            // $NON-NLS-1$
            File rawFile = new File(rawData);
            if (!rawFile.exists()) {
                throw new IllegalArgumentException(rawData);
            }
        }
        // import namespace="http://xxx" schemaLocation="xxxx"
        if (namespaceURI != null && schemaLocation.endsWith(".xsd")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            URL url = new java.net.URI(namespaceURI + "/" + rawData).toURL();
            uri = false;
            rawData = IOUtils.toString(url.openConnection().getInputStream());
            // $NON-NLS-1$//$NON-NLS-2$
            rawData = rawData.replaceAll("<!DOCTYPE(.*?)>", "");
        }
        if (rawData.equals("http://www.w3.org/2001/03/xml.xsd")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            URL url = new java.net.URI("http://www.w3.org/2001/03/xml.xsd").toURL();
            uri = false;
            rawData = IOUtils.toString(url.openConnection().getInputStream());
            // $NON-NLS-1$//$NON-NLS-2$
            rawData = rawData.replaceAll("<!DOCTYPE(.*?)>", "");
        }
        if (uri) {
            File file = new File(rawData);
            if (file.exists()) {
                fin = new FileInputStream(file);
                source = new InputSource(fin);
            } else {
                source = new InputSource(new StringReader(Util.getResponseFromURL(rawData, treeObj)));
            }
        } else {
            source = new InputSource(new StringReader(rawData));
        }
        try {
            document = documentBuilder.parse(source);
        } catch (SAXParseException ex) {
            exceptions.add(ex);
            return null;
        }
        schema = XSDSchemaImpl.createSchema(document.getDocumentElement());
        ResourceSet resourceSet = new ResourceSetImpl();
        Resource resource = resourceSet.createResource(fileURI);
        resourceSet.getAdapterFactories().add(new AdapterFactoryImpl() {

            class SchemaLocator extends AdapterImpl implements XSDSchemaLocator {

                public XSDSchema locateSchema(XSDSchema xsdSchema, String namespaceURI, String rawSchemaLocationURI, String resolvedSchemaLocation) {
                    XSDSchema schema;
                    Integer rawCnt = schemaMonitor.get(rawSchemaLocationURI);
                    if (rawCnt == null) {
                        rawCnt = 0;
                    } else {
                        rawCnt++;
                    }
                    schemaMonitor.put(rawSchemaLocationURI, rawCnt);
                    if (rawCnt >= 10) {
                        schemaMonitor.put(rawSchemaLocationURI, -1);
                        return null;
                    }
                    try {
                        schema = Util.getXSDSchema(namespaceURI, rawSchemaLocationURI, imports, treeObj, true, exceptions, schemaMonitor);
                    } catch (Exception e) {
                        return XSDSchemaImpl.getSchemaForSchema(namespaceURI);
                    }
                    schema.setTargetNamespace(namespaceURI);
                    schema.setElement(schema.getDocument().getDocumentElement());
                    return schema;
                }

                public boolean isAdatperForType(Object type) {
                    return type == XSDSchemaLocator.class;
                }
            }

            protected SchemaLocator schemaLocator = new SchemaLocator();

            @Override
            public boolean isFactoryForType(Object type) {
                return type == XSDSchemaLocator.class;
            }

            @Override
            public Adapter adaptNew(Notifier target, Object type) {
                return schemaLocator;
            }
        });
        // import namespace="http://xxx" schemaLocation="xxxx"
        if (namespaceURI != null && schemaLocation.endsWith(".xsd")) {
            // $NON-NLS-1$
            schema.setSchemaLocation(schemaLocation);
        } else {
            schema.setSchemaLocation(fileURI.toString());
            // set the schema for schema QName prefix to "xsd"
            // $NON-NLS-1$
            schema.setSchemaForSchemaQNamePrefix("xsd");
        }
        // catch up the NPE to make sure data model can still run in case of unknown conflict
        try {
            resource.getContents().add(schema);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        // Add the root schema to the resource that was created above
        Iterator<Integer> iter = schemaMonitor.values().iterator();
        while (iter.hasNext()) {
            Integer it = iter.next();
            if (it.intValue() == -1) {
                return schema;
            }
        }
        importSchema(schema, imports, schemaMonitor);
        schema.setElement(document.getDocumentElement());
        return schema;
    } finally {
        if (fin != null) {
            fin.close();
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) Adapter(org.eclipse.emf.common.notify.Adapter) Document(org.w3c.dom.Document) URI(org.eclipse.emf.common.util.URI) URL(java.net.URL) SAXParseException(org.xml.sax.SAXParseException) XSDSchemaLocator(org.eclipse.xsd.util.XSDSchemaLocator) StringReader(java.io.StringReader) XSDSchema(org.eclipse.xsd.XSDSchema) Notifier(org.eclipse.emf.common.notify.Notifier) AdapterFactoryImpl(org.eclipse.emf.common.notify.impl.AdapterFactoryImpl) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) AdapterImpl(org.eclipse.emf.common.notify.impl.AdapterImpl) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) FileInputStream(java.io.FileInputStream) ConnectException(java.net.ConnectException) IOException(java.io.IOException) WebServiceException(javax.xml.ws.WebServiceException) MissingJarsException(com.amalto.workbench.service.MissingJarsException) MalformedURLException(java.net.MalformedURLException) SAXParseException(org.xml.sax.SAXParseException) InaccessibleWSDLException(com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XObject(com.sun.org.apache.xpath.internal.objects.XObject) TreeObject(com.amalto.workbench.models.TreeObject) EObject(org.eclipse.emf.ecore.EObject) File(java.io.File) ZipFile(java.util.zip.ZipFile) ZipToFile(org.talend.mdm.commmon.util.workbench.ZipToFile) XSDSchemaLocator(org.eclipse.xsd.util.XSDSchemaLocator)

Aggregations

TreeObject (com.amalto.workbench.models.TreeObject)1 MissingJarsException (com.amalto.workbench.service.MissingJarsException)1 XObject (com.sun.org.apache.xpath.internal.objects.XObject)1 InaccessibleWSDLException (com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ZipFile (java.util.zip.ZipFile)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 WebServiceException (javax.xml.ws.WebServiceException)1 Adapter (org.eclipse.emf.common.notify.Adapter)1 Notifier (org.eclipse.emf.common.notify.Notifier)1 AdapterFactoryImpl (org.eclipse.emf.common.notify.impl.AdapterFactoryImpl)1 AdapterImpl (org.eclipse.emf.common.notify.impl.AdapterImpl)1 URI (org.eclipse.emf.common.util.URI)1