public override INodePointer CreateChild(SifXPathContext context, string name, int i) { SifVersion version = Version; IElementDef subEleDef = GetChildDef(name); SifFormatter formatter = Adk.Dtd.GetFormatter(version); SifElement sifElement = (SifElement)fElement; // Check to see if this child has a render surrogate defined IRenderSurrogate rs = subEleDef.GetVersionInfo(version).GetSurrogate(); if (rs != null) { return(rs.CreateChild(this, formatter, version, context)); } if (subEleDef.Field) { SifSimpleType ssf = subEleDef.TypeConverter.GetSifSimpleType(null); SimpleField sf = formatter.SetField(sifElement, subEleDef, ssf, version); return(SimpleFieldPointer.Create(this, sf)); } else { SifElement newEle = SifElement.Create(sifElement, subEleDef); formatter.AddChild(sifElement, newEle, version); return(new SifElementPointer(this, newEle, version)); } }
internal static INodePointer Create(INodePointer parent, SifElement element, SifVersion version) { if (version.Major < 2) { IElementDef fieldDef = element.ElementDef; IRenderSurrogate rs = fieldDef.GetVersionInfo(version).GetSurrogate(); if (rs != null) { return(rs.CreateNodePointer(parent, element, version)); } } return(new SifElementPointer(parent, element, version)); }
/// <summary> Write a SIF element in the version of SIF currently in effect for this /// SifWriter. /// </summary> /// <param name="element">The SIF Element instance to Write to the output stream /// <param name="isLegacy">if true, this method assumes that it needs to do more work, /// such as looking for rendering surrogates for the specific version of SIF</param> /// </param> private void WriteElement(SifElement element, bool isLegacy) { IElementDef def = element.ElementDef; if (!(Include(element) && def.IsSupported(fVersion))) { return; } if (isLegacy) { IRenderSurrogate surrogate = def.GetVersionInfo(fVersion).GetSurrogate(); if (surrogate != null) { surrogate.RenderRaw(fWriter, fVersion, element, fFormatter); return; } } if (element.IsEmpty() || !HasContent(element, fVersion)) { if (element is XMLData) { XmlDocument doc = ((XMLData)element).Xml; doc.Save(fWriter); } else { Write(element, EMPTY, isLegacy); } } else { Write(element, OPEN, isLegacy); ICollection <Element> elements = fFormatter.GetContent(element, fVersion); foreach (Element childElement in elements) { if (childElement is SifElement) { WriteElement((SifElement)childElement, isLegacy); } else { Write((SimpleField)childElement, isLegacy); } } fWriter.WriteEndElement(); } }
/// <summary> /// Creates a new NodePointer representing the SimpleField /// </summary> /// <param name="pointerParent"></param> /// <param name="field"></param> /// <returns></returns> public static INodePointer Create( SifElementPointer pointerParent, SimpleField field) { SifVersion version = pointerParent.Version; if (pointerParent.IsLegacyVersion) { IElementDef fieldDef = field.ElementDef; IRenderSurrogate rs = fieldDef.GetVersionInfo(version).GetSurrogate(); if (rs != null) { return(rs.CreateNodePointer(pointerParent, field, version)); } } if (field.ElementDef.IsAttribute(version)) { return(new SimpleFieldAttributePointer(pointerParent, field, version)); } else { return(new SimpleFieldElementPointer(pointerParent, field, version)); } }
private SifElement ParseElementStream(XmlReader reader, SifVersion version, IDtd dtd, IZone zone, SifParserFlags flags) { bool legacyParse = version.CompareTo(SifVersion.SIF20) < 0; // The current SIFElement being parsed SifElement currentElement = null; // The actual tag name of the current element SifFormatter formatter = Adk.Dtd.GetFormatter(version); reader.MoveToContent(); bool doneParsing = false; while (!(reader.EOF || doneParsing)) { switch (reader.NodeType) { case XmlNodeType.Element: if (reader.LocalName == "SIF_Message") { // Special case for embedded SIF_Message envelopes if ((flags & SifParserFlags.ExpectInnerEnvelope) != 0) { SifElement msgElement = ReadSIFMessageElement(reader, dtd, zone, SifParserFlags.None, version); currentElement.AddChild(msgElement); currentElement = msgElement; } else { throw new AdkParsingException ("Unexpected SIF_Message encountered in parsing", zone); } } else { String xmlName = reader.LocalName; if (xmlName == "Teacher") { Console.WriteLine("Ready to break"); } IElementDef foundDef = LookupElementDef(currentElement, reader, dtd, version, zone); if (foundDef == null) { if (legacyParse) { ParseLegacyXML(reader, version, zone, currentElement, formatter, xmlName); continue; } else if (currentElement != null && currentElement.ElementDef.Name.Equals("XMLData")) { // Parse this into a DOM and set on the XMLData // element XmlReader nestedReader = reader.ReadSubtree(); XmlDocument doc = new XmlDocument(); doc.Load(nestedReader); ((XMLData)currentElement).Xml = doc; continue; } else { String _tag = currentElement != null ? currentElement.ElementDef.Name + "/" + xmlName : xmlName; throw new SifException(SifErrorCategoryCode.Xml, SifErrorCodes.XML_GENERIC_VALIDATION_3, "Unknown element or attribute", _tag + " is not a recognized element of SIF " + version.ToString(), zone); } } if (legacyParse) { IElementVersionInfo evi = foundDef.GetVersionInfo(version); if (evi != null) { IRenderSurrogate rs = evi.GetSurrogate(); if (rs != null) { using (XmlReader subtreeReader = reader.ReadSubtree()) { bool shouldContinue = true; subtreeReader.Read(); try { shouldContinue = rs.ReadRaw(subtreeReader, version, currentElement, formatter); } catch (AdkTypeParseException atpe) { HandleTypeParseException("Unable to parse value: " + atpe.Message, atpe, zone); } subtreeReader.Close(); // advance to the next tag reader.Read(); if (shouldContinue) { continue; } else { throw new SifException(SifErrorCategoryCode.Xml, SifErrorCodes.XML_GENERIC_VALIDATION_3, "Unknown element or attribute", reader.LocalName + " was not able to be parsed by " + rs, zone); } } } } } if (foundDef.Field) { SetFieldValueFromElement (foundDef, currentElement, reader, version, formatter, zone); // Advance to the next tag do { reader.Read(); } while ( !(reader.EOF || reader.NodeType == XmlNodeType.Element || reader.NodeType == XmlNodeType.EndElement)); continue; } else if (reader.IsEmptyElement) { // The .Net XmlReader does not return an EndElement event for // tags with empty content. Therefore, this region of the code is // slightly different from Java ReadSifElementFromElementNode(foundDef, reader, dtd, currentElement, formatter, version, zone); } else { currentElement = ReadSifElementFromElementNode(foundDef, reader, dtd, currentElement, formatter, version, zone); } } break; case XmlNodeType.Text: if (currentElement.ElementDef.HasSimpleContent) { SetFieldValueFromElement(currentElement.ElementDef, currentElement, reader, version, formatter, zone); // The XML Reader cursor is automatically advanced by this method, so we // need to continue on without calling read() continue; } break; case XmlNodeType.EndElement: if (currentElement.Parent != null) { currentElement = (SifElement)currentElement.Parent; while (legacyParse && currentElement.ElementDef.IsCollapsed(version)) { currentElement = (SifElement)currentElement.Parent; } } if (reader.LocalName == "SIF_Message") { // We need to return here. If we let the reader keep reading, and we are reading an embedded // SIF_Message, it will keep parsing the end tags and not let the stack of SifElement objects // propertly unwind. We're done anyway. doneParsing = true; } break; } // Advance the cursor reader.Read(); } if (currentElement == null) { return(null); } else { // Now, unwind and pop off the top element parsed Element top = currentElement; Element current; while ((current = top.Parent) != null) { top = current; } return((SifElement)top); } }