1 | #Region "Microsoft.VisualBasic::d4411acf6097d66a4d5f3b9c0de3cb1a, Microsoft.VisualBasic.Core\ComponentModel\File\ITextFile.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Class ITextFile |
35 | ' |
36 | ' Properties: (+2 Overloads) FilePath |
37 | ' |
38 | ' Function: __getDefaultPath, getEncoding, getPath, Save, ToString |
39 | ' |
40 | ' Sub: CopyTo, (+2 Overloads) Dispose |
41 | ' |
42 | ' |
43 | ' /********************************************************************************/ |
44 | |
45 | #End Region |
46 | |
47 | Imports System.Text |
48 | Imports System.Web.Script.Serialization |
49 | Imports System.Xml.Serialization |
50 | Imports Microsoft.VisualBasic.CommandLine.Reflection |
51 | Imports Microsoft.VisualBasic.Language |
52 | Imports Microsoft.VisualBasic.Scripting.MetaData |
53 | Imports Microsoft.VisualBasic.Text |
54 | |
55 | Namespace ComponentModel |
56 | |
57 | ''' <summary> |
58 | ''' Object model of the text file doucment.(文本文件的对象模型,这个文本文件对象在Disposed的时候会自动保存其中的数据) |
59 | ''' </summary> |
60 | ''' <remarks></remarks> |
61 | Public MustInherit Class ITextFile : Inherits BaseClass |
62 | Implements IDisposable |
63 | Implements ISaveHandle |
64 | Implements IFileReference |
65 | #If NET_40 = 0 Then |
66 | Implements Settings.IProfile |
67 | #End If |
68 | |
69 | ''' <summary> |
70 | ''' The storage filepath of this text file. |
71 | ''' </summary> |
72 | ''' <value></value> |
73 | ''' <returns></returns> |
74 | ''' <remarks></remarks> |
75 | ''' #If NET_40 = 0 Then |
76 | <XmlIgnore> <ScriptIgnore> |
77 | Public Overridable Property FilePath As String Implements Settings.IProfile.FilePath, IFileReference.FilePath |
78 | #Else |
79 | <XmlIgnore> <ScriptIgnore> |
80 | Public Overridable Property FilePath As String |
81 | #End If |
82 | |
83 | #If NET_40 = 0 Then |
84 | Public MustOverride Function Save(Optional path As String = "", Optional encoding As Encoding = Nothing) As Boolean Implements Settings.IProfile.Save, ISaveHandle.Save |
85 | #Else |
86 | Public MustOverride Function Save(Optional FilePath As String = "", Optional Encoding As Encoding = Nothing) As Boolean Implements ISaveHandle.Save |
87 | #End If |
88 | |
89 | Public Overrides Function ToString() As String |
90 | Dim path As String = FilePath |
91 | |
92 | If String.IsNullOrEmpty(path) Then |
93 | Return MyBase.ToString |
94 | Else |
95 | Return path.ToFileURL |
96 | End If |
97 | End Function |
98 | |
99 | Protected Friend Sub CopyTo(Of T As ITextFile)(ByRef TextFile As T) |
100 | TextFile.Extension = Extension |
101 | TextFile.FilePath = FilePath |
102 | End Sub |
103 | |
104 | ''' <summary> |
105 | ''' Automatically determine the path paramater: If the target path is empty, then return |
106 | ''' the file object path <see cref="FilePath"></see> property, if not then return the |
107 | ''' <paramref name="path"></paramref> directly. |
108 | ''' (当<paramref name="path"></paramref>的值不为空的时候,本对象之中的路径参数将会被替换,反之返回本对象的路径参数) |
109 | ''' </summary> |
110 | ''' <param name="path">用户所输入的文件路径</param> |
111 | ''' <returns></returns> |
112 | ''' <remarks></remarks> |
113 | Protected Overridable Function getPath(path As String) As String |
114 | If String.IsNullOrEmpty(path) Then |
115 | path = FilePath |
116 | Else |
117 | FilePath = path |
118 | End If |
119 | |
120 | If String.IsNullOrEmpty(path) Then |
121 | FilePath = __getDefaultPath() |
122 | Return FilePath |
123 | End If |
124 | |
125 | Return path |
126 | End Function |
127 | |
128 | Protected Overridable Function __getDefaultPath() As String |
129 | Return "" |
130 | End Function |
131 | |
132 | Protected Shared Function getEncoding(encoding As Encoding) As Encoding |
133 | If encoding Is Nothing Then |
134 | Return Encoding.Default |
135 | Else |
136 | Return encoding |
137 | End If |
138 | End Function |
139 | |
140 | #Region "IDisposable Support" |
141 | Protected disposedValue As Boolean ' 检测冗余的调用 |
142 | |
143 | ' IDisposable |
144 | Protected Overridable Overloads Sub Dispose(disposing As Boolean) |
145 | If Not Me.disposedValue Then |
146 | If disposing Then |
147 | Call Save(encoding:=UTF8WithoutBOM) |
148 | ' TODO: 释放托管状态(托管对象)。 |
149 | End If |
150 | |
151 | ' TODO: 释放非托管资源(非托管对象)并重写下面的 Finalize()。 |
152 | ' TODO: 将大型字段设置为 null。 |
153 | End If |
154 | Me.disposedValue = True |
155 | End Sub |
156 | |
157 | ' TODO: 仅当上面的 Dispose( disposing As Boolean)具有释放非托管资源的代码时重写 Finalize()。 |
158 | 'Protected Overrides Sub Finalize() |
159 | ' ' 不要更改此代码。 请将清理代码放入上面的 Dispose( disposing As Boolean)中。 |
160 | ' Dispose(False) |
161 | ' MyBase.Finalize() |
162 | 'End Sub |
163 | |
164 | ' Visual Basic 添加此代码是为了正确实现可处置模式。 |
165 | Public Overloads Sub Dispose() Implements IDisposable.Dispose |
166 | ' 不要更改此代码。 请将清理代码放入上面的 Dispose (disposing As Boolean)中。 |
167 | Dispose(True) |
168 | GC.SuppressFinalize(Me) |
169 | End Sub |
170 | #End Region |
171 | |
172 | Public Function Save(Optional path As String = "", Optional encoding As Encodings = Encodings.UTF8) As Boolean Implements ISaveHandle.Save |
173 | Return Save(path, encoding.CodePage) |
174 | End Function |
175 | End Class |
176 | End Namespace |