1"""XML-REFERENCE values from :rfc:`9253`."""
2
3from typing import ClassVar
4from urllib.parse import unquote, urlparse
5
6from icalendar.compatibility import Self
7from icalendar.prop.uri import vUri
8
9
10class vXmlReference(vUri):
11 """An XML-REFERENCE.
12
13 The associated value references an associated XML artifact and
14 is a URI with an XPointer anchor value.
15
16 This is defined in :rfc:`9253`, Section 7.
17 """
18
19 default_value: ClassVar[str] = "XML-REFERENCE"
20
21 @property
22 def xml_reference(self) -> str:
23 """The XML reference URI of this property."""
24 return self.uri
25
26 @property
27 def x_pointer(self) -> str | None:
28 """The XPointer of the URI.
29
30 The XPointer is defined in `W3C.WD-xptr-xpointer-20021219
31 <https://www.rfc-editor.org/rfc/rfc9253.html#W3C.WD-xptr-xpointer-20021219>`_,
32 and its use as an anchor is defined in `W3C.REC-xptr-framework-20030325
33 <https://www.rfc-editor.org/rfc/rfc9253.html#W3C.REC-xptr-framework-20030325>`_.
34
35 Returns:
36 The decoded x-pointer or ``None`` if no valid x-pointer is found.
37 """
38 parsed = urlparse(self.xml_reference)
39 fragment = unquote(parsed.fragment)
40 if not fragment.startswith("xpointer(") or not fragment.endswith(")"):
41 return None
42 return fragment[9:-1]
43
44 @classmethod
45 def examples(cls) -> list[Self]:
46 """Examples of vXmlReference."""
47 return [cls("http://example.com/doc.xml#xpointer(/doc/element)")]
48
49
50__all__ = ["vXmlReference"]