1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
(* $Id$ *)
open Pxp_types
open Pxp_document
open Netcgi
open Netcgi_types
(* Configuration: At least the [encoding] should be the same for all
* XML data the program processes. Furthermore, it is a good idea
* to use only one namespace manager.
*
* We declare the following normprefixes:
* l: The namespace for library data
* h: The namespace for XHTML
*)
let mng = Pxp_dtd.create_namespace_manager();;
mng # add_namespace "l" "http://sample/library/ns";;
mng # add_namespace "h" "http://www.w3.org/1999/xhtml";;
let config =
{ default_config with
encoding = `Enc_utf8;
enable_namespace_processing = Some mng
} ;;
(* The [scope] is only needed for the generated output.
* It is sufficient to mention the XHTML namespace. It is
* bound to the default prefix.
*)
let scope = Pxp_dtd.create_namespace_scope
~decl:[ "", "http://www.w3.org/1999/xhtml" ]
mng;;
(* We use UTF-8 as representation, tell pxp-pp: *)
<:pxp_charset< representation="UTF-8" >>;;
(* Our library has books, and other media, which are either video
* or audio. This DTD describes only the overall structure.
* It does not (and cannot) enforce certain formats of data fields
* (we would need schemas for that).
*
* The elements are part of the namespace http://sample/library,
* and so we are using the namespace prefix "l".
*)
let library_dtd_string =
<:pxp_text<
<!-- This DTD has the SYSTEM URI http://sample/library/dtd -->
<!ELEMENT l:library (l:book|l:video|l:audio)*>
<!ELEMENT l:book (l:author+,l:title,l:year?,l:publisher?,l:location?)>
<!ELEMENT l:video (l:director,l:actor*,l:genre,l:title,l:year?,l:medium?)>
<!ELEMENT l:audio (l:artist+,l:title,l:year?,l:medium?)>
<!ELEMENT l:author (#PCDATA)>
<!ELEMENT l:title (#PCDATA)>
<!ELEMENT l:year (#PCDATA)>
<!ELEMENT l:publisher (#PCDATA)>
<!ELEMENT l:location (#PCDATA)>
<!ELEMENT l:director (#PCDATA)>
<!ELEMENT l:actor (#PCDATA)>
<!ELEMENT l:genre (#PCDATA)>
<!ELEMENT l:medium (#PCDATA)>
<!ELEMENT l:artist (#PCDATA)>
>>;;
(* Create the DTD object: *)
let library_dtd =
Pxp_dtd_parser.parse_dtd_entity config (from_string library_dtd_string) ;;
(* The catalog defines the meaning of the SYSTEM IDs: *)
let catalog =
new Pxp_reader.lookup_system_id_as_string
[ "http://sample/library/dtd", library_dtd_string ] ;;
(* Parse library data:
*
* We decide to read the data into O'Caml values first, and to transform
* them in a second step. Alternatively, it is also possible to carry
* out the transformation in only one step, but in general I do not
* recommend this.
*)
let parse_library src =
(* Parse the library data found in [src]. The data are immediately
* validated. Note, however, that we do not know yet which DTD was
* used for validation, and whether the right root element was
* used. These checks must be programmed manually.
*
* The XML tree is passed over to [factory]
*)
let spec = Pxp_tree_parser.default_namespace_spec in
let lib = Pxp_tree_parser.parse_document_entity config src spec in
(* Check whether [lib] refers to the right DTD: *)
( match lib # dtd # id with
Some(External(System "http://sample/library/dtd")) ->
() (* accepted *)
| Some(External _) ->
failwith "Library data file refers to the wrong DTD"
| Some(Derived _) ->
failwith "Library data file refers to the wrong DTD or extends the DTD"
| Some Internal ->
failwith "Library data file must refer to external DTD"
| None ->
failwith "DTD name completely missing in library data file"
);
(* Check whether the root element is acceptable: For this test,
* we take the name of the real root element, because the namespace
* has been processed, and not the name declared in the DTD.
*)
( match lib # root # node_type with
T_element "l:library" ->
() (* accepted *)
| _ ->
failwith "Wrong root element"
);
lib
;;
(* Internal representation of the library data:
*
* The objects are created by
* [ new <class> xml]
* where [xml] is the corresponding part of the XML tree.
*
* The following classes are later extended by the method [transform]
* which is only declared for now.
*)
class virtual ['ext] book (xml : 'ext node) = object
val mutable authors = []
val mutable title = ""
val mutable year = None
val mutable publisher = None
val mutable location = None
initializer (
(* Fill the instance variables from the XML tree [xml]:
* The first method is to scan the children, and to match the element
* names.
*)
assert(xml # node_type = T_element "l:book"); (* assumption *)
xml # iter_nodes
(fun child ->
match child # node_type with
T_element "l:author" ->
authors <- authors @ [ child # data ]
| T_element "l:title" ->
title <- child # data
| T_element "l:year" ->
year <- Some (child # data)
| T_element "l:publisher" ->
publisher <- Some (child # data)
| T_element "l:location" ->
location <- Some (child # data)
| _ ->
assert false
(* This cannot happen because the tree is validated *)
);
)
method virtual transform : unit -> 'ext node
end
class virtual ['ext] video (xml : 'ext node) = object
val mutable director = ""
val mutable actors = []
val mutable genre = ""
val mutable title = ""
val mutable year = None
val mutable medium = None
initializer (
(* Fill the instance variables from the XML tree [xml]:
* The second method is a stream parser. It is a bit overkill for this
* purpose, but a very good choice for complex structures.
*)
assert(xml # node_type = T_element "l:video"); (* assumption *)
(* Note that this grammar performs the actions from right to left! *)
let rec parse_node =
parser
[< '(E_start_tag("l:video",_,_,_));
_ = parse_children;
'(E_end_tag(_,_));
(* 'E_end_of_stream *)
>] -> ()
and parse_children =
parser
[< '(E_start_tag("l:director",_,_,_));
data = parse_optional_char_data;
'(E_end_tag(_,_));
rest = parse_children;
>] ->
director <- data
| [< '(E_start_tag("l:actor",_,_,_));
data = parse_optional_char_data;
'(E_end_tag(_,_));
rest = parse_children;
>] ->
actors <- data :: actors
| [< '(E_start_tag("l:genre",_,_,_));
data = parse_optional_char_data;
'(E_end_tag(_,_));
rest = parse_children;
>] ->
genre <- data
| [< '(E_start_tag("l:title",_,_,_));
data = parse_optional_char_data;
'(E_end_tag(_,_));
rest = parse_children;
>] ->
title <- data
| [< '(E_start_tag("l:year",_,_,_));
data = parse_optional_char_data;
'(E_end_tag(_,_));
rest = parse_children;
>] ->
year <- Some data
| [< '(E_start_tag("l:medium",_,_,_));
data = parse_optional_char_data;
'(E_end_tag(_,_));
rest = parse_children;
>] ->
medium <- Some data
| [< >] ->
()
and parse_optional_char_data =
parser
[< 'E_char_data data >] -> data
| [< >] -> ""
in
parse_node (Stream.from (liquefy ~omit_positions:true (`Node xml)))
)
method virtual transform : unit -> 'ext node
end
class virtual ['ext] audio (xml : 'ext node) = object
val mutable artists = []
val mutable title = ""
val mutable year = None
val mutable medium = None
initializer (
(* Fill the instance variables from the XML tree [xml]:
* The third method is similar to the first, but uses an
* alist instead of direct pattern matching.
*)
assert(xml # node_type = T_element "l:audio"); (* assumption *)
let actions =
[ "l:artist", (fun data -> artists <- artists @ [data]);
"l:title", (fun data -> title <- data);
"l:year", (fun data -> year <- Some data);
"l:medium", (fun data -> medium <- Some data)
] in
xml # iter_nodes
(fun child ->
match child # node_type with
T_element name ->
let action =
try List.assoc name actions
with Not_found -> assert false in
action child#data
| _ ->
assert false
(* This cannot happen because the tree is validated *)
);
)
method virtual transform : unit -> 'ext node
end
class virtual ['ext] library (xml : 'ext node) factory = object
val mutable items = []
initializer (
assert(xml # node_type = T_element "l:library"); (* assumption *)
xml # iter_nodes
(fun child ->
match child # node_type with
T_element name ->
let creator =
try List.assoc name factory
with Not_found -> assert false in
let item = creator child in
items <- items @ [item]
| _ ->
assert false
)
)
method virtual transform : unit -> 'ext node
end
(* Transformation of libary data to XHTML:
*
* These classes extend the former by the missing [transform]
* method. This method returns XHTML trees.
*)
class ['ext] book_xhtml dtd xml = object
inherit ['ext] book xml
method transform() =
(* Verbose style: Every field is transformed by its own
* rule.
*)
let spec = Pxp_tree_parser.default_namespace_spec in
(fun ~year_nodes ~publisher_nodes ~location_nodes ->
<:pxp_tree<
<:scope>
<h:table>
( [ <h:tr> <h:th colspan="2" class="category"> "Book"
<h:tr> [ <h:td class="label">"Authors:"
<h:td class="data">
<*>(: String.concat ", " authors :)
]
<h:tr> [ <h:td class="label">"Title:"
<h:td class="data"><*>title
]
]
@ year_nodes
@ publisher_nodes
@ location_nodes
)
>>
)
~year_nodes:(
match year with
Some y ->
<:pxp_tree<
[ <:scope>
<h:tr> [ <h:td class="label">"Year:"
<h:td class="data"><*>y
]
]
>>
| None ->
[]
)
~publisher_nodes:(
match publisher with
Some p ->
<:pxp_tree<
[ <:scope>
<h:tr> [ <h:td class="label">"Published by:"
<h:td class="data"><*>p
]
]
>>
| None ->
[]
)
~location_nodes:(
match location with
Some l ->
<:pxp_tree<
[ <:scope>
<h:tr> [ <h:td class="label">"Location:"
<h:td class="data"><*>l
]
]
>>
| None ->
[]
)
end ;;
class ['ext] video_xhtml dtd xml = object
inherit ['ext] video xml
(* When all fields can be transformed in the same way, this method
* is much more effective. The field transformation has been factored
* out, and is now its own function.
*)
method transform() =
let spec = Pxp_tree_parser.default_namespace_spec in
let fields =
("Director", director) ::
("Actors", String.concat ", " actors) ::
("Genre", genre) ::
("Title", title) ::
( ( match year with
Some y -> [ "Year", y ]
| None -> []
) @
( match medium with
Some m -> [ "Medium", m ]
| None -> []
)
) in
let transform_field (label, data) =
<:pxp_tree<
<:scope>
<h:tr> [ <h:td class="label"><*>label ^ ":"
<h:td class="data"><*>data
]
>>
in
<:pxp_tree<
<:scope>
<h:table>
[ <h:tr> <h:th colspan="2" class="category"> "Video" ] @
(: List.map transform_field fields :)
>>
end ;;
class ['ext] audio_xhtml dtd xml = object
inherit ['ext] audio xml
(* Same method as for video *)
method transform() =
let spec = Pxp_tree_parser.default_namespace_spec in
let fields =
("Artists", String.concat ", " artists) ::
("Title", title) ::
( ( match year with
Some y -> [ "Year", y ]
| None -> []
) @
( match medium with
Some m -> [ "Medium", m ]
| None -> []
)
) in
let transform_field (label, data) =
<:pxp_tree<
<:scope>
<h:tr> [ <h:td class="label"><*>label ^ ":"
<h:td class="data"><*>data
]
>>
in
<:pxp_tree<
<:scope>
<h:table>
[ <h:tr> <h:th colspan="2" class="category"> "Audio" ] @
(: List.map transform_field fields :)
>>
end
class ['ext] library_xhtml dtd xml = object
inherit ['ext] library xml [ "l:book", new book_xhtml dtd;
"l:video", new video_xhtml dtd;
"l:audio", new audio_xhtml dtd ]
method transform() =
let spec = Pxp_tree_parser.default_namespace_spec in
let transform_item item =
<:pxp_tree<
<:scope>
<h:li>[ "Item: " (: item # transform() :) ]
>>
in
<:pxp_tree<
<:scope>
<h:ul>
(: List.map transform_item items :)
>>
end
let generate_xhtml_page dtd lib =
let spec = Pxp_tree_parser.default_namespace_spec in
let css =
<:pxp_text<
.category {
text-align: left;
font-weight: bold;
background-color: cyan;
}
>>
in
<:pxp_tree<
<:scope>
<h:html>
[ <h:head>
[ <h:title>"My little library"
<h:style type="text/css"><*>css
]
<h:body>
[ <h:h1>"My little library"
(: lib # transform() :)
]
]
>> ;;
(* Sample contents: This could also be read from an external file. *)
let library_contents =
<:pxp_text<<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE l:library SYSTEM "http://sample/library/dtd">
<library xmlns="http://sample/library/ns">
<book>
<author>Henning Mankell</author>
<title>Der Mann, der lchelte</title>
<year>2003</year>
<publisher>Deutscher Taschenbuch Verlag GmbH & Co KG</publisher>
<location>Mnchen</location>
</book>
<book>
<author>Frank Schtzing</author>
<title>Der Schwarm</title>
<year>2004</year>
<publisher>Verlag Kiepenheuer & Witsch</publisher>
<location>Kln</location>
</book>
<video>
<director>James Cameron</director>
<actor>Leonardo DiCaprio</actor>
<actor>Kate Winslet</actor>
<actor>Billy Zane</actor>
<actor>Kathy Bates</actor>
<actor>Frances Fisher</actor>
<actor>Bernard Hill</actor>
<actor>Jonathan Hyde</actor>
<actor>Danny Nucci</actor>
<actor>Gloria Stuart</actor>
<actor>David Warner</actor>
<actor>Victor Garber</actor>
<actor>Bill Paxton</actor>
<genre>Epos</genre>
<title>Titanic</title>
<year>1997</year>
<medium>DVD</medium>
</video>
<video>
<director>Michael Bully Herbig</director>
<actor>Michael Bully Herbig</actor>
<actor>Christian Tramitz</actor>
<actor>Marie Bumer</actor>
<actor>Hilmi Szer</actor>
<actor>Rick Kavanian</actor>
<actor>Sky Du Mont</actor>
<genre>Comedy</genre>
<title>Der Schuh des Manitu Extra Large</title>
<year>2003</year>
<medium>DVD</medium>
</video>
<audio>
<artist>Pet Shop Boys</artist>
<title>actually</title>
<year>1987</year>
<medium>CD</medium>
</audio>
</library>
>>;;
let main () =
let cgi =
new std_activation ~operating_type:buffered_transactional_optype () in
try
let dtd =
Pxp_dtd_parser.create_empty_dtd config in
dtd # set_namespace_manager mng;
let src =
from_string ~alt:[ catalog ] library_contents in
let page =
generate_xhtml_page
dtd
(new library_xhtml
dtd
(parse_library src) # root) in
cgi # set_header
~content_type:"application/xhtml+xml; charset=UTF-8"
();
cgi # output # output_string "<?xml version='1.0' encoding='UTF-8'?>\n";
page # display
(`Out_netchannel (cgi#output :> Netchannels.out_obj_channel)) `Enc_utf8;
cgi # output # commit_work()
with
error ->
cgi # output # rollback_work();
cgi # set_header
~status:`Forbidden (* Indicate the error *)
~cache:`No_cache
~content_type:"text/plain; charset=ISO-8859-1"
();
cgi # output # output_string "Software error:\n\n";
cgi # output # output_string (string_of_exn error);
cgi # output # commit_work()
;;
try
main()
with
error ->
prerr_endline (string_of_exn error);
exit 1
;;
|