/// <summary> /// protected virtual void WriteAttributeSelector /// </summary> /// <param name="writer">XmlWriter writer</param> /// <param name="data">XacmlAttributeSelector data</param> protected virtual void WriteAttributeSelector(XmlWriter writer, XacmlAttributeSelector data) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (data == null) { throw new ArgumentNullException(nameof(data)); } writer.WriteStartElement(XacmlConstants.Prefixes.Policy, XacmlConstants.ElementNames.AttributeSelector, this.Version.NamespacePolicy); writer.WriteAttributeString(XacmlConstants.AttributeNames.RequestContextPath, data.Path.ToString()); writer.WriteAttributeString(XacmlConstants.AttributeNames.DataType, data.DataType.OriginalString); writer.WriteAttributeString(XacmlConstants.AttributeNames.MustBePresent, data.MustBePresent.ToString()); writer.WriteEndElement(); }
/// <summary> /// Write ApplyType (Not element!) /// </summary> /// <param name="writer"></param> /// <param name="apply"></param> private void WriteApplyType(XmlWriter writer, XacmlApply apply) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (apply == null) { throw new ArgumentNullException(nameof(apply)); } writer.WriteAttributeString(XacmlConstants.AttributeNames.FunctionId, apply.FunctionId.OriginalString); foreach (IXacmlApply applyElem in apply.Parameters) { Type applyElemType = applyElem.GetType(); if (applyElemType == typeof(XacmlAttributeSelector)) { XacmlAttributeSelector elem = applyElem as XacmlAttributeSelector; writer.WriteStartElement(XacmlConstants.Prefixes.Policy, XacmlConstants.ElementNames.AttributeSelector, this.Version.NamespacePolicy); writer.WriteAttributeString(XacmlConstants.AttributeNames.RequestContextPath, elem.Path); writer.WriteAttributeString(XacmlConstants.AttributeNames.DataType, elem.DataType.OriginalString); if (elem.MustBePresent.HasValue) { writer.WriteAttributeString(XacmlConstants.AttributeNames.DataType, XmlConvert.ToString(elem.MustBePresent.Value)); } writer.WriteEndElement(); } else if (applyElemType == typeof(XacmlResourceAttributeDesignator)) { this.WriteAttributeDesignator(writer, applyElem as XacmlResourceAttributeDesignator); } else if (applyElemType == typeof(XacmlActionAttributeDesignator)) { this.WriteAttributeDesignator(writer, applyElem as XacmlActionAttributeDesignator); } else if (applyElemType == typeof(XacmlEnvironmentAttributeDesignator)) { this.WriteAttributeDesignator(writer, applyElem as XacmlEnvironmentAttributeDesignator); } else if (applyElemType == typeof(XacmlSubjectAttributeDesignator)) { this.WriteAttributeDesignator(writer, applyElem as XacmlSubjectAttributeDesignator); } else if (applyElemType == typeof(XacmlAttributeValue)) { this.WriteAttributeValue(writer, applyElem as XacmlAttributeValue); } else if (applyElemType == typeof(XacmlFunction)) { writer.WriteStartElement(XacmlConstants.Prefixes.Policy, XacmlConstants.ElementNames.Function, this.Version.NamespacePolicy); writer.WriteAttributeString(XacmlConstants.AttributeNames.FunctionId, (applyElem as XacmlFunction).FunctionId.OriginalString); writer.WriteEndElement(); } else if (applyElemType == typeof(XacmlApply)) { this.WriteApply(writer, applyElem as XacmlApply); } } }
/// <summary> /// Reads the match. /// </summary> /// <param name="reader">The reader.</param> /// <returns></returns> protected virtual XacmlMatch ReadMatch(XmlReader reader) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } var gaMatchId = reader.GetAttribute("MatchId"); if (string.IsNullOrEmpty(gaMatchId)) { throw ThrowHelperXml(reader, "MatchId IsNullOrEmpty"); } Func <string, string, ReadElement <XacmlAttributeDesignator>, XacmlMatch> read = (matchType, designatorType, readFuncAttributeDesignator) => { XacmlMatch ret = null; if (!reader.IsStartElement(matchType, this.Version.NamespacePolicy)) { throw ThrowHelperXml(reader, string.Format("{0} NotStartElement", matchType)); } reader.ReadStartElement(matchType, this.Version.NamespacePolicy); var attributeValue = ReadAttributeValue(reader); IDictionary <Tuple <string, string>, Action> dicts = null; XacmlAttributeSelector sel = null; XacmlAttributeDesignator des = null; dicts = new Dictionary <Tuple <string, string>, Action>() { { new Tuple <string, string>(designatorType, this.Version.NamespacePolicy), () => des = readFuncAttributeDesignator(reader) }, { new Tuple <string, string>(XacmlConstants.ElementNames.AttributeSelector, this.Version.NamespacePolicy), () => sel = ReadAttributeSelector(reader) }, }; this.ReadChoice(reader, dicts, isRequired: true); reader.ReadEndElement(); switch (matchType) { case XacmlConstants.ElementNames.ActionMatch: ret = des != null ? new XacmlActionMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, (XacmlActionAttributeDesignator)des) : new XacmlActionMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, sel); break; case XacmlConstants.ElementNames.SubjectMatch: ret = des != null ? new XacmlSubjectMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, (XacmlSubjectAttributeDesignator)des) : new XacmlSubjectMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, sel); break; case XacmlConstants.ElementNames.ResourceMatch: ret = des != null ? new XacmlResourceMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, (XacmlResourceAttributeDesignator)des) : new XacmlResourceMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, sel); break; default: break; } return(ret); }; if (reader.LocalName == XacmlConstants.ElementNames.ActionMatch) { return(read(reader.LocalName, XacmlConstants.ElementNames.ActionAttributeDesignator, ReadAttributeDesignator)); } else if (reader.LocalName == XacmlConstants.ElementNames.SubjectMatch) { return(read(reader.LocalName, XacmlConstants.ElementNames.SubjectAttributeDesignator, ReadAttributeDesignator)); } else if (reader.LocalName == XacmlConstants.ElementNames.ResourceMatch) { return(read(reader.LocalName, XacmlConstants.ElementNames.ResourceAttributeDesignator, ReadAttributeDesignator)); } else { throw ThrowHelperXml(reader, string.Format("Incorrect start element. redaer.LocalName={0} ReadMatch", reader.LocalName)); } }