View Javadoc

1   /*
2    * Copyright (c) 2003, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of XmlWriter nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
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; // current attribute string
61      private boolean empty;      // is the current node empty
62      private boolean closed;     // is the current node closed...
63  
64      private Object emptyMode;  // the strategy to use for emptiness
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          // check empty-mode
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              // same as IGNORE_EMPTY_MODE
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 }