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>XspfExtension</b> is generated from xspf.rng by Relaxer.
42   * This class is derived from:
43   *
44   * <!-- for programmer
45   * <element name="extension">
46   *             <attribute name="application">
47   *                 <data type="anyURI"/>
48   *             </attribute>
49   *
50   *             <zeroOrMore>
51   *                 <choice>
52   *                     <element>
53   *                         <anyName/>
54   *                         <ref name="anything"/>
55   *                     </element>
56   *                     <text/>
57   *                 </choice>
58   *             </zeroOrMore>
59   *         </element>-->
60   * <!-- for javadoc -->
61   * <pre> &lt;element name="extension"&gt;
62   *             &lt;attribute name="application"&gt;
63   *                 &lt;data type="anyURI"/&gt;
64   *             &lt;/attribute&gt;
65   *
66   *             &lt;zeroOrMore&gt;
67   *                 &lt;choice&gt;
68   *                     &lt;element&gt;
69   *                         &lt;anyName/&gt;
70   *                         &lt;ref name="anything"/&gt;
71   *                     &lt;/element&gt;
72   *                     &lt;text/&gt;
73   *                 &lt;/choice&gt;
74   *             &lt;/zeroOrMore&gt;
75   *         &lt;/element&gt;</pre>
76   *
77   * @version xspf.rng (Wed Sep 27 17:36:25 EDT 2006)
78   * @author  Relaxer 1.1b (http://www.relaxer.org)
79   * @author Emil A. Lefkof III <info@melloware.com>
80   */
81  public class XspfExtension
82      implements java.io.Serializable,
83                 Cloneable {
84  	
85      // List<IXspfExtensionMixed>
86      private java.util.List content_ = new java.util.ArrayList();
87      private String application_;
88  
89      /**
90       * Creates a <code>XspfExtension</code>.
91       *
92       */
93      public XspfExtension() {
94          application_ = "";
95      }
96  
97      /**
98       * Creates a <code>XspfExtension</code>.
99       *
100      * @param source
101      */
102     public XspfExtension(XspfExtension source) {
103         setup(source);
104     }
105 
106     /**
107      * Creates a <code>XspfExtension</code> by the Stack <code>stack</code>
108      * that contains Elements.
109      * This constructor is supposed to be used internally
110      * by the Relaxer system.
111      *
112      * @param stack
113      */
114     public XspfExtension(RStack stack) {
115         setup(stack);
116     }
117 
118     /**
119      * Creates a <code>XspfExtension</code> by the Document <code>doc</code>.
120      *
121      * @param doc
122      */
123     public XspfExtension(Document doc) {
124         setup(doc.getDocumentElement());
125     }
126 
127     /**
128      * Creates a <code>XspfExtension</code> by the Element <code>element</code>.
129      *
130      * @param element
131      */
132     public XspfExtension(Element element) {
133         setup(element);
134     }
135 
136     /**
137      * Creates a <code>XspfExtension</code> by the File <code>file</code>.
138      *
139      * @param file
140      * @exception IOException
141      * @exception SAXException
142      * @exception ParserConfigurationException
143      */
144     public XspfExtension(File file)
145                   throws IOException, SAXException, ParserConfigurationException {
146         setup(file);
147     }
148 
149     /**
150      * Creates a <code>XspfExtension</code>
151      * by the String representation of URI <code>uri</code>.
152      *
153      * @param uri
154      * @exception IOException
155      * @exception SAXException
156      * @exception ParserConfigurationException
157      */
158     public XspfExtension(String uri)
159                   throws IOException, SAXException, ParserConfigurationException {
160         setup(uri);
161     }
162 
163     /**
164      * Creates a <code>XspfExtension</code> by the URL <code>url</code>.
165      *
166      * @param url
167      * @exception IOException
168      * @exception SAXException
169      * @exception ParserConfigurationException
170      */
171     public XspfExtension(URL url)
172                   throws IOException, SAXException, ParserConfigurationException {
173         setup(url);
174     }
175 
176     /**
177      * Creates a <code>XspfExtension</code> by the InputStream <code>in</code>.
178      *
179      * @param in
180      * @exception IOException
181      * @exception SAXException
182      * @exception ParserConfigurationException
183      */
184     public XspfExtension(InputStream in)
185                   throws IOException, SAXException, ParserConfigurationException {
186         setup(in);
187     }
188 
189     /**
190      * Creates a <code>XspfExtension</code> by the InputSource <code>is</code>.
191      *
192      * @param is
193      * @exception IOException
194      * @exception SAXException
195      * @exception ParserConfigurationException
196      */
197     public XspfExtension(InputSource is)
198                   throws IOException, SAXException, ParserConfigurationException {
199         setup(is);
200     }
201 
202     /**
203      * Creates a <code>XspfExtension</code> by the Reader <code>reader</code>.
204      *
205      * @param reader
206      * @exception IOException
207      * @exception SAXException
208      * @exception ParserConfigurationException
209      */
210     public XspfExtension(Reader reader)
211                   throws IOException, SAXException, ParserConfigurationException {
212         setup(reader);
213     }
214 
215     /**
216      * Tests if a Element <code>element</code> is valid
217      * for the <code>XspfExtension</code>.
218      *
219      * @param element
220      * @return boolean
221      */
222     public static boolean isMatch(Element element) {
223         if (!URelaxer.isTargetElement(element, "extension")) {
224             return (false);
225         }
226         RStack target = new RStack(element);
227         if (!URelaxer.hasAttributeHungry(target, "application")) {
228             return (false);
229         }
230         if (RString.isMatch(target)) {
231             ;
232         }
233         while (true) {
234             if (XspfExtensionAnyElement.isMatchHungry(target)) {
235                 ;
236             } else {
237                 break;
238             }
239         }
240         if (!target.isEmptyElement()) {
241             return (false);
242         }
243         return (true);
244     }
245 
246     /**
247      * Tests if elements contained in a Stack <code>stack</code>
248      * is valid for the <code>XspfExtension</code>.
249      * This mehtod is supposed to be used internally
250      * by the Relaxer system.
251      *
252      * @param stack
253      * @return boolean
254      */
255     public static boolean isMatch(RStack stack) {
256         Element element = stack.peekElement();
257         if (element == null) {
258             return (false);
259         }
260         return (isMatch(element));
261     }
262 
263     /**
264      * Tests if elements contained in a Stack <code>stack</code>
265      * is valid for the <code>XspfExtension</code>.
266      * This method consumes the stack contents during matching operation.
267      * This mehtod is supposed to be used internally
268      * by the Relaxer system.
269      *
270      * @param stack
271      * @return boolean
272      */
273     public static boolean isMatchHungry(RStack stack) {
274         Element element = stack.peekElement();
275         if (element == null) {
276             return (false);
277         }
278         if (isMatch(element)) {
279             stack.popElement();
280             return (true);
281         } else {
282             return (false);
283         }
284     }
285 
286     /**
287      * Gets the String property <b>application</b>.
288      *
289      * @return String
290      */
291     public String getApplication() {
292         return (application_);
293     }
294 
295     /**
296      * Gets the property value as String.
297      *
298      * @return String
299      */
300     public String getApplicationAsString() {
301         return (URelaxer.getString(getApplication()));
302     }
303 
304     /**
305      * Gets the IXspfExtensionMixed property <b>content</b>.
306      *
307      * @return IXspfExtensionMixed[]
308      */
309     public IXspfExtensionMixed[] getContent() {
310         IXspfExtensionMixed[] array = new IXspfExtensionMixed[content_.size()];
311         return ((IXspfExtensionMixed[])content_.toArray(array));
312     }
313 
314     /**
315      * Gets the IXspfExtensionMixed property <b>content</b> by index.
316      *
317      * @param index
318      * @return IXspfExtensionMixed
319      */
320     public IXspfExtensionMixed getContent(int index) {
321         return ((IXspfExtensionMixed)content_.get(index));
322     }
323 
324     /**
325      * Sets the String property <b>application</b>.
326      *
327      * @param application
328      */
329     public void setApplication(String application) {
330         this.application_ = application;
331     }
332 
333     /**
334      * Sets the property value by String.
335      *
336      * @param string
337      */
338     public void setApplicationByString(String string) {
339         setApplication(URelaxer.getString(string));
340     }
341 
342     /**
343      * Sets a mixed content by <code>String</code>.
344      *
345      * @param value
346      */
347     public void setContent(String value) {
348         setContent(new RString(value));
349     }
350 
351     /**
352      * Sets a mixed content by <code>org.w3c.dom.Node</code>.
353      *
354      * @param value
355      */
356     public void setContent(org.w3c.dom.Node value) {
357         setContent(new RString(value));
358     }
359 
360     /**
361      * Sets the IXspfExtensionMixed property <b>content</b>.
362      *
363      * @param content
364      */
365     public void setContent(IXspfExtensionMixed[] content) {
366         this.content_.clear();
367         for (int i = 0; i < content.length; i++) {
368             addContent(content[i]);
369         }
370     }
371 
372     /**
373      * Sets the IXspfExtensionMixed property <b>content</b>.
374      *
375      * @param content
376      */
377     public void setContent(IXspfExtensionMixed content) {
378         this.content_.clear();
379         addContent(content);
380     }
381 
382     /**
383      * Sets the IXspfExtensionMixed property <b>content</b> by index.
384      *
385      * @param index
386      * @param content
387      */
388     public void setContent(int index, IXspfExtensionMixed content) {
389         this.content_.set(index, content);
390     }
391 
392     /**
393      * Sets a mixed content by <code>String</code>.
394      *
395      * @param value
396      */
397     public void setContentByString(String value) {
398         setContent(new RString(value));
399     }
400 
401     /**
402      * Adds a mixed content by <code>String</code>.
403      *
404      * @param value
405      */
406     public void addContent(String value) {
407         addContent(new RString(value));
408     }
409 
410     /**
411      * Adds a mixed content by <code>org.w3c.dom.Node</code>.
412      *
413      * @param value
414      */
415     public void addContent(org.w3c.dom.Node value) {
416         addContent(new RString(value));
417     }
418 
419     /**
420      * Adds the IXspfExtensionMixed property <b>content</b>.
421      *
422      * @param content
423      */
424     public void addContent(IXspfExtensionMixed content) {
425         this.content_.add(content);
426     }
427 
428     /**
429      * Adds the IXspfExtensionMixed property <b>content</b>.
430      *
431      * @param content
432      */
433     public void addContent(IXspfExtensionMixed[] content) {
434         for (int i = 0; i < content.length; i++) {
435             addContent(content[i]);
436         }
437     }
438 
439     /**
440      * Adds the IXspfExtensionMixed property <b>content</b> by index.
441      *
442      * @param index
443      * @param content
444      */
445     public void addContent(int index, IXspfExtensionMixed content) {
446         this.content_.add(index, content);
447     }
448 
449     /**
450      * Adds a mixed content by <code>String</code>.
451      *
452      * @param value
453      */
454     public void addContentByString(String value) {
455         addContent(new RString(value));
456     }
457 
458     /**
459      * Clear the IXspfExtensionMixed property <b>content</b>.
460      *
461      */
462     public void clearContent() {
463         this.content_.clear();
464     }
465 
466     /**
467      * @return Object
468      */
469     public Object clone() {
470         return (new XspfExtension((XspfExtension)this));
471     }
472 
473     /**
474      * Creates a DOM document representation of the object.
475      *
476      * @exception ParserConfigurationException
477      * @return Document
478      */
479     public Document makeDocument()
480                           throws ParserConfigurationException {
481         Document doc = UJAXP.makeDocument();
482         makeElement(doc);
483         return (doc);
484     }
485 
486     /**
487      * Creates a DOM representation of the object.
488      * Result is appended to the Node <code>parent</code>.
489      *
490      * @param parent
491      */
492     public void makeElement(Node parent) {
493         Document doc;
494         if (parent instanceof Document) {
495             doc = (Document)parent;
496         } else {
497             doc = parent.getOwnerDocument();
498         }
499         Element element = doc.createElement("extension");
500         if (parent instanceof Document) {
501             element.setAttribute("xmlns", "http://xspf.org/ns/0/");
502         }
503         int size;
504         if (this.application_ != null) {
505             URelaxer.setAttributePropertyByString(element, "application", this.application_);
506         }
507         size = this.content_.size();
508         for (int i = 0; i < size; i++) {
509             IXspfExtensionMixed value = (IXspfExtensionMixed)this.content_.get(i);
510             value.makeElement(element);
511         }
512         parent.appendChild(element);
513     }
514 
515     /**
516      * Makes an XML text representation.
517      *
518      * @param buffer
519      */
520     public void makeTextAttribute(StringBuffer buffer) {
521     }
522 
523     /**
524      * Makes an XML text representation.
525      *
526      * @param buffer
527      * @exception IOException
528      */
529     public void makeTextAttribute(Writer buffer)
530                            throws IOException {
531     }
532 
533     /**
534      * Makes an XML text representation.
535      *
536      * @param buffer
537      */
538     public void makeTextAttribute(PrintWriter buffer) {
539     }
540 
541     /**
542      * Makes an XML text representation.
543      *
544      * @return String
545      */
546     public String makeTextDocument() {
547         StringBuffer buffer = new StringBuffer();
548         makeTextElement(buffer);
549         return (new String(buffer));
550     }
551 
552     /**
553      * Makes an XML text representation.
554      *
555      * @param buffer
556      */
557     public void makeTextElement(StringBuffer buffer) {
558         int size;
559         buffer.append("<extension");
560         buffer.append(" xmlns=\"http://xspf.org/ns/0/\"");
561         if (application_ != null) {
562             buffer.append(" application=\"");
563             buffer.append(URelaxer.escapeAttrQuot(URelaxer.getString(getApplication())));
564             buffer.append("\"");
565         }
566         size = this.content_.size();
567         for (int i = 0; i < size; i++) {
568             IXspfExtensionMixed value = (IXspfExtensionMixed)this.content_.get(i);
569             value.makeTextAttribute(buffer);
570         }
571         buffer.append(">");
572         size = this.content_.size();
573         for (int i = 0; i < size; i++) {
574             IXspfExtensionMixed value = (IXspfExtensionMixed)this.content_.get(i);
575             value.makeTextElement(buffer);
576         }
577         buffer.append("</extension>");
578     }
579 
580     /**
581      * Makes an XML text representation.
582      *
583      * @param buffer
584      * @exception IOException
585      */
586     public void makeTextElement(Writer buffer)
587                          throws IOException {
588         int size;
589         buffer.write("<extension");
590         buffer.write(" xmlns=\"http://xspf.org/ns/0/\"");
591         if (application_ != null) {
592             buffer.write(" application=\"");
593             buffer.write(URelaxer.escapeAttrQuot(URelaxer.getString(getApplication())));
594             buffer.write("\"");
595         }
596         size = this.content_.size();
597         for (int i = 0; i < size; i++) {
598             IXspfExtensionMixed value = (IXspfExtensionMixed)this.content_.get(i);
599             value.makeTextAttribute(buffer);
600         }
601         buffer.write(">");
602         size = this.content_.size();
603         for (int i = 0; i < size; i++) {
604             IXspfExtensionMixed value = (IXspfExtensionMixed)this.content_.get(i);
605             value.makeTextElement(buffer);
606         }
607         buffer.write("</extension>");
608     }
609 
610     /**
611      * Makes an XML text representation.
612      *
613      * @param buffer
614      */
615     public void makeTextElement(PrintWriter buffer) {
616         int size;
617         buffer.print("<extension");
618         buffer.print(" xmlns=\"http://xspf.org/ns/0/\"");
619         if (application_ != null) {
620             buffer.print(" application=\"");
621             buffer.print(URelaxer.escapeAttrQuot(URelaxer.getString(getApplication())));
622             buffer.print("\"");
623         }
624         size = this.content_.size();
625         for (int i = 0; i < size; i++) {
626             IXspfExtensionMixed value = (IXspfExtensionMixed)this.content_.get(i);
627             value.makeTextAttribute(buffer);
628         }
629         buffer.print(">");
630         size = this.content_.size();
631         for (int i = 0; i < size; i++) {
632             IXspfExtensionMixed value = (IXspfExtensionMixed)this.content_.get(i);
633             value.makeTextElement(buffer);
634         }
635         buffer.print("</extension>");
636     }
637 
638     /**
639      * Remove the IXspfExtensionMixed property <b>content</b> by index.
640      *
641      * @param index
642      */
643     public void removeContent(int index) {
644         this.content_.remove(index);
645     }
646 
647     /**
648      * Remove the IXspfExtensionMixed property <b>content</b> by object.
649      *
650      * @param content
651      */
652     public void removeContent(IXspfExtensionMixed content) {
653         this.content_.remove(content);
654     }
655 
656     /**
657      * Initializes the <code>XspfExtension</code> by the XspfExtension <code>source</code>.
658      *
659      * @param source
660      */
661     public void setup(XspfExtension source) {
662         int size;
663         application_ = source.application_;
664         this.content_.clear();
665         size = source.content_.size();
666         for (int i = 0; i < size; i++) {
667             addContent((IXspfExtensionMixed)source.getContent(i).clone());
668         }
669     }
670 
671     /**
672      * Initializes the <code>XspfExtension</code> by the Document <code>doc</code>.
673      *
674      * @param doc
675      */
676     public void setup(Document doc) {
677         setup(doc.getDocumentElement());
678     }
679 
680     /**
681      * Initializes the <code>XspfExtension</code> by the Element <code>element</code>.
682      *
683      * @param element
684      */
685     public void setup(Element element) {
686         init(element);
687     }
688 
689     /**
690      * Initializes the <code>XspfExtension</code> by the Stack <code>stack</code>
691      * that contains Elements.
692      * This constructor is supposed to be used internally
693      * by the Relaxer system.
694      *
695      * @param stack
696      */
697     public void setup(RStack stack) {
698         init(stack.popElement());
699     }
700 
701     /**
702      * Initializes the <code>XspfExtension</code> by the File <code>file</code>.
703      *
704      * @param file
705      * @exception IOException
706      * @exception SAXException
707      * @exception ParserConfigurationException
708      */
709     public void setup(File file)
710                throws IOException, SAXException, ParserConfigurationException {
711         setup(file.toURL());
712     }
713 
714     /**
715      * Initializes the <code>XspfExtension</code>
716      * by the String representation of URI <code>uri</code>.
717      *
718      * @param uri
719      * @exception IOException
720      * @exception SAXException
721      * @exception ParserConfigurationException
722      */
723     public void setup(String uri)
724                throws IOException, SAXException, ParserConfigurationException {
725         setup(UJAXP.getDocument(uri, UJAXP.FLAG_NONE));
726     }
727 
728     /**
729      * Initializes the <code>XspfExtension</code> by the URL <code>url</code>.
730      *
731      * @param url
732      * @exception IOException
733      * @exception SAXException
734      * @exception ParserConfigurationException
735      */
736     public void setup(URL url)
737                throws IOException, SAXException, ParserConfigurationException {
738         setup(UJAXP.getDocument(url, UJAXP.FLAG_NONE));
739     }
740 
741     /**
742      * Initializes the <code>XspfExtension</code> by the InputStream <code>in</code>.
743      *
744      * @param in
745      * @exception IOException
746      * @exception SAXException
747      * @exception ParserConfigurationException
748      */
749     public void setup(InputStream in)
750                throws IOException, SAXException, ParserConfigurationException {
751         setup(UJAXP.getDocument(in, UJAXP.FLAG_NONE));
752     }
753 
754     /**
755      * Initializes the <code>XspfExtension</code> by the InputSource <code>is</code>.
756      *
757      * @param is
758      * @exception IOException
759      * @exception SAXException
760      * @exception ParserConfigurationException
761      */
762     public void setup(InputSource is)
763                throws IOException, SAXException, ParserConfigurationException {
764         setup(UJAXP.getDocument(is, UJAXP.FLAG_NONE));
765     }
766 
767     /**
768      * Initializes the <code>XspfExtension</code> by the Reader <code>reader</code>.
769      *
770      * @param reader
771      * @exception IOException
772      * @exception SAXException
773      * @exception ParserConfigurationException
774      */
775     public void setup(Reader reader)
776                throws IOException, SAXException, ParserConfigurationException {
777         setup(UJAXP.getDocument(reader, UJAXP.FLAG_NONE));
778     }
779 
780     /**
781      * Gets number of the IXspfExtensionMixed property <b>content</b>.
782      *
783      * @return int
784      */
785     public int sizeContent() {
786         return (content_.size());
787     }
788 
789     /**
790      * Returns a String representation of this object.
791      * While this method informs as XML format representaion,
792      *  it's purpose is just information, not making
793      * a rigid XML documentation.
794      *
795      * @return String
796      */
797     public String toString() {
798         try {
799             return (makeTextDocument());
800         } catch (Exception e) {
801             return (super.toString());
802         }
803     }
804 
805     /**
806      * @param element
807      */
808     private void init(Element element) {
809         RStack stack = new RStack(element);
810         try {
811             application_ = URelaxer.getAttributePropertyAsString(element, "application");
812         } catch (IllegalArgumentException e) {
813             ;
814         }
815         this.content_.clear();
816         
817         while (true) {
818             if (RString.isMatch(stack)) {
819                 addContent(new RString(stack));
820             } else if (XspfExtensionAnyElement.isMatch(stack)) {
821                 addContent(new XspfExtensionAnyElement(stack));
822             } else {
823                 break;
824             }
825         }
826     }
827 }