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