/// <summary> /// Adds a SimpleField parsed from a specific version of SIF to the parent. /// </summary> /// <param name="contentParent">The element to add content to</param> /// <param name="fieldDef">The metadata definition of the field to set</param> /// <param name="data">The value to set to the field</param> /// <param name="version">The version of SIF that the SIFElement is being constructed /// from</param> /// <returns></returns> public virtual SimpleField SetField(SifElement contentParent, IElementDef fieldDef, SifSimpleType data, SifVersion version) { return(contentParent.SetField(fieldDef, data)); }
private void Write(SimpleField f, bool isLegacy) { if (!Include(f)) { return; } if (isLegacy) { IRenderSurrogate surrogate = f.ElementDef.GetVersionInfo(fVersion).GetSurrogate(); if (surrogate != null) { surrogate.RenderRaw(fWriter, fVersion, f, fFormatter); return; } } // "<tag [attr...]>[text]" or "<tag [attr...]/>" String fieldValue = null; SifSimpleType simpleValue = f.SifValue; if (simpleValue != null) { fieldValue = simpleValue.ToString(fFormatter); } if (fieldValue == null) { if (!isLegacy) { fWriter.WriteStartElement(f.ElementDef.Tag(fVersion)); fWriter.WriteAttributeString(NIL, XmlSchema.InstanceNamespace, "true"); //fWriter.WriteElementString( f.ElementDef.Tag( fVersion ), null ); fWriter.WriteFullEndElement(); } else { // The specified version of SIF doesn't support // the xsi:nil attribute. Set the value to an empty // string fWriter.WriteStartElement(f.ElementDef.Tag(fVersion)); fWriter.WriteFullEndElement(); } } else { fWriter.WriteStartElement(f.ElementDef.Tag(fVersion)); if (f.DoNotEncode) { fWriter.WriteRaw(fieldValue); } else { fWriter.WriteString(fieldValue); } fWriter.WriteEndElement(); } }
/// <summary> /// Overriden to test value equality. The underlying <see cref="SifEnum.Value"/> property is compared to /// determine if it is equal. /// </summary> /// <param name="obj">The SifEnum to compare against</param> /// <returns>True if the objects have the same value, otherwise False</returns> public override bool Equals(object obj) { // Test reference comparison first ( fastest ) if (ReferenceEquals(this, obj)) { return(true); } else if (ReferenceEquals(obj, null)) { return(false); } SifSimpleType comparedEnum = obj as SifSimpleType; if (ReferenceEquals(comparedEnum, null)) { return(false); } if (this.RawValue == null) { return(comparedEnum.RawValue == null); } return(this.RawValue.Equals(comparedEnum.RawValue)); }
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); } }
private void Write(SifElement o, int mode, Boolean isLegacy) { if (!Include(o)) { return; } // "<tag [attr...]>[text]" or "<tag [attr...]/>" string tag = o.ElementDef.Tag(fVersion); fWriter.WriteStartElement(tag); if (!fRootAttributesWritten) { writeRootAttributes(false); } WriteAttributes(o); if (mode == EMPTY) { fWriter.WriteEndElement(); } else { // Check for a text value (or an xs:nil value) SimpleField elementValue = o.GetField(o.ElementDef); if (elementValue != null) { SifSimpleType sst = elementValue.SifValue; if (sst == null || sst.RawValue == null) { // The value of this element has been set and it is // null. This should be rendered as 'xs:nil' in SIF 2.x and greater if (!isLegacy) { fWriter.WriteAttributeString(NIL, XmlSchema.InstanceNamespace, "true"); } } else { if (o.DoNotEncode) { fWriter.WriteRaw(o.TextValue); } else { String xmlValue = sst.ToString(fFormatter); fWriter.WriteString(xmlValue); } } } } }
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(); } }
/// <summary> /// Constructor /// </summary> /// <param name="parent">The SIFElement that is the parent of this field</param> /// <param name="def">The metadata definition of this field</param> /// <param name="inValue">A typed subclass of SIFSimpleType</param> /// <exception cref="ArgumentNullException">Thrown if the value passed in is null</exception> public SimpleField(SifElement parent, IElementDef def, SifSimpleType inValue) : base(def, parent) { if (inValue == null) { throw new ArgumentNullException ( string.Format ("Cannot construct an instance of {0} with a null value. Create an appropriate SifSimpleType subclass to wrap the null value.", this.GetType().FullName), "inValue"); } fValue = inValue; }
/// <summary> Write the attributes of a SifElement to the output stream</summary> /// <param name="o">The SifElement whose attributes are to be written /// </param> private void WriteAttributes(SifElement o) { // TODO: We need to make sure the GetFields() API returns a usable collection ICollection <SimpleField> fields = fFormatter.GetFields(o, fVersion); foreach (SimpleField f in fields) { IElementVersionInfo evi = f.ElementDef.GetVersionInfo(fVersion); if (evi != null && evi.IsAttribute) { // Null attribute values are not supported in SIF, unlike // element values, which can be represented with xs:nil SifSimpleType sst = f.SifValue; if (sst.RawValue != null) { String tag = evi.Tag; Boolean handled = false; if (tag.StartsWith("x")) { if (evi.Tag.Equals("xml:lang")) { fWriter.WriteAttributeString("xml", "lang", null, sst.ToString(fFormatter)); } else if (evi.Tag.Equals("xsi:type")) { fWriter.WriteAttributeString("type", XmlSchema.InstanceNamespace, sst.ToString(fFormatter)); } handled = true; } if (!handled) { fWriter.WriteStartAttribute(evi.Tag, string.Empty); fWriter.WriteString(sst.ToString(fFormatter)); fWriter.WriteEndAttribute(); } } } } if (fSerializeIds && o.XmlId != null) { fWriter.WriteAttributeString("id", XML_NAMESPACE, o.XmlId); } }
/// <summary> /// Compares the current object's key value to the given key /// </summary> /// <param name="key"></param> /// <returns></returns> public bool KeyEquals(object key) { if (key == null) { return(false); } IElementDef[] keys = this.KeyFields; if (keys == null) { return(false); } if (keys.Length == 1) { SimpleField field = GetField(keys[0]); if (field != null) { SifSimpleType data = field.SifValue; if (key is SifSimpleType) { return(key.Equals(data)); } else { object fieldValue = data.RawValue; return(key.Equals(fieldValue)); } } else { return(false); } } else { return(key.Equals(this.Key)); } }
/// <summary> /// Evaluates a single SIF_Condition against an object and returns whether it matches or not /// </summary> /// <param name="cond"></param> /// <param name="context"></param> /// <param name="culture"></param> /// <returns></returns> /// <exception cref="OpenADK.Library.AdkSchemaException">If the condition contains references to invalid elements</exception> private bool EvaluateCondition(SifXPathContext context, Condition cond, CultureInfo culture) { // TODO: Add support for comparison using the SIF Data Types Element def = context.GetElementOrAttribute(cond.GetXPath()); String conditionValue = cond.Value; String elementValue = null; if (def != null) { SifSimpleType value = def.SifValue; if (value != null) { // Format the value to string, based on the query version elementValue = value.ToString(EffectiveVersion); } else { // TODO: Not sure if this would ever return a value if the above does not elementValue = def.TextValue; } } if (elementValue == null || conditionValue == null) { // Don't use standard comparision because it will fail. If // one or the other value is null, it cannot be compared, except for // if the operator is EQ or NOT bool bothAreNull = (elementValue == null && conditionValue == null); switch (cond.Operators) { case ComparisonOperators.EQ: case ComparisonOperators.GE: case ComparisonOperators.LE: return(bothAreNull); case ComparisonOperators.NE: return(!bothAreNull); default: // For any other operator, the results are indeterminate with // null values. Return false in this case. return(false); } } int compareLevel = String.Compare(elementValue, conditionValue, false, culture); switch (cond.Operators) { case ComparisonOperators.EQ: return(compareLevel == 0); case ComparisonOperators.NE: return(compareLevel != 0); case ComparisonOperators.GT: return(compareLevel > 0); case ComparisonOperators.LT: return(compareLevel < 0); case ComparisonOperators.GE: return(compareLevel >= 0); case ComparisonOperators.LE: return(compareLevel <= 0); } return(false); }
/// <summary> /// Adds a SimpleField parsed from a specific version of SIF to the parent. /// </summary> /// <param name="contentParent">The element to add content to</param> /// <param name="fieldDef">The metadata definition of the field to set</param> /// <param name="data">The value to set to the field</param> /// <param name="version">The version of SIF that the SIFElement is being constructed /// from</param> /// <returns></returns> public virtual SimpleField SetField(SifElement contentParent, IElementDef fieldDef, SifSimpleType data, SifVersion version) { return contentParent.SetField(fieldDef, data); }
/// <summary> /// Sets the value of this field /// </summary> /// <param name="id"></param> /// <param name="value"></param> /// <returns></returns> public override SimpleField SetField( IElementDef id, SifSimpleType value) { // verify the parameter values if( id == null || !(CommonDTD.PARTIALDATETYPE.Name.Equals( id.Name ))){ throw new ArgumentException("ElementDef must be CommonDTD.PARTIALDATETYPE" ); } if( value != null && value.DataType != SifDataType.String ){ throw new ArgumentException( "Value type must be SIFDataType.STRING" ); } // Allow any datatype to be set, but convert it to a string // This is important, because the value of this could be set to // an int from mappings, because it was an int in SIF 1.5 if( value != null && value.DataType != SifDataType.String ){ value = new SifString( value.ToString() ); } // Parse the text value into its representative parts SimpleField returnValue = base.SetField(id, value); parseTextValue(); return returnValue; }
/// <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); }