Browse Source

Fixed FxCop issues in ICSharpCode.AvalonEdit.Xml

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@4909 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 17 years ago
parent
commit
c7b89dc68f
  1. 17
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/AXmlAttributeCollection.cs
  2. 34
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/AXmlContainer.cs
  3. 4
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/AXmlObject.cs
  4. 13
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/CanonicalPrintAXmlVisitor.cs
  5. 8
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/FilteredCollection.cs
  6. 9
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/MergedCollection.cs
  7. 37
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/TagMatchingHeuristics.cs
  8. 2
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/TagReader.cs
  9. 5
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/TrackedSegmentCollection.cs

17
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/AXmlAttributeCollection.cs

@ -16,6 +16,8 @@ namespace ICSharpCode.AvalonEdit.Xml
public class AXmlAttributeCollection: FilteredCollection<AXmlAttribute, AXmlObjectCollection<AXmlObject>> public class AXmlAttributeCollection: FilteredCollection<AXmlAttribute, AXmlObjectCollection<AXmlObject>>
{ {
/// <summary> Empty unbound collection </summary> /// <summary> Empty unbound collection </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes",
Justification = "InsertItem prevents modifying the Empty collection")]
public static readonly AXmlAttributeCollection Empty = new AXmlAttributeCollection(); public static readonly AXmlAttributeCollection Empty = new AXmlAttributeCollection();
/// <summary> Create unbound collection </summary> /// <summary> Create unbound collection </summary>
@ -73,6 +75,10 @@ namespace ICSharpCode.AvalonEdit.Xml
/// <inheritdoc/> /// <inheritdoc/>
protected override void InsertItem(int index, AXmlAttribute item) protected override void InsertItem(int index, AXmlAttribute item)
{ {
// prevent insertions into the static 'Empty' instance
if (this == Empty)
throw new NotSupportedException("Cannot insert into AXmlAttributeCollection.Empty");
AddToHashtable(item); AddToHashtable(item);
item.Changing += item_Changing; item.Changing += item_Changing;
item.Changed += item_Changed; item.Changed += item_Changed;
@ -91,10 +97,17 @@ namespace ICSharpCode.AvalonEdit.Xml
/// <inheritdoc/> /// <inheritdoc/>
protected override void SetItem(int index, AXmlAttribute item) protected override void SetItem(int index, AXmlAttribute item)
{ {
throw new NotSupportedException(); RemoveFromHashtable(this[index]);
this[index].Changing -= item_Changing;
this[index].Changed -= item_Changed;
AddToHashtable(item);
item.Changing += item_Changing;
item.Changed += item_Changed;
base.SetItem(index, item);
} }
// Every item in the collectoin should be registered to these handlers // Every item in the collection should be registered to these handlers
// so that we can handle renames // so that we can handle renames
void item_Changing(object sender, AXmlObjectEventArgs e) void item_Changing(object sender, AXmlObjectEventArgs e)

34
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/AXmlContainer.cs

@ -64,7 +64,15 @@ namespace ICSharpCode.AvalonEdit.Xml
/// <inheritdoc/> /// <inheritdoc/>
public override IEnumerable<AXmlObject> GetSelfAndAllChildren() public override IEnumerable<AXmlObject> GetSelfAndAllChildren()
{ {
return new AXmlObject[] { this }.Flatten(i => i is AXmlContainer ? ((AXmlContainer)i).Children : null); return (new AXmlObject[] { this }).Flatten(
delegate(AXmlObject i) {
AXmlContainer container = i as AXmlContainer;
if (container != null)
return container.Children;
else
return null;
}
);
} }
/// <summary> /// <summary>
@ -77,8 +85,9 @@ namespace ICSharpCode.AvalonEdit.Xml
foreach(AXmlObject child in this.Children) { foreach(AXmlObject child in this.Children) {
if ((child is AXmlAttribute || child is AXmlText) && offset == child.EndOffset) return child; if ((child is AXmlAttribute || child is AXmlText) && offset == child.EndOffset) return child;
if (child.StartOffset < offset && offset < child.EndOffset) { if (child.StartOffset < offset && offset < child.EndOffset) {
if (child is AXmlContainer) { AXmlContainer container = child as AXmlContainer;
return ((AXmlContainer)child).GetChildAtOffset(offset); if (container != null) {
return container.GetChildAtOffset(offset);
} else { } else {
return child; return child;
} }
@ -139,9 +148,10 @@ namespace ICSharpCode.AvalonEdit.Xml
// Dangling object - either a new parser object or removed tree (still cached) // Dangling object - either a new parser object or removed tree (still cached)
item.Parent = this; item.Parent = this;
item.Document = this.Document; item.Document = this.Document;
if (item is AXmlContainer) { AXmlContainer container = item as AXmlContainer;
foreach(AXmlObject child in ((AXmlContainer)item).Children) { if (container != null) {
((AXmlContainer)item).SetParentPointersInTree(child); foreach(AXmlObject child in container.Children) {
container.SetParentPointersInTree(child);
} }
} }
} else if (item.Parent == this) { } else if (item.Parent == this) {
@ -157,7 +167,7 @@ namespace ICSharpCode.AvalonEdit.Xml
// becuase otherwise this item would be included twice => safe to change parents // becuase otherwise this item would be included twice => safe to change parents
// Maintain cache constraint by setting parents to null // Maintain cache constraint by setting parents to null
foreach(AXmlObject ancest in item.GetAncestors().ToList()) { foreach(AXmlObject ancest in item.GetAncestors().ToList()) {
ancest.Parent = null; ancest.Parent = null;
} }
item.Parent = this; item.Parent = this;
// Rest of the tree is consistent - do not recurse // Rest of the tree is consistent - do not recurse
@ -237,8 +247,9 @@ namespace ICSharpCode.AvalonEdit.Xml
// Keep only one item with given offset (we might have several due to deletion) // Keep only one item with given offset (we might have several due to deletion)
srcChildren.Remove(child.StartOffset); srcChildren.Remove(child.StartOffset);
// If contaner that needs updating // If contaner that needs updating
if (child is AXmlContainer && child.LastUpdatedFrom != srcChild) AXmlContainer childAsContainer = child as AXmlContainer;
((AXmlContainer)child).RemoveChildrenNotIn(((AXmlContainer)srcChild).Children); if (childAsContainer != null && child.LastUpdatedFrom != srcChild)
childAsContainer.RemoveChildrenNotIn(((AXmlContainer)srcChild).Children);
i++; i++;
} else { } else {
RemoveChild(i); RemoveChild(i);
@ -261,8 +272,9 @@ namespace ICSharpCode.AvalonEdit.Xml
// Does it need updating? // Does it need updating?
if (child.LastUpdatedFrom != srcChild) { if (child.LastUpdatedFrom != srcChild) {
child.UpdateDataFrom(srcChild); child.UpdateDataFrom(srcChild);
if (child is AXmlContainer) AXmlContainer childAsContainer = child as AXmlContainer;
((AXmlContainer)child).InsertAndUpdateChildrenFrom(((AXmlContainer)srcChild).Children); if (childAsContainer != null)
childAsContainer.InsertAndUpdateChildrenFrom(((AXmlContainer)srcChild).Children);
} }
} else { } else {
InsertChild(i, srcChild); InsertChild(i, srcChild);

4
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/AXmlObject.cs

@ -144,12 +144,16 @@ namespace ICSharpCode.AvalonEdit.Xml
} }
/// <summary> Recursively gets self and all nested nodes. </summary> /// <summary> Recursively gets self and all nested nodes. </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate",
Justification = "Using a method makes the API look more LINQ-like and indicates that the returned collection is computed every time.")]
public virtual IEnumerable<AXmlObject> GetSelfAndAllChildren() public virtual IEnumerable<AXmlObject> GetSelfAndAllChildren()
{ {
return new AXmlObject[] { this }; return new AXmlObject[] { this };
} }
/// <summary> Get all ancestors of this node </summary> /// <summary> Get all ancestors of this node </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate",
Justification = "Using a method makes the API look more LINQ-like and indicates that the returned collection is computed every time.")]
public IEnumerable<AXmlObject> GetAncestors() public IEnumerable<AXmlObject> GetAncestors()
{ {
AXmlObject curr = this.Parent; AXmlObject curr = this.Parent;

13
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/CanonicalPrintAXmlVisitor.cs

@ -39,12 +39,15 @@ namespace ICSharpCode.AvalonEdit.Xml
public override void VisitDocument(AXmlDocument document) public override void VisitDocument(AXmlDocument document)
{ {
foreach(AXmlObject child in document.Children) { foreach(AXmlObject child in document.Children) {
AXmlTag tag = child as AXmlTag; AXmlTag childAsTag = child as AXmlTag;
// Only procssing instructions or elements // Only procssing instructions or elements
if (tag != null && tag.IsProcessingInstruction && tag.Name != "xml") { if (childAsTag != null && childAsTag.IsProcessingInstruction && childAsTag.Name != "xml") {
VisitTag(tag); VisitTag(childAsTag);
} else if (child is AXmlElement) { } else {
VisitElement((AXmlElement)child); AXmlElement childAsElement = child as AXmlElement;
if (childAsElement != null) {
VisitElement(childAsElement);
}
} }
} }
} }

8
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/FilteredCollection.cs

@ -17,9 +17,9 @@ namespace ICSharpCode.AvalonEdit.Xml
/// Collection that presents only some items from the wrapped collection. /// Collection that presents only some items from the wrapped collection.
/// It implicitely filters object that are not of type T (or derived). /// It implicitely filters object that are not of type T (or derived).
/// </summary> /// </summary>
public class FilteredCollection<T, C>: ObservableCollection<T> where C: INotifyCollectionChanged, IList public class FilteredCollection<T, TCollection>: ObservableCollection<T> where TCollection : INotifyCollectionChanged, IList
{ {
C source; TCollection source;
Predicate<object> condition; Predicate<object> condition;
List<int> srcPtrs = new List<int>(); // Index to the original collection List<int> srcPtrs = new List<int>(); // Index to the original collection
@ -27,10 +27,10 @@ namespace ICSharpCode.AvalonEdit.Xml
protected FilteredCollection() {} protected FilteredCollection() {}
/// <summary> Wrap the given collection. Items of type other then T are filtered </summary> /// <summary> Wrap the given collection. Items of type other then T are filtered </summary>
public FilteredCollection(C source) : this (source, x => true) { } public FilteredCollection(TCollection source) : this (source, x => true) { }
/// <summary> Wrap the given collection. Items of type other then T are filtered. Items not matching the condition are filtered. </summary> /// <summary> Wrap the given collection. Items of type other then T are filtered. Items not matching the condition are filtered. </summary>
public FilteredCollection(C source, Predicate<object> condition) public FilteredCollection(TCollection source, Predicate<object> condition)
{ {
this.source = source; this.source = source;
this.condition = condition; this.condition = condition;

9
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/MergedCollection.cs

@ -15,13 +15,14 @@ namespace ICSharpCode.AvalonEdit.Xml
/// <summary> /// <summary>
/// Two collections in sequence /// Two collections in sequence
/// </summary> /// </summary>
public class MergedCollection<T, C>: ObservableCollection<T> where C: INotifyCollectionChanged, IList<T> public class MergedCollection<T, TCollection> : ObservableCollection<T> where TCollection : INotifyCollectionChanged, IList<T>
{ {
C a; TCollection a;
C b; TCollection b;
/// <summary> Create a wrapper containing elements of 'a' and then 'b' </summary> /// <summary> Create a wrapper containing elements of 'a' and then 'b' </summary>
public MergedCollection(C a, C b) [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")]
public MergedCollection(TCollection a, TCollection b)
{ {
this.a = a; this.a = a;
this.b = b; this.b = b;

37
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/TagMatchingHeuristics.cs

@ -153,10 +153,12 @@ namespace ICSharpCode.AvalonEdit.Xml
break; break;
} }
AXmlObject nested = ReadTextOrElement(objStream); AXmlObject nested = ReadTextOrElement(objStream);
if (nested is AXmlElement) {
if (!((AXmlElement)nested).IsProperlyNested) AXmlElement nestedAsElement = nested as AXmlElement;
if (nestedAsElement != null) {
if (!nestedAsElement.IsProperlyNested)
element.IsProperlyNested = false; element.IsProperlyNested = false;
element.AddChildren(Split((AXmlElement)nested).ToList()); element.AddChildren(Split(nestedAsElement).ToList());
} else { } else {
element.AddChild(nested); element.AddChild(nested);
} }
@ -333,15 +335,16 @@ namespace ICSharpCode.AvalonEdit.Xml
return bestConfig.Document.Reverse().ToList(); return bestConfig.Document.Reverse().ToList();
} }
/// <summary> Get posible configurations after considering fiven object </summary> /// <summary> Get posible configurations after considering given object </summary>
Configurations ProcessObject(Configurations oldConfigs, AXmlObject obj) Configurations ProcessObject(Configurations oldConfigs, AXmlObject obj)
{ {
AXmlParser.Log("Processing {0}", obj); AXmlParser.Log("Processing {0}", obj);
AXmlTag tag = obj as AXmlTag; AXmlTag objAsTag = obj as AXmlTag;
AXmlParser.Assert(obj is AXmlTag || obj is AXmlText || obj is AXmlElement, obj.GetType().Name + " not expected"); AXmlElement objAsElement = obj as AXmlElement;
if (obj is AXmlElement) AXmlParser.DebugAssert(objAsTag != null || objAsElement != null || obj is AXmlText, obj.GetType().Name + " not expected");
AXmlParser.Assert(((AXmlElement)obj).IsProperlyNested, "Element not proprly nested"); if (objAsElement != null)
AXmlParser.Assert(objAsElement.IsProperlyNested, "Element not properly nested");
Configurations newConfigs = new Configurations(); Configurations newConfigs = new Configurations();
@ -351,22 +354,22 @@ namespace ICSharpCode.AvalonEdit.Xml
var oldDocument = oldConfig.Document; var oldDocument = oldConfig.Document;
int oldCost = oldConfig.Cost; int oldCost = oldConfig.Cost;
if (tag != null && tag.IsStartTag) { if (objAsTag != null && objAsTag.IsStartTag) {
newConfigs.Add(new Configuration { // Push start-tag (cost 0) newConfigs.Add(new Configuration { // Push start-tag (cost 0)
StartTags = oldStartTags.Push(tag), StartTags = oldStartTags.Push(objAsTag),
Document = oldDocument.Push(tag), Document = oldDocument.Push(objAsTag),
Cost = oldCost, Cost = oldCost,
}); });
} else if (tag != null && tag.IsEndTag) { } else if (objAsTag != null && objAsTag.IsEndTag) {
newConfigs.Add(new Configuration { // Ignore (cost 1) newConfigs.Add(new Configuration { // Ignore (cost 1)
StartTags = oldStartTags, StartTags = oldStartTags,
Document = oldDocument.Push(StartTagPlaceholder).Push(tag), Document = oldDocument.Push(StartTagPlaceholder).Push(objAsTag),
Cost = oldCost + 1, Cost = oldCost + 1,
}); });
if (!oldStartTags.IsEmpty && oldStartTags.Peek().Name != tag.Name) { if (!oldStartTags.IsEmpty && oldStartTags.Peek().Name != objAsTag.Name) {
newConfigs.Add(new Configuration { // Pop 1 item (cost 1) - not mathcing newConfigs.Add(new Configuration { // Pop 1 item (cost 1) - not mathcing
StartTags = oldStartTags.Pop(), StartTags = oldStartTags.Pop(),
Document = oldDocument.Push(tag), Document = oldDocument.Push(objAsTag),
Cost = oldCost + 1, Cost = oldCost + 1,
}); });
} }
@ -375,10 +378,10 @@ namespace ICSharpCode.AvalonEdit.Xml
var doc = oldDocument; var doc = oldDocument;
foreach(AXmlTag poped in oldStartTags) { foreach(AXmlTag poped in oldStartTags) {
popedCount++; popedCount++;
if (poped.Name == tag.Name) { if (poped.Name == objAsTag.Name) {
newConfigs.Add(new Configuration { // Pop 'x' items (cost x-1) - last one is matching newConfigs.Add(new Configuration { // Pop 'x' items (cost x-1) - last one is matching
StartTags = startTags.Pop(), StartTags = startTags.Pop(),
Document = doc.Push(tag), Document = doc.Push(objAsTag),
Cost = oldCost + popedCount - 1, Cost = oldCost + popedCount - 1,
}); });
} }

2
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/TagReader.cs

@ -208,7 +208,7 @@ namespace ICSharpCode.AvalonEdit.Xml
string ReadOpeningBracket() string ReadOpeningBracket()
{ {
// We are using a lot of string literals so that the memory instances are shared // We are using a lot of string literals so that the memory instances are shared
int start = this.CurrentLocation; //int start = this.CurrentLocation;
if (TryRead('<')) { if (TryRead('<')) {
if (TryRead('/')) { if (TryRead('/')) {
return "</"; return "</";

5
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Xml/TrackedSegmentCollection.cs

@ -106,8 +106,9 @@ namespace ICSharpCode.AvalonEdit.Xml
RemoveSyntaxErrorsOf(obj); RemoveSyntaxErrorsOf(obj);
AXmlParser.Log("Stopped tracking {0}", obj); AXmlParser.Log("Stopped tracking {0}", obj);
if (obj is AXmlContainer) { AXmlContainer container = obj as AXmlContainer;
foreach(AXmlObject child in ((AXmlContainer)obj).Children) { if (container != null) {
foreach (AXmlObject child in container.Children) {
RemoveParsedObject(child); RemoveParsedObject(child);
} }
} }

Loading…
Cancel
Save