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);
}
}
}
}
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).");
}
}
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;
}
Aggregations