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.File;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.PrintWriter;
28  import java.io.Reader;
29  import java.io.Writer;
30  import java.net.URL;
31  
32  import javax.xml.parsers.ParserConfigurationException;
33  
34  import org.w3c.dom.Document;
35  import org.w3c.dom.Element;
36  import org.w3c.dom.Node;
37  import org.xml.sax.InputSource;
38  import org.xml.sax.SAXException;
39  
40  /**
41   * <b>XspfMeta</b> is generated from xspf.rng by Relaxer.
42   * This class is derived from:
43   *
44   * <!-- for programmer
45   * <element name="meta">
46   *             <attribute name="rel">
47   *                 <data type="anyURI"/>
48   *             </attribute>
49   *
50   *             <text/>
51   *         </element>-->
52   * <!-- for javadoc -->
53   * <pre> &lt;element name="meta"&gt;
54   *             &lt;attribute name="rel"&gt;
55   *                 &lt;data type="anyURI"/&gt;
56   *             &lt;/attribute&gt;
57   *
58   *             &lt;text/&gt;
59   *         &lt;/element&gt;</pre>
60   *
61   * @version xspf.rng (Wed Sep 27 17:36:25 EDT 2006)
62   * @author  Relaxer 1.1b (http://www.relaxer.org)
63   * @author Emil A. Lefkof III <info@melloware.com>
64   */
65  public class XspfMeta
66      implements java.io.Serializable,
67                 Cloneable {
68      private String content_;
69      private String rel_;
70  
71      /**
72       * Creates a <code>XspfMeta</code>.
73       *
74       */
75      public XspfMeta() {
76          rel_ = "";
77      }
78  
79      /**
80       * Creates a <code>XspfMeta</code>.
81       *
82       * @param source
83       */
84      public XspfMeta(XspfMeta source) {
85          setup(source);
86      }
87  
88      /**
89       * Creates a <code>XspfMeta</code> by the Stack <code>stack</code>
90       * that contains Elements.
91       * This constructor is supposed to be used internally
92       * by the Relaxer system.
93       *
94       * @param stack
95       */
96      public XspfMeta(RStack stack) {
97          setup(stack);
98      }
99  
100     /**
101      * Creates a <code>XspfMeta</code> by the Document <code>doc</code>.
102      *
103      * @param doc
104      */
105     public XspfMeta(Document doc) {
106         setup(doc.getDocumentElement());
107     }
108 
109     /**
110      * Creates a <code>XspfMeta</code> by the Element <code>element</code>.
111      *
112      * @param element
113      */
114     public XspfMeta(Element element) {
115         setup(element);
116     }
117 
118     /**
119      * Creates a <code>XspfMeta</code> by the File <code>file</code>.
120      *
121      * @param file
122      * @exception IOException
123      * @exception SAXException
124      * @exception ParserConfigurationException
125      */
126     public XspfMeta(File file)
127              throws IOException, SAXException, ParserConfigurationException {
128         setup(file);
129     }
130 
131     /**
132      * Creates a <code>XspfMeta</code>
133      * by the String representation of URI <code>uri</code>.
134      *
135      * @param uri
136      * @exception IOException
137      * @exception SAXException
138      * @exception ParserConfigurationException
139      */
140     public XspfMeta(String uri)
141              throws IOException, SAXException, ParserConfigurationException {
142         setup(uri);
143     }
144 
145     /**
146      * Creates a <code>XspfMeta</code> by the URL <code>url</code>.
147      *
148      * @param url
149      * @exception IOException
150      * @exception SAXException
151      * @exception ParserConfigurationException
152      */
153     public XspfMeta(URL url)
154              throws IOException, SAXException, ParserConfigurationException {
155         setup(url);
156     }
157 
158     /**
159      * Creates a <code>XspfMeta</code> by the InputStream <code>in</code>.
160      *
161      * @param in
162      * @exception IOException
163      * @exception SAXException
164      * @exception ParserConfigurationException
165      */
166     public XspfMeta(InputStream in)
167              throws IOException, SAXException, ParserConfigurationException {
168         setup(in);
169     }
170 
171     /**
172      * Creates a <code>XspfMeta</code> by the InputSource <code>is</code>.
173      *
174      * @param is
175      * @exception IOException
176      * @exception SAXException
177      * @exception ParserConfigurationException
178      */
179     public XspfMeta(InputSource is)
180              throws IOException, SAXException, ParserConfigurationException {
181         setup(is);
182     }
183 
184     /**
185      * Creates a <code>XspfMeta</code> by the Reader <code>reader</code>.
186      *
187      * @param reader
188      * @exception IOException
189      * @exception SAXException
190      * @exception ParserConfigurationException
191      */
192     public XspfMeta(Reader reader)
193              throws IOException, SAXException, ParserConfigurationException {
194         setup(reader);
195     }
196 
197     /**
198      * Tests if a Element <code>element</code> is valid
199      * for the <code>XspfMeta</code>.
200      *
201      * @param element
202      * @return boolean
203      */
204     public static boolean isMatch(Element element) {
205     	if (!URelaxer.isTargetElement(element, "meta")) {
206             return (false);
207         }
208         RStack target = new RStack(element);
209         if (!URelaxer.hasAttributeHungry(target, "rel")) {
210             return (false);
211         }
212         if (!target.isEmptyElement()) {
213             return (false);
214         }
215         return (true);
216     }
217 
218     /**
219      * Tests if elements contained in a Stack <code>stack</code>
220      * is valid for the <code>XspfMeta</code>.
221      * This mehtod is supposed to be used internally
222      * by the Relaxer system.
223      *
224      * @param stack
225      * @return boolean
226      */
227     public static boolean isMatch(RStack stack) {
228         Element element = stack.peekElement();
229         if (element == null) {
230             return (false);
231         }
232         return (isMatch(element));
233     }
234 
235     /**
236      * Tests if elements contained in a Stack <code>stack</code>
237      * is valid for the <code>XspfMeta</code>.
238      * This method consumes the stack contents during matching operation.
239      * This mehtod is supposed to be used internally
240      * by the Relaxer system.
241      *
242      * @param stack
243      * @return boolean
244      */
245     public static boolean isMatchHungry(RStack stack) {
246         Element element = stack.peekElement();
247         if (element == null) {
248             return (false);
249         }
250         if (isMatch(element)) {
251             stack.popElement();
252             return (true);
253         } else {
254             return (false);
255         }
256     }
257 
258     /**
259      * Gets the String property <b>content</b>.
260      *
261      * @return String
262      */
263     public String getContent() {
264         return (content_);
265     }
266 
267     /**
268      * Gets the property value as String.
269      *
270      * @return String
271      */
272     public String getContentAsString() {
273         return (URelaxer.getString(getContent()));
274     }
275 
276     /**
277      * Gets the String property <b>rel</b>.
278      *
279      * @return String
280      */
281     public String getRel() {
282         return (rel_);
283     }
284 
285     /**
286      * Gets the property value as String.
287      *
288      * @return String
289      */
290     public String getRelAsString() {
291         return (URelaxer.getString(getRel()));
292     }
293 
294     /**
295      * Sets the String property <b>content</b>.
296      *
297      * @param content
298      */
299     public void setContent(String content) {
300         this.content_ = content;
301     }
302 
303     /**
304      * Sets the property value by String.
305      *
306      * @param string
307      */
308     public void setContentByString(String string) {
309         setContent(string);
310     }
311 
312     /**
313      * Sets the String property <b>rel</b>.
314      *
315      * @param rel
316      */
317     public void setRel(String rel) {
318         this.rel_ = rel;
319     }
320 
321     /**
322      * Sets the property value by String.
323      *
324      * @param string
325      */
326     public void setRelByString(String string) {
327         setRel(URelaxer.getString(string));
328     }
329 
330     /**
331      * @return Object
332      */
333     public Object clone() {
334         return (new XspfMeta((XspfMeta)this));
335     }
336 
337     /**
338      * Creates a DOM document representation of the object.
339      *
340      * @exception ParserConfigurationException
341      * @return Document
342      */
343     public Document makeDocument()
344                           throws ParserConfigurationException {
345         Document doc = UJAXP.makeDocument();
346         makeElement(doc);
347         return (doc);
348     }
349 
350     /**
351      * Creates a DOM representation of the object.
352      * Result is appended to the Node <code>parent</code>.
353      *
354      * @param parent
355      */
356     public void makeElement(Node parent) {
357         Document doc;
358         if (parent instanceof Document) {
359             doc = (Document)parent;
360         } else {
361             doc = parent.getOwnerDocument();
362         }
363         Element element = doc.createElement("meta");
364         if (parent instanceof Document) {
365             element.setAttribute("xmlns", "http://xspf.org/ns/0/");
366         }
367         URelaxer.setElementPropertyByString(element, this.content_);
368         if (this.rel_ != null) {
369             URelaxer.setAttributePropertyByString(element, "rel", this.rel_);
370         }
371         parent.appendChild(element);
372     }
373 
374     /**
375      * Makes an XML text representation.
376      *
377      * @param buffer
378      */
379     public void makeTextAttribute(StringBuffer buffer) {
380     }
381 
382     /**
383      * Makes an XML text representation.
384      *
385      * @param buffer
386      * @exception IOException
387      */
388     public void makeTextAttribute(Writer buffer)
389                            throws IOException {
390     }
391 
392     /**
393      * Makes an XML text representation.
394      *
395      * @param buffer
396      */
397     public void makeTextAttribute(PrintWriter buffer) {
398     }
399 
400     /**
401      * Makes an XML text representation.
402      *
403      * @return String
404      */
405     public String makeTextDocument() {
406         StringBuffer buffer = new StringBuffer();
407         makeTextElement(buffer);
408         return (new String(buffer));
409     }
410 
411     /**
412      * Makes an XML text representation.
413      *
414      * @param buffer
415      */
416     public void makeTextElement(StringBuffer buffer) {
417         buffer.append("<meta");
418         buffer.append(" xmlns=\"http://xspf.org/ns/0/\"");
419         if (rel_ != null) {
420             buffer.append(" rel=\"");
421             buffer.append(URelaxer.escapeAttrQuot(URelaxer.getString(getRel())));
422             buffer.append("\"");
423         }
424         buffer.append(">");
425         if (content_ != null) {
426             buffer.append(URelaxer.escapeCharData(URelaxer.getString(getContent())));
427         }
428         buffer.append("</meta>");
429     }
430 
431     /**
432      * Makes an XML text representation.
433      *
434      * @param buffer
435      * @exception IOException
436      */
437     public void makeTextElement(Writer buffer)
438                          throws IOException {
439         buffer.write("<meta");
440         buffer.write(" xmlns=\"http://xspf.org/ns/0/\"");
441         if (rel_ != null) {
442             buffer.write(" rel=\"");
443             buffer.write(URelaxer.escapeAttrQuot(URelaxer.getString(getRel())));
444             buffer.write("\"");
445         }
446         buffer.write(">");
447         if (content_ != null) {
448             buffer.write(URelaxer.escapeCharData(URelaxer.getString(getContent())));
449         }
450         buffer.write("</meta>");
451     }
452 
453     /**
454      * Makes an XML text representation.
455      *
456      * @param buffer
457      */
458     public void makeTextElement(PrintWriter buffer) {
459         buffer.print("<meta");
460         buffer.print(" xmlns=\"http://xspf.org/ns/0/\"");
461         if (rel_ != null) {
462             buffer.print(" rel=\"");
463             buffer.print(URelaxer.escapeAttrQuot(URelaxer.getString(getRel())));
464             buffer.print("\"");
465         }
466         buffer.print(">");
467         if (content_ != null) {
468             buffer.print(URelaxer.escapeCharData(URelaxer.getString(getContent())));
469         }
470         buffer.print("</meta>");
471     }
472 
473     /**
474      * Initializes the <code>XspfMeta</code> by the XspfMeta <code>source</code>.
475      *
476      * @param source
477      */
478     public void setup(XspfMeta source) {
479         content_ = source.content_;
480         rel_ = source.rel_;
481     }
482 
483     /**
484      * Initializes the <code>XspfMeta</code> by the Document <code>doc</code>.
485      *
486      * @param doc
487      */
488     public void setup(Document doc) {
489         setup(doc.getDocumentElement());
490     }
491 
492     /**
493      * Initializes the <code>XspfMeta</code> by the Element <code>element</code>.
494      *
495      * @param element
496      */
497     public void setup(Element element) {
498         init(element);
499     }
500 
501     /**
502      * Initializes the <code>XspfMeta</code> by the Stack <code>stack</code>
503      * that contains Elements.
504      * This constructor is supposed to be used internally
505      * by the Relaxer system.
506      *
507      * @param stack
508      */
509     public void setup(RStack stack) {
510         init(stack.popElement());
511     }
512 
513     /**
514      * Initializes the <code>XspfMeta</code> by the File <code>file</code>.
515      *
516      * @param file
517      * @exception IOException
518      * @exception SAXException
519      * @exception ParserConfigurationException
520      */
521     public void setup(File file)
522                throws IOException, SAXException, ParserConfigurationException {
523         setup(file.toURL());
524     }
525 
526     /**
527      * Initializes the <code>XspfMeta</code>
528      * by the String representation of URI <code>uri</code>.
529      *
530      * @param uri
531      * @exception IOException
532      * @exception SAXException
533      * @exception ParserConfigurationException
534      */
535     public void setup(String uri)
536                throws IOException, SAXException, ParserConfigurationException {
537         setup(UJAXP.getDocument(uri, UJAXP.FLAG_NONE));
538     }
539 
540     /**
541      * Initializes the <code>XspfMeta</code> by the URL <code>url</code>.
542      *
543      * @param url
544      * @exception IOException
545      * @exception SAXException
546      * @exception ParserConfigurationException
547      */
548     public void setup(URL url)
549                throws IOException, SAXException, ParserConfigurationException {
550         setup(UJAXP.getDocument(url, UJAXP.FLAG_NONE));
551     }
552 
553     /**
554      * Initializes the <code>XspfMeta</code> by the InputStream <code>in</code>.
555      *
556      * @param in
557      * @exception IOException
558      * @exception SAXException
559      * @exception ParserConfigurationException
560      */
561     public void setup(InputStream in)
562                throws IOException, SAXException, ParserConfigurationException {
563         setup(UJAXP.getDocument(in, UJAXP.FLAG_NONE));
564     }
565 
566     /**
567      * Initializes the <code>XspfMeta</code> by the InputSource <code>is</code>.
568      *
569      * @param is
570      * @exception IOException
571      * @exception SAXException
572      * @exception ParserConfigurationException
573      */
574     public void setup(InputSource is)
575                throws IOException, SAXException, ParserConfigurationException {
576         setup(UJAXP.getDocument(is, UJAXP.FLAG_NONE));
577     }
578 
579     /**
580      * Initializes the <code>XspfMeta</code> by the Reader <code>reader</code>.
581      *
582      * @param reader
583      * @exception IOException
584      * @exception SAXException
585      * @exception ParserConfigurationException
586      */
587     public void setup(Reader reader)
588                throws IOException, SAXException, ParserConfigurationException {
589         setup(UJAXP.getDocument(reader, UJAXP.FLAG_NONE));
590     }
591 
592     /**
593      * Returns a String representation of this object.
594      * While this method informs as XML format representaion,
595      *  it's purpose is just information, not making
596      * a rigid XML documentation.
597      *
598      * @return String
599      */
600     public String toString() {
601         try {
602             return (makeTextDocument());
603         } catch (Exception e) {
604             return (super.toString());
605         }
606     }
607 
608     /**
609      * @param element
610      */
611     private void init(Element element) {
612         try {
613             content_ = URelaxer.getElementPropertyAsString(element);
614         } catch (IllegalArgumentException e) {
615             ;
616         }
617         try {
618             rel_ = URelaxer.getAttributePropertyAsString(element, "rel");
619         } catch (IllegalArgumentException e) {
620             ;
621         }
622     }
623 }