/// <summary>Gets an element or attribute value identified by an XPath-like query string.</summary> /// NOTE: This method makes calls to SIFXPathContext. If multiple calls to /// <c>GetElementOrAttribute</c> are being done, it is much more efficient to create /// a new <c>SifXPathContext</c> by calling <c>SifXPathContext.NewInstance(sdo)</c> and then /// calling <c>.GetElementorAttributeon</c> on that SifXPathContext instance /// </remarks> /// <param name="xpath">An XPath-like query string that identifies the element or /// attribute to get. The string must reference elements and attributes /// by their <i>version-independent</i> names. /// </param> /// <returns> An Element instance encapsulating the element or attribute if /// found. If not found, <c>null</c> is returned. To retrieve the /// value of the Element, call its <c>getTextValue</c> method. /// </returns> public virtual Element GetElementOrAttribute(string xpath) { // XPath Navigation using this API causes the object to have to // remember the SIF Version being evaluated if (fVersion == null) { fVersion = Adk.SifVersion; } SifXPathContext spc = SifXPathContext.NewSIFContext(this); return(spc.GetElementOrAttribute(xpath)); }
/// <summary> /// If SIFElement restrictions are placed on this query, this method /// will take the SIFDataObject and call setChanged(false). It will then /// go through each of the SIFElement restrictions, resolve them, and /// call setChanged(true) on those elements only. This will cause the /// object to be rendered properly using SIFWriter. /// </summary> /// <param name="sdo"></param> public void SetRenderingRestrictionsTo(SifDataObject sdo) { if (sdo == null || fFieldRestrictions == null) { return; } sdo.SetChanged(false); // Go through and only set the filtered items to true SifXPathContext context = SifXPathContext.NewSIFContext(sdo); foreach (ElementRef elementRef in fFieldRestrictions) { String xPath = elementRef.XPath; Element e = context.GetElementOrAttribute(xPath); if (e != null) { e.SetChanged(); } } sdo.EnsureRootElementRendered(); }
/// <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); }
public void testSifResponseSender010() { string queryStr = @"<SIF_Query> <SIF_QueryObject ObjectName='SectionInfo'> <SIF_Element>@RefId</SIF_Element> <SIF_Element>@SchoolCourseInfoRefId</SIF_Element> <SIF_Element>@SchoolYear</SIF_Element> <SIF_Element>LocalId</SIF_Element> <SIF_Element>ScheduleInfoList/ScheduleInfo/@TermInfoRefId</SIF_Element> <SIF_Element>Description</SIF_Element> <SIF_Element>LanguageOfInstruction</SIF_Element> <SIF_Element>LanguageOfInstruction/Code</SIF_Element> </SIF_QueryObject> </SIF_Query>"; string sectionInfoStr = @"<SectionInfo RefId='D9C9889878144863B190C7D3428D7953' SchoolCourseInfoRefId='587F89D23EDD4761A59C04BA0D39E8D9' SchoolYear='2008'> <LocalId>1</LocalId> <Description>section 19</Description> <ScheduleInfoList> <ScheduleInfo TermInfoRefId='0D8165B1ADB34780BD1DFF9E38A7B935'> <TeacherList> <StaffPersonalRefId>F9D3916707634682B84C530BCF96B5CA</StaffPersonalRefId> </TeacherList> <SectionRoomList> <RoomInfoRefId>EED167D761CD493EA94A875F56ABB0CB</RoomInfoRefId> </SectionRoomList> <MeetingTimeList> <MeetingTime> <TimetableDay>R</TimetableDay> <TimetablePeriod>6</TimetablePeriod> </MeetingTime> <MeetingTime> <TimetableDay>F</TimetableDay> <TimetablePeriod>6</TimetablePeriod> </MeetingTime> <MeetingTime> <TimetableDay>W</TimetableDay> <TimetablePeriod>6</TimetablePeriod> </MeetingTime> <MeetingTime> <TimetableDay>M</TimetableDay> <TimetablePeriod>6</TimetablePeriod> </MeetingTime> <MeetingTime> <TimetableDay>T</TimetableDay> <TimetablePeriod>6</TimetablePeriod> </MeetingTime> </MeetingTimeList> </ScheduleInfo> </ScheduleInfoList> <MediumOfInstruction><Code>0605</Code></MediumOfInstruction> <LanguageOfInstruction><Code>eng</Code></LanguageOfInstruction> <SummerSchool>No</SummerSchool> </SectionInfo>"; SifParser parser = SifParser.NewInstance(); SIF_Query sifquery = (SIF_Query)parser.Parse(queryStr); SectionInfo section = (SectionInfo)parser.Parse(sectionInfoStr); Query query = new Query(sifquery); String SifRequestMsgId = Adk.MakeGuid(); String sourceId = "TEST_SOURCEID"; SifVersion testVersion = SifVersion.LATEST; int maxBufferSize = int.MaxValue; MessageDispatcher testDispatcher = new MessageDispatcher(Zone); Zone.SetDispatcher(testDispatcher); Zone.Connect(ProvisioningFlags.None); InMemoryProtocolHandler testProto = (InMemoryProtocolHandler)Zone.ProtocolHandler; testProto.clear(); SifResponseSender srs = new SifResponseSender(); srs.Open(Zone, SifRequestMsgId, sourceId, testVersion, maxBufferSize, query); srs.Write(section); srs.Close(); // Retrieve the SIF_Response message off the protocol handler and asssert the results SIF_Response response = (SIF_Response)testProto.readMsg(); Assert.AreEqual(SifRequestMsgId, response.SIF_RequestMsgId); Assert.AreEqual(1, response.SIF_PacketNumber.Value); Assert.AreEqual("No", response.SIF_MorePackets); SIF_Header header = response.SIF_Header; Assert.AreEqual(sourceId, header.SIF_DestinationId); SifDataObject responseObject = (SifDataObject)response.SIF_ObjectData.GetChildList()[0]; Assert.IsNotNull(responseObject); Console.Out.WriteLine(responseObject.ToXml()); SifXPathContext context = SifXPathContext.NewSIFContext(responseObject); foreach (ElementRef reference in query.FieldRestrictionRefs) { Element found = context.GetElementOrAttribute(reference.XPath); Assert.IsNotNull(found, reference.XPath); } Element sectionInfoList = responseObject.GetElementOrAttribute("ScheduleInfoList/ScheduleInfo/SectionInfoList"); Assert.IsNull(sectionInfoList); }