Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.Xml-5.5.0/AXmlParser.cs @ 15938

Last change on this file since 15938 was 11804, checked in by jkarder, 10 years ago

#2077:

  • added fancy xml documentation
  • fixed configurations and plattforms
File size: 6.5 KB
Line 
1// Copyright (c) 2009-2013 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.Generic;
21using System.Diagnostics;
22using System.Linq;
23using System.Threading;
24using ICSharpCode.NRefactory.Editor;
25using ICSharpCode.NRefactory.Utils;
26
27namespace ICSharpCode.NRefactory.Xml
28{
29  /// <summary>
30  /// XML parser that is error tolerant.
31  /// </summary>
32  public class AXmlParser
33  {
34    /// <summary>
35    /// Generate syntax error when seeing entity reference other then the built-in ones
36    /// </summary>
37    public bool UnknownEntityReferenceIsError { get; set; }
38   
39    IList<AXmlObject> CreatePublic(IList<InternalObject> internalObjects)
40    {
41      var publicObjects = new AXmlObject[internalObjects.Count];
42      int pos = 0;
43      for (int i = 0; i < internalObjects.Count; i++) {
44        publicObjects[i] = internalObjects[i].CreatePublicObject(null, pos);
45        pos += internalObjects[i].Length;
46      }
47      return Array.AsReadOnly(publicObjects);
48    }
49   
50    /// <summary>
51    /// Parses a document into a flat list of tags.
52    /// </summary>
53    /// <returns>Parsed tag soup.</returns>
54    public IList<AXmlObject> ParseTagSoup(ITextSource textSource,
55                                          CancellationToken cancellationToken = default(CancellationToken))
56    {
57      if (textSource == null)
58        throw new ArgumentNullException("textSource");
59      var reader = new TagReader(this, textSource, false);
60      var internalObjects = reader.ReadAllObjects(cancellationToken);
61      return CreatePublic(internalObjects);
62    }
63   
64    /// <summary>
65    /// Parses a document incrementally into a flat list of tags.
66    /// </summary>
67    /// <param name="oldParserState">The parser state from a previous call to ParseIncremental(). Use null for the first call.</param>
68    /// <param name="newTextSource">The text source for the new document version.</param>
69    /// <param name="newParserState">Out: the new parser state, pass this to the next ParseIncremental() call.</param>
70    /// <param name="cancellationToken">Optional: cancellation token.</param>
71    /// <returns>Parsed tag soup.</returns>
72    public IList<AXmlObject> ParseTagSoupIncremental(
73      IncrementalParserState oldParserState, ITextSource newTextSource, out IncrementalParserState newParserState,
74      CancellationToken cancellationToken = default(CancellationToken))
75    {
76      if (newTextSource == null)
77        throw new ArgumentNullException("newTextSource");
78      var internalObjects = InternalParseIncremental(oldParserState, newTextSource, out newParserState, false, cancellationToken);
79      return CreatePublic(internalObjects);
80    }
81   
82    List<InternalObject> InternalParseIncremental(
83      IncrementalParserState oldParserState, ITextSource newTextSource, out IncrementalParserState newParserState,
84      bool collapseProperlyNestedElements, CancellationToken cancellationToken)
85    {
86      var reader = new TagReader(this, newTextSource, collapseProperlyNestedElements);
87      ITextSourceVersion newVersion = newTextSource.Version;
88      var reuseMap = oldParserState != null ? oldParserState.GetReuseMapTo(newVersion) : null;
89     
90      List<InternalObject> internalObjects;
91      if (reuseMap != null)
92        internalObjects = reader.ReadAllObjectsIncremental(oldParserState.Objects, reuseMap, cancellationToken);
93      else
94        internalObjects = reader.ReadAllObjects(cancellationToken);
95     
96      if (newVersion != null)
97        newParserState = new IncrementalParserState(newTextSource.TextLength, newVersion, internalObjects.ToArray());
98      else
99        newParserState = null;
100     
101      return internalObjects;
102    }
103   
104    /// <summary>
105    /// Parses a document.
106    /// </summary>
107    public AXmlDocument Parse(ITextSource textSource, CancellationToken cancellationToken = default(CancellationToken))
108    {
109      if (textSource == null)
110        throw new ArgumentNullException("textSource");
111      var reader = new TagReader(this, textSource, true);
112      var internalObjects = reader.ReadAllObjects(cancellationToken);
113      var heuristic = new TagMatchingHeuristics(textSource);
114      return new AXmlDocument(null, 0, heuristic.CreateDocument(internalObjects, cancellationToken));
115    }
116   
117    /// <summary>
118    /// Parses a document incrementally into a flat list of tags.
119    /// </summary>
120    /// <param name="oldParserState">The parser state from a previous call to ParseIncremental(). Use null for the first call.</param>
121    /// <param name="newTextSource">The text source for the new document version.</param>
122    /// <param name="newParserState">Out: the new parser state, pass this to the next ParseIncremental() call.</param>
123    /// <param name="cancellationToken">Optional: cancellation token.</param>
124    /// <returns>Parsed tag soup.</returns>
125    public AXmlDocument ParseIncremental(
126      IncrementalParserState oldParserState, ITextSource newTextSource, out IncrementalParserState newParserState,
127      CancellationToken cancellationToken = default(CancellationToken))
128    {
129      if (newTextSource == null)
130        throw new ArgumentNullException("newTextSource");
131      var internalObjects = InternalParseIncremental(oldParserState, newTextSource, out newParserState, true, cancellationToken);
132      var heuristic = new TagMatchingHeuristics(newTextSource);
133      return new AXmlDocument(null, 0, heuristic.CreateDocument(internalObjects, cancellationToken));
134    }
135   
136    /// <summary>
137    /// Checks whether the given name is a valid XML name.
138    /// </summary>
139    public static bool IsValidXmlName(string name)
140    {
141      if (string.IsNullOrWhiteSpace(name))
142        throw new ArgumentException("The XML name cannot be null, empty or consist solely of white space", "name");
143      return TagReader.IsValidName(name);
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.