Search in sources :

Example 1 with XSDParser

use of org.eclipse.xsd.util.XSDParser in project webtools.sourceediting by eclipse.

the class CommonDirectivesSection method doHandleEvent.

// TODO: common up code with XSDSelectIncludeFileWizard
public void doHandleEvent(Event event) {
    // $NON-NLS-1$
    errorText.setText("");
    if (event.widget == schemaLocationText) {
        // $NON-NLS-1$
        String errorMessage = "";
        isValidSchemaLocation = true;
        String xsdModelFile = schemaLocationText.getText();
        // $NON-NLS-1$
        String namespace = "";
        XSDSchema externalSchema = null;
        if (xsdModelFile.length() == 0) {
            // $NON-NLS-1$
            handleSchemaLocationChange(xsdModelFile, "", null);
            return;
        }
        try {
            IFile currentIFile = null;
            IEditorInput editorInput = getActiveEditor().getEditorInput();
            if (editorInput instanceof IFileEditorInput) {
                currentIFile = ((IFileEditorInput) editorInput).getFile();
            }
            URI newURI = URI.createURI(xsdModelFile);
            String xsdFile = URIHelper.getRelativeURI(newURI.toString(), currentIFile.getFullPath().toString());
            // $NON-NLS-1$
            final String normalizedXSDFile = URIHelper.normalize(xsdFile, currentIFile.getLocation().toString(), "");
            final String normalizedURI = URI.encodeFragment(URIHelper.addImpliedFileProtocol(normalizedXSDFile), true).toString();
            XSDParser parser = new XSDParser(new HashMap());
            parser.parse(normalizedURI);
            externalSchema = parser.getSchema();
            if (externalSchema != null) {
                String extNamespace = externalSchema.getTargetNamespace();
                // $NON-NLS-1$
                if (extNamespace == null)
                    extNamespace = "";
                namespace = extNamespace;
                if (externalSchema.getDiagnostics() != null && externalSchema.getDiagnostics().size() > 0) {
                    isValidSchemaLocation = false;
                    // $NON-NLS-1$
                    errorMessage = XSDEditorPlugin.getResourceString("_UI_INCORRECT_XML_SCHEMA", xsdModelFile);
                } else {
                    String currentNameSpace = xsdSchema.getTargetNamespace();
                    if (input instanceof XSDInclude || input instanceof XSDRedefine) {
                        // Check the namespace to make sure they are the same as current file
                        if (extNamespace != null) {
                            if (currentNameSpace != null && !extNamespace.equals(currentNameSpace)) {
                                // $NON-NLS-1$
                                errorMessage = XSDEditorPlugin.getResourceString("_UI_DIFFERENT_NAME_SPACE", xsdModelFile);
                                isValidSchemaLocation = false;
                            }
                        }
                    } else {
                        // Check the namespace to make sure they are different from the current file
                        if (extNamespace != null) {
                            if (currentNameSpace != null && extNamespace.equals(currentNameSpace)) {
                                // $NON-NLS-1$
                                errorMessage = XSDEditorPlugin.getResourceString("_UI_SAME_NAME_SPACE", xsdModelFile);
                                isValidSchemaLocation = false;
                            }
                        }
                    }
                }
            } else {
                errorMessage = Messages._UI_ERROR_INVALID_FILE;
                isValidSchemaLocation = false;
            }
        } catch (Exception e) {
            errorMessage = Messages._UI_ERROR_INVALID_FILE;
            isValidSchemaLocation = false;
        } finally {
            if (!isValidSchemaLocation) {
                errorText.setText(errorMessage);
                int length = errorText.getText().length();
                red = new Color(null, 255, 0, 0);
                StyleRange style = new StyleRange(0, length, red, schemaLocationText.getBackground());
                errorText.setStyleRange(style);
            } else {
                handleSchemaLocationChange(xsdModelFile, namespace, externalSchema);
            }
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) XSDRedefine(org.eclipse.xsd.XSDRedefine) HashMap(java.util.HashMap) Color(org.eclipse.swt.graphics.Color) StyleRange(org.eclipse.swt.custom.StyleRange) URI(org.eclipse.emf.common.util.URI) IFileEditorInput(org.eclipse.ui.IFileEditorInput) XSDParser(org.eclipse.xsd.util.XSDParser) IEditorInput(org.eclipse.ui.IEditorInput) XSDSchema(org.eclipse.xsd.XSDSchema) XSDInclude(org.eclipse.xsd.XSDInclude)

Example 2 with XSDParser

use of org.eclipse.xsd.util.XSDParser in project tmdm-common by Talend.

the class MetadataRepository method load.

public void load(InputStream inputStream, ValidationHandler handler) {
    if (inputStream == null) {
        throw new IllegalArgumentException("Input stream can not be null.");
    }
    // Validates data model using shared studio / server classes
    // Load user defined data model now
    Map<String, Object> options = new HashMap<String, Object>();
    options.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
    XSDParser parse = new XSDParser(options);
    parse.parse(inputStream);
    XSDSchema schema = parse.getSchema();
    if (schema == null) {
        throw new IllegalStateException("No schema parsed from input (make sure stream contains a data model).");
    }
    schema.validate();
    EList<XSDDiagnostic> diagnostics = schema.getDiagnostics();
    for (XSDDiagnostic diagnostic : diagnostics) {
        XSDDiagnosticSeverity severity = diagnostic.getSeverity();
        if (XSDDiagnosticSeverity.ERROR_LITERAL.equals(severity)) {
            handler.error((TypeMetadata) null, "XSD validation error: " + diagnostic.getMessage(), null, -1, -1, ValidationError.XML_SCHEMA);
        } else if (XSDDiagnosticSeverity.WARNING_LITERAL.equals(severity)) {
            handler.error((TypeMetadata) null, "XSD validation warning: " + diagnostic.getMessage(), null, -1, -1, ValidationError.XML_SCHEMA);
        }
    }
    XmlSchemaWalker.walk(schema, this);
    // TMDM-4876 Additional processing for entity inheritance
    resolveAdditionalSuperTypes(this);
    // "Freeze" all types (ensure all soft references now point to actual types in the repository).
    nonInstantiableTypes.put(getUserNamespace(), freezeTypes(nonInstantiableTypes.get(getUserNamespace())));
    // "Freeze" all reusable type usages in the data model.
    freezeUsages();
    entityTypes.put(getUserNamespace(), freezeTypes(entityTypes.get(getUserNamespace())));
    // Validate types
    for (TypeMetadata type : getUserComplexTypes()) {
        if (!XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(type.getNamespace())) {
            type.validate(handler);
        }
    }
    for (TypeMetadata type : getNonInstantiableTypes()) {
        if (!XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(type.getNamespace())) {
            type.validate(handler);
        }
    }
    // Perform data model-scoped validation (e.g. cycles).
    ValidationFactory.getRule(this).perform(handler);
    handler.end();
    if (handler.getErrorCount() != 0) {
        LOGGER.error("Could not parse data model (" + handler.getErrorCount() + " error(s) found).");
    }
}
Also used : XSDDiagnostic(org.eclipse.xsd.XSDDiagnostic) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) XSDParser(org.eclipse.xsd.util.XSDParser) XSDDiagnosticSeverity(org.eclipse.xsd.XSDDiagnosticSeverity) XSDSchema(org.eclipse.xsd.XSDSchema)

Example 3 with XSDParser

use of org.eclipse.xsd.util.XSDParser in project webtools.sourceediting by eclipse.

the class XSDSelectIncludeFileWizard method doLoadExternalModel.

/**
 * Create a MOF model for the imported file
 */
protected String doLoadExternalModel(IProgressMonitor monitor, String xsdModelFile, String xsdFileName) {
    String errorMessage = null;
    String currentNameSpace = mainSchema.getTargetNamespace();
    monitor.beginTask("Loading XML Schema", 100);
    monitor.worked(50);
    XSDParser parser = new XSDParser();
    parser.parse(xsdModelFile);
    externalSchema = parser.getSchema();
    if (externalSchema != null) {
        String extNamespace = externalSchema.getTargetNamespace();
        namespace = extNamespace;
        if (externalSchema.getDiagnostics() != null && externalSchema.getDiagnostics().size() > 0) {
            errorMessage = XSDEditorPlugin.getPlugin().getString("_UI_INCORRECT_XML_SCHEMA", xsdFileName);
        } else {
            if (isInclude) {
                // Check the namespace to make sure they are the same as current file
                if (extNamespace != null) {
                    if (currentNameSpace != null && !extNamespace.equals(currentNameSpace)) {
                        errorMessage = XSDEditorPlugin.getPlugin().getString("_UI_DIFFERENT_NAME_SPACE", xsdFileName);
                    }
                }
            } else {
                // Check the namespace to make sure they are different from the current file
                if (extNamespace != null) {
                    if (currentNameSpace != null && extNamespace.equals(currentNameSpace)) {
                        errorMessage = XSDEditorPlugin.getPlugin().getString("_UI_SAME_NAME_SPACE", xsdFileName);
                    }
                }
            }
        }
    } else {
        errorMessage = XSDEditorPlugin.getPlugin().getString("_UI_INCORRECT_XML_SCHEMA", xsdFileName);
    }
    monitor.subTask("Finish Loading");
    monitor.worked(80);
    return errorMessage;
}
Also used : XSDParser(org.eclipse.xsd.util.XSDParser)

Aggregations

XSDParser (org.eclipse.xsd.util.XSDParser)3 HashMap (java.util.HashMap)2 XSDSchema (org.eclipse.xsd.XSDSchema)2 LinkedHashMap (java.util.LinkedHashMap)1 IFile (org.eclipse.core.resources.IFile)1 URI (org.eclipse.emf.common.util.URI)1 StyleRange (org.eclipse.swt.custom.StyleRange)1 Color (org.eclipse.swt.graphics.Color)1 IEditorInput (org.eclipse.ui.IEditorInput)1 IFileEditorInput (org.eclipse.ui.IFileEditorInput)1 XSDDiagnostic (org.eclipse.xsd.XSDDiagnostic)1 XSDDiagnosticSeverity (org.eclipse.xsd.XSDDiagnosticSeverity)1 XSDInclude (org.eclipse.xsd.XSDInclude)1 XSDRedefine (org.eclipse.xsd.XSDRedefine)1