| 47 | |
| 48 | == SAX Writer Example == |
| 49 | |
| 50 | SAX writer allows to generate XML documents and streams. Below is small example of its use to generate XMPP stream. |
| 51 | |
| 52 | {{{ |
| 53 | with Ada.Wide_Wide_Text_IO; |
| 54 | |
| 55 | with League.Strings; |
| 56 | with XML.SAX.Attributes; |
| 57 | with XML.SAX.Pretty_Writers; |
| 58 | |
| 59 | procedure SAX_Writer_Example is |
| 60 | |
| 61 | use League.Strings; |
| 62 | |
| 63 | function "+" (Item : Wide_Wide_String) return Universal_String |
| 64 | renames To_Universal_String; |
| 65 | |
| 66 | Lang_Attribute : constant Universal_String := +"xml:lang"; |
| 67 | Stream_URI : constant Universal_String |
| 68 | := +"http://etherx.jabber.org/streams"; |
| 69 | Stream_Prefix : constant Universal_String := +"stream"; |
| 70 | Stream_Element : constant Universal_String := +"stream"; |
| 71 | Features_Element : constant Universal_String := +"features"; |
| 72 | Client_URI : constant Universal_String := +"jabber:client"; |
| 73 | Id_Attribute : constant Universal_String := +"id"; |
| 74 | From_Attribute : constant Universal_String := +"from"; |
| 75 | Version_Attribute : constant Universal_String := +"version"; |
| 76 | Compression_URI : constant Universal_String |
| 77 | := +"http://jabber.org/features/compress"; |
| 78 | Compression_Element : constant Universal_String := +"compression"; |
| 79 | Method_Element : constant Universal_String := +"method"; |
| 80 | |
| 81 | Writer : XML.SAX.Pretty_Writers.SAX_Pretty_Writer; |
| 82 | Attributes : XML.SAX.Attributes.SAX_Attributes; |
| 83 | |
| 84 | begin |
| 85 | Writer.Start_Document; |
| 86 | Writer.Start_Prefix_Mapping (Namespace_URI => Client_URI); |
| 87 | Writer.Start_Prefix_Mapping |
| 88 | (Namespace_URI => Stream_URI, Prefix => Stream_Prefix); |
| 89 | |
| 90 | -- Open stream element |
| 91 | |
| 92 | Attributes.Set_Value |
| 93 | (Qualified_Name => Id_Attribute, Value => +"656571601"); |
| 94 | Attributes.Set_Value |
| 95 | (Qualified_Name => From_Attribute, Value => +"jabber.ru"); |
| 96 | Attributes.Set_Value (Qualified_Name => Version_Attribute, Value => +"1.0"); |
| 97 | Attributes.Set_Value (Qualified_Name => Lang_Attribute, Value => +"ru"); |
| 98 | Writer.Start_Element |
| 99 | (Namespace_URI => Stream_URI, |
| 100 | Local_Name => Stream_Element, |
| 101 | Attributes => Attributes); |
| 102 | |
| 103 | -- Open features element |
| 104 | |
| 105 | Writer.Start_Element |
| 106 | (Namespace_URI => Stream_URI, Local_Name => Features_Element); |
| 107 | |
| 108 | -- Open compression element |
| 109 | |
| 110 | Writer.Start_Prefix_Mapping (Namespace_URI => Compression_URI); |
| 111 | Writer.Start_Element |
| 112 | (Namespace_URI => Compression_URI, Local_Name => Compression_Element); |
| 113 | |
| 114 | -- Output method |
| 115 | |
| 116 | Writer.Start_Element |
| 117 | (Namespace_URI => Compression_URI, Local_Name => Method_Element); |
| 118 | Writer.Characters (+"zlib"); |
| 119 | Writer.End_Element |
| 120 | (Namespace_URI => Compression_URI, Local_Name => Method_Element); |
| 121 | |
| 122 | -- Close compression element |
| 123 | |
| 124 | Writer.End_Element |
| 125 | (Namespace_URI => Compression_URI, Local_Name => Compression_Element); |
| 126 | Writer.End_Prefix_Mapping; |
| 127 | |
| 128 | -- Close features element |
| 129 | |
| 130 | Writer.End_Element |
| 131 | (Namespace_URI => Stream_URI, Local_Name => Features_Element); |
| 132 | |
| 133 | -- Close stream element |
| 134 | |
| 135 | Writer.End_Element |
| 136 | (Namespace_URI => Stream_URI, Local_Name => Stream_Element); |
| 137 | Writer.End_Prefix_Mapping (Prefix => Stream_Prefix); |
| 138 | Writer.End_Prefix_Mapping; |
| 139 | Writer.End_Document; |
| 140 | |
| 141 | Ada.Wide_Wide_Text_IO.Put_Line (Writer.Text.To_Wide_Wide_String); |
| 142 | end SAX_Writer_Example; |
| 143 | }}} |
| 144 | |
| 145 | This program generates following output (output is formatted to be more readable): |
| 146 | |
| 147 | {{{ |
| 148 | <?xml version="1.0"?> |
| 149 | <stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" |
| 150 | id="656571601" from="jabber.ru" version="1.0" xml:lang="ru"> |
| 151 | <stream:features xmlns="jabber:client"> |
| 152 | <compression xmlns="http://jabber.org/features/compress"> |
| 153 | <method>zlib</method> |
| 154 | </compression> |
| 155 | </stream:features> |
| 156 | </stream:stream> |
| 157 | }}} |