001/* 002 * Serializer.java July 2006 003 * 004 * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net> 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 015 * implied. See the License for the specific language governing 016 * permissions and limitations under the License. 017 */ 018 019package org.simpleframework.xml; 020 021import java.io.InputStream; 022import java.io.OutputStream; 023import java.io.Reader; 024import java.io.Writer; 025import java.io.File; 026 027import org.simpleframework.xml.stream.InputNode; 028import org.simpleframework.xml.stream.OutputNode; 029 030/** 031 * The <code>Serializer</code> interface is used to represent objects 032 * that can serialize and deserialize objects to an from XML. This 033 * exposes several <code>read</code> and <code>write</code> methods 034 * that can read from and write to various sources. Typically an 035 * object will be read from an XML file and written to some other 036 * file or stream. 037 * <p> 038 * An implementation of the <code>Serializer</code> interface is free 039 * to use any desired XML parsing framework. If a framework other 040 * than the Java streaming API for XML is required then it should be 041 * wrapped within the <code>org.simpleframework.xml.stream</code> API, 042 * which offers a framework neutral facade. 043 * 044 * @author Niall Gallagher 045 */ 046public interface Serializer { 047 048 /** 049 * This <code>read</code> method will read the contents of the XML 050 * document from the provided source and convert it into an object 051 * of the specified type. If the XML source cannot be deserialized 052 * or there is a problem building the object graph an exception 053 * is thrown. The instance deserialized is returned. 054 * 055 * @param type this is the class type to be deserialized from XML 056 * @param source this provides the source of the XML document 057 * 058 * @return the object deserialized from the XML document 059 * 060 * @throws Exception if the object cannot be fully deserialized 061 */ 062 <T> T read(Class<? extends T> type, String source) throws Exception; 063 064 /** 065 * This <code>read</code> method will read the contents of the XML 066 * document from the provided source and convert it into an object 067 * of the specified type. If the XML source cannot be deserialized 068 * or there is a problem building the object graph an exception 069 * is thrown. The instance deserialized is returned. 070 * 071 * @param type this is the class type to be deserialized from XML 072 * @param source this provides the source of the XML document 073 * 074 * @return the object deserialized from the XML document 075 * 076 * @throws Exception if the object cannot be fully deserialized 077 */ 078 <T> T read(Class<? extends T> type, File source) throws Exception; 079 080 /** 081 * This <code>read</code> method will read the contents of the XML 082 * document from the provided source and convert it into an object 083 * of the specified type. If the XML source cannot be deserialized 084 * or there is a problem building the object graph an exception 085 * is thrown. The instance deserialized is returned. 086 * 087 * @param type this is the class type to be deserialized from XML 088 * @param source this provides the source of the XML document 089 * 090 * @return the object deserialized from the XML document 091 * 092 * @throws Exception if the object cannot be fully deserialized 093 */ 094 <T> T read(Class<? extends T> type, InputStream source) throws Exception; 095 096 /** 097 * This <code>read</code> method will read the contents of the XML 098 * document from the provided source and convert it into an object 099 * of the specified type. If the XML source cannot be deserialized 100 * or there is a problem building the object graph an exception 101 * is thrown. The instance deserialized is returned. 102 * 103 * @param type this is the class type to be deserialized from XML 104 * @param source this provides the source of the XML document 105 * 106 * @return the object deserialized from the XML document 107 * 108 * @throws Exception if the object cannot be fully deserialized 109 */ 110 <T> T read(Class<? extends T> type, Reader source) throws Exception; 111 112 /** 113 * This <code>read</code> method will read the contents of the XML 114 * document from the provided source and convert it into an object 115 * of the specified type. If the XML source cannot be deserialized 116 * or there is a problem building the object graph an exception 117 * is thrown. The instance deserialized is returned. 118 * 119 * @param type this is the class type to be deserialized from XML 120 * @param source this provides the source of the XML document 121 * 122 * @return the object deserialized from the XML document 123 * 124 * @throws Exception if the object cannot be fully deserialized 125 */ 126 <T> T read(Class<? extends T> type, InputNode source) throws Exception; 127 128 /** 129 * This <code>read</code> method will read the contents of the XML 130 * document from the provided source and convert it into an object 131 * of the specified type. If the XML source cannot be deserialized 132 * or there is a problem building the object graph an exception 133 * is thrown. The instance deserialized is returned. 134 * 135 * @param type this is the class type to be deserialized from XML 136 * @param source this provides the source of the XML document 137 * @param strict this determines whether to read in strict mode 138 * 139 * @return the object deserialized from the XML document 140 * 141 * @throws Exception if the object cannot be fully deserialized 142 */ 143 <T> T read(Class<? extends T> type, String source, boolean strict) throws Exception; 144 145 /** 146 * This <code>read</code> method will read the contents of the XML 147 * document from the provided source and convert it into an object 148 * of the specified type. If the XML source cannot be deserialized 149 * or there is a problem building the object graph an exception 150 * is thrown. The instance deserialized is returned. 151 * 152 * @param type this is the class type to be deserialized from XML 153 * @param source this provides the source of the XML document 154 * @param strict this determines whether to read in strict mode 155 * 156 * @return the object deserialized from the XML document 157 * 158 * @throws Exception if the object cannot be fully deserialized 159 */ 160 <T> T read(Class<? extends T> type, File source, boolean strict) throws Exception; 161 162 /** 163 * This <code>read</code> method will read the contents of the XML 164 * document from the provided source and convert it into an object 165 * of the specified type. If the XML source cannot be deserialized 166 * or there is a problem building the object graph an exception 167 * is thrown. The instance deserialized is returned. 168 * 169 * @param type this is the class type to be deserialized from XML 170 * @param source this provides the source of the XML document 171 * @param strict this determines whether to read in strict mode 172 * 173 * @return the object deserialized from the XML document 174 * 175 * @throws Exception if the object cannot be fully deserialized 176 */ 177 <T> T read(Class<? extends T> type, InputStream source, boolean strict) throws Exception; 178 179 /** 180 * This <code>read</code> method will read the contents of the XML 181 * document from the provided source and convert it into an object 182 * of the specified type. If the XML source cannot be deserialized 183 * or there is a problem building the object graph an exception 184 * is thrown. The instance deserialized is returned. 185 * 186 * @param type this is the class type to be deserialized from XML 187 * @param source this provides the source of the XML document 188 * @param strict this determines whether to read in strict mode 189 * 190 * @return the object deserialized from the XML document 191 * 192 * @throws Exception if the object cannot be fully deserialized 193 */ 194 <T> T read(Class<? extends T> type, Reader source, boolean strict) throws Exception; 195 196 /** 197 * This <code>read</code> method will read the contents of the XML 198 * document from the provided source and convert it into an object 199 * of the specified type. If the XML source cannot be deserialized 200 * or there is a problem building the object graph an exception 201 * is thrown. The instance deserialized is returned. 202 * 203 * @param type this is the class type to be deserialized from XML 204 * @param source this provides the source of the XML document 205 * @param strict this determines whether to read in strict mode 206 * 207 * @return the object deserialized from the XML document 208 * 209 * @throws Exception if the object cannot be fully deserialized 210 */ 211 <T> T read(Class<? extends T> type, InputNode source, boolean strict) throws Exception; 212 213 /** 214 * This <code>read</code> method will read the contents of the XML 215 * document from the provided source and populate the object with 216 * the values deserialized. This is used as a means of injecting an 217 * object with values deserialized from an XML document. If the 218 * XML source cannot be deserialized or there is a problem building 219 * the object graph an exception is thrown. 220 * 221 * @param value this is the object to deserialize the XML in to 222 * @param source this provides the source of the XML document 223 * 224 * @return the same instance provided is returned when finished 225 * 226 * @throws Exception if the object cannot be fully deserialized 227 */ 228 <T> T read(T value, String source) throws Exception; 229 230 /** 231 * This <code>read</code> method will read the contents of the XML 232 * document from the provided source and populate the object with 233 * the values deserialized. This is used as a means of injecting an 234 * object with values deserialized from an XML document. If the 235 * XML source cannot be deserialized or there is a problem building 236 * the object graph an exception is thrown. 237 * 238 * @param value this is the object to deserialize the XML in to 239 * @param source this provides the source of the XML document 240 * 241 * @return the same instance provided is returned when finished 242 * 243 * @throws Exception if the object cannot be fully deserialized 244 */ 245 <T> T read(T value, File source) throws Exception; 246 247 /** 248 * This <code>read</code> method will read the contents of the XML 249 * document from the provided source and populate the object with 250 * the values deserialized. This is used as a means of injecting an 251 * object with values deserialized from an XML document. If the 252 * XML source cannot be deserialized or there is a problem building 253 * the object graph an exception is thrown. 254 * 255 * @param value this is the object to deserialize the XML in to 256 * @param source this provides the source of the XML document 257 * 258 * @return the same instance provided is returned when finished 259 * 260 * @throws Exception if the object cannot be fully deserialized 261 */ 262 <T> T read(T value, InputStream source) throws Exception; 263 264 /** 265 * This <code>read</code> method will read the contents of the XML 266 * document from the provided source and populate the object with 267 * the values deserialized. This is used as a means of injecting an 268 * object with values deserialized from an XML document. If the 269 * XML source cannot be deserialized or there is a problem building 270 * the object graph an exception is thrown. 271 * 272 * @param value this is the object to deserialize the XML in to 273 * @param source this provides the source of the XML document 274 * 275 * @return the same instance provided is returned when finished 276 * 277 * @throws Exception if the object cannot be fully deserialized 278 */ 279 <T> T read(T value, Reader source) throws Exception; 280 281 /** 282 * This <code>read</code> method will read the contents of the XML 283 * document from the provided source and populate the object with 284 * the values deserialized. This is used as a means of injecting an 285 * object with values deserialized from an XML document. If the 286 * XML source cannot be deserialized or there is a problem building 287 * the object graph an exception is thrown. 288 * 289 * @param value this is the object to deserialize the XML in to 290 * @param source this provides the source of the XML document 291 * 292 * @return the same instance provided is returned when finished 293 * 294 * @throws Exception if the object cannot be fully deserialized 295 */ 296 <T> T read(T value, InputNode source) throws Exception; 297 298 /** 299 * This <code>read</code> method will read the contents of the XML 300 * document from the provided source and populate the object with 301 * the values deserialized. This is used as a means of injecting an 302 * object with values deserialized from an XML document. If the 303 * XML source cannot be deserialized or there is a problem building 304 * the object graph an exception is thrown. 305 * 306 * @param value this is the object to deserialize the XML in to 307 * @param source this provides the source of the XML document 308 * @param strict this determines whether to read in strict mode 309 * 310 * @return the same instance provided is returned when finished 311 * 312 * @throws Exception if the object cannot be fully deserialized 313 */ 314 <T> T read(T value, String source, boolean strict) throws Exception; 315 316 /** 317 * This <code>read</code> method will read the contents of the XML 318 * document from the provided source and populate the object with 319 * the values deserialized. This is used as a means of injecting an 320 * object with values deserialized from an XML document. If the 321 * XML source cannot be deserialized or there is a problem building 322 * the object graph an exception is thrown. 323 * 324 * @param value this is the object to deserialize the XML in to 325 * @param source this provides the source of the XML document 326 * @param strict this determines whether to read in strict mode 327 * 328 * @return the same instance provided is returned when finished 329 * 330 * @throws Exception if the object cannot be fully deserialized 331 */ 332 <T> T read(T value, File source, boolean strict) throws Exception; 333 334 /** 335 * This <code>read</code> method will read the contents of the XML 336 * document from the provided source and populate the object with 337 * the values deserialized. This is used as a means of injecting an 338 * object with values deserialized from an XML document. If the 339 * XML source cannot be deserialized or there is a problem building 340 * the object graph an exception is thrown. 341 * 342 * @param value this is the object to deserialize the XML in to 343 * @param source this provides the source of the XML document 344 * @param strict this determines whether to read in strict mode 345 * 346 * @return the same instance provided is returned when finished 347 * 348 * @throws Exception if the object cannot be fully deserialized 349 */ 350 <T> T read(T value, InputStream source, boolean strict) throws Exception; 351 352 /** 353 * This <code>read</code> method will read the contents of the XML 354 * document from the provided source and populate the object with 355 * the values deserialized. This is used as a means of injecting an 356 * object with values deserialized from an XML document. If the 357 * XML source cannot be deserialized or there is a problem building 358 * the object graph an exception is thrown. 359 * 360 * @param value this is the object to deserialize the XML in to 361 * @param source this provides the source of the XML document 362 * @param strict this determines whether to read in strict mode 363 * 364 * @return the same instance provided is returned when finished 365 * 366 * @throws Exception if the object cannot be fully deserialized 367 */ 368 <T> T read(T value, Reader source, boolean strict) throws Exception; 369 370 /** 371 * This <code>read</code> method will read the contents of the XML 372 * document from the provided source and populate the object with 373 * the values deserialized. This is used as a means of injecting an 374 * object with values deserialized from an XML document. If the 375 * XML source cannot be deserialized or there is a problem building 376 * the object graph an exception is thrown. 377 * 378 * @param value this is the object to deserialize the XML in to 379 * @param source this provides the source of the XML document 380 * @param strict this determines whether to read in strict mode 381 * 382 * @return the same instance provided is returned when finished 383 * 384 * @throws Exception if the object cannot be fully deserialized 385 */ 386 <T> T read(T value, InputNode source, boolean strict) throws Exception; 387 388 /** 389 * This <code>validate</code> method will validate the contents of 390 * the XML document against the specified XML class schema. This is 391 * used to perform a read traversal of the class schema such that 392 * the document can be tested against it. This is preferred to 393 * reading the document as it does not instantiate the objects or 394 * invoke any callback methods, thus making it a safe validation. 395 * 396 * @param type this is the class type to be validated against XML 397 * @param source this provides the source of the XML document 398 * 399 * @return true if the document matches the class XML schema 400 * 401 * @throws Exception if the class XML schema does not fully match 402 */ 403 boolean validate(Class type, String source) throws Exception; 404 405 /** 406 * This <code>validate</code> method will validate the contents of 407 * the XML document against the specified XML class schema. This is 408 * used to perform a read traversal of the class schema such that 409 * the document can be tested against it. This is preferred to 410 * reading the document as it does not instantiate the objects or 411 * invoke any callback methods, thus making it a safe validation. 412 * 413 * @param type this is the class type to be validated against XML 414 * @param source this provides the source of the XML document 415 * 416 * @return true if the document matches the class XML schema 417 * 418 * @throws Exception if the class XML schema does not fully match 419 */ 420 boolean validate(Class type, File source) throws Exception; 421 422 /** 423 * This <code>validate</code> method will validate the contents of 424 * the XML document against the specified XML class schema. This is 425 * used to perform a read traversal of the class schema such that 426 * the document can be tested against it. This is preferred to 427 * reading the document as it does not instantiate the objects or 428 * invoke any callback methods, thus making it a safe validation. 429 * 430 * @param type this is the class type to be validated against XML 431 * @param source this provides the source of the XML document 432 * 433 * @return true if the document matches the class XML schema 434 * 435 * @throws Exception if the class XML schema does not fully match 436 */ 437 boolean validate(Class type, InputStream source) throws Exception; 438 439 /** 440 * This <code>validate</code> method will validate the contents of 441 * the XML document against the specified XML class schema. This is 442 * used to perform a read traversal of the class schema such that 443 * the document can be tested against it. This is preferred to 444 * reading the document as it does not instantiate the objects or 445 * invoke any callback methods, thus making it a safe validation. 446 * 447 * @param type this is the class type to be validated against XML 448 * @param source this provides the source of the XML document 449 * 450 * @return true if the document matches the class XML schema 451 * 452 * @throws Exception if the class XML schema does not fully match 453 */ 454 boolean validate(Class type, Reader source) throws Exception; 455 456 /** 457 * This <code>validate</code> method will validate the contents of 458 * the XML document against the specified XML class schema. This is 459 * used to perform a read traversal of the class schema such that 460 * the document can be tested against it. This is preferred to 461 * reading the document as it does not instantiate the objects or 462 * invoke any callback methods, thus making it a safe validation. 463 * 464 * @param type this is the class type to be validated against XML 465 * @param source this provides the source of the XML document 466 * 467 * @return true if the document matches the class XML schema 468 * 469 * @throws Exception if the class XML schema does not fully match 470 */ 471 boolean validate(Class type, InputNode source) throws Exception; 472 473 /** 474 * This <code>validate</code> method will validate the contents of 475 * the XML document against the specified XML class schema. This is 476 * used to perform a read traversal of the class schema such that 477 * the document can be tested against it. This is preferred to 478 * reading the document as it does not instantiate the objects or 479 * invoke any callback methods, thus making it a safe validation. 480 * 481 * @param type this is the class type to be validated against XML 482 * @param source this provides the source of the XML document 483 * @param strict this determines whether to read in strict mode 484 * 485 * @return true if the document matches the class XML schema 486 * 487 * @throws Exception if the class XML schema does not fully match 488 */ 489 boolean validate(Class type, String source, boolean strict) throws Exception; 490 491 /** 492 * This <code>validate</code> method will validate the contents of 493 * the XML document against the specified XML class schema. This is 494 * used to perform a read traversal of the class schema such that 495 * the document can be tested against it. This is preferred to 496 * reading the document as it does not instantiate the objects or 497 * invoke any callback methods, thus making it a safe validation. 498 * 499 * @param type this is the class type to be validated against XML 500 * @param source this provides the source of the XML document 501 * @param strict this determines whether to read in strict mode 502 * 503 * @return true if the document matches the class XML schema 504 * 505 * @throws Exception if the class XML schema does not fully match 506 */ 507 boolean validate(Class type, File source, boolean strict) throws Exception; 508 509 /** 510 * This <code>validate</code> method will validate the contents of 511 * the XML document against the specified XML class schema. This is 512 * used to perform a read traversal of the class schema such that 513 * the document can be tested against it. This is preferred to 514 * reading the document as it does not instantiate the objects or 515 * invoke any callback methods, thus making it a safe validation. 516 * 517 * @param type this is the class type to be validated against XML 518 * @param source this provides the source of the XML document 519 * @param strict this determines whether to read in strict mode 520 * 521 * @return true if the document matches the class XML schema 522 * 523 * @throws Exception if the class XML schema does not fully match 524 */ 525 boolean validate(Class type, InputStream source, boolean strict) throws Exception; 526 527 /** 528 * This <code>validate</code> method will validate the contents of 529 * the XML document against the specified XML class schema. This is 530 * used to perform a read traversal of the class schema such that 531 * the document can be tested against it. This is preferred to 532 * reading the document as it does not instantiate the objects or 533 * invoke any callback methods, thus making it a safe validation. 534 * 535 * @param type this is the class type to be validated against XML 536 * @param source this provides the source of the XML document 537 * @param strict this determines whether to read in strict mode 538 * 539 * @return true if the document matches the class XML schema 540 * 541 * @throws Exception if the class XML schema does not fully match 542 */ 543 boolean validate(Class type, Reader source, boolean strict) throws Exception; 544 545 /** 546 * This <code>validate</code> method will validate the contents of 547 * the XML document against the specified XML class schema. This is 548 * used to perform a read traversal of the class schema such that 549 * the document can be tested against it. This is preferred to 550 * reading the document as it does not instantiate the objects or 551 * invoke any callback methods, thus making it a safe validation. 552 * 553 * @param type this is the class type to be validated against XML 554 * @param source this provides the source of the XML document 555 * @param strict this determines whether to read in strict mode 556 * 557 * @return true if the document matches the class XML schema 558 * 559 * @throws Exception if the class XML schema does not fully match 560 */ 561 boolean validate(Class type, InputNode source, boolean strict) throws Exception; 562 563 /** 564 * This <code>write</code> method will traverse the provided object 565 * checking for field annotations in order to compose the XML data. 566 * This uses the <code>getClass</code> method on the object to 567 * determine the class file that will be used to compose the schema. 568 * If there is no <code>Root</code> annotation for the class then 569 * this will throw an exception. The root annotation is the only 570 * annotation required for an object to be serialized. 571 * 572 * @param source this is the object that is to be serialized 573 * @param out this is where the serialized XML is written to 574 * 575 * @throws Exception if the schema for the object is not valid 576 */ 577 void write(Object source, File out) throws Exception; 578 579 /** 580 * This <code>write</code> method will traverse the provided object 581 * checking for field annotations in order to compose the XML data. 582 * This uses the <code>getClass</code> method on the object to 583 * determine the class file that will be used to compose the schema. 584 * If there is no <code>Root</code> annotation for the class then 585 * this will throw an exception. The root annotation is the only 586 * annotation required for an object to be serialized. 587 * 588 * @param source this is the object that is to be serialized 589 * @param out this is where the serialized XML is written to 590 * 591 * @throws Exception if the schema for the object is not valid 592 */ 593 void write(Object source, OutputStream out) throws Exception; 594 595 /** 596 * This <code>write</code> method will traverse the provided object 597 * checking for field annotations in order to compose the XML data. 598 * This uses the <code>getClass</code> method on the object to 599 * determine the class file that will be used to compose the schema. 600 * If there is no <code>Root</code> annotation for the class then 601 * this will throw an exception. The root annotation is the only 602 * annotation required for an object to be serialized. 603 * 604 * @param source this is the object that is to be serialized 605 * @param out this is where the serialized XML is written to 606 * 607 * @throws Exception if the schema for the object is not valid 608 */ 609 void write(Object source, Writer out) throws Exception; 610 611 /** 612 * This <code>write</code> method will traverse the provided object 613 * checking for field annotations in order to compose the XML data. 614 * This uses the <code>getClass</code> method on the object to 615 * determine the class file that will be used to compose the schema. 616 * If there is no <code>Root</code> annotation for the class then 617 * this will throw an exception. The root annotation is the only 618 * annotation required for an object to be serialized. 619 * 620 * @param source this is the object that is to be serialized 621 * @param root this is where the serialized XML is written to 622 * 623 * @throws Exception if the schema for the object is not valid 624 */ 625 void write(Object source, OutputNode root) throws Exception; 626}