Debian Codegraph @

cds/savot/writer/SavotWriter.java

raw symbols SCIP
1 package cds.savot.writer;
2
3 //Copyright 2002-2014 - UDS/CNRS
4 //The SAVOT library is distributed under the terms
5 //of the GNU General Public License version 3.
6 //
7 //This file is part of SAVOT.
8 //
9 //SAVOT is free software: you can redistribute it and/or modify
10 //it under the terms of the GNU General Public License as published by
11 //the Free Software Foundation, version 3 of the License.
12 //
13 //SAVOT is distributed in the hope that it will be useful,
14 //but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 //GNU General Public License for more details.
17 //
18 //The GNU General Public License is available in COPYING file
19 //along with SAVOT.
20 //
21 //SAVOT - Simple Access to VOTable - Parser
22 //
23 //Author, Co-Author: Andre Schaaff (CDS), Laurent Bourges (JMMC)
24 import cds.savot.model.CoosysSet;
25 import cds.savot.model.FieldRefSet;
26 import cds.savot.model.FieldSet;
27 import cds.savot.model.GroupSet;
28 import cds.savot.model.InfoSet;
29 import cds.savot.model.LinkSet;
30 import cds.savot.model.OptionSet;
31 import cds.savot.model.ParamRefSet;
32 import cds.savot.model.ParamSet;
33 import cds.savot.model.ResourceSet;
34 import cds.savot.model.SavotBinary;
35 import cds.savot.model.SavotCoosys;
36 import cds.savot.model.SavotData;
37 import cds.savot.model.SavotField;
38 import cds.savot.model.SavotFieldRef;
39 import cds.savot.model.SavotFits;
40 import cds.savot.model.SavotGroup;
41 import cds.savot.model.SavotInfo;
42 import cds.savot.model.SavotLink;
43 import cds.savot.model.SavotMax;
44 import cds.savot.model.SavotMin;
45 import cds.savot.model.SavotOption;
46 import cds.savot.model.SavotParam;
47 import cds.savot.model.SavotParamRef;
48 import cds.savot.model.SavotResource;
49 import cds.savot.model.SavotStream;
50 import cds.savot.model.SavotTD;
51 import cds.savot.model.SavotTR;
52 import cds.savot.model.SavotTable;
53 import cds.savot.model.SavotTableData;
54 import cds.savot.model.SavotVOTable;
55 import cds.savot.model.SavotValues;
56 import cds.savot.model.TRSet;
57 import cds.savot.model.TableSet;
58 import java.io.BufferedOutputStream;
59 import java.io.BufferedWriter;
60 import java.io.FileOutputStream;
61 import java.io.IOException;
62 import java.io.OutputStream;
63 import java.io.OutputStreamWriter;
64 import java.io.PrintWriter;
65 import java.io.Writer;
66 import java.util.List;
67 import java.util.regex.Pattern;
68 import java.util.zip.GZIPOutputStream;
69
70 /**
71 * <p>
72 * VOTable document generation from memory
73 * </p>
74 *
75 *
76 * @author Andre Schaaff
77 *
78 * 6 June 2005 : the user can now write a VOTable document flow step by
79 * step, the previous method is available too (writing of a whole
80 * document) (kickoff 31 May 02)
81 */
82 @SuppressWarnings({"deprecation", "UseOfSystemOutOrSystemErr"})
83 public final class SavotWriter {
84
85 private static final String tdempty = "<TD/>";
86 private static final String tdbegin = "<TD>";
87 private static final String tdend = "</TD>";
88 private static final String trbegin = "<TR>";
89 private static final String trend = "</TR>\n";
90 private static final String tabledatabegin = "\n<TABLEDATA>\n";
91 private static final String tabledataend = "</TABLEDATA>";
92 private static final String databegin = "\n<DATA>";
93 private static final String dataend = "\n</DATA>";
94 private static final String tableend = "\n</TABLE>";
95 private static final String resourceend = "\n</RESOURCE>";
96 private static final String descriptionbegin = "\n<DESCRIPTION>";
97 private static final String descriptionend = "</DESCRIPTION>";
98 private static final String groupend = "\n</GROUP>";
99 private static final String definitionsbegin = "\n<DEFINITIONS>";
100 private static final String definitionsend = "\n</DEFINITIONS>";
101 private static final String paramend = "\n</PARAM>";
102 private static final String fieldend = "\n</FIELD>";
103 private static final String linkend = "</LINK>";
104 private static final String valuesend = "\n</VALUES>";
105 private static final String fitsend = "\n</FITS>";
106 private static final String binarybegin = "\n<BINARY>";
107 private static final String binaryend = "\n</BINARY>";
108 private static final String coosysend = "</COOSYS>";
109 private static final String streamend = "\n</STREAM>";
110 private static final String minend = "</MIN>";
111 private static final String maxend = "</MAX>";
112 private static final String optionend = "\n</OPTION>";
113
114 /* members */
115 // default xml top
116 private String top1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
117 private String styleSheet = "";
118 private boolean attributeEntities = true;
119 private boolean elementEntities = true;
120 /** buffered writer */
121 private Writer bw = null;
122 /* Regexp matchers */
123 private final Pattern patternAMP = Pattern.compile("&");
124 private final Pattern patternQUOT = Pattern.compile("\"");
125 private final Pattern patternLT = Pattern.compile("<");
126 private final Pattern patternGT = Pattern.compile(">");
127
128 /**
129 * Public constructor
130 */
131 public SavotWriter() {
132 // nop
133 }
134
135 /**
136 * Change the default XML document head Default value <?xml
137 * version="1.0 encoding="UTF-8"?>
138 *
139 * @param top1
140 * @since VOTable 1.2
141 *
142 */
143 public void setTop1(final String top1) {
144 this.top1 = top1;
145 }
146
147 /**
148 * Set a stylesheet Also possible with setTop1
149 *
150 * @param href
151 * @since VOTable 1.2
152 *
153 */
154 public void setStyleSheet(final String href) {
155 this.styleSheet = href;
156 }
157
158 /**
159 * Enable or disable Attribute entities mapping
160 *
161 * @param entities
162 * true if Attribute entities are taken into account
163 */
164 public void enableAttributeEntities(final boolean entities) {
165 this.attributeEntities = entities;
166 }
167
168 /**
169 * Enable or disable Element entities mapping
170 *
171 * @param entities
172 * true if Element entities are taken into account
173 */
174 public void enableElementEntities(final boolean entities) {
175 this.elementEntities = entities;
176 }
177
178 /**
179 * Enable or disable Attribute and Element entities mapping
180 *
181 * @param entities
182 * true if all entities are taken into account
183 */
184 public void enableEntities(final boolean entities) {
185 this.attributeEntities = entities;
186 this.elementEntities = entities;
187 }
188
189 /**
190 * Encode special characters to entities
191 * @param src
192 * @return src
193 */
194 public String encodeAttribute(final String src) {
195 if (attributeEntities) {
196 String out = patternAMP.matcher(src).replaceAll("&amp;"); // Character [&] (xml restriction)
197 out = patternLT.matcher(out).replaceAll("&lt;"); // Character [<] (xml restriction)
198 out = patternGT.matcher(out).replaceAll("&gt;"); // Character [>] (xml restriction)
199 out = patternQUOT.matcher(out).replaceAll("&quot;"); // Character ["] (attribute delimiter)
200 return out;
201 }
202 return src;
203 }
204
205 /**
206 * Encode special characters to entities
207 * @param src
208 * @return src
209 */
210 public String encodeElement(final String src) {
211 if (elementEntities) {
212 String out = patternAMP.matcher(src).replaceAll("&amp;"); // Character [&] (xml restriction)
213 out = patternLT.matcher(out).replaceAll("&lt;"); // Character [<] (xml restriction)
214 out = patternGT.matcher(out).replaceAll("&gt;"); // Character [>] (xml restriction)
215 return out;
216 }
217 return src;
218 }
219
220 /**
221 * Generates a VOTable XML document corresponding to the internal model The
222 * result is sent to the standard output
223 *
224 * @param votable
225 * object corresponding to the savot internal model
226 * @throws IOException
227 */
228 public void generateDocument(final SavotVOTable votable) throws IOException {
229 generateDocument(votable, null, null);
230 }
231
232 /**
233 * Generates a VOTable XML document corresponding to the internal model
234 *
235 * @param votable
236 * object corresponding to the savot internal model
237 * @param stream
238 * the result is sent to this stream
239 * @throws IOException
240 */
241 public void generateDocument(final SavotVOTable votable, final OutputStream stream)
242 throws IOException {
243 generateDocument(votable, null, stream);
244 }
245
246 /**
247 * Generates a VOTable XML document corresponding to the internal model
248 *
249 * @param votable
250 * object corresponding to the savot internal model
251 * @param file
252 * is sent to this file
253 * @throws IOException
254 */
255 public void generateDocument(final SavotVOTable votable, final String file)
256 throws IOException {
257 generateDocument(votable, file, null);
258 }
259
260 /**
261 * Generates a VOTable XML document corresponding to the internal model
262 *
263 * @param votable
264 * SavotVOTable
265 * @param file
266 * String
267 * @param stream
268 * OutputStream
269 * @throws IOException
270 */
271 public void generateDocument(final SavotVOTable votable, final String file,
272 final OutputStream stream) throws IOException {
273
274 if (file != null) {
275 initStream(file);
276 } else if (stream != null) {
277 initStream(stream);
278 } else {
279 initStream(new PrintWriter(System.out));
280 }
281
282 writeDocumentHead(votable);
283
284 // write COOSYS elements - deprecated since VOTable 1.2
285 writeCoosys(votable.getCoosys());
286
287 // write INFO elements
288 writeInfo(votable.getInfos());
289
290 // write PARAM elements
291 writeParam(votable.getParams());
292
293 // write GROUP elements - since VOTable 1.2
294 writeGroup(votable.getGroups());
295
296 // RESOURCE
297 writeResource(votable.getResources());
298
299 // write INFO elements - since VOTable 1.2
300 writeInfo(votable.getInfosAtEnd());
301
302 writeDocumentEnd();
303 }
304
305 /**
306 * Init the Stream for the output
307 *
308 * @param file
309 * String
310 * @throws IOException
311 */
312 public void initStream(final String file) throws IOException {
313 final boolean compressed = file.endsWith("gz");
314
315 final OutputStream outStream = getOutputStream(new FileOutputStream(file), compressed);
316 initStream(new OutputStreamWriter(outStream));
317 }
318
319 /**
320 * Init the Stream for the output
321 *
322 * @param stream
323 * OutputStream
324 * @throws IOException
325 */
326 public void initStream(final OutputStream stream) throws IOException {
327 final OutputStream outStream = getOutputStream(stream, false);
328 initStream(new OutputStreamWriter(outStream));
329 }
330
331 /**
332 * Init the Stream for the output using the given Writer
333 *
334 * @param writer writer implementation to write into
335 */
336 public void initStream(final Writer writer) {
337 bw = new BufferedWriter(writer, 64 * 1024); // 64K text buffer
338 }
339
340 /**
341 * Get a buffered output stream or a gzip output stream
342 * @param outstream stream to wrap
343 * @param compressed true to indicate to use a gzip input stream
344 * @return input stream
345 * @throws IOException useless
346 */
347 private OutputStream getOutputStream(final OutputStream outstream, final boolean compressed) throws IOException {
348 final int bufferSize = 64 * 1024; // 64K write buffer
349
350 final OutputStream out;
351 if (compressed) {
352 out = new GZIPOutputStream(outstream, bufferSize); // 512 by default
353 } else {
354 out = new BufferedOutputStream(outstream, bufferSize);
355 }
356
357 return out;
358 }
359
360 /**
361 * Write a comment
362 *
363 * @param comment
364 * String
365 * @throws IOException
366 */
367 public void writeComment(final String comment) throws IOException {
368 if (comment.length() != 0) {
369 bw.append("\n<!-- ").append(comment).append(" -->\n");
370 }
371 }
372
373 /**
374 * Write a description
375 *
376 * @param description
377 * @throws IOException
378 *
379 */
380 public void writeDescription(final String description) throws IOException {
381 if (description.length() != 0) {
382 // DESCRIPTION
383 bw.append(descriptionbegin).append(encodeElement(description)).append(descriptionend);
384 }
385 }
386
387 /**
388 * Write a VOTable XML head
389 *
390 * @param votable
391 * @throws IOException
392 */
393 public void writeDocumentHead(final SavotVOTable votable) throws IOException {
394 final Writer w = bw; // local copy
395
396 w.write(top1);
397
398 writeComment(votable.getAbove());
399
400 // XSL stylesheet
401 if (styleSheet.length() != 0) {
402 w.append("\n<?xml-stylesheet type=\"text/xsl\" href=\"").append(styleSheet).append("\" ?>");
403 }
404
405 w.write("\n<VOTABLE");
406 if (votable.getXmlns().length() != 0 || votable.getXmlnsxsi().length() != 0 || votable.getXsischema().length() != 0 || votable.getXsinoschema().length() != 0) {
407 if (votable.getXmlns().length() != 0) {
408 w.append(" xmlns=\"").append(votable.getXmlns()).append('"');
409 }
410
411 if (votable.getXmlnsxsi().length() != 0) {
412 w.append(" xmlns:xsi=\"").append(votable.getXmlnsxsi()).append('"');
413 }
414
415 if (votable.getXsischema().length() != 0) {
416 w.append(" xsi:schemaLocation=\"").append(votable.getXsischema()).append('"');
417 }
418
419 if (votable.getXsinoschema().length() != 0) {
420 w.append(" xsi:noNamespaceSchemaLocation=\"").append(votable.getXsinoschema()).append('"');
421 }
422
423 }
424 w.write(" version=");
425
426 if (votable.getVersion().length() != 0) {
427 if (votable.getId().length() != 0) {
428 w.append('"').append(votable.getVersion()).append('"').append(" ID=\"").append(votable.getId()).append('"').append('>');
429 } else {
430 w.append('"').append(votable.getVersion()).append('"').append('>');
431 }
432 } else {
433 if (votable.getId().length() != 0) {
434 w.append('"').append("1.2").append('"').append(" ID=\"").append(votable.getId()).append('"').append('>');
435 } else {
436 w.append('"').append("1.2").append('"').append('>');
437 }
438 }
439
440 writeComment(votable.getBelow());
441
442 // write DESCRIPTION element
443 writeDescription(votable.getDescription());
444
445 // deprecated since VOTable 1.1
446 if (votable.getDefinitions() != null) {
447 // DEFINITIONS begin
448 w.write(definitionsbegin);
449
450 // write COOSYS elements
451 writeCoosys(votable.getDefinitions().getCoosys());
452
453 // write PARAM elements
454 writeParam(votable.getDefinitions().getParams());
455
456 // DEFINITIONS end
457 w.write(definitionsend);
458 }
459 w.flush();
460 }
461
462 /**
463 * Write a VOTable XML end
464 *
465 * @throws IOException
466 */
467 public void writeDocumentEnd() throws IOException {
468 bw.write("\n</VOTABLE>\n");
469 bw.flush();
470 bw.close();
471 bw = null;
472 }
473
474 /**
475 * Write a COOSYS set
476 *
477 * @param coosysSet
478 * @throws IOException
479 */
480 public void writeCoosys(final CoosysSet coosysSet) throws IOException {
481 final Writer w = bw; // local copy
482
483 SavotCoosys coosys;
484
485 for (int i = 0, len = coosysSet.getItemCount(); i < len; i++) {
486 coosys = coosysSet.getItemAt(i);
487
488 writeComment(coosys.getAbove());
489
490 w.write("\n<COOSYS");
491
492 if (coosys.getId().length() != 0) {
493 w.append(" ID=\"").append(coosys.getId()).append('"');
494 }
495
496 if (coosys.getEquinox().length() != 0) {
497 w.append(" equinox=\"").append(coosys.getEquinox()).append('"');
498 }
499
500 if (coosys.getEpoch().length() != 0) {
501 w.append(" epoch=\"").append(coosys.getEpoch()).append('"');
502 }
503
504 if (coosys.getSystem().length() != 0) {
505 w.append(" system=\"").append(coosys.getSystem()).append('"');
506 }
507
508 if (coosys.getContent().length() != 0) {
509 w.write(">");
510
511 writeComment(coosys.getBelow());
512
513 w.write(coosys.getContent());
514
515 w.write(coosysend);
516 } else {
517 /* no content */
518 w.write("/>");
519
520 writeComment(coosys.getBelow());
521 }
522 }
523 }
524
525 /**
526 * Write a PARAM set
527 *
528 * @param params
529 * @throws IOException
530 */
531 public void writeParam(final ParamSet params) throws IOException {
532 if (params != null) {
533 final Writer w = bw; // local copy
534
535 SavotParam param;
536
537 for (int i = 0, len = params.getItemCount(); i < len; i++) {
538 param = params.getItemAt(i);
539
540 writeComment(param.getAbove());
541
542 w.write("\n<PARAM");
543
544 if (param.getName().length() != 0) {
545 w.append(" name=\"").append(encodeAttribute(param.getName())).append('"');
546 }
547
548 if (param.getId().length() != 0) {
549 w.append(" ID=\"").append(encodeAttribute(param.getId())).append('"');
550 }
551
552 if (param.getDataType().length() != 0) {
553 w.append(" datatype=\"").append(encodeAttribute(param.getDataType())).append('"');
554 }
555
556 if (param.getArraySize().length() != 0) {
557 w.append(" arraysize=\"").append(encodeAttribute(param.getArraySize())).append('"');
558 }
559
560 if (param.getPrecision().length() != 0) {
561 w.append(" precision=\"").append(encodeAttribute(param.getPrecision())).append('"');
562 }
563
564 if (param.getWidth().length() != 0) {
565 w.append(" width=\"").append(encodeAttribute(param.getWidth())).append('"');
566 }
567
568 // since VOTable version 1.2
569 if (param.getXtype().length() != 0) {
570 w.append(" xtype=\"").append(encodeAttribute(param.getXtype())).append('"');
571 }
572
573 if (param.getRef().length() != 0) {
574 w.append(" ref=\"").append(encodeAttribute(param.getRef())).append('"');
575 }
576
577 if (param.getUcd().length() != 0) {
578 w.append(" ucd=\"").append(encodeAttribute(param.getUcd())).append('"');
579 }
580
581 if (param.getUtype().length() != 0) {
582 w.append(" utype=\"").append(encodeAttribute(param.getUtype())).append('"');
583 }
584
585 // Mandatory value:
586 w.append(" value=\"").append(encodeAttribute(param.getValue())).append('"');
587
588 if (param.getUnit().length() != 0) {
589 w.append(" unit=\"").append(encodeAttribute(param.getUnit())).append('"');
590 }
591
592 if (param.getValues() != null || param.getLinks().getItemCount() != 0 || param.getDescription().length() != 0) {
593 w.write(">");
594
595 writeComment(param.getBelow());
596
597 // write DESCRIPTION element
598 writeDescription(param.getDescription());
599
600 // write VALUES element
601 writeValues(param.getValues());
602
603 // write LINK elements
604 writeLink(param.getLinks());
605
606 // write PARAM end
607 w.write(paramend);
608 } else {
609 w.write("/>");
610
611 writeComment(param.getBelow());
612 }
613 }
614 }
615 }
616
617 /**
618 * Write a PARAMref set
619 *
620 * @param refparams
621 * @throws IOException
622 */
623 public void writeParamRef(final ParamRefSet refparams) throws IOException {
624 if (refparams != null) {
625 final Writer w = bw; // local copy
626
627 SavotParamRef paramref;
628
629 for (int i = 0, len = refparams.getItemCount(); i < len; i++) {
630 paramref = refparams.getItemAt(i);
631
632 writeComment(paramref.getAbove());
633
634 w.write("\n<PARAMref");
635
636 if (paramref.getRef().length() != 0) {
637 w.append(" ref=\"").append(encodeAttribute(paramref.getRef())).append('"');
638 }
639
640 if (paramref.getUcd().length() != 0) {
641 w.append(" ucd=\"").append(encodeAttribute(paramref.getUcd())).append('"');
642 }
643
644 if (paramref.getUtype().length() != 0) {
645 w.append(" utype=\"").append(encodeAttribute(paramref.getUtype())).append('"');
646 }
647
648 w.write("/>");
649
650 writeComment(paramref.getBelow());
651 }
652 }
653 }
654
655 /**
656 * Write a LINK set
657 *
658 * @param linkSet
659 * @throws IOException
660 */
661 public void writeLink(final LinkSet linkSet) throws IOException {
662 final Writer w = bw; // local copy
663
664 SavotLink link;
665
666 for (int i = 0, len = linkSet.getItemCount(); i < len; i++) {
667 link = linkSet.getItemAt(i);
668
669 writeComment(link.getAbove());
670
671 w.write("\n<LINK");
672
673 if (link.getId().length() != 0) {
674 w.append(" ID=\"").append(encodeAttribute(link.getId())).append('"');
675 }
676
677 if (link.getContentRole().length() != 0) {
678 w.append(" content-role=\"").append(encodeAttribute(link.getContentRole())).append('"');
679 }
680
681 if (link.getContentType().length() != 0) {
682 w.append(" content-type=\"").append(encodeAttribute(link.getContentType())).append('"');
683 }
684
685 if (link.getTitle().length() != 0) {
686 w.append(" title=\"").append(encodeAttribute(link.getTitle())).append('"');
687 }
688
689 if (link.getValue().length() != 0) {
690 w.append(" value=\"").append(encodeAttribute(link.getValue())).append('"');
691 }
692
693 if (link.getHref().length() != 0) {
694 w.append(" href=\"").append(encodeAttribute(link.getHref()) + '"');
695 }
696
697 if (link.getGref().length() != 0) {
698 w.append(" gref=\"").append(encodeAttribute(link.getGref()) + '"');
699 }
700
701 if (link.getAction().length() != 0) {
702 w.append(" action=\"").append(encodeAttribute(link.getAction()) + '"');
703 }
704
705 if (link.getContent().length() != 0) {
706 w.write(">");
707
708 writeComment(link.getBelow());
709
710 w.write(link.getContent());
711
712 w.write(linkend);
713 } else {
714 /* no content */
715 w.write("/>");
716
717 writeComment(link.getBelow());
718 }
719 }
720 }
721
722 /**
723 * Write an INFO set
724 *
725 * @param infoSet
726 * @throws IOException
727 */
728 public void writeInfo(final InfoSet infoSet) throws IOException {
729 if (infoSet != null) {
730 final Writer w = bw; // local copy
731
732 SavotInfo info;
733
734 for (int i = 0, len = infoSet.getItemCount(); i < len; i++) {
735 info = infoSet.getItemAt(i);
736
737 // INFO
738 writeComment(info.getAbove());
739
740 w.write("\n<INFO");
741
742 if (info.getId().length() != 0) {
743 w.append(" ID=\"").append(encodeAttribute(info.getId())).append('"');
744 }
745
746 if (info.getName().length() != 0) {
747 w.append(" name=\"").append(encodeAttribute(info.getName())).append('"');
748 }
749
750 if (info.getValue().length() != 0) {
751 w.append(" value=\"").append(encodeAttribute(info.getValue())).append('"');
752 }
753
754 // since VOTable version 1.2
755 if (info.getXtype().length() != 0) {
756 w.append(" xtype=\"").append(encodeAttribute(info.getXtype())).append('"');
757 }
758
759 // since VOTable version 1.2
760 if (info.getRef().length() != 0) {
761 w.append(" ref=\"").append(encodeAttribute(info.getRef())).append('"');
762 }
763
764 // since VOTable version 1.2
765 if (info.getUnit().length() != 0) {
766 w.append(" unit=\"").append(encodeAttribute(info.getUnit())).append('"');
767 }
768
769 // since VOTable version 1.2
770 if (info.getUcd().length() != 0) {
771 w.append(" ucd=\"").append(encodeAttribute(info.getUcd())).append('"');
772 }
773
774 // since VOTable version 1.2
775 if (info.getUtype().length() != 0) {
776 w.append(" utype=\"").append(encodeAttribute(info.getUtype())).append('"');
777 }
778
779 // only for VOTable version before 1.2
780 if (info.getContent().length() != 0) {
781 w.write(">");
782
783 writeComment(info.getBelow());
784
785 w.append(encodeElement(info.getContent())).append("</INFO>");
786
787 } else {
788 // from VOTable 1.2
789 if (info.getDescription().length() != 0 || info.getValues() != null || info.getLinks().getItemCount() != 0) {
790 w.write(">");
791
792 writeComment(info.getBelow());
793
794 // write DESCRIPTION element
795 writeDescription(info.getDescription());
796
797 // write VALUES element
798 writeValues(info.getValues());
799
800 // write LINK elements
801 writeLink(info.getLinks());
802
803 w.write("</INFO>");
804 } else {
805 w.write("/>");
806 }
807 }
808 }
809 }
810 }
811
812 /**
813 * Write a FIELD set
814 *
815 * @param fieldSet
816 * @throws IOException
817 */
818 public void writeField(final FieldSet fieldSet) throws IOException {
819 final Writer w = bw; // local copy
820
821 SavotField field;
822
823 for (int i = 0, len = fieldSet.getItemCount(); i < len; i++) {
824 field = fieldSet.getItemAt(i);
825
826 writeComment(field.getAbove());
827
828 w.write("\n<FIELD");
829
830 if (field.getName().length() != 0) {
831 w.append(" name=\"").append(encodeAttribute(field.getName())).append('"');
832 }
833
834 if (field.getId().length() != 0) {
835 w.append(" ID=\"").append(encodeAttribute(field.getId())).append('"');
836 }
837
838 if (field.getDataType().length() != 0) {
839 w.append(" datatype=\"").append(encodeAttribute(field.getDataType())).append('"');
840 }
841
842 if (field.getArraySize().length() != 0) {
843 w.append(" arraysize=\"").append(encodeAttribute(field.getArraySize())).append('"');
844 }
845
846 if (field.getPrecision().length() != 0) {
847 w.append(" precision=\"").append(encodeAttribute(field.getPrecision())).append('"');
848 }
849
850 if (field.getWidth().length() != 0) {
851 w.append(" width=\"").append(encodeAttribute(field.getWidth())).append('"');
852 }
853
854 // since VOTable version 1.2
855 if (field.getXtype().length() != 0) {
856 w.append(" xtype=\"").append(encodeAttribute(field.getXtype())).append('"');
857 }
858
859 if (field.getRef().length() != 0) {
860 w.append(" ref=\"").append(encodeAttribute(field.getRef())).append('"');
861 }
862
863 if (field.getUcd().length() != 0) {
864 w.append(" ucd=\"").append(encodeAttribute(field.getUcd())).append('"');
865 }
866
867 if (field.getUtype().length() != 0) {
868 w.append(" utype=\"").append(encodeAttribute(field.getUtype())).append('"');
869 }
870
871 if (field.getType().length() != 0) {
872 w.append(" type=\"").append(encodeAttribute(field.getType())).append('"');
873 }
874
875 if (field.getUnit().length() != 0) {
876 w.append(" unit=\"").append(encodeAttribute(field.getUnit())).append('"');
877 }
878
879 if (field.getDescription().length() != 0 || field.getValues() != null || field.getLinks().getItemCount() != 0) {
880 w.write(">");
881
882 writeComment(field.getBelow());
883
884 // write DESCRIPTION element
885 writeDescription(field.getDescription());
886
887 // write VALUES element
888 writeValues(field.getValues());
889
890 // write LINK elements
891 writeLink(field.getLinks());
892
893 w.write(fieldend);
894 } else {
895 w.write("/>");
896 }
897 }
898 }
899
900 /**
901 * Write a FIELD set
902 *
903 * @param fieldRefSet
904 * @throws IOException
905 */
906 public void writeFieldRef(final FieldRefSet fieldRefSet) throws IOException {
907 final Writer w = bw; // local copy
908
909 SavotFieldRef fieldref;
910
911 for (int i = 0, len = fieldRefSet.getItemCount(); i < len; i++) {
912 fieldref = fieldRefSet.getItemAt(i);
913
914 writeComment(fieldref.getAbove());
915
916 w.write("\n<FIELDref");
917
918 if (fieldref.getRef().length() != 0) {
919 w.append(" ref=\"").append(encodeAttribute(fieldref.getRef())).append('"');
920 }
921
922 if (fieldref.getUcd().length() != 0) {
923 w.append(" ucd=\"").append(encodeAttribute(fieldref.getUcd())).append('"');
924 }
925
926 if (fieldref.getUtype().length() != 0) {
927 w.append(" utype=\"").append(encodeAttribute(fieldref.getUtype())).append('"');
928 }
929
930 w.write("/>");
931 }
932 }
933
934 /**
935 * Write a STREAM element
936 *
937 * @param stream
938 * @throws IOException
939 */
940 public void writeStream(final SavotStream stream) throws IOException {
941 final Writer w = bw; // local copy
942
943 writeComment(stream.getAbove());
944
945 w.write("\n<STREAM");
946
947 if (stream.getType().length() != 0) {
948 w.append(" type=\"").append(encodeAttribute(stream.getType())).append('"');
949 }
950
951 if (stream.getHref().length() != 0) {
952 w.append(" href=\"").append(encodeAttribute(stream.getHref())).append('"');
953 }
954
955 if (stream.getActuate().length() != 0) {
956 w.append(" actuate=\"").append(encodeAttribute(stream.getActuate())).append('"');
957 }
958
959 if (stream.getEncoding().length() != 0) {
960 w.append(" encoding=\"").append(encodeAttribute(stream.getEncoding())).append('"');
961 }
962
963 if (stream.getExpires().length() != 0) {
964 w.append(" expires=\"").append(encodeAttribute(stream.getExpires())).append('"');
965 }
966
967 if (stream.getRights().length() != 0) {
968 w.append(" rights=\"").append(encodeAttribute(stream.getRights())).append('"');
969 }
970
971 w.write(">");
972
973 writeComment(stream.getBelow());
974
975 if (stream.getContent().length() != 0) {
976 w.write(stream.getContent());
977 }
978
979 w.write(streamend);
980 }
981
982 /**
983 * Write a BINARY element
984 *
985 * @param binary
986 * @throws IOException
987 */
988 public void writeBinary(final SavotBinary binary) throws IOException {
989 if (binary.getStream() != null) {
990 final Writer w = bw; // local copy
991
992 writeComment(binary.getAbove());
993
994 w.write(binarybegin);
995
996 writeComment(binary.getBelow());
997
998 writeStream(binary.getStream());
999
1000 w.write(binaryend);
1001 }
1002 }
1003
1004 /**
1005 * Write a VALUES element
1006 *
1007 * @param values
1008 * @throws IOException
1009 */
1010 public void writeValues(final SavotValues values) throws IOException {
1011 if (values != null) {
1012 final Writer w = bw; // local copy
1013
1014 writeComment(values.getAbove());
1015
1016 w.write("\n<VALUES");
1017
1018 if (values.getId().length() != 0) {
1019 w.append(" ID=\"").append(encodeAttribute(values.getId())).append('"');
1020 }
1021
1022 if (values.getType().length() != 0) {
1023 w.append(" type=\"").append(encodeAttribute(values.getType())).append('"');
1024 }
1025
1026 if (values.getNull().length() != 0) {
1027 w.append(" null=\"").append(encodeAttribute(values.getNull())).append('"');
1028 }
1029
1030 if (values.getRef().length() != 0) {
1031 w.append(" ref=\"").append(encodeAttribute(values.getRef())).append('"');
1032 }
1033
1034 if (values.getInvalid().length() != 0) {
1035 w.append(" invalid=\"").append(encodeAttribute(values.getInvalid())).append('"');
1036 }
1037
1038 w.write(">");
1039
1040 writeComment(values.getBelow());
1041
1042 // MIN element
1043 if (values.getMin() != null) {
1044 SavotMin min = values.getMin();
1045 writeMin(min);
1046 }
1047
1048 // MAX element
1049 if (values.getMax() != null) {
1050 SavotMax max = values.getMax();
1051 writeMax(max);
1052 }
1053
1054 // write OPTION elements
1055 writeOption(values.getOptions());
1056
1057 w.write(valuesend);
1058 }
1059 }
1060
1061 /**
1062 * Write a FITS element
1063 *
1064 * @param fits
1065 * @throws IOException
1066 */
1067 public void writeFits(final SavotFits fits) throws IOException {
1068 final Writer w = bw; // local copy
1069
1070 writeComment(fits.getAbove());
1071
1072 w.write("\n<FITS");
1073
1074 if (fits.getExtnum().length() != 0) {
1075 w.append(" extnum=\"").append(encodeAttribute(fits.getExtnum())).append('"');
1076 }
1077
1078 w.write(">");
1079
1080 writeComment(fits.getBelow());
1081
1082 // STREAM element
1083 if (fits.getStream() != null) {
1084 // write STREAM element
1085 writeStream(fits.getStream());
1086 }
1087
1088 w.write(fitsend);
1089 }
1090
1091 /**
1092 * Write a MIN element
1093 *
1094 * @param min
1095 * @throws IOException
1096 */
1097 public void writeMin(final SavotMin min) throws IOException {
1098 final Writer w = bw; // local copy
1099
1100 writeComment(min.getAbove());
1101
1102 w.write("\n<MIN");
1103
1104 if (min.getValue().length() != 0) {
1105 w.append(" value=\"").append(encodeAttribute(min.getValue())).append('"');
1106 }
1107
1108 if (min.getInclusive().length() != 0) {
1109 w.append(" inclusive=\"").append(encodeAttribute(min.getInclusive())).append('"');
1110 }
1111
1112 if (min.getContent().length() != 0) {
1113 w.write(">");
1114
1115 writeComment(min.getBelow());
1116
1117 w.write(min.getContent());
1118
1119 w.write(minend);
1120 } else {
1121 /* no content */
1122 w.write("/>");
1123
1124 writeComment(min.getBelow());
1125 }
1126 }
1127
1128 /**
1129 * Write a MAX element
1130 *
1131 * @param max
1132 * @throws IOException
1133 */
1134 public void writeMax(final SavotMax max) throws IOException {
1135 final Writer w = bw; // local copy
1136
1137 writeComment(max.getAbove());
1138
1139 w.write("\n<MAX");
1140
1141 if (max.getValue().length() != 0) {
1142 w.append(" value=\"").append(encodeAttribute(max.getValue())).append('"');
1143 }
1144
1145 if (max.getInclusive().length() != 0) {
1146 w.append(" inclusive=\"").append(encodeAttribute(max.getInclusive())).append('"');
1147 }
1148
1149 if (max.getContent().length() != 0) {
1150 w.write(">");
1151
1152 writeComment(max.getBelow());
1153
1154 w.write(max.getContent());
1155
1156 w.write(maxend);
1157 } else {
1158 /* no content */
1159 w.write("/>");
1160
1161 writeComment(max.getBelow());
1162 }
1163 }
1164
1165 /**
1166 * Write an OPTION set
1167 *
1168 * @param optionSet
1169 * @throws IOException
1170 */
1171 public void writeOption(final OptionSet optionSet) throws IOException {
1172 final Writer w = bw; // local copy
1173
1174 SavotOption option;
1175
1176 for (int i = 0, len = optionSet.getItemCount(); i < len; i++) {
1177 option = optionSet.getItemAt(i);
1178
1179 writeComment(option.getAbove());
1180
1181 w.write("\n<OPTION");
1182
1183 if (option.getName().length() != 0) {
1184 w.append(" name=\"").append(encodeAttribute(option.getName())).append('"');
1185 }
1186
1187 if (option.getValue().length() != 0) {
1188 w.append(" value=\"").append(encodeAttribute(option.getValue())).append('"');
1189 }
1190
1191 // write recursive options
1192 if (option.getOptions().getItemCount() != 0) {
1193 w.write(">");
1194
1195 writeComment(option.getBelow());
1196
1197 writeOption(option.getOptions());
1198
1199 w.write(optionend);
1200 } else {
1201 w.write("/>");
1202
1203 writeComment(option.getBelow());
1204 }
1205 }
1206 }
1207
1208 /**
1209 * Write a GROUP set
1210 *
1211 * @param groupSet
1212 * @throws IOException
1213 */
1214 public void writeGroup(final GroupSet groupSet) throws IOException {
1215 final Writer w = bw; // local copy
1216
1217 SavotGroup group;
1218
1219 for (int i = 0, len = groupSet.getItemCount(); i < len; i++) {
1220 group = groupSet.getItemAt(i);
1221
1222 writeComment(group.getAbove());
1223
1224 w.write("\n<GROUP");
1225
1226 if (group.getId().length() != 0) {
1227 w.append(" ID=\"").append(encodeAttribute(group.getId())).append('"');
1228 }
1229
1230 if (group.getName().length() != 0) {
1231 w.append(" name=\"").append(encodeAttribute(group.getName())).append('"');
1232 }
1233
1234 if (group.getRef().length() != 0) {
1235 w.append(" ref=\"").append(encodeAttribute(group.getRef())).append('"');
1236 }
1237
1238 if (group.getUcd().length() != 0) {
1239 w.append(" ucd=\"").append(encodeAttribute(group.getUcd())).append('"');
1240 }
1241
1242 if (group.getUtype().length() != 0) {
1243 w.append(" utype=\"").append(encodeAttribute(group.getUtype())).append('"');
1244 }
1245
1246 w.write(">");
1247
1248 writeComment(group.getBelow());
1249
1250 // write DESCRIPTION element
1251 writeDescription(group.getDescription());
1252
1253 // write FIELDref elements
1254 writeFieldRef(group.getFieldsRef());
1255
1256 // write PARAMref elements
1257 writeParamRef(group.getParamsRef());
1258
1259 // write PARAM elements
1260 writeParam(group.getParams());
1261
1262 // write recursive groups
1263 writeGroup(group.getGroups());
1264
1265 w.write(groupend);
1266 }
1267 }
1268
1269 /**
1270 * Write a TABLE begin
1271 *
1272 * @param table
1273 * SavotTable
1274 * @throws IOException
1275 */
1276 public void writeTableBegin(final SavotTable table) throws IOException {
1277 final Writer w = bw; // local copy
1278
1279 writeComment(table.getAbove());
1280
1281 // TABLE
1282 w.write("\n<TABLE");
1283
1284 if (table.getId().length() != 0) {
1285 w.append(" ID=\"").append(encodeAttribute(table.getId())).append('"');
1286 }
1287
1288 if (table.getName().length() != 0) {
1289 w.append(" name=\"").append(encodeAttribute(table.getName())).append('"');
1290 }
1291
1292 if (table.getRef().length() != 0) {
1293 w.append(" ref=\"").append(encodeAttribute(table.getRef())).append('"');
1294 }
1295
1296 if (table.getUcd().length() != 0) {
1297 w.append(" ucd=\"").append(encodeAttribute(table.getUcd())).append('"');
1298 }
1299
1300 if (table.getUtype().length() != 0) {
1301 w.append(" utype=\"").append(encodeAttribute(table.getUtype())).append('"');
1302 }
1303
1304 if (table.getNrows().length() != 0) {
1305 w.append(" nrows=\"").append(encodeAttribute(table.getNrows())).append('"');
1306 }
1307
1308 w.write(">");
1309
1310 writeComment(table.getBelow());
1311
1312 // write DESCRIPTION element
1313 writeDescription(table.getDescription());
1314 }
1315
1316 /**
1317 * Write a TABLE end
1318 * @throws IOException
1319 */
1320 public void writeTableEnd() throws IOException {
1321 // </TABLE>
1322 bw.write(tableend);
1323 }
1324
1325 /**
1326 * Write a RESOURCE begin
1327 *
1328 * @param resource
1329 * SavotResource
1330 * @throws IOException
1331 */
1332 public void writeResourceBegin(final SavotResource resource) throws IOException {
1333 final Writer w = bw; // local copy
1334
1335 // RESOURCE
1336 writeComment(resource.getAbove());
1337
1338 w.write("\n<RESOURCE");
1339
1340 if (resource.getName().length() != 0) {
1341 w.append(" name=\"").append(encodeAttribute(resource.getName())).append('"');
1342 }
1343
1344 if (resource.getId().length() != 0) {
1345 w.append(" ID=\"").append(encodeAttribute(resource.getId())).append('"');
1346 }
1347
1348 if (resource.getUtype().length() != 0) {
1349 w.append(" utype=\"").append(encodeAttribute(resource.getUtype())).append('"');
1350 }
1351
1352 if (resource.getType().length() != 0) {
1353 w.append(" type=\"").append(encodeAttribute(resource.getType())).append('"');
1354 }
1355
1356 w.write(">");
1357
1358 writeComment(resource.getBelow());
1359
1360 // write DESCRIPTION element
1361 writeDescription(resource.getDescription());
1362 }
1363
1364 /**
1365 * Write a RESOURCE end
1366 * @throws IOException
1367 */
1368 public void writeResourceEnd() throws IOException {
1369 // </RESOURCE>
1370 bw.write(resourceend);
1371 }
1372
1373 /**
1374 * Write a TABLEDATA begin
1375 * @throws IOException
1376 */
1377 public void writeTableDataBegin() throws IOException {
1378 // <TABLEDATA>
1379 bw.write(tabledatabegin);
1380 }
1381
1382 /**
1383 * Write a TABLEDATA end
1384 * @throws IOException
1385 */
1386 public void writeTableDataEnd() throws IOException {
1387 // </TABLEDATA>
1388 bw.write(tabledataend);
1389 }
1390
1391 /**
1392 * Write a DATA begin
1393 * @throws IOException
1394 */
1395 public void writeDataBegin() throws IOException {
1396 // <DATA>
1397 bw.write(databegin);
1398 }
1399
1400 /**
1401 * Write a DATA end
1402 * @throws IOException
1403 */
1404 public void writeDataEnd() throws IOException {
1405 // </DATA>
1406 bw.write(dataend);
1407 }
1408
1409 /**
1410 * Write a TR
1411 * @param tr
1412 * @throws IOException
1413 */
1414 public void writeTR(final SavotTR tr) throws IOException {
1415 final Writer w = bw; // local copy
1416 final boolean doEncode = elementEntities;
1417 String v;
1418
1419 // <TR>
1420 w.write(trbegin);
1421
1422 final List<SavotTD> tds = tr.getTDSet().getItems();
1423 SavotTD td;
1424
1425 for (int i = 0, tdLen = tds.size(); i < tdLen; i++) {
1426 td = tds.get(i);
1427
1428 // use raw values to test null values efficiently:
1429 v = td.getRawContent();
1430
1431 if (v != null) {
1432 // <TD>
1433 w.write(tdbegin);
1434
1435 w.write((doEncode) ? encodeElement(v) : v);
1436
1437 // </TD>
1438 w.write(tdend);
1439
1440 } else {
1441 w.write(tdempty);
1442 }
1443 }
1444 // </TR>
1445 w.write(trend);
1446 }
1447
1448 /**
1449 * Write a RESOURCE set
1450 *
1451 * @param resourceset
1452 * ResourceSet
1453 * @throws IOException
1454 */
1455 public void writeResource(final ResourceSet resourceset) throws IOException {
1456 if (resourceset != null) {
1457 SavotResource resource;
1458 TableSet tableSet;
1459 SavotTable table;
1460 SavotData data;
1461 SavotTableData tableData;
1462 TRSet trs;
1463
1464 for (int i = 0, len = resourceset.getItemCount(); i < len; i++) {
1465 resource = resourceset.getItemAt(i);
1466
1467 // <RESOURCE>
1468 writeResourceBegin(resource);
1469
1470 // write INFO elements
1471 writeInfo(resource.getInfos());
1472
1473 // write COOSYS elements
1474 writeCoosys(resource.getCoosys());
1475
1476 // write GROUP elements - since VOTable 1.2
1477 writeGroup(resource.getGroups());
1478
1479 // write PARAM elements
1480 writeParam(resource.getParams());
1481
1482 // write LINK elements
1483 writeLink(resource.getLinks());
1484
1485 // TABLE elements
1486 tableSet = resource.getTables();
1487
1488 for (int j = 0, tableLen = tableSet.getItemCount(); j < tableLen; j++) {
1489 table = tableSet.getItemAt(j);
1490
1491 // <TABLE>
1492 writeTableBegin(table);
1493
1494 // write FIELD elements
1495 writeField(table.getFields());
1496
1497 // write PARAM elements
1498 writeParam(table.getParams());
1499
1500 // write GROUP elements
1501 writeGroup(table.getGroups());
1502
1503 // write LINK elements
1504 writeLink(table.getLinks());
1505
1506 data = table.getData();
1507
1508 if (data != null) {
1509 // <DATA>
1510 writeDataBegin();
1511
1512 tableData = data.getTableData();
1513
1514 if (tableData != null) {
1515 // <TABLEDATA>
1516 writeTableDataBegin();
1517
1518 trs = tableData.getTRs();
1519
1520 for (int r = 0, trLen = trs.getItemCount(); r < trLen; r++) {
1521 // <TR>
1522 writeTR(trs.getItemAt(r));
1523 }
1524
1525 // </TABLE>
1526 writeTableDataEnd();
1527
1528 } // TABLE / DATA / TABLEDATA
1529
1530 // write BINARY element
1531 if (data.getBinary() != null) {
1532 writeBinary(data.getBinary());
1533 }
1534
1535 // write FITS element
1536 if (data.getFits() != null) {
1537 writeFits(data.getFits());
1538 }
1539
1540 // </DATA>
1541 writeDataEnd();
1542
1543 } // TABLE / DATA
1544
1545 // write INFO (at End) elements - since VOTable 1.2
1546 writeInfo(table.getInfosAtEnd());
1547
1548 // </TABLE>
1549 writeTableEnd();
1550
1551 } // TABLE
1552
1553 writeResource(resource.getResources());
1554
1555 // write INFO (at End) elements - since VOTable 1.2
1556 writeInfo(resource.getInfosAtEnd());
1557
1558 // </RESOURCE>
1559 writeResourceEnd();
1560 }
1561 }
1562 }
1563 }