public SemanticValidator(FileFormatVersions format, ApplicationType app)
        {
            FileFormat = format;
            AppType = app;

            _curReg = new SemanticConstraintRegistry(format, app);
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public SemanticConstraintRegistry(FileFormatVersions format, ApplicationType appType)
        {
            _format = format;
            _appType = appType;

            Initialize();
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the SchemaValidator. 
        /// </summary>
        /// <param name="fileFormat">The target Open XML format.</param>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the "fileFormat" parameter is not FileFormat.Office2007, FileFormat.Office2010 or FileFormat.O15.</exception>
        internal SchemaValidator(FileFormatVersions fileFormat)
        {
            if (fileFormat == FileFormatVersions.Office2007)
            {
                this._sdbSchemaDatas = SdbSchemaDatas.GetOffice2007SchemaDatas();
            }
            else if(fileFormat == FileFormatVersions.Office2010)
            {
                this._sdbSchemaDatas = SdbSchemaDatas.GetOffice2010SchemaDatas();
            }
            else if (fileFormat == FileFormatVersions.Office2013)
            {
                this._sdbSchemaDatas = SdbSchemaDatas.GetOffice2013SchemaDatas();
            }
            else
            {
                string message = String.Format(System.Globalization.CultureInfo.CurrentUICulture,
                                                    ExceptionMessages.FileFormatNotSupported,
                                                    fileFormat);
                throw new ArgumentOutOfRangeException("fileFormat", message);
            }

            this._schemaTypeValidator = new SchemaTypeValidator(this._sdbSchemaDatas);
#if DEBUG
            this._fileFormat = fileFormat;
#endif
        }
        /// <summary>
        /// Initializes a new instance of the ValidationSettings.
        /// </summary>
        /// <param name="fileFormat">The target file format.</param>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the "fileFormat" parameter is not FileFormat.Office2007,  or FileFormat.Office2010 or FileFormat.Office2013.</exception>
        internal ValidationSettings(FileFormatVersions fileFormat)
        {
            fileFormat.ThrowExceptionIfFileFormatNotSupported("fileFormat");
            this.FileFormat = fileFormat;

            this.MaxNumberOfErrors = _defaultMaxNumberOfErrorsReturned;
        }
    /// <summary>
    /// Whether this element is available in a specific version of Office Application.
    /// </summary>
    /// <param name="version">The Office file format version.</param>
    /// <returns>Returns true if the element is defined in the specified version.</returns>
    internal override bool IsInVersion(FileFormatVersions version)
    {
		if((7 & (int)version) > 0)
		{
			return true;
		}
		return false;
    }
        internal AttributeConstraint(XsdAttributeUse xsdAttributeUse, SimpleTypeRestriction simpleTypeConstraint, FileFormatVersions supportedVersion)
        {
            Debug.Assert(simpleTypeConstraint != null);

            this.XsdAttributeUse = xsdAttributeUse;
            this.SimpleTypeConstraint = simpleTypeConstraint;
            this.SupportedVersion = supportedVersion;
        }
        /// <summary>
        /// Register a constraint to this registry.
        /// </summary>
        public void RegisterConstraint(int elementTypeID, int ancestorTypeID, FileFormatVersions fileFormat, ApplicationType appType, SemanticConstraint constraint )
        {
            if ((fileFormat & _format) == _format && (appType & _appType) == _appType)
            {

                AddConstraintToDic(constraint, ancestorTypeID, _cleanList);
                AddConstraintToDic(constraint, elementTypeID, _semConstraintMap);
            }
        }
 /// <summary>
 /// Initialize a instance of PartConstraintRule class.
 /// </summary>
 /// <param name="partClassName">The class name of the part.</param>
 /// <param name="partContentType">The content type of the part.</param>
 /// <param name="minOccursIsNonZero">The MinOccursIsNonZero data.</param>
 /// <param name="maxOccursGreatThanOne">The MaxOccursGreatThanOne data.</param>
 /// <param name="fileFormat">The file format version information.</param>
 internal PartConstraintRule(string partClassName, string partContentType, 
                             bool minOccursIsNonZero, bool maxOccursGreatThanOne,
                             FileFormatVersions fileFormat)
 {
     this.PartClassName = partClassName;
     this.PartContentType = partContentType;
     this.MinOccursIsNonZero = minOccursIsNonZero;
     this.MaxOccursGreatThanOne = maxOccursGreatThanOne;
     this.FileFormat = fileFormat;
 }
Example #9
0
        internal static OpenXmlSimpleType CreateTargetValueObject(RedirectedRestriction redirectedRestriction, FileFormatVersions fileFormatVersion)
        {
            switch (fileFormatVersion)
            {
                case FileFormatVersions.Office2007:
                    return O12UnionHelper.CreateTargetValueObject(redirectedRestriction);

                case FileFormatVersions.Office2010:
                    return O14UnionHelper.CreateTargetValueObject(redirectedRestriction);

                case FileFormatVersions.Office2013:
                    return O15UnionHelper.CreateTargetValueObject(redirectedRestriction);

                default:
                    Debug.Assert(fileFormatVersion == FileFormatVersions.Office2007 || fileFormatVersion == FileFormatVersions.Office2010 || fileFormatVersion == FileFormatVersions.Office2013);
                    break;
            }
            return null;
        }
Example #10
0
        internal static OpenXmlSimpleType[] CreatePossibleMembers(UnionValueRestriction unionValueRestriction, FileFormatVersions fileFormatVersion)
        {
            switch (fileFormatVersion)
            {
                case FileFormatVersions.Office2007:
                    return O12UnionHelper.CreatePossibleMembers(unionValueRestriction);

                case FileFormatVersions.Office2010:
                    return O14UnionHelper.CreatePossibleMembers(unionValueRestriction);

                case FileFormatVersions.Office2013:
                    return O15UnionHelper.CreatePossibleMembers(unionValueRestriction);

                default:
                    Debug.Assert(fileFormatVersion == FileFormatVersions.Office2007 || fileFormatVersion == FileFormatVersions.Office2010 || fileFormatVersion == FileFormatVersions.Office2013);
                    break;
            }
            return null;
        }
 /// <summary>
 /// Initializes a new instance of the OpenXmlValidator.
 /// </summary>
 /// <param name="fileFormat">The target filr format to be validated aginst.</param>
 /// <remarks>
 /// Default to FileFormat.Office2007.
 /// </remarks>
 /// <exception cref="ArgumentOutOfRangeException">Thrown when the "fileFormat" parameter is not FileFormat.Office2007, FileFormat.Office2010 or FileFormat.O15.</exception>
 public OpenXmlValidator(FileFormatVersions fileFormat)
 {
     fileFormat.ThrowExceptionIfFileFormatNotSupported("fileFormat");
     this._settings = new ValidationSettings(fileFormat);
 }
        /// <summary>
        /// Trys to create an OpenXmlElement from the specified namesapce URI and local name.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="fileFormat">The specified file format version.</param>
        /// <param name="namespaceUri">The namespace URI of the requested child element.</param>
        /// <param name="localName">The local name of the requested child element.</param>
        /// <returns>A new OpenXmlElement if the parent element can contains a child with the specified namespace and local name. Otherwise, returns null.</returns>
        internal static OpenXmlElement TryCreateValidChild(this OpenXmlElement parent, FileFormatVersions fileFormat, string namespaceUri, string localName)
        {
            Debug.Assert(parent is OpenXmlCompositeElement);
            Debug.Assert(localName != null);

            var newElement = parent.ElementFactory(string.Empty, localName, namespaceUri);
            if (newElement is OpenXmlUnknownElement || !newElement.IsInVersion(fileFormat))
            {
                return null;
            }
            return newElement;
        }
Example #13
0
 public static bool IsInFileFormat(string ns,FileFormatVersions format)
 {
     if (format == FileFormatVersions.Office2007)
     {
         if (_O12NamespaceSet.Contains(ns))
         {
             return true;
         }
         return false;
     }
     else if (format == FileFormatVersions.Office2010)
     {
         if (_O14NamespaceSet.Contains(ns))
         {
             return true;
         }
         return false;
     }
     else if (format == FileFormatVersions.Office2013)
     {
         if (_O15NamespaceSet.Contains(ns))
         {
             return true;
         }
         return false;
     }
     throw new NotImplementedException();
 }
 /// <summary>
 /// Initialize a new instance of ChildElementTypeAttribute.
 /// </summary>
 /// <param name="elementType">Specifies the type of the possible child element.</param>
 public ChildElementInfoAttribute(Type elementType)
 {
     _type = elementType;
     format = FileFormatVersions.Office2007 | FileFormatVersions.Office2010;
 }
 /// <summary>
 /// Initialize a new instance of ChildElementTypeAttribute.
 /// </summary>
 /// <param name="elementType">Specifies the type of the possible child element.</param>
 /// <param name="availableInOfficeVersion">Specifies the office version(s) where the child element is available.</param>
 public ChildElementInfoAttribute(Type elementType, FileFormatVersions availableInOfficeVersion)
 {
     _type = elementType;
     format = availableInOfficeVersion;
 }
 /// <summary>
 /// Whether this element is available in a specific version of Office Application.
 /// For OpenXmlUnknownElement, always return false, no matter what the version is.
 /// </summary>
 /// <param name="version">The Office file format version.</param>
 /// <returns>Returns true if the element is defined in the specified version.</returns>
 internal override bool IsInVersion(FileFormatVersions version)
 {
     return false;
 }
Example #17
0
        internal ElementAction GetElementAction(OpenXmlElement element, FileFormatVersions format)
        {
            if (format == (FileFormatVersions.Office2010 | FileFormatVersions.Office2007) || format == (FileFormatVersions.Office2010 | FileFormatVersions.Office2007 | FileFormatVersions.Office2013))
            {
                return ElementAction.Normal;
            }

            if (element is AlternateContent)
            {
                return ElementAction.ACBlock;
            } 
            
            if (element.IsInVersion(format))
            {
                return ElementAction.Normal;
            }

            if (IsIgnorableNs(element.NamespaceUri))
            {
                if (IsPreservedElement(element.NamespaceUri, element.LocalName))
                {
                    return ElementAction.Normal;
                }
                if (IsProcessContent(element.NamespaceUri, element.LocalName))
                {
                    return ElementAction.ProcessContent;
                }
                return ElementAction.Ignore;
            }

            return ElementAction.Normal;
        }
 /// <summary>
 /// Determines whether the source FileFormatVersions includes the target FileFormatVersions. 
 /// </summary>
 /// <param name="source">The source FileFormatVersions to be tested.</param>
 /// <param name="target">The target FileFormatVersions be tested against.</param>
 /// <returns>Returns true when (source &amp; target) == target.</returns>
 internal static bool Includes(this FileFormatVersions source, FileFormatVersions target)
 {
     return (source & target) == target;
 }
        internal static OpenXmlElement GetNextChildMc(this OpenXmlElement parent, OpenXmlElement child, MCContext mcContext, FileFormatVersions format)
        {
            var next = child.GetNextNonMiscElementSibling();
            var mcTier = child.Parent;

            if (next == null && mcTier != parent)
            {
                // the child must be under element in ProcessContent or ACB
                if (mcTier is AlternateContentChoice || mcTier is AlternateContentFallback)
                {
                    mcTier = mcTier.Parent;
                }
                Debug.Assert(mcTier != null);

                // there is no more next sibling in this level, then try to find the next siblig of the up level.
                return parent.GetNextChildMc(mcTier, mcContext, format);
            }

            return parent.GetChildMc(next, mcContext, format);
        }
 /// <summary>
 /// Initializes a new instance of the OfficeAvailabilityAttribute class.
 /// </summary>
 /// <param name="officeVersion">The Office version where this class or property is available. 
 /// If there is more than one version, use bitwise OR to specify multiple versions.</param>
 public OfficeAvailabilityAttribute(FileFormatVersions officeVersion)
 {
     OfficeVersion = officeVersion;
 }
 public static bool IsInFileFormat(string ns, FileFormatVersions format)
 {
     if (format == FileFormatVersions.Office2007)
     {
         if (NamespaceIdMap._O12NamespaceSet.Contains(ns))
             return true;
         else
             return false;
     }
     else
     {
         if (format != FileFormatVersions.Office2010)
             throw new NotImplementedException();
         if (NamespaceIdMap._O14NamespaceSet.Contains(ns))
             return true;
         else
             return false;
     }
 }
 internal static OpenXmlElement GetFirstChildMc(this OpenXmlElement parent, MCContext mcContext, FileFormatVersions format)
 {
     var child = parent.GetFirstNonMiscElementChild();
     return parent.GetChildMc(child, mcContext, format);
 }
Example #23
0
        internal OpenXmlCompositeElement GetContentFromACBlock(AlternateContent acblk, FileFormatVersions format)
        {
            Debug.Assert(format != (FileFormatVersions.Office2007 | FileFormatVersions.Office2010 | FileFormatVersions.Office2013));

            foreach (var choice in acblk.ChildElements.OfType<AlternateContentChoice>())
            {
                if(choice.Requires == null)
                {
                    //should we throw exception here?
                    continue;
                }
                string reqs = choice.Requires.InnerText.Trim();
                if (string.IsNullOrEmpty(reqs))
                {
                    //should we throw exception here?
                    continue;
                }

                bool chooce = true;
                foreach (var req in reqs.Split(new char[] { ' ' }))
                {
                    //fix bug 537858
                    //the LookupNamespaceDeleget is from xmlReader
                    //bug when we try to GetContentFromACBlock, the reader has already moved to the next element of ACB
                    //so we should use the element's LookupNamespace function to find it
                    //string ns = LookupNamespaceDelegate(req);
                    string ns = choice.LookupNamespace(req);
                    if (ns == null)
                    {
                        if (this._noExceptionOnError)
                        {
                            chooce = false;
                            break;
                        }
                        else
                        {
                            var msg = String.Format(System.Globalization.CultureInfo.CurrentUICulture, ExceptionMessages.UnknowMCContent, req);
                            throw new InvalidMCContentException(msg);
                        }
                    }
                    if (!NamespaceIdMap.IsInFileFormat(ns, format))
                    {
                        chooce = false;
                        break;
                    }
                }

                if (chooce)
                {
                    return choice;
                }
            }
            var fallback = acblk.GetFirstChild<AlternateContentFallback>();
            if (fallback != null)
            {
                return fallback;
            }
            return null;
        }
        private static OpenXmlElement GetChildMc(this OpenXmlElement parent, OpenXmlElement child, MCContext mcContext, FileFormatVersions format)
        {
            // Use stack to cache the next siblings in different levels.
            Stack<OpenXmlElement> nextSiblings = new Stack<OpenXmlElement>();

            while (child != null)
            {
                var acb = child as AlternateContent;
                if (acb == null && child.IsInVersion(format))
                {
                    return child;
                }
                else
                {
                    mcContext.PushMCAttributes2(child.MCAttributes, child.LookupNamespace);
                    if (acb != null)
                    {
                        nextSiblings.Push(child.GetNextNonMiscElementSibling());
                        var select = mcContext.GetContentFromACBlock(acb, format);
                        if (select != null)
                        {
                            child = select.GetFirstNonMiscElementChild();
                        }
                        else
                        {
                            // The ACB has no children elements. 
                            // case like: <acb/> <acb><choice/><fallback/></acb>
                            child = null;
                        }
                    }
                    else
                    {
                        // Ignorable element, skip it
                        if (mcContext.IsIgnorableNs(child.NamespaceUri))
                        {
                            // Any element marked with ProcessContent should be an Ignorable Element
                            if (mcContext.IsProcessContent(child))
                            {
                                nextSiblings.Push(child.GetNextNonMiscElementSibling());
                                //
                                child = child.GetFirstNonMiscElementChild();
                            }
                            else
                            {
                                child = child.GetNextNonMiscElementSibling();
                            }
                        }
                        else
                        {
                            mcContext.PopMCAttributes2();
                            return child;
                        }
                    }
                    mcContext.PopMCAttributes2();
                }

                while (child == null && nextSiblings.Count > 0)
                {
                    child = nextSiblings.Pop();
                }
            }

            // child is null.
            return child;
        }
Example #25
0
        internal AttributeAction GetAttributeAction(string ns, string localName, FileFormatVersions format)
        {
            if (format == (FileFormatVersions.Office2010|FileFormatVersions.Office2007) || format == (FileFormatVersions.Office2010|FileFormatVersions.Office2007|FileFormatVersions.Office2013))
            {
                return AttributeAction.Normal;
            }

            if (string.IsNullOrEmpty(ns))
            {
                return AttributeAction.Normal;
            }

            if (NamespaceIdMap.IsInFileFormat(ns, format))
            {
                return AttributeAction.Normal;
            }

            if (!IsIgnorableNs(ns))
            {
                return AttributeAction.Normal;
            }

            if (IsPreservedAttribute(ns, localName))
            {
                return AttributeAction.Normal;
            }

            return AttributeAction.Ignore;
        }