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 package com.generationjava.io.xml;
33
34 import java.io.IOException;
35
36 /***
37 * Has various strategies for dealing with null or empty-string
38 * values.
39 */
40 public class EmptyElementXmlWriter extends DelegatingXmlWriter {
41
42 /***
43 * Ignores empty concepts and prints out all attributes/entities.
44 * So it will print out foo="null"
45 */
46 public static final Object IGNORE_EMPTY_MODE = new Object();
47
48 /***
49 * Only considers null to be empty, so empty strings are outputted.
50 */
51 public static final Object NULL_EMPTY_MODE = new Object();
52
53 /***
54 * Considers null and an empty string to be 'empty'.
55 * If somethign is empty, it will not output them.
56 * This is the default mode.
57 */
58 public static final Object EMPTY_MODE = new Object();
59
60 private StringBuffer attrs;
61 private boolean empty;
62 private boolean closed;
63
64 private Object emptyMode;
65
66 /***
67 * Create an EmptyElementXmlWriter on top of an existing java.io.Writer.
68 */
69 public EmptyElementXmlWriter(XmlWriter xmlwriter) {
70 super(xmlwriter);
71 this.emptyMode = EmptyElementXmlWriter.EMPTY_MODE;
72 }
73
74 /***
75 * The emptiness strategy to use. Emptiness is when it decides
76 * that an element should be ignored.
77 */
78 public XmlWriter setEmptyMode(Object mode) {
79 if(mode != EmptyElementXmlWriter.EMPTY_MODE &&
80 mode != EmptyElementXmlWriter.IGNORE_EMPTY_MODE &&
81 mode != EmptyElementXmlWriter.NULL_EMPTY_MODE
82 )
83 {
84 throw new IllegalArgumentException("Illegal mode: "+mode);
85 }
86 this.emptyMode = mode;
87 return this;
88 }
89
90 private boolean checkEmpty(Object value) {
91
92 if(this.emptyMode == EmptyElementXmlWriter.EMPTY_MODE) {
93 return (value == null) || "".equals(value);
94 } else
95 if(this.emptyMode == EmptyElementXmlWriter.NULL_EMPTY_MODE) {
96 return (value == null);
97 } else {
98
99 return false;
100 }
101 }
102
103 /***
104 *
105 * @param name String name of tag
106 */
107 public XmlWriter writeEntity(String name) throws IOException {
108 if(checkEmpty(name)) {
109 return this;
110 } else {
111 return super.writeEntity(name);
112 }
113 }
114
115 /***
116 *
117 * @param String name of attribute.
118 * @param Object value of attribute.
119 */
120 public XmlWriter writeAttribute(String attr, Object value) throws IOException {
121
122 if(checkEmpty(attr)) {
123 return this;
124 }
125
126 if(checkEmpty(value)) {
127 return this;
128 }
129
130 return super.writeAttribute(attr, value);
131 }
132
133 /***
134 * Output body text. Any xml characters are escaped.
135 */
136 public XmlWriter writeText(Object text) throws IOException {
137
138 if(checkEmpty(text)) {
139 return this;
140 }
141
142 return super.writeText(text);
143 }
144
145 /***
146 * Write out a chunk of CDATA. This helper method surrounds the
147 * passed in data with the CDATA tag.
148 *
149 * @param String of CDATA text.
150 */
151 public XmlWriter writeCData(String cdata) throws IOException {
152
153 if(checkEmpty(cdata)) {
154 return this;
155 }
156
157 return super.writeCData(cdata);
158 }
159
160 /***
161 * Write out a chunk of comment. This helper method surrounds the
162 * passed in data with the xml comment tag.
163 *
164 * @param String of text to comment.
165 */
166 public XmlWriter writeComment(String comment) throws IOException {
167
168 if(checkEmpty(comment)) {
169 return this;
170 }
171
172 return super.writeComment(comment);
173 }
174
175 }