📄 xmlview.cs
字号:
#region IPrintable interface
public PrintDocument PrintDocument{
get {
return xmlEditor.PrintDocument;
}
}
#endregion
#region ITextEditorControlProvider interface
public TextEditorControl TextEditorControl {
get {
return xmlEditor;
}
}
#endregion
#region IPositionable interface
/// <summary>
/// Moves the cursor to the specified line and column.
/// </summary>
public void JumpTo(int line, int column)
{
xmlEditor.ActiveTextAreaControl.JumpTo(line, column);
}
#endregion
protected override void OnFileNameChanged(EventArgs e)
{
base.OnFileNameChanged(e);
xmlEditor.FileName = base.FileName;
}
static bool IsFileReadOnly(string fileName)
{
return (File.GetAttributes(fileName) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
}
/// <summary>
/// Checks that the file extension refers to an xml file as
/// specified in the SyntaxModes.xml file.
/// </summary>
static bool IsXmlFileExtension(string extension)
{
bool isXmlFileExtension = false;
IHighlightingStrategy strategy = HighlightingManager.Manager.FindHighlighter(XmlView.Language);
if (strategy != null) {
foreach (string currentExtension in strategy.Extensions) {
if (String.Compare(extension, currentExtension, true) == 0) {
isXmlFileExtension = true;
break;
}
}
}
return isXmlFileExtension;
}
/// <summary>
/// Forces the editor to update its folds.
/// </summary>
void UpdateFolding()
{
xmlEditor.Document.FoldingManager.UpdateFoldings(String.Empty, null);
RefreshMargin();
}
/// <summary>
/// Repaints the folds in the margin.
/// </summary>
void RefreshMargin()
{
WorkbenchSingleton.SafeThreadAsyncCall((RefreshDelegate)xmlEditor.ActiveTextAreaControl.TextArea.Refresh,
xmlEditor.ActiveTextAreaControl.TextArea.FoldMargin);
}
/// <summary>
/// Sets the dirty flag since the document has changed.
/// </summary>
void DocumentChanged(object sender, DocumentEventArgs e)
{
IsDirty = true;
}
/// <summary>
/// Updates the line, col, overwrite/insert mode in the status bar.
/// </summary>
void CaretUpdate(object sender, EventArgs e)
{
CaretChanged(sender, e);
CaretModeChanged(sender, e);
}
/// <summary>
/// Updates the line, col information in the status bar.
/// </summary>
void CaretChanged(object sender, EventArgs e)
{
Point pos = xmlEditor.Document.OffsetToPosition(xmlEditor.ActiveTextAreaControl.Caret.Offset);
LineSegment line = xmlEditor.Document.GetLineSegment(pos.Y);
StatusBarService.SetCaretPosition(pos.X + 1, pos.Y + 1, xmlEditor.ActiveTextAreaControl.Caret.Offset - line.Offset + 1);
}
/// <summary>
/// Updates the insert/overwrite mode text in the status bar.
/// </summary>
void CaretModeChanged(object sender, EventArgs e)
{
StatusBarService.SetInsertMode(xmlEditor.ActiveTextAreaControl.Caret.CaretMode == CaretMode.InsertMode);
}
/// <summary>
/// Creates the file system watcher.
/// </summary>
void SetWatcher()
{
try {
if (this.watcher == null) {
this.watcher = new FileSystemWatcher();
this.watcher.Changed += new FileSystemEventHandler(this.OnFileChangedEvent);
} else {
this.watcher.EnableRaisingEvents = false;
}
this.watcher.Path = Path.GetDirectoryName(xmlEditor.FileName);
this.watcher.Filter = Path.GetFileName(xmlEditor.FileName);
this.watcher.NotifyFilter = NotifyFilters.LastWrite;
this.watcher.EnableRaisingEvents = true;
} catch (Exception) {
watcher = null;
}
}
/// <summary>
/// Shows the "File was changed" dialog if the file was
/// changed externally.
/// </summary>
void GotFocusEvent(object sender, EventArgs e)
{
lock (this) {
if (wasChangedExternally) {
wasChangedExternally = false;
string message = StringParser.Parse("${res:ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor.TextEditorDisplayBinding.FileAlteredMessage}", new string[,] {{"File", Path.GetFullPath(xmlEditor.FileName)}});
if (MessageService.AskQuestion(message, "${res:MainWindow.DialogName}")) {
Load(xmlEditor.FileName);
} else {
IsDirty = true;
}
}
}
}
void OnFileChangedEvent(object sender, FileSystemEventArgs e)
{
if(e.ChangeType != WatcherChangeTypes.Deleted) {
wasChangedExternally = true;
if (xmlEditor.IsHandleCreated)
xmlEditor.BeginInvoke(new MethodInvoker(OnFileChangedEventInvoked));
}
}
void OnFileChangedEventInvoked()
{
if (((Form)ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.Workbench).Focused) {
GotFocusEvent(this, EventArgs.Empty);
}
}
/// <summary>
/// Gets the xml validation output window.
/// </summary>
MessageViewCategory Category {
get {
if (category == null) {
category = new MessageViewCategory(CategoryName);
CompilerMessageView cmv = (CompilerMessageView)WorkbenchSingleton.Workbench.GetPad(typeof(CompilerMessageView)).PadContent;
cmv.AddCategory(category);
}
return category;
}
}
/// <summary>
/// Brings output window pad to the front.
/// </summary>
void ShowOutputWindow()
{
WorkbenchSingleton.Workbench.GetPad(typeof(CompilerMessageView)).BringPadToFront();
}
/// <summary>
/// Writes a line of text to the output window.
/// </summary>
/// <param name="message">The message to send to the output
/// window.</param>
void OutputWindowWriteLine(string message)
{
LoggingService.Info("WriteLine message=" + message);
Category.AppendText(String.Concat(message, Environment.NewLine));
}
bool ShowErrorListAfterBuild {
get {
return PropertyService.Get("SharpDevelop.ShowErrorListAfterBuild", true);
}
}
void ShowErrorList()
{
if (ShowErrorListAfterBuild && TaskService.SomethingWentWrong) {
WorkbenchSingleton.Workbench.GetPad(typeof(ErrorListPad)).BringPadToFront();
}
}
void AddTask(string fileName, string message, int column, int line, TaskType taskType)
{
TaskService.Add(new Task(fileName, message, column, line, taskType));
}
/// <summary>
/// Displays the validation error.
/// </summary>
void DisplayValidationError(string fileName, string message, int column, int line)
{
OutputWindowWriteLine(message);
OutputWindowWriteLine(String.Empty);
OutputWindowWriteLine(StringParser.Parse("${res:MainWindow.XmlValidationMessages.ValidationFailed}"));
AddTask(fileName, message, column, line, TaskType.Error);
}
/// <summary>
/// Updates the default schema associated with the xml editor.
/// </summary>
void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
string extension = Path.GetExtension(xmlEditor.FileName).ToLowerInvariant();
if (e.Key == extension) {
SetDefaultSchema(extension);
} else if (e.Key == XmlEditorAddInOptions.ShowAttributesWhenFoldedPropertyName) {
UpdateFolding();
xmlEditor.Refresh();
}
}
/// <summary>
/// Sets the default schema and namespace prefix that the xml editor will use.
/// </summary>
void SetDefaultSchema(string extension)
{
xmlEditor.DefaultSchemaCompletionData = XmlSchemaManager.GetSchemaCompletionData(extension);
xmlEditor.DefaultNamespacePrefix = XmlSchemaManager.GetNamespacePrefix(extension);
}
/// <summary>
/// Updates the default schema association since the schema
/// may have been added.
/// </summary>
void UserSchemaAdded(object source, EventArgs e)
{
SetDefaultSchema(Path.GetExtension(xmlEditor.FileName).ToLowerInvariant());
}
/// <summary>
/// Updates the default schema association since the schema
/// may have been removed.
/// </summary>
void UserSchemaRemoved(object source, EventArgs e)
{
SetDefaultSchema(Path.GetExtension(xmlEditor.FileName).ToLowerInvariant());
}
/// <summary>
/// Displays the transformed output.
/// </summary>
void ShowTransformOutput(string xml)
{
// Pretty print the xml.
xml = SimpleFormat(IndentedFormat(xml));
// Display the output xml.
XslOutputView view = XslOutputView.Instance;
if (view == null) {
view = new XslOutputView();
view.LoadContent(xml);
WorkbenchSingleton.Workbench.ShowView(view);
} else {
// Transform output window already opened.
view.LoadContent(xml);
view.WorkbenchWindow.SelectWindow();
}
}
/// <summary>
/// Returns a formatted xml string using a simple formatting algorithm.
/// </summary>
static string SimpleFormat(string xml)
{
return xml.Replace("><", ">\r\n<");
}
/// <summary>
/// Runs an XSL transform on the input xml.
/// </summary>
/// <param name="input">The input xml to transform.</param>
/// <param name="transform">The transform xml.</param>
/// <returns>The output of the transform.</returns>
static string Transform(string input, string transform)
{
StringReader inputString = new StringReader(input);
XPathDocument sourceDocument = new XPathDocument(inputString);
StringReader transformString = new StringReader(transform);
XPathDocument transformDocument = new XPathDocument(transformString);
XslCompiledTransform xslTransform = new XslCompiledTransform();
xslTransform.Load(transformDocument, XsltSettings.Default, new XmlUrlResolver());
MemoryStream outputStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(outputStream, Encoding.UTF8);
xslTransform.Transform(sourceDocument, null, writer);
int preambleLength = Encoding.UTF8.GetPreamble().Length;
byte[] outputBytes = outputStream.ToArray();
return UTF8Encoding.UTF8.GetString(outputBytes, preambleLength, outputBytes.Length - preambleLength);
}
/// <summary>
/// Returns a pretty print version of the given xml.
/// </summary>
/// <param name="xml">Xml string to pretty print.</param>
/// <returns>A pretty print version of the specified xml. If the
/// string is not well formed xml the original string is returned.
/// </returns>
static string IndentedFormat(string xml)
{
string indentedText = String.Empty;
try
{
XmlTextReader reader = new XmlTextReader(new StringReader(xml));
reader.WhitespaceHandling = WhitespaceHandling.None;
StringWriter indentedXmlWriter = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(indentedXmlWriter);
writer.Indentation = 1;
writer.IndentChar = '\t';
writer.Formatting = Formatting.Indented;
writer.WriteNode(reader, false);
writer.Flush();
indentedText = indentedXmlWriter.ToString();
}
catch(Exception)
{
indentedText = xml;
}
return indentedText;
}
/// <summary>
/// Checks that the xml in this view is well-formed.
/// </summary>
bool IsWellFormed {
get {
try
{
XmlDocument Document = new XmlDocument( );
Document.LoadXml(Text);
return true;
}
catch(XmlException ex)
{
string fileName = FileName;
if (fileName == null || fileName.Length == 0) {
fileName = TitleName;
}
AddTask(fileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1, TaskType.Error);
}
return false;
}
}
/// <summary>
/// Validates the given xsl string,.
/// </summary>
bool IsValidXsl(string xml)
{
try
{
WorkbenchSingleton.Workbench.GetPad(typeof(CompilerMessageView)).BringPadToFront();
StringReader reader = new StringReader(xml);
XPathDocument doc = new XPathDocument(reader);
XslCompiledTransform xslTransform = new XslCompiledTransform();
xslTransform.Load(doc, XsltSettings.Default, new XmlUrlResolver());
return true;
}
catch(XsltCompileException ex)
{
string message = String.Empty;
if(ex.InnerException != null)
{
message = ex.InnerException.Message;
}
else
{
message = ex.ToString();
}
AddTask(StylesheetFileName, message, ex.LineNumber - 1, ex.LinePosition - 1, TaskType.Error);
}
catch(XsltException ex)
{
AddTask(StylesheetFileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1, TaskType.Error);
}
catch(XmlException ex)
{
AddTask(StylesheetFileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1, TaskType.Error);
}
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -