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