aboutsummaryrefslogtreecommitdiff
path: root/xacml-translation/json_xacml_translator/lib/translator.ml
diff options
context:
space:
mode:
authorJakob L. Kreuze2023-12-21 13:20:58 -0500
committerJakob L. Kreuze2023-12-21 13:20:58 -0500
commitbe7067d632aa665c20f96d3b344a84c95b0f8ff8 (patch)
treefc5564a72cc42c8a21d3e943f645afcaae78f5a5 /xacml-translation/json_xacml_translator/lib/translator.ml
parent5918f84a1a05881d93d9fab83104feca9981bfaa (diff)
Check-in remaining files
Diffstat (limited to 'xacml-translation/json_xacml_translator/lib/translator.ml')
-rw-r--r--xacml-translation/json_xacml_translator/lib/translator.ml81
1 files changed, 81 insertions, 0 deletions
diff --git a/xacml-translation/json_xacml_translator/lib/translator.ml b/xacml-translation/json_xacml_translator/lib/translator.ml
new file mode 100644
index 0000000..13125ab
--- /dev/null
+++ b/xacml-translation/json_xacml_translator/lib/translator.ml
@@ -0,0 +1,81 @@
+open Base
+
+type xml_element = (string * (string * string) list * Xml.xml list)
+
+let rec get_attr (needle : string) : (string * string) list -> string option = function
+ | (k, v) :: xs -> if String.(k = needle) then Some v else get_attr needle xs
+ | _ -> None
+
+let get_exn opt message =
+ match opt with
+ | Some x -> x
+ | _ -> failwith message
+
+let get_attr_exn needle attrs : string =
+ get_exn (get_attr needle attrs) "missing attribute"
+
+let xml_element_of_children (children : Xml.xml list) : xml_element list =
+ List.map children ~f:(function
+ | Element x -> x
+ | _ -> failwith "called on element with non-element children")
+
+let xml_element_of_Xml : Xml.xml -> xml_element = function
+ | Element (elt) -> elt
+ | _ -> failwith "not an element"
+
+(* --- *)
+
+let compile_attribute_value category identifier issuer ((tag_name, attributes, children) : xml_element) =
+ assert String.(tag_name = "AttributeValue");
+ let typ = get_attr_exn "DataType" attributes in
+ match children with
+ | [PCData s] ->
+ (match issuer with
+ | Some issuer ->
+ `Assoc
+ [
+ ("category", `String category);
+ ("identifier", `String identifier);
+ ("issuer", `String issuer);
+ ("value", `Assoc [("kind", `String "primitive");
+ ("type", `String typ);
+ ("value", `String s)])
+ ]
+ | _ ->
+ `Assoc
+ [
+ ("category", `String category);
+ ("identifier", `String identifier);
+ ("value", `Assoc [("kind", `String "primitive");
+ ("type", `String typ);
+ ("value", `String s)])
+ ])
+ | _ -> failwith "ill-formed attribute value"
+
+let compile_attribute category ((tag_name, attributes, children) : xml_element) =
+ assert String.(tag_name = "Attribute");
+ let identifier = get_attr_exn "AttributeId" attributes in
+ let issuer = get_attr "Issuer" attributes in
+ compile_attribute_value category identifier issuer (List.nth_exn children 0 |> xml_element_of_Xml)
+
+let compile_attributes ((tag_name, attributes, children) : xml_element) : _ =
+ assert String.(tag_name = "Attributes");
+ let category = get_attr_exn "Category" attributes in
+ children
+ |> xml_element_of_children
+ |> List.map ~f:(compile_attribute category)
+ (* |> List.concat *)
+
+let compile_request' : Xml.xml -> _ = function
+ | Element (("Request", _attributes, children)) ->
+ List.map ~f:xml_element_of_Xml children
+ |> List.map ~f:compile_attributes
+ |> List.concat
+ | Element ((tag_name, _, _)) ->
+ failwith (Printf.sprintf "(%s)" tag_name)
+ | PCData s -> failwith s
+
+let compile_request (request : string) : string =
+ let request = Xml.parse_string request in
+ let attributes = compile_request' request in
+ Yojson.Basic.to_string (`List attributes)

© 2015 - 2026 Jakob L. Kreuze