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> 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); } }
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); } } } } }
/// <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> /// 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; }