internal override void DetermineIfArtifactIsDesignerSafe()
 {
     // XmlSchemaValidator by default does not report any errors or warning if the namespace of the validated document
     // does not match the targetNamespace in the schema considering the schema not being applicable. With XmlReader
     // it is possible to pass XmlSchemaValidationFlags.ReportValidationWarnings to be notified if this happens. However
     // it is not possible to pass this flag when using XDocument.Validate. Therefore before trying to validate the Xml
     // we just check that this is a known edmx namespace. If it is not we set the flag to false and skip validating.
     if (IsDesignerSafe = SchemaManager.GetEDMXNamespaceNames().Contains(XDocument.Root.Name.NamespaceName))
     {
         XDocument.Validate(
             EscherAttributeContentValidator.GetInstance(SchemaVersion).EdmxSchemaSet,
             (sender, args) => IsDesignerSafe = false);
     }
 }
Ejemplo n.º 2
0
        internal override bool IsXmlValid()
        {
            // If there is a VSXmlModelProvider, we should be able to find a docdata for it.
            // In any other case, it doesn't matter whether there is document data or not.
            var docData = VSHelpers.GetDocData(PackageManager.Package, Uri.LocalPath);

            Debug.Assert(
                !(XmlModelProvider is VSXmlModelProvider) || docData != null, "Using a VSXmlModelProvider but docData is null for Artifact!");

            try
            {
                XmlDocument xmldoc;
                if (docData != null)
                {
                    var textLines = VSHelpers.GetVsTextLinesFromDocData(docData);
                    Debug.Assert(textLines != null, "Failed to get IVSTextLines from docdata");

                    xmldoc = EdmUtils.SafeLoadXmlFromString(VSHelpers.GetTextFromVsTextLines(textLines));
                }
                else
                {
                    // If there is no docdata then attempt to create the XmlDocument from the internal
                    // XLinq tree in the artifact
                    xmldoc = new XmlDocument();
                    xmldoc.Load(XDocument.CreateReader());
                }
                // For the most part, the Edmx schema version of an artifact should be in sync with the schema version
                // that is compatible with the project's target framework; except when the user adds an existing edmx to a project (the version could be different).
                // For all cases, we always want to validate using the XSD's version that matches the artifact's version.
                var documentSchemaVersion = base.SchemaVersion;
                Debug.Assert(
                    EntityFrameworkVersion.IsValidVersion(documentSchemaVersion),
                    "The EF Schema Version is not valid. Value:"
                    + (documentSchemaVersion != null ? documentSchemaVersion.ToString() : "null"));

                // does the XML parse? If not, the load call below will throw
                if (EntityFrameworkVersion.IsValidVersion(documentSchemaVersion))
                {
                    var nsMgr = SchemaManager.GetEdmxNamespaceManager(xmldoc.NameTable, documentSchemaVersion);
                    // Do XSD validation on the document.
                    xmldoc.Schemas = EscherAttributeContentValidator.GetInstance(documentSchemaVersion).EdmxSchemaSet;
                    var svec = new SchemaValidationErrorCollector();

                    // remove runtime specific lines
                    // find the ConceptualModel Schema node
                    RemoveRunTimeNode(xmldoc, "/edmx:Edmx/edmx:Configurations", nsMgr);
                    RemoveRunTimeNode(xmldoc, "/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels", nsMgr);
                    RemoveRunTimeNode(xmldoc, "/edmx:Edmx/edmx:Runtime/edmx:StorageModels", nsMgr);
                    RemoveRunTimeNode(xmldoc, "/edmx:Edmx/edmx:Runtime/edmx:Mappings", nsMgr);

                    xmldoc.Validate(svec.ValidationCallBack);

                    return(svec.ErrorCount == 0);
                }
            }
            catch
            {
            }

            return(false);
        }
Ejemplo n.º 3
0
 internal override AttributeContentValidator GetAttributeContentValidator(EFArtifact artifact)
 {
     return(EscherAttributeContentValidator.GetInstance(artifact.SchemaVersion));
 }
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            if (null == _conceptualEntityModel)
            {
                Debug.Fail("Null ConceptualEntityModel");
                return;
            }

            var artifact = _conceptualEntityModel.Artifact;

            if (null == artifact)
            {
                Debug.Fail("Null Artifact");
                return;
            }

            var artifactSet = artifact.ArtifactSet;

            if (null == artifactSet)
            {
                Debug.Fail("Null ArtifactSet");
                return;
            }

            // make sure this name doesn't conflict with an EntityContainer name
            foreach (var bec in _conceptualEntityModel.EntityContainers())
            {
                if (_newNamespace == bec.LocalName.Value)
                {
                    var msg = string.Format(
                        CultureInfo.CurrentCulture, Resources.EntityContainerNameConflictsWithNamespaceName, _newNamespace);
                    throw new CommandValidationFailedException(msg);
                }
            }

            // check to see if the new namespace is valid
            if (EscherAttributeContentValidator.GetInstance(artifact.SchemaVersion)
                .IsValidAttributeValue(_newNamespace, _conceptualEntityModel.Namespace))
            {
                var previousConceptualNamespace = _conceptualEntityModel.Namespace.Value;
                if (string.IsNullOrEmpty(previousConceptualNamespace))
                {
                    Debug.Fail("Null or empty conceptual namespace");
                    return;
                }

                // find all Symbols in the ArtifactSet which have first part
                // equal to existing previousConceptualNamespace
                var allElementsWithConceptualNamespaceSymbol =
                    artifactSet.GetElementsContainingFirstSymbolPart(previousConceptualNamespace);

                // change all references which include the namespace to the new namespace
                foreach (var element in allElementsWithConceptualNamespaceSymbol)
                {
                    var itemBindings = element.GetDependentBindings();
                    foreach (var itemBinding in itemBindings)
                    {
                        itemBinding.UpdateRefNameNamespaces(
                            previousConceptualNamespace, _newNamespace);
                    }
                }

                // now update the namespace attribute itself
                _conceptualEntityModel.Namespace.Value = _newNamespace;

                // symbols need to be recalculated throughout the
                // CSDL, MSL & DesignerInfo sections, and bindings need to be rebound ...
                XmlModelHelper.NormalizeAndResolve(_conceptualEntityModel);
                if (artifact.MappingModel() != null)
                {
                    XmlModelHelper.NormalizeAndResolve(artifact.MappingModel());
                }
                if (artifact.DesignerInfo() != null)
                {
                    XmlModelHelper.NormalizeAndResolve(artifact.DesignerInfo());
                }
            }
            else
            {
                // if not a valid namespace, throw an error message
                var msg = string.Format(CultureInfo.CurrentCulture, Resources.InvalidNamespaceName, _newNamespace);
                throw new CommandValidationFailedException(msg);
            }
        }