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