Exemple #1
0
 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);
     }
 }
Exemple #2
0
 /// <summary>
 /// Parses the XML string value and returns the proper SimpleField instance to hold
 /// the element value
 /// </summary>
 /// <param name="parent">The parent SIFElement of this field</param>
 /// <param name="id">The metadata definition of the field</param>
 /// <param name="formatter">The formatter to use for the specific version of SIF being parsed</param>
 /// <param name="xmlValue">A string representing the XML payload being used for this version of SIF</param>
 /// <returns>A simple field initialized with the proper value</returns>
 /// <exception cref="AdkTypeParseException">thrown if the value cannot be parsed according to the
 /// formatting rules for this version of SIF</exception>
 public SimpleField ParseField(
     SifElement parent,
     IElementDef id,
     SifFormatter formatter,
     string xmlValue)
 {
     return(Parse(formatter, xmlValue).CreateField(parent, id));
 }
 /// <summary>
 /// Converts the given SIFSimpleType instance to an XML string value
 /// </summary>
 /// <param name="formatter">The formatter to sue for the current version of SIF being written to</param>
 /// <param name="sifType">The value to write</param>
 /// <returns>A string representing the XML payload used for the specified version
 /// of SIF. All types are nullable in SIF, so the resulting value could be null/</returns>
 public override String ToString(SifFormatter formatter,
                                 AdkDataType <DateTime?> sifType)
 {
     if (sifType.Value.HasValue)
     {
         return(formatter.ToTimeString(sifType.Value));
     }
     return(null);
 }
            /// <summary>
            /// Converts the given SIFSimpleType instance to an XML string value
            /// </summary>
            /// <param name="formatter">The formatter to sue for the current version of SIF being written to</param>
            /// <param name="value">The value to write</param>
            /// <returns>A string representing the XML payload used for the specified version
            /// of SIF. All types are nullable in SIF, so the resulting value could be null/</returns>
            public override String ToString(SifFormatter formatter,
                                            AdkDataType <float?> value)
            {
                float?i = value.Value;

                if (i.HasValue)
                {
                    return(formatter.ToString(i));
                }
                return(null);
            }
Exemple #5
0
        private void SetFieldValueFromAttribute(
            SifElement element,
            XmlReader reader,
            IDtd dtd,
            SifVersion version,
            SifFormatter formatter,
            IZone zone)
        {
            IElementDef elementDef = element.ElementDef;
            IElementDef field      = dtd.LookupElementDef(element.ElementDef, reader.LocalName);

            if (field == null && reader.Prefix != null)
            {
                if (reader.LocalName == SifWriter.NIL && reader.NamespaceURI == XmlSchema.InstanceNamespace)
                {
                    TypeConverter converter = elementDef.TypeConverter;
                    if (converter != null)
                    {
                        SifSimpleType sst = converter.GetSifSimpleType(null);
                        element.SetField(elementDef, sst);
                    }
                    return;
                }
                else if (reader.Name.StartsWith("xmlns"))
                {
                    return;
                }
                else
                {
                    field = dtd.LookupElementDef(elementDef, reader.Prefix + ":" + reader.LocalName);
                }
            }

            if (field != null)
            {
                string        strVal = reader.Value.Trim();
                SifSimpleType val    = ParseValue(field, strVal, version, formatter, zone);
                element.SetField(field, val);
            }
            else if (element.ElementDef.EarliestVersion >= SifVersion.SIF20 && version < SifVersion.SIF20)
            {
                Adk.Log.Warn("Field " + element.ElementDef.ClassName + "." + (reader.Prefix == null ? reader.LocalName : reader.Prefix + ":" + reader.LocalName) + " does not exist in the sif 2.0 specification onwards.  It may or may not be valid in sif 1.5r1.  It will be ignored.");
            }
            else
            {
                // TODO: Log and gracefully ignore, depending on whether the ADK is set to strict or loose parsing
                throw new SifException
                          (SifErrorCategoryCode.Xml, SifErrorCodes.XML_GENERIC_VALIDATION_3,
                          "Unknown element or attribute",
                          reader.LocalName + " is not a recognized attribute of the " +
                          elementDef.Name + " element (SIF " +
                          element.EffectiveSIFVersion.ToString() + ")", zone);
            }
        }
 /// <summary>
 /// Parses the given XML string value into a SIFSimpleType instance
 /// </summary>
 /// <param name="formatter">the formatter to use for the specific version of SIF being parsed</param>
 /// <param name="xmlValue">The XML string value</param>
 /// <returns>A SIFSimpleType instance</returns>
 /// <exception cref="AdkTypeParseException">thrown if the value cannot be parsed</exception>
 public override SifSimpleType Parse(SifFormatter formatter,
                                     string xmlValue)
 {
     try
     {
         return(new SifTime(formatter.ToTime(xmlValue)));
     }
     catch (FormatException fe)
     {
         throw new AdkTypeParseException(
                   "Error converting value '" + xmlValue + "' to a Time object: " + fe.Message, null, fe);
     }
 }
Exemple #7
0
        private void SetFieldValueFromElement(IElementDef def,
                                              SifElement element,
                                              XmlReader reader,
                                              SifVersion version,
                                              SifFormatter formatter,
                                              IZone zone)
        {
            // Check for xsi:nill
            if (reader.IsEmptyElement)
            {
                // no data to set
                return;
            }

            // Look for the xsi:nill attribute that signals a null value
            while (reader.MoveToNextAttribute())
            {
                if (reader.LocalName == SifWriter.NIL && reader.NamespaceURI == XmlSchema.InstanceNamespace)
                {
                    SifSimpleType val = def.TypeConverter.GetSifSimpleType(null);
                    element.SetField(def, val);
                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        reader.Read();
                    }
                    return;
                }
                else
                {
                    // TODO: This is an unknown attribute. Log and continue
                }
            }

            while (reader.NodeType == XmlNodeType.Element)
            {
                reader.Read();
            }

            if (reader.NodeType == XmlNodeType.Text)
            {
                SifSimpleType val = ParseValue(def, reader.Value.Trim(), version, formatter, zone);
                element.SetField(def, val);
            }
            // TODO: Not sure if this will always advance as expected.
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                reader.Read();
            }
        }
Exemple #8
0
        private SifElement ReadSifElementFromElementNode(
            IElementDef def,
            XmlReader reader,
            IDtd dtd,
            SifElement parent,
            SifFormatter formatter,
            SifVersion version,
            IZone zone)
        {
            SifElement element;

            try
            {
                element = SifElement.Create(parent, def);
            }
            catch (TypeLoadException tle)
            {
                throw new AdkParsingException
                          ("Could not create an instance of " + def.FQClassName + " to wrap a " +
                          reader.LocalName + " element because that class doesn't exist", zone, tle);
            }
            catch (Exception ex)
            {
                throw new AdkParsingException
                          ("Could not create an instance of " + def.FQClassName, zone, ex);
            }

            element.ElementDef = def;
            element.SifVersion = version;

            if (parent != null)
            {
                element = formatter.AddChild(parent, element, version);
            }

            // Set the attributes to fields of the SifElement
            while (reader.MoveToNextAttribute())
            {
                SetFieldValueFromAttribute(element, reader, dtd, version, formatter, zone);
            }

            return(element);
        }
Exemple #9
0
 /// <summary>
 /// Returns the string representation of this data field, using the specified formatter
 /// </summary>
 /// <param name="formatter"></param>
 /// <returns></returns>
 public override String ToString(SifFormatter formatter)
 {
     return(GetTypeConverter().ToString(formatter, this));
 }
Exemple #10
0
 /// <summary>
 /// Utility method to generate a GUID for SIF Data Objects and messages.
 /// </summary>
 /// <returns>a new GUID formatted as a SIF Refid</returns>
 public static string MakeGuid()
 {
     return(SifFormatter.GuidToSifRefID(Guid.NewGuid()));
 }
Exemple #11
0
 /// <summary>
 /// Sets the text value of this element using the appropriate formatter
 /// </summary>
 /// <param name="strValue"></param>
 /// <param name="formatter"></param>
 public void SetTextValue(string strValue,
                          SifFormatter formatter)
 {
     fValue = fValue.TypeConverter.Parse(formatter, strValue);
 }
Exemple #12
0
 /// <summary>
 /// Parses the given XML string value into a SIFSimpleType instance
 /// </summary>
 /// <param name="formatter">the formatter to use for the specific version of SIF being parsed</param>
 /// <param name="xmlValue">The XML string value</param>
 /// <returns>A SIFSimpleType instance</returns>
 /// <exception cref="AdkParsingException">thrown if the value cannot be parsed</exception>
 public abstract SifSimpleType Parse(SifFormatter formatter,
                                     string xmlValue);
Exemple #13
0
        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);
            }
        }
Exemple #14
0
        private void SetFieldValueFromElement( IElementDef def,
                                               SifElement element,
                                               XmlReader reader,
                                               SifVersion version,
                                               SifFormatter formatter,
                                               IZone zone )
        {
            // Check for xsi:nill
            if ( reader.IsEmptyElement )
            {
                // no data to set
                return;
            }

            // Look for the xsi:nill attribute that signals a null value
            while ( reader.MoveToNextAttribute() )
            {
                if (reader.LocalName == SifWriter.NIL && reader.NamespaceURI == XmlSchema.InstanceNamespace)
                {
                    SifSimpleType val = def.TypeConverter.GetSifSimpleType( null );
                    element.SetField( def, val );
                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        reader.Read();
                    }
                    return;
                }
                else
                {
                    // TODO: This is an unknown attribute. Log and continue
                }
            }

            while ( reader.NodeType == XmlNodeType.Element )
            {
                reader.Read();
            }

            if ( reader.NodeType == XmlNodeType.Text )
            {
                SifSimpleType val = ParseValue( def, reader.Value.Trim(), version, formatter, zone );
                element.SetField( def, val );
            }
            // TODO: Not sure if this will always advance as expected.
            while ( reader.NodeType != XmlNodeType.EndElement )
            {
                reader.Read();
            }
        }
Exemple #15
0
        private SifElement ReadSifElementFromElementNode(
            IElementDef def,
            XmlReader reader,
            IDtd dtd,
            SifElement parent,
            SifFormatter formatter,
            SifVersion version,
            IZone zone )
        {
            SifElement element;
            try
            {
                element = SifElement.Create( parent, def );
            }
            catch ( TypeLoadException tle )
            {
                throw new AdkParsingException
                    ( "Could not create an instance of " + def.FQClassName + " to wrap a " +
                      reader.LocalName + " element because that class doesn't exist", zone, tle );
            }
            catch ( Exception ex )
            {
                throw new AdkParsingException
                    ( "Could not create an instance of " + def.FQClassName, zone, ex );
            }

            element.ElementDef = def;
            element.SifVersion = version;

            if ( parent != null )
            {
                element = formatter.AddChild( parent, element, version );
            }

            // Set the attributes to fields of the SifElement
            while ( reader.MoveToNextAttribute() )
            {
                SetFieldValueFromAttribute( element, reader, dtd, version, formatter, zone );
            }

            return element;
        }
        private void assertThrowsFormatException(SifFormatter formatter)
        {
            bool threwProperException = false;

            // Boolean
            try
            {
                formatter.ToBool("asdf");
            }
            catch (FormatException )
            {
                threwProperException = true;
            }
            Assertion.Assert("NumberFormatException was not thrown for toBoolean()", threwProperException);

            // DECIMAL
            threwProperException = false;
            try
            {
                formatter.ToDecimal("asdf");
            }
            catch (FormatException)
            {
                threwProperException = true;
            }
            Assertion.Assert("IllegalArgumentException was not thrown for toDecimal()", threwProperException);

            // DATE
            threwProperException = false;
            try
            {
                formatter.ToDate("asdf");
            }
            catch (FormatException)
            {
                threwProperException = true;
            }
            Assertion.Assert("IllegalArgumentException was not thrown for ToDate()", threwProperException);

            //
            // DateTime and Duration are not supported by the SIF1xFormatter
            //
            if (!(formatter is Sif1xFormatter))
            {
                // DATETIME
                threwProperException = false;
                try
                {
                    formatter.ToDateTime("asdf");
                }
                catch (FormatException)
                {
                    threwProperException = true;
                }
                Assertion.Assert("IllegalArgumentException was not thrown for ToDateTime()", threwProperException);

                // DURATION
                threwProperException = false;
                try
                {
                    formatter.ToTimeSpan("asdf");
                }
                catch (FormatException)
                {
                    threwProperException = true;
                }

                Assertion.Assert("IllegalArgumentException was not thrown for toDuration()", threwProperException);
            }
            // INT
            threwProperException = false;
            try
            {
                formatter.ToInt("asdf");
            }
            catch (FormatException)
            {
                threwProperException = true;
            }
            Assertion.Assert("IllegalArgumentException was not thrown for toint()", threwProperException);

            // TIME
            threwProperException = false;
            try
            {
                formatter.ToTime("asdf");
            }
            catch (FormatException)
            {
                threwProperException = true;
            }
            Assertion.Assert("IllegalArgumentException was not thrown for toTime()", threwProperException);
        }
        private void assertintParsing(SifFormatter formatter, String stringValue, int value)
        {
            Console.WriteLine("Testing int parse of '" + stringValue + "' using " + formatter.ToString());
            int? testValue = formatter.ToInt(stringValue);
            Assertion.AssertEquals("int Value", value, testValue);
            Assertion.AssertEquals("String Value", stringValue, formatter.ToString(value));

            testValue = formatter.ToInt(null);
            Assertion.AssertNull("int value should be null", testValue);
        }
        private void assertFloatParsing(SifFormatter formatter, String stringValue, float value)
        {
            Console.WriteLine( "Testing Float parse of '" + stringValue + "' using " + formatter.ToString() );
            float? testValue = formatter.ToFloat( stringValue );
            Assert.AreEqual( value, testValue );
            Assert.AreEqual( stringValue, formatter.ToString( value ), "String Value" );

            testValue = formatter.ToFloat( null );
            Assert.IsFalse( testValue.HasValue, "Float value should be null" );
        }
        private void AssertDateParsing(SifFormatter formatter, String stringValue, Calendar value)
        {
            Console.WriteLine("Testing Date parse of '" + stringValue + "' using " + formatter.ToString());
            //Calendar testValue = formatter.ToDate(stringValue);
            DateTime testValue = (DateTime) formatter.ToDate(stringValue);
            Assertion.AssertEquals("Date Value", value, testValue);
            Assertion.AssertEquals("String Value", stringValue, (String) formatter.ToDateString(testValue));

            testValue = (DateTime) formatter.ToDate(null);
            Assertion.AssertNull("Date value should be null", testValue);
        }
        private void assertBooleanParsing(SifFormatter formatter, String stringValue, Boolean value)
        {
            Console.WriteLine("Testing Boolean parse of '" + stringValue + "' using " + formatter.ToString());
            Boolean? testValue = formatter.ToBool(stringValue);
            Assertion.AssertEquals("Boolean Value", value, testValue);
            Assertion.AssertEquals("String Value", stringValue, formatter.ToString(value));

            testValue = formatter.ToBool(null);
            Assertion.AssertNull("Boolean value should be null", testValue);
        }
 /// <summary>
 /// Creates an instance of DefaultValueBuilder that builds values based
 /// on the SIFDataMap, using the specified <c>SIFFormatter</c> instance
 /// </summary>
 /// <param name="data"></param>
 /// <param name="formatter"></param>
 public DefaultValueBuilder(IFieldAdaptor data, SifFormatter formatter)
 {
     fVars      = data;
     fFormatter = formatter;
 }
Exemple #22
0
        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 );
            }
        }
Exemple #23
0
 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 AssertTimeParsing(DateTime? assertedDateTime, SifFormatter formatter, string test, String statement)
 {
     DateTime? parsedValue;
     parsedValue = formatter.ToTime(test);
     Console.WriteLine(statement);
     Console.WriteLine("parsing: " + test);
     Console.WriteLine("Asserted: {0} : {1}", assertedDateTime.Value, assertedDateTime.Value.ToFileTimeUtc());
     Console.WriteLine("Parsed: {0} : {1}", parsedValue.Value, parsedValue.Value.ToFileTimeUtc());
     Console.WriteLine();
     assertTimes( statement, assertedDateTime, parsedValue);
 }
Exemple #25
0
        private void SetFieldValueFromAttribute(
            SifElement element,
            XmlReader reader,
            IDtd dtd,
            SifVersion version,
            SifFormatter formatter,
            IZone zone )
        {
            IElementDef elementDef = element.ElementDef;
            IElementDef field = dtd.LookupElementDef( element.ElementDef, reader.LocalName );
            if ( field == null && reader.Prefix != null )
            {
                if(reader.LocalName == SifWriter.NIL && reader.NamespaceURI == XmlSchema.InstanceNamespace )
                {
                    TypeConverter converter = elementDef.TypeConverter;
                    if ( converter != null )
                    {
                        SifSimpleType sst = converter.GetSifSimpleType( null );
                        element.SetField(elementDef, sst);
                    }
                    return;
                }
                else if( reader.Name.StartsWith( "xmlns" ) )
                {
                    return;
                }
                else
                {
                    field = dtd.LookupElementDef(elementDef, reader.Prefix + ":" + reader.LocalName);
                }
            }

            if( field != null )
            {
                string strVal = reader.Value.Trim();
                SifSimpleType val = ParseValue( field, strVal, version, formatter, zone );
                element.SetField( field, val );
            }
            else if (element.ElementDef.EarliestVersion >= SifVersion.SIF20 && version < SifVersion.SIF20)
            {
                Adk.Log.Warn("Field " + element.ElementDef.ClassName + "." + (reader.Prefix == null ? reader.LocalName : reader.Prefix + ":" + reader.LocalName ) + " does not exist in the sif 2.0 specification onwards.  It may or may not be valid in sif 1.5r1.  It will be ignored."  );
            }
            else
            {
                // TODO: Log and gracefully ignore, depending on whether the ADK is set to strict or loose parsing
                throw new SifException
                    (SifErrorCategoryCode.Xml, SifErrorCodes.XML_GENERIC_VALIDATION_3,
                      "Unknown element or attribute",
                      reader.LocalName + " is not a recognized attribute of the " +
                      elementDef.Name + " element (SIF " +
                      element.EffectiveSIFVersion.ToString() + ")", zone);
            }
        }
Exemple #26
0
 /// <summary>
 /// Converts the given SIFSimpleType instance to an XML string value
 /// </summary>
 /// <param name="formatter">The formatter to sue for the current version of SIF being written to</param>
 /// <param name="value">The value to write</param>
 /// <returns>A string representing the XML payload used for the specified version
 /// of SIF. All types are nullable in SIF, so the resulting value could be null/</returns>
 public abstract String ToString(SifFormatter formatter,
                                 AdkDataType <T> value);
Exemple #27
0
 private void SetSifVersion(SifVersion version)
 {
     fVersion   = version;
     fFormatter = Adk.Dtd.GetFormatter(version);
 }
Exemple #28
0
 SifSimpleType TypeConverter.Parse(SifFormatter formatter,
                                   string xmlValue)
 {
     return(Parse(formatter, xmlValue));
 }
 /// <summary>
 /// Returns the string representation of this data field, using the specified formatter
 /// </summary>
 /// <param name="formatter"></param>
 /// <returns></returns>
 public abstract String ToString(SifFormatter formatter);
Exemple #30
0
 /// <summary>
 /// Returns the string representation of this data field, using the specified formatter
 /// </summary>
 /// <param name="formatter"></param>
 /// <returns></returns>
 public override String ToString(SifFormatter formatter)
 {
     return(fValue);
 }
Exemple #31
0
 private void SetSifVersion( SifVersion version )
 {
     fVersion = version;
     fFormatter = Adk.Dtd.GetFormatter( version );
 }
 /// <summary>
 /// Converts the given SIFSimpleType instance to an XML string value
 /// </summary>
 /// <param name="formatter">The formatter to sue for the current version of SIF being written to</param>
 /// <param name="sifType">The value to write</param>
 /// <returns>A string representing the XML payload used for the specified version
 /// of SIF. All types are nullable in SIF, so the resulting value could be null/</returns>
 public override String ToString(SifFormatter formatter,
                                 AdkDataType <string> sifType)
 {
     return(sifType.Value);
 }
Exemple #33
0
        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);
            }
        }
 /// <summary>
 /// Returns the string representation of this data field, using the specified formatter
 /// </summary>
 /// <param name="formatter"></param>
 /// <returns></returns>
 public abstract String ToString( SifFormatter formatter );