Exemplo n.º 1
0
        public override void WriteHTML(WriterHtmlDocumentWriter writer)
        {
            if (intListStyle == ParagraphListStyle.BulletedList)
            {
                writer.WriteStartElement("ul");
            }
            else
            {
                writer.WriteStartElement("ol");
            }
            DomElementList list = WriterUtils.MergeElements(this.Elements, false);

            if (list != null && list.Count > 0)
            {
                foreach (DomElement element in list)
                {
                    if (writer.IncludeSelectionOndly == false ||
                        element.HasSelection)
                    {
                        element.WriteHTML(writer);
                    }
                }
            }
            writer.WriteEndElement();
        }
Exemplo n.º 2
0
            public int Compare(HighlightInfo x, HighlightInfo y)
            {
                DomElement element1 = x.OwnerElement;
                DomElement element2 = y.OwnerElement;

                if (element1 == element2)
                {
                    return(0);
                }

                DomElementList parents1 = WriterUtils.GetParentList(element1);

                parents1.Insert(0, element1);
                parents1.Reverse();
                DomElementList parents2 = WriterUtils.GetParentList(element2);

                parents2.Insert(0, element2);
                parents2.Reverse();
                int minLength = Math.Min(parents1.Count, parents2.Count);

                for (int iCount = 1; iCount < minLength; iCount++)
                {
                    int result = parents1[iCount].ElementIndex - parents2[iCount].ElementIndex;
                    if (result != 0)
                    {
                        return(result);
                    }
                }
                return(parents1.Count - parents2.Count);
            }
Exemplo n.º 3
0
 /// <summary>
 /// 文档加载后的处理
 /// </summary>
 /// <param name="format">文档存储格式</param>
 public override void AfterLoad(FileFormat format)
 {
     if (FixElementsForSerialize(false))
     {
         //WriterUtils.SplitElements(this.Elements);
     }
     WriterUtils.SplitElements(this.Elements, false);
     //if (_ElementsForSerialize != null && _ElementsForSerialize.Count > 0)
     //{
     //    this.Elements.Clear();
     //    foreach (XTextElement element in _ElementsForSerialize)
     //    {
     //        if (element is XTextParagraphElement)
     //        {
     //            this.Elements.AddRange(element.Elements);
     //        }
     //        else
     //        {
     //            this.Elements.Add(element);
     //        }
     //    }//foreach
     //    foreach (XTextElement element in this.Elements)
     //    {
     //        element.OwnerDocument = this.OwnerDocument ;
     //        element.Parent = this;
     //    }
     //    WriterUtils.SplitElements(this.Elements);
     //}
     foreach (DomElement element in this.Elements)
     {
         element.Parent = this;
         element.AfterLoad(format);
     }
     base.AfterLoad(format);
 }
Exemplo n.º 4
0
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="newFileName">使用新文件名</param>
        /// <param name="args">事件参数</param>
        /// <returns>操作是否成功</returns>
        public virtual bool SaveDocument(bool newFileName, WriterCommandEventArgs args)
        {
            string      fileName = args.Document.FileName;
            IFileSystem fs       = args.Host.FileSystems.Docuemnt;

            if (args.Parameter is string)
            {
                if (newFileName == false)
                {
                    fileName = (string)args.Parameter;
                }
            }
            else if (args.Parameter is System.IO.Stream)
            {
                System.IO.Stream stream2 = (System.IO.Stream)args.Parameter;
                args.Document.Save(stream2, FileFormat.XML);
                return(true);
            }
            else if (args.Parameter is System.IO.TextWriter)
            {
                System.IO.TextWriter writer = (System.IO.TextWriter)args.Parameter;
                args.Document.Save(writer, FileFormat.XML);
                return(true);
            }
            if (args.ShowUI)
            {
                if (fileName == null || fileName.Trim().Length == 0 || newFileName)
                {
                    fileName = fs.BrowseSave(args.Host.Services, fileName);
                    if (string.IsNullOrEmpty(fileName))
                    {
                        return(false);
                    }
                }
            }//if

            if (fileName == null || fileName.Trim().Length == 0)
            {
                return(false);
            }

            VFileInfo  info   = fs.GetFileInfo(args.Host.Services, fileName);
            FileFormat format = WriterUtils.ParseFileFormat(info.Format);

            System.IO.Stream stream = fs.Save(args.Host.Services, fileName);
            if (stream != null)
            {
                using (stream)
                {
                    args.Document.Save(stream, format);
                }
                return(true);
            }
            else
            {
                // 未能打开文件,保存失败。
                return(false);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Writes the odata.type instance annotation with the specified type name.
        /// </summary>
        /// <param name="typeName">The type name to write.</param>
        public void WriteODataTypeInstanceAnnotation(string typeName)
        {
            Debug.Assert(typeName != null, "typeName != null");

            // "@odata.type": #"typename"
            WriteInstanceAnnotationName(ODataAnnotationNames.ODataType);
            jsonWriter.WriteValue(PrefixTypeName(WriterUtils.RemoveEdmPrefixFromTypeName(typeName)));
        }
Exemplo n.º 6
0
 internal void SetWidthForTab()
 {
     if (_CharValue == '\t')
     {
         this.Width = WriterUtils.GetTabWidth(
             this.Left - this.OwnerDocument.Left,
             this.OwnerDocument.DefaultStyle.TabWidth);
     }
 }
        /// <summary>
        /// Writes the odata.type property annotation for the specified property with the specified type name.
        /// </summary>
        /// <param name="propertyName">The name of the property for which to write the odata.type annotation.</param>
        /// <param name="typeName">The type name to write.</param>
        public void WriteODataTypePropertyAnnotation(string propertyName, string typeName)
        {
            Debug.Assert(!string.IsNullOrEmpty(propertyName), "!string.IsNullOrEmpty(propertyName)");
            Debug.Assert(typeName != null, "typeName != null");

            // "<propertyName>@odata.type": #"typename"
            WritePropertyAnnotationName(propertyName, ODataAnnotationNames.ODataType);
            jsonWriter.WriteValue(WriterUtils.PrefixTypeNameForWriting(typeName, odataVersion));
        }
Exemplo n.º 8
0
 /// <summary>
 /// Writes the m:type attribute for a property given the name of the type.
 /// </summary>
 /// <param name="typeName">The type name to write.</param>
 private void WritePropertyTypeAttribute(string typeName)
 {
     // m:type attribute
     this.XmlWriter.WriteAttributeString(
         AtomConstants.ODataMetadataNamespacePrefix,
         AtomConstants.AtomTypeAttributeName,
         AtomConstants.ODataMetadataNamespace,
         ODataAtomWriterUtils.PrefixTypeName(WriterUtils.RemoveEdmPrefixFromTypeName(typeName)));
 }
        /// <summary>
        /// Writes the odata.type propert annotation for the specified property with the specified type name.
        /// </summary>
        /// <param name="jsonWriter">The JSON writer to write to.</param>
        /// <param name="propertyName">The name of the property for which to write the odata.type annotation.</param>
        /// <param name="typeName">The type name to write.</param>
        internal static void WriteODataTypePropertyAnnotation(IJsonWriter jsonWriter, string propertyName, string typeName)
        {
            Debug.Assert(jsonWriter != null, "jsonWriter != null");
            Debug.Assert(!string.IsNullOrEmpty(propertyName), "!string.IsNullOrEmpty(propertyName)");
            Debug.Assert(typeName != null, "typeName != null");

            // "<propertyName>@odata.type": #"typename"
            WritePropertyAnnotationName(jsonWriter, propertyName, ODataAnnotationNames.ODataType);
            jsonWriter.WriteValue(PrefixTypeName(WriterUtils.RemoveEdmPrefixFromTypeName(typeName)));
        }
Exemplo n.º 10
0
        public void TypeNameShouldBeWrittenIfSpatialValueIsMoreDerivedThanMetadataType()
        {
            var typeFromMetadata = EdmCoreModel.Instance.GetSpatial(EdmPrimitiveTypeKind.Geography, true);
            var typeFromValue    = EdmCoreModel.Instance.GetSpatial(EdmPrimitiveTypeKind.GeographyPoint, false);
            var result           = this.typeNameOracle.GetValueTypeNameForWriting(new ODataPrimitiveValue(Microsoft.Spatial.GeographyPoint.Create(42, 42)),
                                                                                  typeFromMetadata,
                                                                                  typeFromValue,
                                                                                  /* isOpenProperty*/ false);

            result.Should().Be("Edm.GeographyPoint");
            WriterUtils.RemoveEdmPrefixFromTypeName(result).Should().Be("GeographyPoint");
        }
Exemplo n.º 11
0
        public void TypeNameShouldBeWrittenIfSpatialValueIsMoreDerivedThanMetadataType_401()
        {
            var typeFromMetadata = EdmCoreModel.Instance.GetSpatial(EdmPrimitiveTypeKind.Geography, true);
            var typeFromValue    = EdmCoreModel.Instance.GetSpatial(EdmPrimitiveTypeKind.GeographyPoint, false);
            var result           = this.typeNameOracle.GetValueTypeNameForWriting(new ODataPrimitiveValue(Microsoft.Spatial.GeographyPoint.Create(42, 42)),
                                                                                  typeFromMetadata,
                                                                                  typeFromValue,
                                                                                  /* isOpenProperty*/ false);

            Assert.Equal("Edm.GeographyPoint", result);
            Assert.Equal("GeographyPoint", WriterUtils.PrefixTypeNameForWriting(result, ODataVersion.V401));
        }
Exemplo n.º 12
0
        public static string WriteObjectTypeGuids(this Application app)
        {
            var str = new StringBuilder();

            str.AppendLine($"\t{WriterUtils.GetClass("ObjectTypeGuids")}");
            str.AppendLine("\t{");
            foreach (var obj in app.Objects)
            {
                str.AppendLine($"\t\t{WriterUtils.GetString(obj)}");
            }
            str.AppendLine("\t}");
            return(str.ToString());
        }
        /// <summary>
        /// Asynchronously writes the odata.type property annotation for the specified property with the specified type name.
        /// </summary>
        /// <param name="propertyName">The name of the property for which to write the odata.type annotation.</param>
        /// <param name="typeName">The type name to write.</param>
        /// <returns>A task that represents the asynchronous write operation.</returns>
        public async Task WriteODataTypePropertyAnnotationAsync(string propertyName, string typeName)
        {
            this.AssertAsynchronous();
            Debug.Assert(!string.IsNullOrEmpty(propertyName), "!string.IsNullOrEmpty(propertyName)");
            Debug.Assert(typeName != null, "typeName != null");

            // "<propertyName>@odata.type": #"typename"
            await WritePropertyAnnotationNameAsync(propertyName, ODataAnnotationNames.ODataType)
            .ConfigureAwait(false);

            await asyncJsonWriter.WriteValueAsync(WriterUtils.PrefixTypeNameForWriting(typeName, odataVersion))
            .ConfigureAwait(false);
        }
Exemplo n.º 14
0
        public void TypeNameShouldBeWrittenForUndeclaredCollectionProperty()
        {
            var typeFromValue = EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(false));
            var result        = this.typeNameOracle.GetValueTypeNameForWriting(new ODataCollectionValue()
            {
                TypeName = "Collection(String)"
            },
                                                                               null,
                                                                               typeFromValue,
                                                                               /* isOpenProperty*/ true);

            result.Should().Be("Collection(Edm.String)");
            WriterUtils.RemoveEdmPrefixFromTypeName(result).Should().Be("Collection(String)");
        }
Exemplo n.º 15
0
        public void TypeNameShouldBeWrittenForUndeclaredCollectionProperty()
        {
            var typeFromValue = EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(false));
            var result        = this.typeNameOracle.GetValueTypeNameForWriting(new ODataCollectionValue()
            {
                TypeName = "Collection(String)"
            },
                                                                               null,
                                                                               typeFromValue,
                                                                               /* isOpenProperty*/ true);

            Assert.Equal("Collection(Edm.String)", result);
            Assert.Equal("#Collection(String)", WriterUtils.PrefixTypeNameForWriting(result, ODataVersion.V4));
        }
        /// <summary>
        /// Writes the odata.type instance annotation with the specified type name.
        /// </summary>
        /// <param name="typeName">The type name to write.</param>
        /// <param name="writeRawValue">Whether to write the raw typeName without removing/adding prefix 'Edm.'/'#'.</param>
        public void WriteODataTypeInstanceAnnotation(string typeName, bool writeRawValue = false)
        {
            Debug.Assert(typeName != null, "typeName != null");

            // "@odata.type": "#typename"
            WriteInstanceAnnotationName(ODataAnnotationNames.ODataType);
            if (writeRawValue)
            {
                jsonWriter.WriteValue(typeName);
            }
            else
            {
                jsonWriter.WriteValue(WriterUtils.PrefixTypeNameForWriting(typeName, odataVersion));
            }
        }
Exemplo n.º 17
0
        public static string WriteObjectGuids(this Application app)
        {
            var str = new StringBuilder();

            foreach (var obj in classWriter.FilterClassNames(app.Objects))
            {
                str.AppendLine($"\t{WriterUtils.GetClass(WriterUtils.GetFieldGuidClass(obj))}");
                str.AppendLine("\t{");
                foreach (var field in obj.Fields)
                {
                    str.AppendLine($"\t\t{WriterUtils.GetString(field)}");
                }
                str.AppendLine("\t}");
            }
            return(str.ToString());
        }
        internal void RaiseFocusEvent(DomElement oldCurrentElement, DomElement newCurrentElement)
        {
            DomElementList oldParents = oldCurrentElement == null ?
                                        new DomElementList() : WriterUtils.GetParentList2(oldCurrentElement);

            if (oldCurrentElement != null)
            {
                oldParents.Insert(0, oldCurrentElement);
            }
            DomElementList newParents = newCurrentElement == null ?
                                        new DomElementList() : WriterUtils.GetParentList2(newCurrentElement);

            if (newCurrentElement != null)
            {
                newParents.Insert(0, newCurrentElement);
            }
            // 触发旧的容器元素的失去输入焦点事件
            foreach (DomElement oldParent in oldParents)
            {
                if (newParents.Contains(oldParent) == false)
                {
                    //((XTextContainerElement)oldParent).OnLostFocus(this, EventArgs.Empty);

                    DocumentEventArgs args2 = new DocumentEventArgs(
                        this.OwnerDocument,
                        oldParent,
                        DocumentEventStyles.LostFocus);
                    oldParent.HandleDocumentEvent(args2);
                }
            }
            // 触发新的容器元素的获得输入焦点事件
            foreach (DomElement newParent in newParents)
            {
                if (oldParents.Contains(newParent) == false)
                {
                    //((XTextContainerElement)newParent).OnGotFocus(this, EventArgs.Empty);

                    DocumentEventArgs args2 = new DocumentEventArgs(
                        this.OwnerDocument,
                        newParent,
                        DocumentEventStyles.GotFocus);
                    newParent.HandleDocumentEvent(args2);
                }
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// 输出RTF文档
        /// </summary>
        /// <param name="writer">RTF文档书写器</param>
        public override void WriteRTF(DCSoft.CSharpWriter.RTF.RTFContentWriter writer)
        {
            DomElementList list = WriterUtils.MergeParagraphs(
                this.Elements,
                writer.IncludeSelectionOnly);

            if (list != null && list.Count > 0)
            {
                foreach (DomElement element in list)
                {
                    //if (writer.IncludeSelectionOnly == false
                    //    || element.HasSelection)
                    {
                        element.WriteRTF(writer);
                    }
                }//foreach
            }
        }
Exemplo n.º 20
0
        protected virtual void WriteContentHTML(WriterHtmlDocumentWriter writer)
        {
            DomElementList list = WriterUtils.MergeElements(
                this.Elements,
                true);

            if (list != null && list.Count > 0)
            {
                foreach (DomElement element in list)
                {
                    if (writer.IncludeSelectionOndly == false ||
                        element.HasSelection)
                    {
                        element.WriteHTML(writer);
                    }
                }
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// 根据内容创建一个新的文档对象,而且不包含已经被逻辑删除的内容.
        /// </summary>
        /// <returns>创建的文档对象</returns>
        public DomDocument CreateDocument()
        {
            DomDocument         document       = ( DomDocument )this.Document.Clone(false);
            DomDocument         sourceDocument = this.Document;
            DomContainerElement body           = document.Body;
            // 寻找所有内容共同的文档容器元素
            DomElementList parents1 = WriterUtils.GetParentList(this.ContentElements.FirstElement);
            DomElementList parents2 = WriterUtils.GetParentList(this.ContentElements.LastElement);

            foreach (DomContainerElement parent in parents1)
            {
                if (parents2.Contains(parent))
                {
                    DomContentElement ce = parent.ContentElement;
                    CloneElements(parent, ref body);
                    break;
                }
            }

            //CloneElements(this.DocumentContent, ref body );

            // 删除用户操作历史记录信息
            foreach (DocumentContentStyle style in document.ContentStyles.Styles)
            {
                style.CreatorIndex = -1;
                style.DeleterIndex = -1;
            }
            document.UserHistories.Clear();

            // 删除没有引用的样式
            document.DeleteUselessStyle();
            document.EditorControl      = null;
            document.DocumentControler  = null;
            document.HighlightManager   = null;
            document.EditorCurrentStyle = null;
            document.HoverElement       = null;
            if (document.UndoList != null)
            {
                document.EndLogUndo();
                document.UndoList.Clear();
            }
            document.FixDomState();
            return(document);
        }
Exemplo n.º 22
0
        public virtual VFileInfo GetFileInfo(IServiceContainer services, string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }
            VFileInfo info = new VFileInfo();

            try
            {
                if (fileName.IndexOf(":") < 0)
                {
                    fileName    = System.IO.Path.Combine(System.Environment.CurrentDirectory, fileName);
                    info        = new VFileInfo(new System.IO.FileInfo(fileName));
                    info.Format = WriterUtils.GetFormat(fileName).ToString();
                }
                else
                {
                    Uri uri = new Uri(fileName);
                    if (uri.Scheme == Uri.UriSchemeFile)
                    {
                        info        = new VFileInfo(new System.IO.FileInfo(uri.LocalPath));
                        info.Format = WriterUtils.GetFormat(uri.LocalPath).ToString();
                    }
                    else
                    {
                        info.Exists   = true;
                        info.Format   = FileFormat.XML.ToString();
                        info.Name     = fileName;
                        info.FullPath = fileName;
                        info.BasePath = WriterUtils.GetBaseURL(fileName);
                    }
                }
            }
            catch
            {
                // 出现错误,认为文件不存在
                info.Exists   = false;
                info.Name     = fileName;
                info.FullPath = fileName;
            }
            return(info);
        }
        /// <summary>
        /// Asynchronously writes the odata.type instance annotation with the specified type name.
        /// </summary>
        /// <param name="typeName">The type name to write.</param>
        /// <param name="writeRawValue">Whether to write the raw typeName without removing/adding prefix 'Edm.'/'#'.</param>
        /// <returns>A task that represents the asynchronous write operation.</returns>
        public async Task WriteODataTypeInstanceAnnotationAsync(string typeName, bool writeRawValue = false)
        {
            this.AssertAsynchronous();
            Debug.Assert(typeName != null, "typeName != null");

            // "@odata.type": "#typename"
            await WriteInstanceAnnotationNameAsync(ODataAnnotationNames.ODataType)
            .ConfigureAwait(false);

            if (writeRawValue)
            {
                await asyncJsonWriter.WriteValueAsync(typeName).ConfigureAwait(false);
            }
            else
            {
                await asyncJsonWriter.WriteValueAsync(WriterUtils.PrefixTypeNameForWriting(typeName, odataVersion))
                .ConfigureAwait(false);
            }
        }
Exemplo n.º 24
0
        protected void FileOpenUrl(object sender, WriterCommandEventArgs args)
        {
            if (args.Mode == WriterCommandEventMode.QueryState)
            {
                args.Enabled = args.Document != null;
            }
            else if (args.Mode == WriterCommandEventMode.Invoke)
            {
                string url = Convert.ToString(args.Parameter);
                if (args.ShowUI)
                {
                    using (DCSoft.WinForms.Native.dlgInputUrl dlg
                               = new DCSoft.WinForms.Native.dlgInputUrl())
                    {
                        dlg.InputURL = url;
                        if (dlg.ShowDialog(args.EditorControl) == DialogResult.OK)
                        {
                            url = dlg.InputURL;
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                if (string.IsNullOrEmpty(url) == false)
                {
                    url = url.Trim();
                    string basePath = WriterUtils.GetBaseURL(url);

                    args.EditorControl.LoadDocument(url, FileFormat.Html);
                    args.Document.FileName = url;
                    args.Document.Modified = false;
                    args.Document.OnSelectionChanged();
                    args.Document.OnDocumentContentChanged();
                    args.Result       = true;
                    args.RefreshLevel = UIStateRefreshLevel.All;
                }
            }
        }
Exemplo n.º 25
0
        protected void InsertFileContent(object sender, WriterCommandEventArgs args)
        {
            if (args.Mode == WriterCommandEventMode.QueryState)
            {
                args.Enabled = args.DocumentControler != null &&
                               args.Document != null &&
                               args.DocumentControler.CanInsertElementAtCurrentPosition(
                    typeof(DomElement));
            }
            else if (args.Mode == WriterCommandEventMode.Invoke)
            {
                args.Result = false;
                DomDocument document = null;
                string      fileName = null;
                if (args.Parameter is string)
                {
                    fileName = (string)args.Parameter;
                }
                else if (args.Parameter is DomDocument)
                {
                    document = (DomDocument)args.Parameter;
                }
                if (document == null)
                {
                    IFileSystem fs = args.Host.FileSystems.Docuemnt;
                    if (fs != null)
                    {
                        if (args.ShowUI)
                        {
                            // 浏览文件
                            fileName = fs.BrowseOpen(args.Host.Services, fileName);
                        }
                        if (string.IsNullOrEmpty(fileName))
                        {
                            return;
                        }
                        VFileInfo info = fs.GetFileInfo(args.Host.Services, fileName);
                        if (info.Exists == false)
                        {
                            // 文件不存在
                            return;
                        }
                        //打开文件
                        if (args.Host.Debuger != null)
                        {
                            args.Host.Debuger.DebugLoadingFile(fileName);
                        }
                        System.IO.Stream stream = fs.Open(args.Host.Services, fileName);
                        if (stream != null)
                        {
                            FileFormat format = WriterUtils.ParseFileFormat(info.Format);
                            using (stream)
                            {
                                // 读取文件,加载文档对象
                                document         = new DomDocument();
                                document.Options = args.Document.Options;

                                document.ServerObject = args.Document.ServerObject;
                                document.Load(stream, format);
                                if (args.Host.Debuger != null)
                                {
                                    args.Host.Debuger.DebugLoadFileComplete((int)stream.Length);
                                }
                            }
                        }
                    }
                }
                if (document != null &&
                    document.Body != null &&
                    document.Body.Elements.Count > 0)
                {
                    // 导入文档内容
                    DomElementList list = document.Body.Elements;
                    args.Document.ImportElements(list);
                    args.DocumentControler.InsertElements(list);
                    args.Result = list;
                }
            }
        }
Exemplo n.º 26
0
        private void CreateElements(
            HTMLElement rootHtmlElement,
            DomElement rootDomElement,
            DocumentContentStyle currentStyle)
        {
            if (rootHtmlElement.ChildNodes == null)
            {
                return;
            }
            foreach (HTMLElement element in rootHtmlElement.ChildNodes)
            {
                if (element is HTMLCommentElement)
                {
                    // 忽略注释
                    continue;
                }
                string tagName = element.FixTagName;
                switch (tagName)
                {
                case "div":
                {
                    CreateElements(element, rootDomElement, currentStyle);
                    DomElement p = this.DomDocument.CreateElement(typeof(DomParagraphFlagElement));
                    p.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(currentStyle);
                    rootDomElement.Elements.Add(p);
                }
                break;

                case "ul":    // 原点式列表
                case "ol":    // 数值式列表
                    foreach (HTMLElement li in element.ChildNodes)
                    {
                        if (li.FixTagName == "li")
                        {
                            CreateElements(li, rootDomElement, currentStyle);
                            DomParagraphFlagElement flag = (DomParagraphFlagElement)this.DomDocument.CreateElement(typeof(DomParagraphFlagElement));
                            DocumentContentStyle    s    = (DocumentContentStyle)currentStyle.Clone();
                            if (tagName == "ul")
                            {
                                s.BulletedList = true;
                            }
                            else
                            {
                                s.NumberedList = true;
                            }
                            flag.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(s);
                            rootDomElement.Elements.Add(flag);
                        } //if
                    }     //foreach
                    break;

                case "pre":
                {
                    // 预览文本
                    DocumentContentStyle s    = CreateStyle(element, currentStyle);
                    DomElementList       list = this.DomDocument.CreateTextElements(element.InnerText, null, s);
                    rootDomElement.Elements.AddRange(list);
                    DomElement p = this.DomDocument.CreateElement(typeof(DomParagraphFlagElement));
                    p.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(currentStyle);
                    rootDomElement.Elements.Add(p);
                    break;
                }

                case "img":
                {
                    // 图片
                    DocumentContentStyle s = CreateStyle(element, currentStyle);
                    s.BorderColor  = Color.Black;
                    s.BorderLeft   = true;
                    s.BorderTop    = true;
                    s.BorderRight  = true;
                    s.BorderBottom = true;
                    s.BorderWidth  = 0;
                    if (element.HasAttribute("border"))
                    {
                        s.BorderWidth = ToInt32(element.GetAttribute("border"));
                    }
                    DomImageElement img = (DomImageElement)this.DomDocument.CreateElement(typeof(DomImageElement));

                    img.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(s);
                    if (element.HasAttribute("width"))
                    {
                        img.Width = (float )ToLength(element.GetAttribute("width"));
                    }
                    if (element.HasAttribute("height"))
                    {
                        img.Height = ( float )ToLength(element.GetAttribute("height"));
                    }
                    if (element.HasAttribute("id"))
                    {
                        img.ID = element.GetAttribute("id");
                    }
                    if (element.HasAttribute("alt"))
                    {
                        img.Alt = element.GetAttribute("alt");
                    }
                    if (element.HasAttribute("title"))
                    {
                        img.Title = element.GetAttribute("title");
                    }
                    if (element.HasAttribute("src"))
                    {
                        XImageValue v   = new XImageValue();
                        string      url = this.HtmlDocument.GetAbsoluteURL(element.GetAttribute("src"));
                        try
                        {
                            string msg = string.Format(WriterStrings.Downloading_URL, url);
                            if (this.DomDocument.EditorControl != null)
                            {
                                this.DomDocument.EditorControl.SetStatusText(msg);
                            }
                            if (this.DomDocument.Options.BehaviorOptions.DebugMode)
                            {
                                System.Diagnostics.Debug.Write(msg);
                            }
                            int len = v.Load(url);
                            if (this.DomDocument.Options.BehaviorOptions.DebugMode)
                            {
                                System.Diagnostics.Debug.WriteLine(WriterUtils.FormatByteSize(len));
                            }
                        }
                        catch (Exception ext)
                        {
                            img.Alt = url + ":" + ext.Message;
                            if (this.DomDocument.Options.BehaviorOptions.DebugMode)
                            {
                                System.Diagnostics.Debug.WriteLine(WriterStrings.Fail);
                            }
                        }
                        if (this.DomDocument.EditorControl != null)
                        {
                            this.DomDocument.EditorControl.SetStatusText(null);
                        }
                        if (v.HasContent == false)
                        {
                            img.Alt = url;
                            if (img.Width == 0)
                            {
                                img.Width = 300;
                            }
                            if (img.Height == 0)
                            {
                                img.Height = 150;
                            }
                        }
                        img.Image = v;
                    }

                    if (img.Width == 0 || img.Height == 0)
                    {
                        img.UpdateSize();
                    }
                    rootDomElement.Elements.Add(img);
                }
                break;

                case "#text":
                {
                    // 纯文本片段
                    string text = DCSoft.Common.StringFormatHelper.NormalizeSpace(element.Text);
                    text = System.Web.HttpUtility.HtmlDecode(text);
                    if (string.IsNullOrEmpty(text) == false)
                    {
                        DomElementList cs = this._DomDocument.CreateTextElements(
                            text,
                            currentStyle,
                            currentStyle);
                        if (cs != null && cs.Count > 0)
                        {
                            rootDomElement.Elements.AddRange(cs);
                        }
                    }
                }
                break;

                case "p":
                {
                    // 段落
                    DocumentContentStyle ps = CreateStyle(element, currentStyle);
                    CreateElements(element, rootDomElement, ps);
                    DomParagraphFlagElement flag = ( DomParagraphFlagElement )_DomDocument.CreateElement(typeof(DomParagraphFlagElement));
                    flag.StyleIndex = _DomDocument.ContentStyles.GetStyleIndex(ps);
                    rootDomElement.Elements.Add(flag);
                }
                break;

                case "br":
                {
                    // 软回车
                    DomLineBreakElement lb = (DomLineBreakElement)_DomDocument.CreateElement(typeof(DomLineBreakElement));
                    rootDomElement.Elements.Add(lb);
                }
                break;

                case "sup":
                {
                    // 上标
                    DocumentContentStyle ss = ( DocumentContentStyle )currentStyle.Clone();
                    ss.Superscript = true;
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "sub":
                {
                    // 下标
                    DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone();
                    ss.Subscript = true;
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "strong":
                case "b":
                {
                    // 粗体
                    DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone();
                    ss.Bold = true;
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "i":
                {
                    // 斜体
                    DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone();
                    ss.Italic = true;
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "strike":
                {
                    // 删除线
                    DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone();
                    ss.Strikeout = true;
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "a":
                {
                    // 超链接
                    DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone();
                    ss.Color = Color.Blue;
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "font":
                {
                    // 字体
                    DocumentContentStyle ss = CreateStyle(element, currentStyle);
                    if (element.HasAttribute("color"))
                    {
                        // 文字颜色
                        ss.Color = ToColor(element.GetAttribute("color"), Color.Black);
                    }
                    if (element.HasAttribute("face"))
                    {
                        // 字体名称
                        ss.FontName = GetFontName(element.GetAttribute("face"));
                    }
                    if (element.HasAttribute("size"))
                    {
                        // 文字大小
                        int size = ToInt32(element.GetAttribute("size"));
                        switch (size)
                        {
                        case 1: ss.FontSize = 7; break;

                        case 2: ss.FontSize = 10; break;

                        case 3: ss.FontSize = 12; break;

                        case 4: ss.FontSize = 14; break;

                        case 5: ss.FontSize = 18; break;

                        case 6: ss.FontSize = 24; break;

                        case 7: ss.FontSize = 35; break;
                        }
                    }
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "h1":
                case "h2":
                case "h3":
                case "h4":
                case "h5":
                case "h6":
                {
                    // 标题
                    rootDomElement.Elements.Add(this.DomDocument.CreateElement(typeof(DomParagraphFlagElement)));
                    float fz = 9;
                    switch (tagName)
                    {
                    case "h1": fz = 24; break;

                    case "h2": fz = 18; break;

                    case "h3": fz = 13; break;

                    case "h4": fz = 12; break;

                    case "h5": fz = 10; break;

                    case "h6": fz = 8; break;
                    }
                    DocumentContentStyle ss = CreateStyle(element, currentStyle);
                    if (XDependencyObject.HasPropertyValue(ss, "FontSize"))
                    {
                        ss.FontSize = fz;
                    }
                    CreateElements(element, rootDomElement, ss);
                    rootDomElement.Elements.Add(this.DomDocument.CreateElement(typeof(DomParagraphFlagElement)));
                }
                break;

                default:
                {
                    DocumentContentStyle ds = CreateStyle(element, (DocumentContentStyle)currentStyle);
                    CreateElements(element, rootDomElement, ds);
                }
                break;
                } //switch
            }     //foreach
        }
Exemplo n.º 27
0
        private void InnerFileOpen(object sender, WriterCommandEventArgs args)
        {
            if (args.Mode == WriterCommandEventMode.QueryState)
            {
                args.Enabled = args.Document != null && args.EditorControl != null;
            }
            if (args.Mode == WriterCommandEventMode.Invoke)
            {
                IFileSystem fs = args.Host.FileSystems.Docuemnt;
                args.Result = false;
                if (args.Document.Modified)
                {
                    if (QuerySave(args) == false)
                    {
                        return;
                    }
                }


                if (args.Parameter is System.IO.Stream)
                {
                    // 用户参数为一个文件流对象,则以XML的格式从这个流中加载数据
                    System.IO.Stream stream = (System.IO.Stream)args.Parameter;
                    args.EditorControl.LoadDocument(stream, FileFormat.XML);
                    args.Document.FileName = null;
                    args.Result            = true;
                    args.Document.OnSelectionChanged();
                    args.Document.OnDocumentContentChanged();
                    return;
                }

                string fileName = null;
                if (args.Parameter is string)
                {
                    // 用户指定文件名了
                    fileName = (string)args.Parameter;
                    if (fileName.StartsWith("rawxml:"))
                    {
                        // 认为是原生态的XML字符串
                        fileName = fileName.Substring("rawxml:".Length);
                        System.IO.StringReader myStr = new System.IO.StringReader(fileName);
                        args.EditorControl.LoadDocument(myStr, FileFormat.XML);
                        args.Document.FileName = null;
                        args.Result            = true;
                        args.Document.OnSelectionChanged();
                        args.Document.OnDocumentContentChanged();
                        return;
                    }
                }
                if (args.ShowUI)
                {
                    fileName = fs.BrowseOpen(args.Host.Services, fileName);
                    if (string.IsNullOrEmpty(fileName))
                    {
                        // 用户取消操作
                        return;
                    }
                }
                VFileInfo info = fs.GetFileInfo(args.Host.Services, fileName);
                if (info.Exists)
                {
                    FileFormat       format = WriterUtils.ParseFileFormat(info.Format);
                    System.IO.Stream stream = fs.Open(args.Host.Services, fileName);
                    if (args.Host.Debuger != null)
                    {
                        args.Host.Debuger.DebugLoadingFile(fileName);
                    }
                    if (stream != null)
                    {
                        int length = 0;
                        using (stream)
                        {
                            //args.Document.FileName = fileName;
                            args.EditorControl.Document.BaseUrl = System.IO.Path.GetDirectoryName(fileName) + "\\";
                            args.Document.FileName = fileName;
                            args.EditorControl.LoadDocument(stream, format);
                            args.Document.FileName = fileName;
                            length = (int)stream.Length;
                        }
                        if (args.Host.Debuger != null)
                        {
                            args.Host.Debuger.DebugLoadFileComplete(length);
                        }
                        args.Document.OnSelectionChanged();
                        args.Document.OnDocumentContentChanged();
                        args.Document.FileName = fileName;
                        args.Result            = true;
                        args.RefreshLevel      = UIStateRefreshLevel.All;
                    }
                    else
                    {
                        args.Result       = false;
                        args.RefreshLevel = UIStateRefreshLevel.None;
                        return;
                    }
                }
                else
                {
                    if (args.ShowUI)
                    {
                        MessageBox.Show(
                            args.EditorControl,
                            string.Format(WriterStrings.FileNotExist_FileName, fileName),
                            WriterStrings.SystemAlert,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
                    }
                    args.Result = false;
                }
            }
        }