001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.planning.servlet.data.xml;
027:
028: import java.io.FilterWriter;
029: import java.io.IOException;
030: import java.io.Writer;
031:
032: /**
033: * for outputting xml tags
034: *
035: * @since 1/24/01
036: **/
037: public class XMLWriter extends FilterWriter {
038:
039: //Variables:
040: public boolean prettyPrint = false;
041: protected int level = 0;
042:
043: //Constructors:
044: ///////////////
045:
046: public XMLWriter(Writer out) {
047: super (out);
048: }
049:
050: public XMLWriter(Writer out, boolean prettyPrint) {
051: super (out);
052: this .prettyPrint = prettyPrint;
053: }
054:
055: //Members:
056: //////////
057:
058: //Helpers:
059:
060: protected void indent() throws IOException {
061: if (prettyPrint) {
062: for (int i = 0; i < level; i++)
063: out.write(' ');
064: }
065: }
066:
067: protected static String noNull(String s) {
068: return s == null ? "" : s;
069: }
070:
071: protected void writeAttr(String attr, String val)
072: throws IOException {
073: out.write(" ");
074: out.write(attr);
075: out.write("=\"");
076: out.write(noNull(val));
077: out.write("\"");
078: }
079:
080: //For External consumption:
081:
082: public void writeHeader() throws IOException {
083: out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
084: }
085:
086: //No attrs single tags:
087:
088: public void sitag(String tag) throws IOException {
089: indent();
090: out.write('<');
091: out.write(tag);
092: out.write("/>");
093: }
094:
095: public void sitagln(String tag) throws IOException {
096: indent();
097: out.write('<');
098: out.write(tag);
099: out.write("/>\n");
100: }
101:
102: //One attr single tags:
103:
104: public void sitag(String tag, String a1, String v1)
105: throws IOException {
106: indent();
107: out.write('<');
108: out.write(tag);
109: writeAttr(a1, v1);
110: out.write("/>");
111: }
112:
113: public void sitagln(String tag, String a1, String v1)
114: throws IOException {
115: sitag(tag, a1, v1);
116: out.write('\n');
117: }
118:
119: //Two attr single tags:
120:
121: public void sitag(String tag, String a1, String v1, String a2,
122: String v2) throws IOException {
123: indent();
124: out.write('<');
125: out.write(tag);
126: writeAttr(a1, v1);
127: writeAttr(a2, v2);
128: out.write("/>");
129: }
130:
131: public void sitagln(String tag, String a1, String v1, String a2,
132: String v2) throws IOException {
133: sitag(tag, a1, v1, a2, v2);
134: out.write('\n');
135: }
136:
137: //Three attr single tags:
138:
139: public void sitagln(String tag, String a1, String v1, String a2,
140: String v2, String a3, String v3) throws IOException {
141: indent();
142: out.write('<');
143: out.write(tag);
144: writeAttr(a1, v1);
145: writeAttr(a2, v2);
146: writeAttr(a3, v3);
147: out.write("/>\n");
148: }
149:
150: //4 attr single tags:
151:
152: public void sitagln(String tag, String a1, String v1, String a2,
153: String v2, String a3, String v3, String a4, String v4)
154: throws IOException {
155: indent();
156: out.write('<');
157: out.write(tag);
158: writeAttr(a1, v1);
159: writeAttr(a2, v2);
160: writeAttr(a3, v3);
161: writeAttr(a4, v4);
162: out.write("/>\n");
163: }
164:
165: //5 attr single tags:
166:
167: public void sitagln(String tag, String a1, String v1, String a2,
168: String v2, String a3, String v3, String a4, String v4,
169: String a5, String v5) throws IOException {
170: indent();
171: out.write('<');
172: out.write(tag);
173: writeAttr(a1, v1);
174: writeAttr(a2, v2);
175: writeAttr(a3, v3);
176: writeAttr(a4, v4);
177: writeAttr(a5, v5);
178: out.write("/>\n");
179: }
180:
181: //6 attr single tags:
182:
183: public void sitagln(String tag, String a1, String v1, String a2,
184: String v2, String a3, String v3, String a4, String v4,
185: String a5, String v5, String a6, String v6)
186: throws IOException {
187: indent();
188: out.write('<');
189: out.write(tag);
190: writeAttr(a1, v1);
191: writeAttr(a2, v2);
192: writeAttr(a3, v3);
193: writeAttr(a4, v4);
194: writeAttr(a5, v5);
195: writeAttr(a6, v6);
196: out.write("/>\n");
197: }
198:
199: //7 attr single tags:
200:
201: public void sitagln(String tag, String a1, String v1, String a2,
202: String v2, String a3, String v3, String a4, String v4,
203: String a5, String v5, String a6, String v6, String a7,
204: String v7) throws IOException {
205: indent();
206: out.write('<');
207: out.write(tag);
208: writeAttr(a1, v1);
209: writeAttr(a2, v2);
210: writeAttr(a3, v3);
211: writeAttr(a4, v4);
212: writeAttr(a5, v5);
213: writeAttr(a6, v6);
214: writeAttr(a7, v7);
215: out.write("/>\n");
216: }
217:
218: //8 attr single tags:
219:
220: public void sitagln(String tag, String a1, String v1, String a2,
221: String v2, String a3, String v3, String a4, String v4,
222: String a5, String v5, String a6, String v6, String a7,
223: String v7, String a8, String v8) throws IOException {
224: indent();
225: out.write('<');
226: out.write(tag);
227: writeAttr(a1, v1);
228: writeAttr(a2, v2);
229: writeAttr(a3, v3);
230: writeAttr(a4, v4);
231: writeAttr(a5, v5);
232: writeAttr(a6, v6);
233: writeAttr(a7, v7);
234: writeAttr(a8, v8);
235: out.write("/>\n");
236: }
237:
238: //9 attr single tags:
239:
240: public void sitagln(String tag, String a1, String v1, String a2,
241: String v2, String a3, String v3, String a4, String v4,
242: String a5, String v5, String a6, String v6, String a7,
243: String v7, String a8, String v8, String a9, String v9)
244: throws IOException {
245: indent();
246: out.write('<');
247: out.write(tag);
248: writeAttr(a1, v1);
249: writeAttr(a2, v2);
250: writeAttr(a3, v3);
251: writeAttr(a4, v4);
252: writeAttr(a5, v5);
253: writeAttr(a6, v6);
254: writeAttr(a7, v7);
255: writeAttr(a8, v8);
256: writeAttr(a9, v9);
257: out.write("/>\n");
258: }
259:
260: //10 attr single tags:
261:
262: public void sitagln(String tag, String a1, String v1, String a2,
263: String v2, String a3, String v3, String a4, String v4,
264: String a5, String v5, String a6, String v6, String a7,
265: String v7, String a8, String v8, String a9, String v9,
266: String a10, String v10) throws IOException {
267: indent();
268: out.write('<');
269: out.write(tag);
270: writeAttr(a1, v1);
271: writeAttr(a2, v2);
272: writeAttr(a3, v3);
273: writeAttr(a4, v4);
274: writeAttr(a5, v5);
275: writeAttr(a6, v6);
276: writeAttr(a7, v7);
277: writeAttr(a8, v8);
278: writeAttr(a9, v9);
279: writeAttr(a10, v10);
280: out.write("/>\n");
281: }
282:
283: //No attrs open and close tags:
284:
285: public void optag(String tag) throws IOException {
286: indent();
287: level++;
288: out.write('<');
289: out.write(tag);
290: out.write('>');
291: }
292:
293: public void optagln(String tag) throws IOException {
294: indent();
295: level++;
296: out.write('<');
297: out.write(tag);
298: out.write(">\n");
299: }
300:
301: public void cltag(String tag) throws IOException {
302: level--;
303: indent();
304: out.write("</");
305: out.write(tag);
306: out.write('>');
307: }
308:
309: public void cltagln(String tag) throws IOException {
310: level--;
311: indent();
312: out.write("</");
313: out.write(tag);
314: out.write(">\n");
315: }
316:
317: public void cltaglnNI(String tag) throws IOException {
318: level--;
319: out.write("</");
320: out.write(tag);
321: out.write(">\n");
322: }
323:
324: //One Attr optagln:
325: public void optagln(String tag, String a1, String v1)
326: throws IOException {
327: indent();
328: level++;
329: out.write('<');
330: out.write(tag);
331: writeAttr(a1, v1);
332: out.write(">\n");
333: }
334:
335: //2 Attr optagln:
336: public void optagln(String tag, String a1, String v1, String a2,
337: String v2) throws IOException {
338: indent();
339: level++;
340: out.write('<');
341: out.write(tag);
342: writeAttr(a1, v1);
343: writeAttr(a2, v2);
344: out.write(">\n");
345: }
346:
347: //3 Attr optagln:
348: public void optagln(String tag, String a1, String v1, String a2,
349: String v2, String a3, String v3) throws IOException {
350: indent();
351: level++;
352: out.write('<');
353: out.write(tag);
354: writeAttr(a1, v1);
355: writeAttr(a2, v2);
356: writeAttr(a3, v3);
357: out.write(">\n");
358: }
359:
360: //4 Attr optagln:
361: public void optagln(String tag, String a1, String v1, String a2,
362: String v2, String a3, String v3, String a4, String v4)
363: throws IOException {
364: indent();
365: level++;
366: out.write('<');
367: out.write(tag);
368: writeAttr(a1, v1);
369: writeAttr(a2, v2);
370: writeAttr(a3, v3);
371: writeAttr(a4, v4);
372: out.write(">\n");
373: }
374:
375: //5 Attr optagln:
376: public void optagln(String tag, String a1, String v1, String a2,
377: String v2, String a3, String v3, String a4, String v4,
378: String a5, String v5) throws IOException {
379: indent();
380: level++;
381: out.write('<');
382: out.write(tag);
383: writeAttr(a1, v1);
384: writeAttr(a2, v2);
385: writeAttr(a3, v3);
386: writeAttr(a4, v4);
387: writeAttr(a5, v5);
388: out.write(">\n");
389: }
390:
391: //6 Attr optagln:
392: public void optagln(String tag, String a1, String v1, String a2,
393: String v2, String a3, String v3, String a4, String v4,
394: String a5, String v5, String a6, String v6)
395: throws IOException {
396: indent();
397: level++;
398: out.write('<');
399: out.write(tag);
400: writeAttr(a1, v1);
401: writeAttr(a2, v2);
402: writeAttr(a3, v3);
403: writeAttr(a4, v4);
404: writeAttr(a5, v5);
405: writeAttr(a6, v6);
406: out.write(">\n");
407: }
408:
409: //7 Attr optagln:
410: public void optagln(String tag, String a1, String v1, String a2,
411: String v2, String a3, String v3, String a4, String v4,
412: String a5, String v5, String a6, String v6, String a7,
413: String v7) throws IOException {
414: indent();
415: level++;
416: out.write('<');
417: out.write(tag);
418: writeAttr(a1, v1);
419: writeAttr(a2, v2);
420: writeAttr(a3, v3);
421: writeAttr(a4, v4);
422: writeAttr(a5, v5);
423: writeAttr(a6, v6);
424: writeAttr(a7, v7);
425: out.write(">\n");
426: }
427:
428: //8 Attr optagln:
429: public void optagln(String tag, String a1, String v1, String a2,
430: String v2, String a3, String v3, String a4, String v4,
431: String a5, String v5, String a6, String v6, String a7,
432: String v7, String a8, String v8) throws IOException {
433: indent();
434: level++;
435: out.write('<');
436: out.write(tag);
437: writeAttr(a1, v1);
438: writeAttr(a2, v2);
439: writeAttr(a3, v3);
440: writeAttr(a4, v4);
441: writeAttr(a5, v5);
442: writeAttr(a6, v6);
443: writeAttr(a7, v7);
444: writeAttr(a8, v8);
445: out.write(">\n");
446: }
447:
448: //9 Attr optagln:
449: public void optagln(String tag, String a1, String v1, String a2,
450: String v2, String a3, String v3, String a4, String v4,
451: String a5, String v5, String a6, String v6, String a7,
452: String v7, String a8, String v8, String a9, String v9)
453: throws IOException {
454: indent();
455: level++;
456: out.write('<');
457: out.write(tag);
458: writeAttr(a1, v1);
459: writeAttr(a2, v2);
460: writeAttr(a3, v3);
461: writeAttr(a4, v4);
462: writeAttr(a5, v5);
463: writeAttr(a6, v6);
464: writeAttr(a7, v7);
465: writeAttr(a8, v8);
466: writeAttr(a9, v9);
467: out.write(">\n");
468: }
469:
470: //10 Attr optagln:
471: public void optagln(String tag, String a1, String v1, String a2,
472: String v2, String a3, String v3, String a4, String v4,
473: String a5, String v5, String a6, String v6, String a7,
474: String v7, String a8, String v8, String a9, String v9,
475: String a10, String v10) throws IOException {
476: indent();
477: level++;
478: out.write('<');
479: out.write(tag);
480: writeAttr(a1, v1);
481: writeAttr(a2, v2);
482: writeAttr(a3, v3);
483: writeAttr(a4, v4);
484: writeAttr(a5, v5);
485: writeAttr(a6, v6);
486: writeAttr(a7, v7);
487: writeAttr(a8, v8);
488: writeAttr(a9, v9);
489: writeAttr(a10, v10);
490: out.write(">\n");
491: }
492:
493: //11 Attr optagln:
494: public void optagln(String tag, String a1, String v1, String a2,
495: String v2, String a3, String v3, String a4, String v4,
496: String a5, String v5, String a6, String v6, String a7,
497: String v7, String a8, String v8, String a9, String v9,
498: String a10, String v10, String a11, String v11)
499: throws IOException {
500: indent();
501: level++;
502: out.write('<');
503: out.write(tag);
504: writeAttr(a1, v1);
505: writeAttr(a2, v2);
506: writeAttr(a3, v3);
507: writeAttr(a4, v4);
508: writeAttr(a5, v5);
509: writeAttr(a6, v6);
510: writeAttr(a7, v7);
511: writeAttr(a8, v8);
512: writeAttr(a9, v9);
513: writeAttr(a10, v10);
514: writeAttr(a11, v11);
515: out.write(">\n");
516: }
517:
518: //12 Attr optagln:
519: public void optagln(String tag, String a1, String v1, String a2,
520: String v2, String a3, String v3, String a4, String v4,
521: String a5, String v5, String a6, String v6, String a7,
522: String v7, String a8, String v8, String a9, String v9,
523: String a10, String v10, String a11, String v11, String a12,
524: String v12) throws IOException {
525: indent();
526: level++;
527: out.write('<');
528: out.write(tag);
529: writeAttr(a1, v1);
530: writeAttr(a2, v2);
531: writeAttr(a3, v3);
532: writeAttr(a4, v4);
533: writeAttr(a5, v5);
534: writeAttr(a6, v6);
535: writeAttr(a7, v7);
536: writeAttr(a8, v8);
537: writeAttr(a9, v9);
538: writeAttr(a10, v10);
539: writeAttr(a11, v11);
540: writeAttr(a12, v12);
541: out.write(">\n");
542: }
543:
544: //13 Attr optagln:
545: public void optagln(String tag, String a1, String v1, String a2,
546: String v2, String a3, String v3, String a4, String v4,
547: String a5, String v5, String a6, String v6, String a7,
548: String v7, String a8, String v8, String a9, String v9,
549: String a10, String v10, String a11, String v11, String a12,
550: String v12, String a13, String v13) throws IOException {
551: indent();
552: level++;
553: out.write('<');
554: out.write(tag);
555: writeAttr(a1, v1);
556: writeAttr(a2, v2);
557: writeAttr(a3, v3);
558: writeAttr(a4, v4);
559: writeAttr(a5, v5);
560: writeAttr(a6, v6);
561: writeAttr(a7, v7);
562: writeAttr(a8, v8);
563: writeAttr(a9, v9);
564: writeAttr(a10, v10);
565: writeAttr(a11, v11);
566: writeAttr(a12, v12);
567: writeAttr(a13, v13);
568: out.write(">\n");
569: }
570:
571: //14 Attr optagln:
572: public void optagln(String tag, String a1, String v1, String a2,
573: String v2, String a3, String v3, String a4, String v4,
574: String a5, String v5, String a6, String v6, String a7,
575: String v7, String a8, String v8, String a9, String v9,
576: String a10, String v10, String a11, String v11, String a12,
577: String v12, String a13, String v13, String a14, String v14)
578: throws IOException {
579: indent();
580: level++;
581: out.write('<');
582: out.write(tag);
583: writeAttr(a1, v1);
584: writeAttr(a2, v2);
585: writeAttr(a3, v3);
586: writeAttr(a4, v4);
587: writeAttr(a5, v5);
588: writeAttr(a6, v6);
589: writeAttr(a7, v7);
590: writeAttr(a8, v8);
591: writeAttr(a9, v9);
592: writeAttr(a10, v10);
593: writeAttr(a11, v11);
594: writeAttr(a12, v12);
595: writeAttr(a13, v13);
596: writeAttr(a14, v14);
597: out.write(">\n");
598: }
599:
600: //No Attrs tagln by data:
601:
602: public void tagln(String tag, String data) throws IOException {
603: optag(tag);
604: out.write(noNull(data));
605: cltaglnNI(tag);
606: }
607:
608: public void tagln(String tag, int i) throws IOException {
609: tagln(tag, Integer.toString(i));
610: }
611:
612: public void tagln(String tag, long l) throws IOException {
613: tagln(tag, Long.toString(l));
614: }
615:
616: public void tagln(String tag, float f) throws IOException {
617: tagln(tag, Float.toString(f));
618: }
619:
620: public void tagln(String tag, double d) throws IOException {
621: tagln(tag, Double.toString(d));
622: }
623:
624: //From FilterWriter:
625: //------------------
626:
627: public void write(int c) throws IOException {
628: out.write(c);
629: }
630:
631: public void write(char[] cbuf, int off, int len) throws IOException {
632: out.write(cbuf, off, len);
633: }
634:
635: public void write(String str, int off, int len) throws IOException {
636: out.write(str, off, len);
637: }
638:
639: public void flush() throws IOException {
640: out.flush();
641: }
642:
643: public void close() throws IOException {
644: out.close();
645: }
646: }
|