private SifSimpleType ParseValue( IElementDef def, String value, SifVersion version, SifFormatter formatter, IZone zone) { try { TypeConverter converter = def.TypeConverter; if (converter == null) { // TODO: Should we not allow this in "STRICT" mode? converter = SifTypeConverters.STRING; } return(converter.Parse(formatter, value)); } catch (AdkTypeParseException pe) { String errorMessage = "Unable to parse element or attribute '" + def.Name + "'" + pe.Message + " (SIF " + version.ToString() + ")"; HandleTypeParseException(errorMessage, pe, zone); return(null); } }
private void writeRootAttributes(bool includeVersion) { if (!fRootAttributesWritten) { if (includeVersion) { fWriter.WriteAttributeString("Version", fVersion.ToString()); } if (fFormatter.SupportsNamespaces) { fWriter.WriteAttributeString("xmlns", XSI_PREFIX, null, XmlSchema.InstanceNamespace); } } fRootAttributesWritten = true; }
/// <summary> /// Reads a SIF_Message element, which sets the version and namespace scope for the rest of the /// xml parsing /// </summary> /// <param name="reader"></param> /// <param name="dtd"></param> /// <param name="zone"></param> /// <param name="flags"></param> /// <param name="defaultVersion"></param> /// <returns></returns> /// <exception cref="OpenADK.Library.AdkParsingException">AdkParsingException is thrown if unable to /// parse the message</exception> /// <exception cref="System.IO.IOException"> IOException is thrown if an error is reported while reading /// the message content</exception> private SifElement ReadSIFMessageElement( XmlReader reader, IDtd dtd, IZone zone, SifParserFlags flags, SifVersion defaultVersion) { SifVersion version = ParseVersion(reader, dtd, zone, flags, defaultVersion); SIF_Message message = new SIF_Message(); // Set the namespace from our working version message.SetXmlns(version.Xmlns); if (version.CompareTo(SifVersion.SIF11) >= 0) { // If we are at SifVersion 1.1 or greater, set the version attribute message.SetVersionAttribute(version.ToString()); } // Advance to the next element if (reader.Read()) { while (reader.NodeType != XmlNodeType.Element) { if (!reader.Read()) { break; } } if (reader.NodeType == XmlNodeType.Element) { SifElement element = ParseElementStream(reader, version, dtd, zone, flags); message.AddChild(element); } } return(message); }
private void ParseLegacyXML( XmlReader reader, SifVersion version, IZone zone, SifElement currentElement, SifFormatter formatter, String xmlName) { bool handled = false; // Determine if any surrogate formatters that are defined as children // of the current element can resolve it // NOTE: Until we fix this in the ADK, elements from the common package loose their // metadata information that was originally defined. IElementDef currentDef = currentElement.ElementDef; IList <IElementDef> children = currentDef.Children; if (children == null || children.Count == 0) { // try to get the actual element def // WARNING! this is somewhat of a hack until // we get support for what we need in the ADK metadata try { SifElement copy = (SifElement)ClassFactory.CreateInstance(currentDef.FQClassName); children = copy.ElementDef.Children; } catch (Exception cnfe) { throw new SifException( SifErrorCategoryCode.Xml, SifErrorCodes.XML_GENERIC_VALIDATION_3, "Unable to parse" + xmlName + " " + version.ToString() + cnfe.Message, zone); } } using (XmlReader subtreeReader = reader.ReadSubtree()) { subtreeReader.Read(); foreach (IElementDef candidate in children) { if (candidate.EarliestVersion.CompareTo(version) > 0) { continue; } IElementVersionInfo evi = candidate.GetVersionInfo(version); if (evi != null) { IRenderSurrogate rs = evi.GetSurrogate(); if (rs != null) { try { bool surrogateHandled = rs.ReadRaw(subtreeReader, version, currentElement, formatter); if (surrogateHandled) { handled = true; break; } } catch (AdkTypeParseException e) { HandleTypeParseException("Unable to parse element or attribute value: " + e.Message, e, zone); handled = true; break; } catch (AdkParsingException e) { throw new SifException(SifErrorCategoryCode.Xml, SifErrorCodes.XML_GENERIC_VALIDATION_3, "unable to parse xml: " + e.Message + version.ToString(), zone); } } } } subtreeReader.Close(); } // advance to the next tag reader.Read(); if (!handled) { 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); } }
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); } }
private void ParseLegacyXML( XmlReader reader, SifVersion version, IZone zone, SifElement currentElement, SifFormatter formatter, String xmlName ) { bool handled = false; // Determine if any surrogate formatters that are defined as children // of the current element can resolve it // NOTE: Until we fix this in the ADK, elements from the common package loose their // metadata information that was originally defined. IElementDef currentDef = currentElement.ElementDef; IList<IElementDef> children = currentDef.Children; if ( children == null || children.Count == 0 ) { // try to get the actual element def // WARNING! this is somewhat of a hack until // we get support for what we need in the ADK metadata try { SifElement copy = (SifElement) ClassFactory.CreateInstance( currentDef.FQClassName ); children = copy.ElementDef.Children; } catch ( Exception cnfe ) { throw new SifException( SifErrorCategoryCode.Xml, SifErrorCodes.XML_GENERIC_VALIDATION_3, "Unable to parse" + xmlName + " " + version.ToString() + cnfe.Message, zone ); } } using (XmlReader subtreeReader = reader.ReadSubtree()) { subtreeReader.Read(); foreach ( IElementDef candidate in children ) { if ( candidate.EarliestVersion.CompareTo( version ) > 0 ) { continue; } IElementVersionInfo evi = candidate.GetVersionInfo( version ); if ( evi != null ) { IRenderSurrogate rs = evi.GetSurrogate(); if ( rs != null ) { try { bool surrogateHandled = rs.ReadRaw( subtreeReader, version, currentElement, formatter ); if ( surrogateHandled ) { handled = true; break; } } catch ( AdkTypeParseException e ) { HandleTypeParseException( "Unable to parse element or attribute value: " + e.Message, e, zone ); handled = true; break; } catch ( AdkParsingException e ) { throw new SifException( SifErrorCategoryCode.Xml, SifErrorCodes.XML_GENERIC_VALIDATION_3, "unable to parse xml: " + e.Message + version.ToString(), zone ); } } } } subtreeReader.Close(); } // advance to the next tag reader.Read(); if ( !handled ) { 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 ); } }
private SifSimpleType ParseValue( IElementDef def, String value, SifVersion version, SifFormatter formatter, IZone zone ) { try { TypeConverter converter = def.TypeConverter; if (converter == null) { // TODO: Should we not allow this in "STRICT" mode? converter = SifTypeConverters.STRING; } return converter.Parse(formatter, value); } catch (AdkTypeParseException pe) { String errorMessage = "Unable to parse element or attribute '" + def.Name + "'" + pe.Message + " (SIF " + version.ToString() + ")"; HandleTypeParseException( errorMessage, pe, zone ); return null; } }
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; } }
private void testSIFPingWithZISVersion( InMemoryProtocolHandler handler, SifVersion testVersion ) { SIF_SystemControl ssc; Zone.Properties.ZisVersion = testVersion.ToString(); Zone.SifPing(); ssc = (SIF_SystemControl) handler.readMsg(); Assert.AreEqual( testVersion, ssc.SifVersion, "SifVersion" ); Assert.AreEqual( testVersion.Xmlns, ssc.GetXmlns(), "SifVersion->Xmlns" ); }
// BEGIN EXTRA METHODS (C:/GitHub/rafidzal/OpenADK-java/adk-generator/../adk-generator/datadef/core/sif20/SIF_Version.txt.cs) /** * Creates a SIF_Version object with the specified SIFVersion * @param version */ public SIF_Version( SifVersion version ) : this(version.ToString()) { }
private void assertSIFVersion(SifVersion assertVersion, int major, int minor, int revision, String tag) { Assert.AreEqual(major, assertVersion.Major, "Major"); Assert.AreEqual(minor, assertVersion.Minor, "Minor"); Assert.AreEqual(revision, assertVersion.Revision, "Revision"); Assert.AreEqual(tag, assertVersion.ToString(), "Tag"); }
private bool RunSingleTest( SifVersion parseVersion, SifVersion writeVersion, string fileName, TextWriter output, SchemaValidator sv) { sv.Clear(); if (VERBOSE) { output.Write("Running test on " + fileName + "\r\n"); } // 1) Read the object into memory SifElement se = null; try { se = AdkObjectParseHelper.ParseFile(fileName, parseVersion); } catch (AdkException adke) { // Parsing failed. However, since this unit test is a complete // test of all available objects, just emit the problem and allow // the test to continue (with a notification of false) output .WriteLine("Error parsing file " + fileName + "\r\n - " + adke); output.WriteLine(); return false; } catch (Exception re) { output.WriteLine("Error parsing file " + fileName + "\r\n - " + re); output.WriteLine(); return false; } // if (VERBOSE) // { // SifWriter writer = new SifWriter(output); // writer.Write(se,parseVersion); // output.Flush(); // } // Before we can validate with the schema, we need to ensure that the // data object is wrapped in a SIF_Message elements, because the SIF // Schema makes that assumption SifMessagePayload smp = SchemaValidator.MakeSIFMessagePayload(se); String tmpFileName = fileName + "." + writeVersion.ToString() + ".adk"; // 2) Write the message out to a file try { SchemaValidator.WriteObject( writeVersion, tmpFileName, smp ); } catch( Exception ex ) { Console.WriteLine( "Error running test on {0}. {1}", tmpFileName, ex ); return false; } // 3) Validate the file bool validated = sv.Validate(tmpFileName); // 4) If validation failed, write the object out for tracing purposes if (!validated) { if (VERBOSE) { SifWriter outWriter = new SifWriter(output); outWriter.Write(se, writeVersion ); outWriter.Flush(); } output.WriteLine("Validation failed on " + tmpFileName ); sv.PrintProblems(output); return false; } // 5) Read the object again into memory try { se = AdkObjectParseHelper.ParseFile(fileName, parseVersion); } catch (AdkException adke) { // Parsing failed. However, since this unit test is a complete // test of all available objects, just emit the problem and allow // the test to continue (with a notification of false) output.WriteLine("Error parsing file " + fileName + ": " + adke.Message ); return false; } catch (Exception re) { output.Write("Error parsing file " + fileName + ": " + re.Message + "\r\n"); return false; } return validated; }