View Javadoc

1   /**
2    * JSpiff
3    * -----------------
4    * Copyright (c) 2005-2006 Emil A. Lefkof III
5    *
6    * I always give it my best shot to make a program useful and solid, but
7    * remeber that there is absolutely no warranty for using this program as
8    * stated in the following terms:
9    *
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   */
22  package com.melloware.jspiff.jaxp;
23  
24  import java.io.IOException;
25  import java.io.PrintWriter;
26  import java.io.Writer;
27  
28  import org.w3c.dom.Document;
29  import org.w3c.dom.Node;
30  
31  /**
32   * <b>RString</b> is a text container class which is used for mixed.
33   * <p>
34   * @version xspf.rng 1.0 (Wed Sep 27 17:36:25 EDT 2006)
35   * @author  Relaxer 1.1b (http://www.relaxer.org)
36   * @author Emil A. Lefkof III <info@melloware.com>
37   */
38  public class RString
39      implements java.io.Serializable,
40                 Cloneable,
41                 IXspfExtensionMixed,
42                 IXspfAnythingMixed {
43  	
44      private boolean cdata_;
45      private Object value_;
46  
47      /**
48       * Creates a <code>RString</code>.
49       *
50       */
51      public RString() {
52      }
53  
54      /**
55       * Creates a <code>RString</code> by the String <code>text</code>.
56       *
57       * @param text
58       */
59      public RString(String text) {
60          value_ = text;
61      }
62  
63      /**
64       * Creates a <code>RString</code> by the DOM node <code>node</code>.
65       *
66       * @param node
67       */
68      public RString(org.w3c.dom.Node node) {
69          value_ = node;
70      }
71  
72      /**
73       * Creates a <code>RString</code> by the Object <code>object</code>.
74       *
75       * @param object
76       */
77      public RString(Object object) {
78          value_ = object;
79      }
80  
81      /**
82       * Creates a <code>RString</code> by the Rstring <code>source</code>.
83       *
84       * @param source
85       */
86      public RString(RString source) {
87          this(source.getContent());
88      }
89  
90      /**
91       * Creates a <code>RString</code> by the Stack <code>stack</code>.
92       * This constructor is supposed to be used internallyby the Relaxer system.
93       *
94       * @param stack
95       */
96      public RString(RStack stack) {
97          setup(stack);
98      }
99  
100     /**
101      * Tests if elements contained in a Stack <code>stack</code>
102      * is valid for the <code>RString</code>.
103      * This mehtod is supposed to be used internally
104      * by the Relaxer system.
105      *
106      * @param stack
107      * @return boolean
108      */
109     public static boolean isMatch(RStack stack) {
110         return (stack.peek() instanceof String);
111     }
112 
113     /**
114      * Gets the content.
115      *
116      * @return Object
117      */
118     public Object getContent() {
119         return (value_);
120     }
121 
122     /**
123      * Gets the text content as String.
124      *
125      * @return String
126      */
127     public String getContentAsString() {
128         if (value_ == null) {
129             ;
130             return (null);
131         } else if (value_ instanceof org.w3c.dom.Node) {
132             return (URelaxer.node2String4Data((Node)value_));
133         } else {
134             return (value_.toString());
135         }
136     }
137 
138     /**
139      * Gets the DOM node.
140      *
141      * @return org.w3c.dom.Node
142      */
143     public org.w3c.dom.Node getNode() {
144         if (value_ instanceof org.w3c.dom.Node) {
145             return ((org.w3c.dom.Node)value_);
146         } else {
147             return (null);
148         }
149     }
150 
151     /**
152      * Gets the object.
153      *
154      * @return Object
155      */
156     public Object getObject() {
157         if ((value_ instanceof String) || (value_ instanceof org.w3c.dom.Node)) {
158             return (null);
159         } else {
160             return (value_);
161         }
162     }
163 
164     /**
165      * Gets the text.
166      *
167      * @return String
168      */
169     public String getText() {
170         if (value_ instanceof String) {
171             return ((String)value_);
172         } else {
173             return (null);
174         }
175     }
176 
177     /**
178      * Sets wheter cdata or not.
179      *
180      * @param cdata
181      */
182     public void setCdata(boolean cdata) {
183         cdata_ = cdata;
184     }
185 
186     /**
187      * Sets the content.
188      *
189      * @param value
190      */
191     public void setContent(Object value) {
192         value_ = value;
193     }
194 
195     /**
196      * Sets the DOM node.
197      *
198      * @param node
199      */
200     public void setNode(org.w3c.dom.Node node) {
201         value_ = node;
202     }
203 
204     /**
205      * Sets the DOM node.
206      *
207      * @param object
208      */
209     public void setObject(Object object) {
210         value_ = object;
211     }
212 
213     /**
214      * Sets the text.
215      *
216      * @param text
217      */
218     public void setText(String text) {
219         value_ = text;
220     }
221 
222     /**
223      * Checks whether cdata or not.
224      *
225      * @return boolean
226      */
227     public boolean isCdata() {
228         return (cdata_);
229     }
230 
231     /**
232      * Clones the String.
233      *
234      * @return Object
235      */
236     public Object clone() {
237         return (new RString(this));
238     }
239 
240     /**
241      * Creates a DOM representation of the object.
242      * Result is appended to the Node <code>parent</code>.
243      *
244      * @param node
245      */
246     public void makeElement(Node node) {
247         Document doc = node.getOwnerDocument();
248         if (value_ instanceof org.w3c.dom.Node) {
249             node.appendChild(doc.importNode((Node)value_, true));
250         } else if (value_ != null) {
251             if (cdata_) {
252                 node.appendChild(doc.createCDATASection(value_.toString()));
253             } else {
254                 node.appendChild(doc.createTextNode(value_.toString()));
255             }
256         }
257     }
258 
259     /**
260      * Makes an XML text representation.
261      *
262      * @param buffer
263      */
264     public void makeTextAttribute(StringBuffer buffer) {
265     }
266 
267     /**
268      * Makes an XML text representation.
269      *
270      * @param buffer
271      * @exception IOException
272      */
273     public void makeTextAttribute(Writer buffer)
274                            throws IOException {
275     }
276 
277     /**
278      * Makes an XML text representation.
279      *
280      * @param buffer
281      */
282     public void makeTextAttribute(PrintWriter buffer) {
283     }
284 
285     /**
286      * Makes an XML text representation.
287      *
288      * @param buffer
289      */
290     public void makeTextElement(StringBuffer buffer) {
291         if (value_ instanceof org.w3c.dom.Node) {
292             buffer.append(URelaxer.node2String4Data((Node)value_));
293         } else {
294             if (cdata_) {
295                 buffer.append("<![CDATA[");
296                 buffer.append(value_.toString());
297                 buffer.append("]]>");
298             } else {
299                 buffer.append(URelaxer.escapeCharData(value_.toString()));
300             }
301         }
302     }
303 
304     /**
305      * Makes an XML text representation.
306      *
307      * @param buffer
308      * @exception IOException
309      */
310     public void makeTextElement(Writer buffer)
311                          throws IOException {
312         if (value_ instanceof org.w3c.dom.Node) {
313             buffer.write(URelaxer.node2String4Data((Node)value_));
314         } else {
315             if (cdata_) {
316                 buffer.write("<![CDATA[");
317                 buffer.write(value_.toString());
318                 buffer.write("]]>");
319             } else {
320                 buffer.write(URelaxer.escapeCharData(value_.toString()));
321             }
322         }
323     }
324 
325     /**
326      * Makes an XML text representation.
327      *
328      * @param buffer
329      */
330     public void makeTextElement(PrintWriter buffer) {
331         if (value_ instanceof org.w3c.dom.Node) {
332             buffer.print(URelaxer.node2String4Data((Node)value_));
333         } else {
334             if (cdata_) {
335                 buffer.print("<![CDATA[");
336                 buffer.print(value_.toString());
337                 buffer.print("]]>");
338             } else {
339                 buffer.print(URelaxer.escapeCharData(value_.toString()));
340             }
341         }
342     }
343 
344     /**
345      * Initializes the <code>RString</code> by the Stack <code>stack</code>
346      * that contains Elements.
347      * This constructor is supposed to be used internallyby the Relaxer system.
348      *
349      * @param stack
350      */
351     public void setup(RStack stack) {
352         value_ = stack.pop().toString();
353     }
354 
355     /**
356      * Gets the String.
357      *
358      * @return String
359      */
360     public String toString() {
361         return (getContentAsString());
362     }
363 }