/// <summary> /// Adds a test pattern /// </summary> private DtdTestpattern CreateTestPattern(string elementName, XmlCursorPos cursorPos) { DtdTestpattern testPattern; var node = cursorPos.ActualNode; System.Xml.XmlNode sibling; switch (cursorPos.PosOnNode) { case XmlCursorPositions.CursorInsideTheEmptyNode: // The parent node is empty, so we only have to test for the allowed elements in it and not expect any sibling elements on the same level testPattern = new DtdTestpattern(elementName, Dtd.GetElementNameFromNode(node)); testPattern.AddElement(elementName); break; default: // If the parent node is the XML document itself, then abort here if (node.ParentNode is System.Xml.XmlDocument) { throw new ApplicationException("No test pattern can be created for the root element. Its validity must be guaranteed by comparison with the DTD root element."); } testPattern = new DtdTestpattern(elementName, Dtd.GetElementNameFromNode(node.ParentNode)); // Traverse all elements within the parent element sibling = node.ParentNode.FirstChild; while (sibling != null) { if (sibling is System.Xml.XmlWhitespace) { // Whitespace tags can be ignored during the check } else { if (sibling == node) // at this point the node must be inserted { if (sibling is System.Xml.XmlComment) { testPattern.AddElement("#COMMENT"); } else { if (this.dtd.DTDElementByName(Dtd.GetElementNameFromNode(node), false) == null) { // This element is not known at all, therefore the element is sometimes not included // throw new ApplicationException(String.Format("unknown Node-Element '{0}'", DTD.GetElementNameFromNode(node))); } else { switch (cursorPos.PosOnNode) { case XmlCursorPositions.CursorOnNodeStartTag: // If the node itself is selected and should be replaced case XmlCursorPositions.CursorOnNodeEndTag: if (elementName == null) // check delete { // Omit element } else // check insert/replace { // Instead of the element present at this position, the element to be tested is inserted here testPattern.AddElement(elementName); } break; case XmlCursorPositions.CursorBehindTheNode: if (elementName == null) // check delete { throw new ApplicationException("CreateTestPattern: Delete must not be checked for XmlCursorPositions.CursorBehindTheNode!"); } else { // element is inserted behind the element to be tested testPattern.AddElement(Dtd.GetElementNameFromNode(node)); testPattern.AddElement(elementName); } break; case XmlCursorPositions.CursorInsideTheEmptyNode: if (elementName == null) // check delete { throw new ApplicationException("CreateTestPattern: Delete must not be checked for XmlCursorPositions.CursorInsideTheEmptyNode!"); } else { throw new ApplicationException("CreateTestPattern: CursorInsideTheEmptyNode canĀ“t be handled at this place!"); } case XmlCursorPositions.CursorInFrontOfNode: if (elementName == null) // check delete { throw new ApplicationException("CreateTestPattern: Delete must not be checked for XmlCursorPositions.CursorInFrontOfNode!"); } else { // Element is inserted before the element to be tested testPattern.AddElement(elementName); testPattern.AddElement(Dtd.GetElementNameFromNode(node)); } break; case XmlCursorPositions.CursorInsideTextNode: if (elementName == null) // check delete { throw new ApplicationException("CreateTestPattern: Delete must not be checked for XmlCursorPositions.CursorInsideTextNode!"); } else { if (Dtd.GetElementNameFromNode(node) != "#PCDATA") { throw new ApplicationException("CreateTestPattern: CursorInsideTextNode, but node.name=" + Dtd.GetElementNameFromNode(node)); } else { // The element to be tested is placed between two text nodes testPattern.AddElement("#PCDATA"); testPattern.AddElement(elementName); testPattern.AddElement("#PCDATA"); } } break; default: throw new ApplicationException("Unknown XmlCursorPositions value:" + cursorPos.PosOnNode); } } } } else // just continue enumerating the elements as usual { testPattern.AddElement(Dtd.GetElementNameFromNode(sibling)); } } sibling = sibling.NextSibling; // to the next element } break; } return(testPattern); }
private bool FitsPatternInElement(DtdTestpattern pattern, DtdElement element) { Match match = element.ChildrenRegExObjekt.Match(pattern.CompareStringForRegEx); return(match.Success); }