libxspf  1.2.0
XspfReader.h
Go to the documentation of this file.
1 /*
2  * libxspf - XSPF playlist handling library
3  *
4  * Copyright (C) 2006-2008, Sebastian Pipping / Xiph.Org Foundation
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer.
14  *
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials
18  * provided with the distribution.
19  *
20  * * Neither the name of the Xiph.Org Foundation nor the names of
21  * its contributors may be used to endorse or promote products
22  * derived from this software without specific prior written
23  * permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
36  * OF THE POSSIBILITY OF SUCH DAMAGE.
37  *
38  * Sebastian Pipping, sping@xiph.org
39  */
40 
46 #ifndef XSPF_READER_H
47 #define XSPF_READER_H
48 
49 
50 #include "XspfDefines.h"
51 #include <string>
52 
53 
54 namespace Xspf {
55 
56 
58 
59 // Messages with ONE "%s" in it
60 #define XSPF_READER_TEXT_ONE_ATTRIBUTE_FORBIDDEN _PT("Attribute '%s' not allowed.")
61 #define XSPF_READER_TEXT_ONE_EXPAT_ERROR _PT("Expat error '%s'")
62 #define XSPF_READER_TEXT_ONE_ELEMENT_FORBIDDEN _PT("Element '%s' not allowed.")
63 #define XSPF_READER_TEXT_ONE_ELEMENT_FORBIDDEN_VERSION_ZERO _PT("Element '%s' not allowed in XSPF-0.")
64 #define XSPF_READER_TEXT_ONE_FILE_READING_ERROR _PT("File '%s' could not be read.")
65 #define XSPF_READER_TEXT_ONE_WRONG_ROOT_NAME _PT("Root element must be '") XSPF_NS_HOME XSPF_NS_SEP_STRING _PT("playlist', not '%s'.")
66 #define XSPF_READER_TEXT_ONE_WRONG_VERSION _PT("Version must be '0' or '1', not '%s'.")
67 
68 // Messages with ZERO "%s" in it
69 #define XSPF_READER_TEXT_ZERO_ATTRIBUTE_MISSING(name) _PT("Attribute '") name _PT("' missing.")
70 #define XSPF_READER_TEXT_ZERO_ELEMENT_MISSING(ns, name) _PT("Element '") ns XSPF_NS_SEP_STRING name _PT("' missing.")
71 #define XSPF_READER_TEXT_ZERO_ELEMENT_MISSING_VERSION_ZERO(ns, name) _PT("Element '") ns XSPF_NS_SEP_STRING name _PT("' missing. This is not allowed in XSPF-0.")
72 #define XSPF_READER_TEXT_ZERO_FILENAME_NULL _PT("Filename must not be NULL.")
73 #define XSPF_READER_TEXT_ZERO_TOO_MANY_ELEMENTS(ns, name) _PT("Only one '") ns XSPF_NS_SEP_STRING name _PT("' allowed.")
74 #define XSPF_READER_TEXT_ZERO_WRONG_ATTRIBUTE_TYPE(attr, type) _PT("Attribute '") attr _PT("' is not a valid ") type _PT(".")
75 #define XSPF_READER_TEXT_ZERO_WRONG_CONTENT_TYPE(ns, elem, type) _PT("Content of '") ns XSPF_NS_SEP_STRING elem _PT("' is not a valid ") type _PT(".")
76 #define XSPF_READER_TEXT_ZERO_TEXT_FORBIDDEN(ns, elem) _PT("Content of '") ns XSPF_NS_SEP_STRING elem _PT("' must be whitespace or child elements, not text.")
77 #define XSPF_READER_TEXT_ZERO_KEY_WITHOUT_VERSION(name) _PT("Attribute '") name _PT("' does not carry version information.")
78 #define XSPF_READER_TEXT_ZERO_KEY_WITH_REL_URI(name) _PT("Attribute '") name _PT("' does not contain an absolute URI.")
79 
81 
82 
88 
98 
101 
104  XSPF_READER_ERROR_MALICIOUS_LOOKUP_DEPTH, //< An entity's lookup depth is too high
105 
106  // Insert new codes HERE!
107 
108  // This one must come last!
109  XSPF_READER_ERROR_EXPAT = 0x1000
110 };
111 
112 
113 /*
114 playlist 1
115  title ?
116  creator ?
117  annotation ?
118  info ?
119  location ?
120  identifier ?
121  image ?
122  date ?
123  license ?
124  attribution ?
125  location *
126  identifier *
127  link *
128  meta *
129  extension *
130  ... *
131  trackList 1
132  track +|*
133  location *
134  identifier *
135  title ?
136  creator ?
137  annotation ?
138  info ?
139  image ?
140  album ?
141  trackNum (uint > 0) ?
142  duration (uint) ?
143  link *
144  meta *
145  extension *
146 */
147 
151 enum XspfTag {
152  // Stack returns 0 if empty
154 
186 
187  // Insert XSPF-2 codes HERE!
188 
189  // This one must come last!
190  TAG_USER = 0x1000
191 };
192 
193 
194 class XspfProps;
195 class XspfDateTime;
196 class XspfTrack;
197 class XspfReaderCallback;
198 template <class T> class XspfStack;
199 class XspfChunkCallback;
200 
202 class XspfExtensionReader;
203 class XspfReaderPrivate;
204 
205 
209 class XspfReader {
210 
211 private:
213  XspfReaderPrivate * const d;
214 
216 public:
222  XspfReader(XspfExtensionReaderFactory * handlerFactory = NULL);
223 
229  XspfReader(XspfReader const & source);
230 
236  XspfReader & operator=(XspfReader const & source);
237 
241  ~XspfReader();
242 
254  int parseFile(XML_Char const * filename, XspfReaderCallback * callback,
255  XML_Char const * baseUri);
256 
269  int parseMemory(char const * memory, int numBytes, XspfReaderCallback * callback,
270  XML_Char const * baseUri);
271 
283  int parseChunks(XspfChunkCallback * inputCallback,
284  XspfReaderCallback * dataCallback, XML_Char const * baseUri);
285 
294  void limitLengthPerEntityValue(bool enabled);
295 
304  void limitLookupSumPerEntityValue(bool enabled);
305 
314  void limitLookupDepthPerEntityValue(bool enabled);
315 
327  void enableMaliciousXmlDetection(bool enabled);
328 
337  void setMaxLengthPerEntityValue(int maxLength);
338 
347  void setMaxLookupSumPerEntityValue(int maxLookupSum);
348 
357  void setMaxLookupDepthPerEntityValue(int maxLookupDepth);
358 
359 private:
363  void makeReusable();
364 
372  bool handleError(int code, XML_Char const * text);
373 
382  bool handleError(int code, XML_Char const * format, XML_Char const * param);
383 
390  void handleFatalError(int code, XML_Char const * text);
391 
399  void handleFatalError(int code, XML_Char const * format, XML_Char const * param);
400 
408  bool handleWarning(int code, XML_Char const * text);
409 
414  void setExpatError();
415 
419  void stop();
420 
427  void handleStart(XML_Char const * fullName, XML_Char const ** atts);
428 
436  bool handleStartOne(XML_Char const * fullName, XML_Char const ** atts);
437 
445  bool handleStartTwo(XML_Char const * fullName, XML_Char const ** atts);
446 
454  bool handleStartThree(XML_Char const * fullName, XML_Char const ** atts);
455 
463  bool handleStartFour(XML_Char const * fullName, XML_Char const ** atts);
464 
471  void handleCharacters(XML_Char const * s, int len);
472 
479  void handleEntityDeclaration(XML_Char const * entityName,
480  XML_Char const * value);
481 
487  void handleEnd(XML_Char const * fullName);
488 
495  bool handleEndOne(XML_Char const * fullName);
496 
503  bool handleEndTwo(XML_Char const * fullName);
504 
511  bool handleEndThree(XML_Char const * fullName);
512 
519  bool handleEndFour(XML_Char const * fullName);
520 
528  bool handlePlaylistAttribs(XML_Char const ** atts);
529 
538  bool handleMetaLinkAttribs(XML_Char const ** atts, XML_Char const * & rel);
539 
548  bool handleExtensionAttribs(XML_Char const ** atts,
549  XML_Char const * & application);
550 
557  bool handleNoAttribsExceptXmlBase(XML_Char const ** atts);
558 
562  void clearError();
563 
571  static void masterStart(void * userData, XML_Char const * fullName, XML_Char const ** atts);
572 
579  static void masterEnd(void * userData, XML_Char const * fullName);
580 
588  static void masterCharacters(void * userData, XML_Char const * s, int len);
589 
603  static void masterEntityDeclaration(void * userData, XML_Char const * entityName,
604  int is_parameter_entity, XML_Char const * value, int value_length,
605  XML_Char const * base, XML_Char const * systemId, XML_Char const * publicId,
606  XML_Char const * notationName);
607 
615  bool onBeforeParse(XspfReaderCallback * callback,
616  XML_Char const * baseUri);
617 
621  void onAfterParse();
622 
623 public:
630  static bool isXmlBase(XML_Char const * attributeKey);
631 
632 private:
639  bool handleXmlBaseAttribute(XML_Char const * xmlBase);
640 
648  XML_Char * makeAbsoluteUri(XML_Char const * sourceUri) const;
649 
655  XspfStack<unsigned int> & getElementStack() const;
656 
662  XspfStack<std::basic_string<XML_Char> > & getBaseUriStack() const;
663 
670  void skipFromHere();
671 
682  bool checkAndSkipNamespace(XML_Char const * fullName,
683  XML_Char const * & localName);
684 
689  void notifySuccess() const;
690 
691  friend class XspfExtensionReader;
692 
693 };
694 
695 
696 } // namespace Xspf
697 
698 #endif // XSPF_READER_H
void enableMaliciousXmlDetection(bool enabled)
playlist.tracklist.track.info tag
Definition: XspfReader.h:178
Required attribute missing.
Definition: XspfReader.h:94
playlist.tracklist.track.link tag
Definition: XspfReader.h:183
playlist.tracklist.track tag
Definition: XspfReader.h:172
playlist.tracklist.track.extension tag
Definition: XspfReader.h:185
playlist.tracklist tag
Definition: XspfReader.h:171
int parseMemory(char const *memory, int numBytes, XspfReaderCallback *callback, XML_Char const *baseUri)
playlist.link tag
Definition: XspfReader.h:168
playlist.tracklist.track.image tag
Definition: XspfReader.h:179
XspfReader(XspfExtensionReaderFactory *handlerFactory=NULL)
First user/extension tag code.
Definition: XspfReader.h:190
A key attribute holds an unversioned URI.
Definition: XspfReader.h:99
static bool isXmlBase(XML_Char const *attributeKey)
XspfReaderReturnCode
Definition: XspfReader.h:86
void limitLengthPerEntityValue(bool enabled)
An entity is taking to much space.
Definition: XspfReader.h:102
playlist.tracklist.track.album tag
Definition: XspfReader.h:180
void setMaxLookupSumPerEntityValue(int maxLookupSum)
An entity takes to many lookups in sum.
Definition: XspfReader.h:103
playlist.annotation tag
Definition: XspfReader.h:158
playlist.attribution.identifier tag
Definition: XspfReader.h:167
playlist.tracklist.track.creator tag
Definition: XspfReader.h:176
playlist.tracklist.track.tracknum tag
Definition: XspfReader.h:181
playlist.tracklist.track.meta tag
Definition: XspfReader.h:184
playlist.extension tag
Definition: XspfReader.h:170
playlist.image tag
Definition: XspfReader.h:162
Everything fine.
Definition: XspfReader.h:87
Element is not allowed at that place.
Definition: XspfReader.h:91
void setMaxLookupDepthPerEntityValue(int maxLookupDepth)
Given base URI is not a valid absolute URI.
Definition: XspfReader.h:97
playlist.title tag
Definition: XspfReader.h:156
int parseFile(XML_Char const *filename, XspfReaderCallback *callback, XML_Char const *baseUri)
playlist.attribution.location tag
Definition: XspfReader.h:166
Attribute not allowed at that place.
Definition: XspfReader.h:95
void setMaxLengthPerEntityValue(int maxLength)
playlist.tracklist.track.annotation tag
Definition: XspfReader.h:177
playlist.tracklist.track.duration tag
Definition: XspfReader.h:182
playlist.identifier tag
Definition: XspfReader.h:161
Element body has invalid format.
Definition: XspfReader.h:96
playlist.date tag
Definition: XspfReader.h:163
void limitLookupDepthPerEntityValue(bool enabled)
Attribute with invalid value.
Definition: XspfReader.h:93
playlist tag
Definition: XspfReader.h:155
XspfReader & operator=(XspfReader const &source)
playlist.license tag
Definition: XspfReader.h:164
int parseChunks(XspfChunkCallback *inputCallback, XspfReaderCallback *dataCallback, XML_Char const *baseUri)
Element occurs more often than allowed.
Definition: XspfReader.h:90
playlist.meta tag
Definition: XspfReader.h:169
A key attribute holds a relative URI.
Definition: XspfReader.h:100
playlist.creator tag
Definition: XspfReader.h:157
playlist.info tag
Definition: XspfReader.h:159
void limitLookupSumPerEntityValue(bool enabled)
playlist.tracklist.track.identifier tag
Definition: XspfReader.h:174
playlist.tracklist.track.title tag
Definition: XspfReader.h:175
playlist.attribution tag
Definition: XspfReader.h:165
Required element missing.
Definition: XspfReader.h:92
playlist.location tag
Definition: XspfReader.h:160
playlist.tracklist.track.location tag
Definition: XspfReader.h:173
Unknown type.
Definition: XspfReader.h:153