Main Page   Data Structures   File List   Data Fields   Globals  

ncxparser.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006 by André Lindhjem <belgarat@sdf.lonestar.org>,     *
00003  *                         Kjetil Holien <kjetil.holien@gmail.com>,        *
00004  *                         Terje Risa <terje.risa@gmail.com> &             *
00005  *                         Øyvind Nerbråten <oyvind@nerbraten.com>         *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************
00022  *
00023  * \file ncxparser.c
00024  * \author André Lindhjem, Kjetil Holien, Terje Risa & Øyvind Nerbråten
00025  * \date 23.02.2006
00026  *
00027  * NCX parser for the Daisy Z39.86-2005 standard. Collects data and stores it in linked list
00028  * in the main datastructure. This linked smil list represents the spine of the
00029  * daisy DTB.
00030  */
00031 
00032 
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <libxml2/libxml/xmlreader.h>
00037 
00038 #include "ncxparser.h"
00039 #include "control.h"
00040 #include "report.h"
00041 #include "common.h"
00042 #include "snprintf/snprintf.h"
00043 
00044 /* Test if the xml reader is present */
00045 #ifdef LIBXML_READER_ENABLED
00046 
00047 
00048 
00049 /* ************************************************** *
00050  *          Static function declarations              *
00051  * ************************************************** */
00052  
00059 static int parseNcxHead (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00060 
00067 static int parseNcxDocTitle (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00068 
00075 static int parseNcxDocAuthor (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00076 
00083 static int parseNcxNavMap (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00084 
00091 static int parseNcxPageList (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00092 
00099 static int parseNcxNavList (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00100 
00107 static int parseNcxSmilCustomTest (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00108 
00115 static int parseNcxMeta (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00116 
00125 static int parseNcxText (xmlTextReaderPtr reader, struct DaisyData *daisydata, struct SmilNode *tmpSmilNode, int mode);
00126 
00135 static int parseNcxAudio (xmlTextReaderPtr reader, struct DaisyData *daisydata, struct SmilNode *tmpSmilNode, int mode);
00136 
00144 static int parseNcxImg (xmlTextReaderPtr reader, struct DaisyData *daisydata, struct SmilNode *tmpSmilNode, int mode);
00145 
00152 static int parseNcxNavInfo (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00153 
00161 static int parseNcxNavLabel (xmlTextReaderPtr reader, struct DaisyData *daisydata, struct SmilNode *tmpSmilNode);
00162 
00169 static int parseNcxNavPoint (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00170 
00177 static int parseNcxContent (xmlTextReaderPtr reader, struct SmilNode *tmpSmilNode);
00178 
00185 static int parseNcxPageTarget (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00186 
00193 static int parseNcxNavTarget (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00194 
00195 
00196 
00197 /* ************************************************** *
00198  *          Global function definitions               *
00199  * ************************************************** */
00200  
00209 int parseNCX (struct DaisyData *daisydata, char *filename)
00210 {
00211     const xmlChar *name;
00212     char *nameChar = NULL;
00213     xmlTextReaderPtr reader;
00214     int ret;
00215     
00216     name = NULL;
00217     /* open the ncx file */
00218     reader = xmlReaderForFile (filename, NULL, 0);
00219     if (reader == NULL)
00220     {
00221         char error[STRLEN];
00222         snprintf (error, sizeof (error),  "Failed to open NCX file: %s", filename);
00223         report (error, REP_WARNING);
00224         return -1;
00225     }
00226     else 
00227     {
00228         if (DEBUG_NCXPARSER)
00229         {
00230             char error[STRLEN];
00231             snprintf (error, sizeof (error), "Parsing NCX file: %s", filename);
00232             report (error, REP_DEBUG);
00233         }
00234         
00235         /* read the first tag */
00236         ret = xmlTextReaderRead(reader);
00237         
00238         /* while there are more tags, parse the next */
00239         if (ret != 1)
00240         {
00241             char error[STRLEN];
00242             snprintf (error, sizeof (error), "Failed to parse NCX file: %s", filename);
00243             report (error, REP_WARNING);
00244             return -1;
00245         }
00246         
00247         /* Attributes:
00248          * version
00249          * xmlns
00250          * xml:lang
00251          * dir
00252          */
00253         
00254         while(ret == 1)
00255         {
00256             /* get name of tag */
00257             name = xmlTextReaderConstName (reader);
00258             if (name == NULL) return ret;
00259             nameChar = tolowercase((const char *)name);
00260             if (nameChar == NULL) return 0;
00261             /* parse tag if <head> */
00262             if (!strcmp (nameChar, "head")) ret = parseNcxHead (reader, daisydata);
00263             /* parse tag if <docTitle> */
00264             else if (!strcmp (nameChar, "doctitle")) ret = parseNcxDocTitle (reader, daisydata);
00265             /* parse tag if <docAuthor> * */
00266             else if (!strcmp (nameChar, "docauthor")) ret = parseNcxDocAuthor (reader, daisydata);
00267             /* parse tag if <navMap> */
00268             else if (!strcmp (nameChar, "navmap")) ret = parseNcxNavMap (reader, daisydata);
00269             /* parse tag if <pageList> ? */
00270             else if (!strcmp (nameChar, "pagelist")) ret = parseNcxPageList (reader, daisydata);
00271             /* parse tag if <navList> * */
00272             else if (!strcmp (nameChar, "navlist")) ret = parseNcxNavList (reader, daisydata);
00273             if (ret == -1) break;
00274             ret = xmlTextReaderRead (reader);
00275         }
00276         xmlFreeTextReader (reader);
00277         if (ret != 0)
00278         {
00279             char error[STRLEN];
00280             snprintf (error, sizeof (error), "Failed to parse NCX file: %s", filename);
00281             report (error, REP_WARNING
00282             );
00283             return -1;
00284         }
00285     }
00286     return 1;
00287 }
00288 
00289 
00290 /* ************************************************** *
00291  *          Static function definitions               *
00292  * ************************************************** */
00293 
00300 static int parseNcxHead (xmlTextReaderPtr reader, struct DaisyData *daisydata)
00301 {
00302     const xmlChar *name = NULL;
00303     char *nameChar = NULL;
00304     int ret, type;
00305     ret = type = 1;
00306 
00307     if (DEBUG_NCXPARSER) report ("Parsing NCX <HEAD>", REP_DEBUG);  
00308     type = xmlTextReaderNodeType(reader);
00309     if (type != 1) 
00310     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <HEAD>", REP_ERROR);
00311         return -1;
00312     }
00313     ret = xmlTextReaderRead (reader);
00314     if (ret != 1) return ret;
00315     name = xmlTextReaderConstName (reader);
00316     if (name == NULL) return ret;
00317     nameChar = tolowercase((const char *)name);
00318     if (nameChar == NULL) return 0;
00319     /* while we haven't come to the head-close tag */
00320     while (strcmp (nameChar, "head"))
00321     {   
00322             /* parse tag if <smilCustomTest> */
00323             if (!strcmp (nameChar, "smilcustomtest")) ret = parseNcxSmilCustomTest (reader, daisydata);
00324             /* parse tag if <meta> */
00325             else if (!strcmp (nameChar, "meta")) ret = parseNcxMeta (reader, daisydata);
00326             if (ret != 1) return ret;
00327             ret = xmlTextReaderRead (reader);
00328             if (ret != 1) return ret;
00329             name = xmlTextReaderConstName (reader);         
00330             if (name == NULL) return ret;
00331             nameChar = tolowercase((const char *)name);
00332             if (nameChar == NULL) return 0;
00333     }
00334     type = xmlTextReaderNodeType(reader);
00335     if (type != 15) 
00336     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <HEAD>, endtag expected", REP_ERROR);
00337         return -1;
00338     }
00339     return ret;
00340 }
00341 
00342 
00349 static int parseNcxSmilCustomTest (xmlTextReaderPtr reader, struct DaisyData *daisydata)
00350 {
00351     int ret = 1;
00352     
00353     /* Attributes:
00354      * id
00355      * defaultState
00356      * override
00357      * bookStruct
00358      */
00359      
00360      daisydata = daisydata;
00361      
00362     if (DEBUG_NCXPARSER) report ("Parsing NCX <SMILCUSTOMTEST>", REP_DEBUG);
00363     ret = xmlTextReaderRead (reader);
00364     return ret;
00365 }
00366 
00367 
00374 static int parseNcxMeta (xmlTextReaderPtr reader, struct DaisyData *daisydata)
00375 {
00376     int ret = 1;
00377     xmlChar *attrvalue = NULL, *attrcontent = NULL;
00378     
00379     /* Attributes:
00380      * name
00381      * content
00382      * scheme
00383      */
00384      
00385     if (DEBUG_NCXPARSER) report ("Parsing NCX <META>", REP_DEBUG);
00386     attrvalue = xmlTextReaderGetAttribute (reader, (xmlChar *)"name");
00387     if (attrvalue != NULL)
00388     {
00389         attrcontent = xmlTextReaderGetAttribute (reader, (xmlChar *)"content");
00390         if (attrcontent != NULL)
00391         {   
00392             /* extract the title */
00393             if (!strcmp ((char *) attrvalue, "dc:Title") || !strcmp ((char *) attrvalue, "dtb:sourceTitle"))
00394             {   
00395                 /* if we haven't parsed a title tag */
00396                 if (daisydata->bookInfo->titleText == NULL)
00397                 {
00398                     daisydata->bookInfo->titleText = (char *) attrcontent;
00399                 }
00400             }
00401             /* extract the total time of the book */
00402             else if (!strcmp ((char *) attrvalue, "dtb:totalTime"))
00403             {
00404                 daisydata->bookInfo->totalTime = (char *) attrcontent;
00405             }                       
00406         }
00407     }
00408     ret = xmlTextReaderRead (reader);
00409     return ret; 
00410 }
00411 
00412 
00419 static int parseNcxDocTitle (xmlTextReaderPtr reader, struct DaisyData *daisydata)
00420 {
00421     const xmlChar *name = NULL;
00422     char *nameChar = NULL;
00423     int ret, type;
00424     ret = type = 1; 
00425     
00426     /* Attributes:
00427      * id
00428      */
00429      
00430      daisydata = daisydata;
00431      
00432     if (DEBUG_NCXPARSER) report ("Parsing NCX <DOCTITLE>", REP_DEBUG);
00433     type = xmlTextReaderNodeType(reader);
00434     if (type != 1) 
00435     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <DOCTITLE>", REP_ERROR);
00436         return -1;
00437     }
00438     ret = xmlTextReaderRead (reader);
00439     name = xmlTextReaderConstName (reader);
00440     if (name == NULL) return ret;
00441     nameChar = tolowercase((const char *)name);
00442     if (nameChar == NULL) return 0;
00443      
00444     /* while we haven't reached the end-tag of doctitle */
00445     while (strcmp (nameChar, "doctitle"))
00446     {   
00447         /* parse tag if <text> */
00448         if (!strcmp (nameChar, "text")) ret = parseNcxText (reader, daisydata, NULL, 0);
00449         /* parse tag if <audio> ? */
00450         else if (!strcmp (nameChar, "audio")) ret = parseNcxAudio (reader, daisydata, NULL, 0);
00451         /* parse tag if <img> ? */
00452         else if (!strcmp (nameChar, "img")) ret = parseNcxImg (reader, daisydata, NULL, 0);
00453         if (ret != 1) return ret;
00454         ret = xmlTextReaderRead (reader);
00455         if (ret != 1) return ret;
00456         name = xmlTextReaderConstName (reader);
00457         if(name == NULL) return 0;
00458         nameChar = tolowercase((const char *)name);
00459         if (nameChar == NULL) return 0;
00460     }
00461     type = xmlTextReaderNodeType(reader);
00462     if (type != 15) 
00463     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <DOCTITLE>, endtag expected", REP_ERROR);
00464         return -1;
00465     }   
00466     return ret;
00467 }
00468 
00469 
00478 static int parseNcxText (xmlTextReaderPtr reader, struct DaisyData *daisydata, struct SmilNode *tmpSmilNode, int mode)
00479 {
00480     xmlChar *value = NULL;
00481     char *tempChar = NULL;
00482     int ret = 1;
00483     
00484     /* Attributes:
00485      * id
00486      * class
00487      */
00488      
00489     if (DEBUG_NCXPARSER) report ("Parsing NCX <TEXT>", REP_DEBUG);
00490     
00491     ret = xmlTextReaderRead (reader);
00492     if (ret != 1) return ret;
00493     
00494     /* if the title tag has a value */
00495     if (xmlTextReaderHasValue (reader))
00496     {
00497         value = xmlTextReaderValue (reader);
00498         tempChar = removewhitespaces ((char *)value);
00499         /* storing the books title in the datastucture */
00500         if (mode == 0) { daisydata->bookInfo->titleText = tempChar; tempChar = NULL; }
00501         /* storing a authors name */
00502         else if (mode == 1) { daisydata->bookInfo->author->name = tempChar; tempChar = NULL; }
00503         /* storing a textpassage corresponding to the navLabel (navInfo not fully implemented) */
00504         else if (mode == 2)
00505         {
00506             if (tmpSmilNode != NULL)
00507             tmpSmilNode->textpassage = tempChar; tempChar = NULL;
00508         }
00509     }
00510     if (value != NULL) xmlFree (value); value = NULL;
00511     if (tempChar != NULL) free (tempChar); tempChar = NULL;
00512     ret = xmlTextReaderRead (reader);
00513     return ret;
00514 }
00515 
00516 
00525 static int parseNcxAudio (xmlTextReaderPtr reader, struct DaisyData *daisydata, struct SmilNode *tmpSmilNode, int mode)
00526 {
00527     xmlChar *tmp = NULL;
00528     char *tempChar = NULL;
00529     int ret = 1;
00530     
00531     /* Attributes:
00532      * id
00533      * class
00534      * src
00535      * clipBegin
00536      * clipEnd
00537      */
00538 
00539     if (DEBUG_NCXPARSER) report ("Parsing NCX <AUDIO>", REP_DEBUG); 
00540     /* fetch the url anchor to the audio file */
00541     tmp = xmlTextReaderGetAttribute (reader, (xmlChar *)"src");
00542     if (tmp != NULL)
00543     {
00544         tempChar = (char *) malloc (strlen((char *)tmp)+1);
00545         strcpy(tempChar, (char *)tmp);
00546         if (DEBUG_NCXPARSER)
00547         {
00548             char error[STRLEN];
00549             snprintf (error, sizeof (error), "Parsing NCX <AUDIO> Audiofile: %s", tmp);
00550             report (error, REP_DEBUG);
00551         }
00552         /* storing the audiofile corresponding to the title in the datastucture */
00553         if (mode == 0) { daisydata->bookInfo->titleAudiofilename = tempChar; tempChar = NULL; }
00554         /* storing a audiofile corresponding to the author */
00555         else if (mode == 1) { daisydata->bookInfo->author->audiofilename = tempChar; tempChar = NULL; }
00556         /* storing an audiofile textpassage corresponding to the navLabel (navInfo not fully implemented) */
00557         else if (mode == 2)
00558         {
00559             if (tmpSmilNode != NULL)
00560             tmpSmilNode->audiofilename = tempChar; tempChar = NULL;
00561         }       
00562         if (tempChar != NULL) free (tempChar); tempChar = NULL;
00563         if (tmp != NULL) xmlFree (tmp); tmp = NULL;
00564         
00565         /* fetch clipBegin */
00566         tmp = xmlTextReaderGetAttribute (reader, (xmlChar *)"clipBegin");
00567         if (tmp != NULL)
00568         {
00569             tempChar = getTime((char *)tmp);
00570             if (DEBUG_NCXPARSER)
00571             {
00572                 char error[STRLEN];
00573                 snprintf (error, sizeof (error), "Parsing NCX <AUDIO> clipBegin: %s", tempChar);
00574                 report (error, REP_DEBUG);
00575             }
00576             /* storing the audiofile's startPosition corresponding to the title in the datastucture */
00577             if (mode == 0) { daisydata->bookInfo->titleAudioStartPos = tempChar; tempChar = NULL; }
00578             /* storing a audiofile's startPosition corresponding to the author */
00579             else if (mode == 1) { daisydata->bookInfo->author->audioStartPos = tempChar; tempChar = NULL; }
00580             /* storing the audiofiles's startPosition to the textpassage corresponding to the navLabel (navInfo not fully implemented) */
00581             else if (mode == 2)
00582             {
00583                 if (tmpSmilNode != NULL)
00584                 tmpSmilNode->audioStartPos = tempChar; tempChar = NULL;
00585             }
00586             if (tempChar != NULL) free (tempChar); tempChar = NULL;
00587             if (tmp != NULL) xmlFree (tmp); tmp = NULL;
00588                         
00589             /* fetch clip-end */
00590             tmp = xmlTextReaderGetAttribute (reader, (xmlChar *)"clipEnd");
00591             if (tmp != NULL)
00592             {
00593                 tempChar = getTime((char *)tmp);
00594                 if (DEBUG_NCXPARSER)
00595                 {
00596                     char error[STRLEN];
00597                     snprintf (error, sizeof (error), "Parsing NCXL <AUDIO> clipEnd: %s", tempChar);
00598                     report (error, REP_DEBUG);
00599                 }
00600                 /* storing the audiofile's stopPosition corresponding to the title in the datastucture */
00601                 if (mode == 0) { daisydata->bookInfo->titleAudioStopPos = tempChar; tempChar = NULL; }
00602                 /* storing a audiofile's stopPosition corresponding to the author */
00603                 else if (mode == 1) { daisydata->bookInfo->author->audioStopPos = tempChar; tempChar = NULL; }
00604                 /* storing the audiofiles's stopPosition to the textpassage corresponding to the navLabel (navInfo not fully implemented) */
00605                 else if (mode == 2)
00606                 {
00607                     if (tmpSmilNode != NULL)
00608                     tmpSmilNode->audioStopPos = tempChar; tempChar = NULL;
00609                 }
00610                 if (tempChar != NULL) free (tempChar); tempChar = NULL;
00611                 if (tmp != NULL) xmlFree (tmp); tmp = NULL;
00612             }
00613         }
00614         else
00615         {
00616             ret = 0;
00617         }
00618     }
00619     else
00620     {
00621         ret = 0;    
00622     }
00623     if (tmp != NULL) xmlFree (tmp); tmp = NULL;
00624     return ret;
00625 }
00626 
00627 
00636 static int parseNcxImg (xmlTextReaderPtr reader, struct DaisyData *daisydata, struct SmilNode *tmpSmilNode, int mode)
00637 {
00638     xmlChar *tmp = NULL;
00639     char *tempChar;
00640     int ret = 1;
00641 
00642     /* Attributes:
00643      * id
00644      * class
00645      * src
00646      */
00647 
00648     if (DEBUG_NCXPARSER) report ("Parsing NCX <IMG>", REP_DEBUG);
00649     /* fetch the url anchor to the audio file */
00650     tmp = xmlTextReaderGetAttribute (reader, (xmlChar *)"src");
00651     if (tmp != NULL)
00652     {
00653         tempChar = (char *) malloc (strlen((char *)tmp)+1);
00654         strcpy(tempChar, (char *)tmp);
00655         if (DEBUG_NCXPARSER)
00656         {
00657             char error[STRLEN];
00658             snprintf (error, sizeof (error), "Parsing NCX <IMG> Imagefile: %s", tmp);
00659             report (error, REP_DEBUG);
00660         }       
00661         /* storing the image corresponding to the title in the datastucture */
00662         if (mode == 0) { daisydata->bookInfo->titleImage = tempChar; tempChar = NULL; }
00663         /* storing the image corresponding to the author */
00664         else if (mode == 1) { daisydata->bookInfo->author->image = tempChar; tempChar = NULL; }
00665         /* storing the image to the textpassage corresponding to the navLabel (navInfo not fully implemented) */
00666         else if (mode == 2)
00667         {
00668             if (tmpSmilNode != NULL)
00669             tmpSmilNode->image = tempChar; tempChar = NULL;
00670         }
00671         if (tempChar != NULL) free (tempChar); tempChar = NULL;
00672         if (tmp != NULL) xmlFree (tmp); tmp = NULL;
00673     }
00674     else
00675     {
00676         ret = 0;    
00677     }
00678     if (tmp != NULL) xmlFree (tmp); tmp = NULL;
00679     return ret;
00680 }
00681 
00688 static int parseNcxDocAuthor (xmlTextReaderPtr reader, struct DaisyData *daisydata)
00689 {
00690     const xmlChar *name = NULL;
00691     char *nameChar = NULL;
00692     int ret, type;
00693     ret = type = 1;
00694     
00695     /* Attributes:
00696      * id
00697      */
00698      
00699     if (DEBUG_NCXPARSER) report ("Parsing NCX <DOCAUTHOR>", REP_DEBUG);
00700     type = xmlTextReaderNodeType(reader);
00701     if (type != 1) 
00702     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <DOCAUTHOR>", REP_ERROR);
00703         return -1;
00704     }
00705     ret = xmlTextReaderRead (reader);
00706     if (ret != 1) return ret;
00707     name = xmlTextReaderConstName (reader);
00708     if (name == NULL) return ret;
00709     nameChar = tolowercase((const char *)name);
00710     if (nameChar == NULL) return 0;
00711     (void) addNewAuthor (daisydata);
00712     /* while the current node is text or audio or img */
00713     while (strcmp (nameChar, "docauthor"))
00714     {   
00715         /* parse tag if <text> */
00716         if (!strcmp (nameChar, "text")) ret = parseNcxText (reader, daisydata, NULL, 1);
00717         /* parse tag if <audio> ? */
00718         else if (!strcmp (nameChar, "audio")) ret = parseNcxAudio (reader, daisydata, NULL, 1);
00719         /* parse tag if <img> ? */
00720         else if (!strcmp (nameChar, "img")) ret = parseNcxImg (reader, daisydata, NULL, 1);
00721         if (ret != 1) return ret;
00722         ret = xmlTextReaderRead (reader);
00723         if (ret != 1) return ret;
00724         name = xmlTextReaderConstName (reader);
00725         if(name == NULL) return 0;
00726         nameChar = tolowercase((const char *)name);
00727         if (nameChar == NULL) return 0;
00728     }
00729     type = xmlTextReaderNodeType(reader);
00730     if (type != 15) 
00731     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <DOCAUTHOR>, endtag expected", REP_ERROR);
00732         return -1;
00733     }   
00734     return ret;
00735 }
00736 
00737 
00744 static int parseNcxNavMap (xmlTextReaderPtr reader, struct DaisyData *daisydata)
00745 {
00746     const xmlChar *name;
00747     char *nameChar = NULL;
00748     int ret, type;
00749     ret = type = 1;
00750     
00751     name = NULL;
00752     /* Attributes:
00753      * 
00754      */
00755     if (DEBUG_NCXPARSER) report ("Parsing NCX <NAVMAP>", REP_DEBUG);
00756     type = xmlTextReaderNodeType(reader);
00757     if (type != 1) 
00758     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <NAVMAP>", REP_ERROR);
00759         return -1;
00760     }
00761     ret = xmlTextReaderRead (reader);
00762     if (ret != 1) return ret;
00763     name = xmlTextReaderConstName (reader);
00764     if (name == NULL) return ret;
00765     nameChar = tolowercase((const char *)name);
00766     if (nameChar == NULL) return 0;
00767     
00768     /* while we haven't reached the end-tag of navMap */
00769     while (strcmp (nameChar, "navmap"))
00770     {   
00771         /* parse tag if <navInfo> * */
00772         if (!strcmp (nameChar, "navinfo")) ret = parseNcxNavInfo (reader, daisydata);
00773         /* parse tag if <navLabel> * */
00774         else if (!strcmp (nameChar, "navlabel")) ret = parseNcxNavLabel (reader, daisydata, NULL);
00775         /* parse tag if <navPoint> + */
00776         else if (!strcmp (nameChar, "navpoint")) ret = parseNcxNavPoint (reader, daisydata);
00777         if (ret != 1) return ret;
00778         ret = xmlTextReaderRead (reader);
00779         if (ret != 1) return ret;
00780         name = xmlTextReaderConstName (reader);
00781         if(name == NULL) return 0;
00782         nameChar = tolowercase((const char *)name);
00783         if (nameChar == NULL) return 0;
00784     }
00785     type = xmlTextReaderNodeType(reader);
00786     if (type != 15) 
00787     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <NAVMAP>, endtag expected", REP_ERROR);
00788         return -1;
00789     }   
00790     return ret;
00791 }
00792 
00799 static int parseNcxPageList (xmlTextReaderPtr reader, struct DaisyData *daisydata)
00800 {
00801     const xmlChar *name = NULL;
00802     char *nameChar = NULL;
00803     int ret, type;
00804     ret = type = 1;
00805     
00806     /* Attributes:
00807      * id
00808      * class
00809      */
00810      
00811     if (DEBUG_NCXPARSER) report ("Parsing NCX <PAGELIST>", REP_DEBUG);
00812     type = xmlTextReaderNodeType(reader);
00813     if (type != 1) 
00814     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <PAGELIST>", REP_ERROR);
00815         return -1;
00816     }
00817     ret = xmlTextReaderRead (reader);
00818     if (ret != 1) return ret;
00819     name = xmlTextReaderConstName (reader);
00820     if (name == NULL) return ret;
00821     nameChar = tolowercase((const char *)name);
00822     if (nameChar == NULL) return 0;
00823     
00824     /* while we haven't come to the end-tag of pageList */
00825     while (strcmp (nameChar, "pagelist"))
00826     {   
00827         /* parse tag if <navInfo> * */
00828         if (!strcmp (nameChar, "navinfo")) ret = parseNcxNavInfo (reader, daisydata);
00829         /* parse tag if <navLabel> * */
00830         else if (!strcmp (nameChar, "navlabel")) ret = parseNcxNavLabel (reader,daisydata, NULL);
00831         /* parse tag if <pageTarget> + */
00832         else if (!strcmp (nameChar, "pagetarget")) ret = parseNcxPageTarget (reader, daisydata);
00833         if (ret != 1) return ret;
00834         ret = xmlTextReaderRead (reader);
00835         if (ret != 1) return ret;
00836         name = xmlTextReaderConstName (reader);
00837         if(name == NULL) return 0;
00838         nameChar = tolowercase((const char *)name);
00839         if (nameChar == NULL) return 0;
00840     }
00841     type = xmlTextReaderNodeType(reader);
00842     if (type != 15) 
00843     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <PAGELIST>, endtag expected", REP_ERROR);
00844         return -1;
00845     }   
00846     return ret;
00847 }
00848 
00855 static int parseNcxNavList (xmlTextReaderPtr reader, struct DaisyData *daisydata)
00856 {
00857     const xmlChar *name = NULL;
00858     char *nameChar = NULL;
00859     int ret, type;
00860     ret = type = 1;
00861 
00862     /* Attributes:
00863      * id
00864      * class
00865      */
00866      
00867     if (DEBUG_NCXPARSER) report ("Parsing NCX <NAVLIST>", REP_DEBUG);
00868     type = xmlTextReaderNodeType(reader);
00869     if (type != 1) 
00870     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <NAVLIST>", REP_ERROR);
00871         return -1;
00872     }
00873     ret = xmlTextReaderRead (reader);
00874     if (ret != 1) return ret;
00875     name = xmlTextReaderConstName (reader);
00876     if (name == NULL) return ret;
00877     nameChar = tolowercase((const char *)name);
00878     if (nameChar == NULL) return 0;
00879     
00880     /* while we haven't come to the end-tag of navList */
00881     while (strcmp (nameChar, "navlist"))
00882     {   
00883         /* parse tag if <navInfo> * */
00884         if (!strcmp (nameChar, "navinfo")) ret = parseNcxNavInfo (reader, daisydata);
00885         /* parse tag if <navLabel> + */
00886         else if (!strcmp (nameChar, "navlabel")) ret = parseNcxNavLabel (reader, daisydata, NULL);
00887         /* parse tag if <navTarget> + */
00888         else if (!strcmp (nameChar, "navtarget")) ret = parseNcxNavTarget (reader, daisydata);
00889         if (ret != 1) return ret;
00890         ret = xmlTextReaderRead (reader);
00891         if (ret != 1) return ret;
00892         name = xmlTextReaderConstName (reader);
00893         if(name == NULL) return 0;
00894         nameChar = tolowercase((const char *)name);
00895         if (nameChar == NULL) return 0;
00896     }   
00897     type = xmlTextReaderNodeType(reader);
00898     if (type != 15) 
00899     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <NAVLIST>, endtag expected", REP_ERROR);
00900         return -1;
00901     }   
00902     return ret;
00903 }
00904 
00905 
00912 static int parseNcxNavInfo (xmlTextReaderPtr reader, struct DaisyData *daisydata)
00913 {   
00914     const xmlChar *name = NULL;
00915     char *nameChar = NULL;
00916     int ret, type;
00917     ret = type = 1;
00918     
00919     /* Attributes:
00920      * xml:lang
00921      * dir
00922      */
00923      
00924     if (DEBUG_NCXPARSER) report ("Parsing NCX <NAVINFO>", REP_DEBUG);
00925     type = xmlTextReaderNodeType(reader);
00926     if (type != 1) 
00927     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <NAVINFO>", REP_ERROR);
00928         return -1;
00929     }
00930     ret = xmlTextReaderRead (reader);
00931     if (ret != 1) return ret;
00932     name = xmlTextReaderConstName (reader);
00933     if (name == NULL) return ret;
00934     nameChar = tolowercase((const char *)name);
00935     if (nameChar == NULL) return 0;
00936     
00937     /* while we haven't reached the end-tag of navInfo */
00938     while (strcmp (nameChar, "navinfo"))
00939     {   
00940         /* parse tag if <text> */
00941         if (!strcmp (nameChar, "text")) ret = parseNcxText (reader, daisydata, NULL, 2);
00942         /* parse tag if <audio> (if <text> then <audio> ?) */
00943         else if (!strcmp (nameChar, "audio")) ret = parseNcxAudio (reader, daisydata, NULL, 2);
00944         /* parse tag if <img> ? */
00945         else if (!strcmp (nameChar, "img")) ret = parseNcxImg (reader, daisydata, NULL, 2);
00946         if (ret != 1) return ret;
00947         ret = xmlTextReaderRead (reader);
00948         if (ret != 1) return ret;
00949         name = xmlTextReaderConstName (reader);
00950         if(name == NULL) return 0;
00951         nameChar = tolowercase((const char *)name);
00952         if (nameChar == NULL) return 0;
00953     }
00954     type = xmlTextReaderNodeType(reader);
00955     if (type != 15) 
00956     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <NAVINFO>, endtag expected", REP_ERROR);
00957         return -1;
00958     }   
00959     return ret;
00960 }
00961 
00962 
00970 static int parseNcxNavLabel (xmlTextReaderPtr reader, struct DaisyData *daisydata, struct SmilNode *tmpSmilNode)
00971 {
00972     const xmlChar *name = NULL;
00973     char *nameChar = NULL;
00974     int ret, type;
00975     ret = type = 1;
00976 
00977     /* Attributes:
00978      * xml:lang
00979      * dir
00980      */
00981     
00982     if (DEBUG_NCXPARSER) report ("Parsing NCX <NAVLABEL>", REP_DEBUG);
00983     type = xmlTextReaderNodeType(reader);
00984     if (type != 1) 
00985     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <NAVLABEL>", REP_ERROR);
00986         return -1;
00987     }
00988     ret = xmlTextReaderRead (reader);
00989     if (ret != 1) return ret;
00990     name = xmlTextReaderConstName (reader);
00991     if (name == NULL) return 0;
00992     nameChar = tolowercase((const char *)name);
00993     if (nameChar == NULL) return 0;
00994     
00995     /* while we haven't reached the end-tag of navLabel */
00996     while (strcmp (nameChar, "navlabel"))
00997     {   
00998         /* parse tag if <text> */
00999         if (!strcmp (nameChar, "text")) ret = parseNcxText (reader, daisydata, tmpSmilNode, 2);
01000         /* parse tag if <audio> (if <text> then <audio> ?) */
01001         else if (!strcmp (nameChar, "audio")) ret = parseNcxAudio (reader, daisydata, tmpSmilNode, 2);
01002         /* parse tag if <img> ? */
01003         else if (!strcmp (nameChar, "img")) ret = parseNcxImg (reader, daisydata, tmpSmilNode, 2);
01004         if (ret != 1) return ret;
01005         ret = xmlTextReaderRead (reader);
01006         if (ret != 1) return ret;
01007         name = xmlTextReaderConstName (reader);
01008         if (name == NULL) return 0;
01009         nameChar = tolowercase((const char *)name);
01010         if (nameChar == NULL) return 0;
01011     }
01012     type = xmlTextReaderNodeType(reader);
01013     if (type != 15) 
01014     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <NAVLABEL>, endtag expected", REP_ERROR);
01015         return -1;
01016     }   
01017     return ret;
01018 }
01019 
01020 
01027 static int parseNcxNavPoint (xmlTextReaderPtr reader,struct DaisyData *daisydata)
01028 {
01029     const xmlChar *name = NULL;
01030     char *nameChar = NULL;
01031     int ret = 1, type = 1;
01032     struct SmilNode *tmpSmilNode = NULL;
01033     
01034     /* Attributes:
01035      * id
01036      * class
01037      * playOrder
01038      */
01039      
01040     if (DEBUG_NCXPARSER) report ("Parsing NCX <NAVPOINT>", REP_DEBUG);
01041     type = xmlTextReaderNodeType(reader);
01042     if (type != 1) 
01043     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <NAVPOINT>", REP_ERROR);
01044         return -1;
01045     }
01046     ret = xmlTextReaderRead (reader);
01047     if (ret != 1) return ret;
01048     name = xmlTextReaderConstName (reader);
01049     if (name == NULL) return ret;
01050     nameChar = tolowercase((const char *)name);
01051     if (nameChar == NULL) return 0;
01052     
01053     /* make a new node in the linked list */
01054     tmpSmilNode = addNewSmilNode(daisydata);
01055     
01056     /* while we haven't reached the end-tag of navPoint */
01057     while (type != 15 && strcmp (nameChar, "navpoint"))
01058     {   
01059         /* parse tag if <navLabel> + */
01060         if (!strcmp (nameChar, "navlabel")) ret = parseNcxNavLabel (reader, daisydata, tmpSmilNode);
01061         /* parse tag if <content> */
01062         else if (!strcmp (nameChar, "content")) ret = parseNcxContent (reader, tmpSmilNode);
01063         /* parse tag if <navPoint> * */
01064         else if (!strcmp (nameChar, "navpoint")) ret = parseNcxNavPoint (reader, daisydata);
01065         if (ret != 1) return ret;
01066         ret = xmlTextReaderRead (reader);
01067         if (ret != 1) return ret;
01068         name = xmlTextReaderConstName (reader);
01069         if (name == NULL) return 0;
01070         nameChar = tolowercase((const char *)name);
01071         if (nameChar == NULL) return 0;
01072         type = xmlTextReaderNodeType(reader);
01073     }   
01074     return ret;
01075 }
01076 
01077 
01084 static int parseNcxContent (xmlTextReaderPtr reader, struct SmilNode *tmpSmilNode)
01085 {
01086     xmlChar *smilsrc = NULL;
01087     char *tempChar = NULL, *tmp = NULL;
01088     int ret = 1;
01089 
01090     /* Attributes:
01091      * id
01092      * src
01093      */
01094 
01095     if (DEBUG_NCXPARSER) report ("Parsing NCX <NAVCONTENT>", REP_DEBUG);
01096     /* gets the href attribute. */
01097     smilsrc = xmlTextReaderGetAttribute (reader, (xmlChar *)"src");
01098     if (smilsrc != NULL)
01099     {
01100         /* splits the text on #. */
01101         tmp = strtok ((char *)smilsrc, "#");
01102         tempChar = (char *) malloc (strlen (tmp)+1);
01103         strcpy(tempChar, tmp);
01104         
01105         if (DEBUG_NCXPARSER)
01106         { 
01107             char error[STRLEN];
01108             snprintf (error, sizeof (error), "Parsing NCX <CONTENT> File anchor: %s", tmp);
01109             report (error, REP_DEBUG);
01110         }
01111         
01112         /* put the anchor in the node */
01113         if (tmpSmilNode != NULL)
01114         tmpSmilNode->anchor = tempChar; tempChar = NULL;
01115         
01116         /* get the fragment identifier */
01117         tmp = strtok (NULL, "#");
01118         tempChar = (char *) malloc (strlen (tmp)+1);
01119         strcpy(tempChar, tmp);
01120         
01121         if (DEBUG_NCXPARSER)
01122         { 
01123             char error[STRLEN];
01124             snprintf (error, sizeof (error), "Parsing NCX <CONTENT> Fragment Identifier: %s", tmp);
01125             report (error, REP_DEBUG);
01126         }
01127         
01128         /* put the fragment identifier in the node */
01129         if (tmpSmilNode != NULL)
01130         tmpSmilNode->fragmentIdentifier = tempChar; tempChar = NULL;
01131         
01132         ret = xmlTextReaderRead (reader);
01133         if (ret != 1) return ret;
01134         if (smilsrc != NULL) xmlFree(smilsrc); smilsrc = NULL;
01135     }
01136     else
01137     {
01138         ret = 0;
01139     }
01140     return ret;
01141 }
01142 
01149 static int parseNcxPageTarget (xmlTextReaderPtr reader, struct DaisyData *daisydata)
01150 {
01151     const xmlChar *name = NULL;
01152     char *nameChar = NULL;
01153     int ret, type;
01154     ret = type = 1;
01155     
01156     /* Attributes:
01157      * id
01158      * value
01159      * type
01160      * class
01161      * playOrder
01162      */
01163     
01164     if (DEBUG_NCXPARSER) report ("Parsing NCX <PAGETARGET>", REP_DEBUG);
01165     type = xmlTextReaderNodeType(reader);
01166     if (type != 1) 
01167     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <PAGETARGET>", REP_ERROR);
01168         return -1;
01169     }
01170     ret = xmlTextReaderRead (reader);
01171     if (ret != 1) return ret;
01172     name = xmlTextReaderConstName (reader);
01173     if (name == NULL) return ret;
01174     nameChar = tolowercase((const char *)name);
01175     if (nameChar == NULL) return 0;
01176     
01177     /* while we haven't come to the end tag of pageTarget */
01178     while (!strcmp (nameChar, "pagetarget"))
01179     {   
01180         /* parse tag if <navLabel> + */
01181         if (!strcmp (nameChar, "navlabel")) ret = parseNcxNavLabel (reader, daisydata, NULL);
01182         /* parse tag if <content> */
01183         else if (!strcmp (nameChar, "content")) ret = parseNcxContent (reader, NULL);
01184         if (ret != 1) return ret;
01185         ret = xmlTextReaderRead (reader);
01186         if (ret != 1) return ret;
01187         name = xmlTextReaderConstName (reader);
01188         if (name == NULL) return 0;
01189         nameChar = tolowercase((const char *)name);
01190         if (nameChar == NULL) return 0;
01191     }
01192     type = xmlTextReaderNodeType(reader);
01193     if (type != 15) 
01194     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <PAGETARGET>, endtag expected", REP_ERROR);
01195         return -1;
01196     }   
01197     return ret;
01198 }
01199 
01200 
01207 static int parseNcxNavTarget (xmlTextReaderPtr reader, struct DaisyData *daisydata)
01208 {
01209     const xmlChar *name = NULL;
01210     char *nameChar = NULL;
01211     int ret, type;
01212     ret = type = 1;
01213     
01214     /* Attributes:
01215      * id
01216      * value
01217      * class
01218      * playOrder
01219      */
01220     
01221     if (DEBUG_NCXPARSER) report ("Parsing NCX <NAVTARGET>", REP_DEBUG);
01222     type = xmlTextReaderNodeType(reader);
01223     if (type != 1) 
01224     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <NAVTARGET>", REP_ERROR);
01225         return -1;
01226     }
01227     ret = xmlTextReaderRead (reader);
01228     if (ret != 1) return ret;
01229     name = xmlTextReaderConstName (reader);
01230     if (name == NULL) return 0;
01231     nameChar = tolowercase((const char *)name);
01232     if (nameChar == NULL) return 0;
01233 
01234     /* while we haven't come to the end tag of navTarget */
01235     while (strcmp (nameChar, "navtarget"))
01236     {
01237         /* parse tag if <navLabel> + */
01238         if (!strcmp (nameChar, "navlabel")) ret = parseNcxNavLabel (reader, daisydata, NULL);
01239         /* parse tag if <content> */
01240         else if (!strcmp (nameChar, "content")) ret = parseNcxContent (reader, NULL);
01241         if (ret != 1) return ret;
01242         ret = xmlTextReaderRead (reader);
01243         if (ret != 1) return ret;
01244         name = xmlTextReaderConstName (reader);
01245         if (name == NULL) return 0;
01246         nameChar = tolowercase((const char *)name);
01247         if (nameChar == NULL) return 0;
01248     }
01249     type = xmlTextReaderNodeType(reader);
01250     if (type != 15) 
01251     {   if (DEBUG_NCXPARSER) report ("Failed parsing NCX <NAVTARGET>, endtag expected", REP_ERROR);
01252         return -1;
01253     }       
01254     return ret;
01255 }
01256 
01257 
01258 #endif

Generated on Tue Sep 5 12:14:07 2006 for libdaisy by doxygen1.2.15