Main Page   Data Structures   File List   Data Fields   Globals  

ncxsmilparser.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 smilparser.c
00024  * \author André Lindhjem, Kjetil Holien, Terje Risa & Øyvind Nerbråten
00025  * \date 23.02.2006
00026  * 
00027  * A parser for the SMIL 2.0 standard used with Daisy Z39.86-2005 books. 
00028  * Collects data and stores it in linked list in the main datastructure. The linked
00029  * list represents the spine of all passages in a smil file.
00030  */
00031  
00032 
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <libxml2/libxml/xmlreader.h>
00037 
00038 #include "ncxsmilparser.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  
00058 static int parseSmilHead (xmlTextReaderPtr reader);
00059 
00065 static int parseSmilLayout (xmlTextReaderPtr reader);
00066 
00072 static int parseSmilMeta (xmlTextReaderPtr reader);
00073 
00079 static int parseSmilRegion (xmlTextReaderPtr reader);
00080 
00086 static int parseSmilCustomAttributes (xmlTextReaderPtr reader);
00087 
00093 static int parseSmilCustomTest (xmlTextReaderPtr reader);
00094 
00101 static int parseSmilBody (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00102 
00109 static int parseSmilSeq (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00110 
00117 static int parseSmilPar (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00118 
00126 static int parseSmilText (xmlTextReaderPtr reader, struct DaisyData *daisydata, struct Node *tmpNode);
00127 
00134 static int parseSmilAudio (xmlTextReaderPtr reader, struct Node *tmpNode);
00135 
00142 static int parseSmilImg (xmlTextReaderPtr reader, struct Node *tmpNode);
00143 
00150 static int parseSmilA (xmlTextReaderPtr reader, struct DaisyData *daisydata);
00151 
00160 static int parseXML (struct DaisyData *daisydata, char* filename, char* fragment, struct Node *tmpNode);
00161 
00167 static int parseXMLHead (xmlTextReaderPtr reader);
00168 
00174 static int parseXMLTitle (xmlTextReaderPtr reader);
00175 
00181 static int parseXMLMeta (xmlTextReaderPtr reader);
00182 
00188 static int parseXMLFrontmatter (xmlTextReaderPtr reader);
00189 
00196 static int parseXMLBodymatter (xmlTextReaderPtr reader, char* fragment, struct Node *tmpNode);
00197 
00203 static int parseXMLRearmatter (xmlTextReaderPtr reader);
00204 
00210 static int parseXMLDoctitle (xmlTextReaderPtr reader);
00211 
00217 static int parseXMLDocauthor (xmlTextReaderPtr reader);
00218 
00224 static int parseXMLLevel (xmlTextReaderPtr reader);
00225 
00233 static int parseXMLLevel1 (xmlTextReaderPtr reader, char* fragment, struct Node *tmpNode);
00234 
00242 static int parseXMLBook (xmlTextReaderPtr reader, char *fragment, struct Node *tmpNode);
00243 
00249 static int parseXMLCovertitle (xmlTextReaderPtr reader);
00250 /*
00251 static int parseXMLPagenum (xmlTextReaderPtr reader);
00252 static int parseXMLH1 (xmlTextReaderPtr reader);
00253 static int parseXMLLevel2 (xmlTextReaderPtr reader);
00254 static int parseXMLBridgehead (xmlTextReaderPtr reader);
00255 static int parseXMLP (xmlTextReaderPtr reader);
00256 static int parseXMLList (xmlTextReaderPtr reader);
00257 static int parseXMLDl (xmlTextReaderPtr reader);
00258 static int parseXMLDiv (xmlTextReaderPtr reader);
00259 static int parseXMLBlockquote (xmlTextReaderPtr reader);
00260 static int parseXMLImg (xmlTextReaderPtr reader);
00261 static int parseXMLImggroup (xmlTextReaderPtr reader);
00262 static int parseXMLPoem (xmlTextReaderPtr reader);
00263 static int parseXMLLinegroup (xmlTextReaderPtr reader);
00264 static int parseXMLByline (xmlTextReaderPtr reader);
00265 static int parseXMLDateline (xmlTextReaderPtr reader);
00266 static int parseXMLEpigraph (xmlTextReaderPtr reader);
00267 static int parseXMLTable (xmlTextReaderPtr reader);
00268 static int parseXMLAddress (xmlTextReaderPtr reader);
00269 static int parseXMLLine (xmlTextReaderPtr reader);
00270 static int parseXMLAuthor (xmlTextReaderPtr reader);
00271 static int parseXMLProdnote (xmlTextReaderPtr reader);
00272 static int parseXMLSidebar (xmlTextReaderPtr reader);
00273 static int parseXMLNote (xmlTextReaderPtr reader);
00274 static int parseXMLA (xmlTextReaderPtr reader);
00275 static int parseXMLCite (xmlTextReaderPtr reader);
00276 static int parseXMLSamp (xmlTextReaderPtr reader);
00277 static int parseXMLKbd (xmlTextReaderPtr reader);
00278 static char* parseXMLHtmltag (xmlTextReaderPtr reader);
00279 */
00280 
00281 /************************ Code for parsing a *.smil files ************************/
00282 
00283 
00284 /* ************************************************** *
00285  *          Global function definitions               *
00286  * ************************************************** */
00287 
00296 int parseNCXSmil (struct DaisyData *daisydata)
00297 {   
00298     const xmlChar *name = NULL;
00299     char *nameChar = NULL;
00300     xmlTextReaderPtr reader = NULL;
00301     int ret = 1, type = 1;
00302     char tmp[STRLEN];
00303     daisydata->smilAlreadyfound = 0;
00304     
00305     snprintf (tmp, sizeof (tmp), "%s%s",daisydata->path,daisydata->smilPos->anchor); 
00306     /* open the smil file */
00307     reader = xmlReaderForFile (tmp, NULL, 0);
00308     if (reader == NULL)
00309     {
00310         char error[STRLEN];
00311         snprintf (error, sizeof (error), "Failed to open SMIL file: %s", daisydata->smilPos->anchor);
00312         report (error, REP_WARNING);
00313         return -1;
00314     }
00315     else 
00316     {
00317         if (DEBUG_NCXSMILPARSER)
00318         {
00319             char error[STRLEN];
00320             snprintf (error, sizeof (error), "Parsing SMIL file: %s", daisydata->smilPos->anchor);
00321             report (error, REP_DEBUG);
00322         }
00323             
00324         /* read the first tag */
00325         ret = xmlTextReaderRead(reader);
00326         if (ret != 1)
00327         {
00328             char error[STRLEN];
00329             snprintf (error, sizeof (error), "Failed to parse SMIL file: %s", daisydata->smilPos->anchor);
00330             report (error, REP_WARNING);
00331             return -1;
00332         }
00333         name = xmlTextReaderConstName (reader);
00334         if (name == NULL) return 0;
00335         nameChar = tolowercase((const char *)name);
00336         if (nameChar == NULL) return 0;
00337         if (!strcmp (nameChar, "smil")) 
00338         {
00339             ret = xmlTextReaderRead(reader);
00340             if (ret != 1)
00341             {
00342                 char error[STRLEN];
00343                 snprintf (error, sizeof (error), "Failed to parse SMIL file: %s", daisydata->smilPos->anchor);
00344                 report (error, REP_WARNING);
00345                 return -1;
00346             }
00347             name = xmlTextReaderConstName (reader);
00348             if (name == NULL) return 0;
00349             nameChar = tolowercase((const char *)name);
00350             if (nameChar == NULL) return 0;
00351             type = xmlTextReaderNodeType(reader);
00352         }
00353         /* while we haven't reached the smil endtag */
00354         while (type != 15 && strcmp (nameChar, "smil"))
00355         {
00356             /* Attributes:
00357              * id
00358              * class
00359              * title
00360              * xmlns
00361              * xml:lang
00362              */
00363              
00364             /* parse tag if <head> */
00365             if (!strcmp (nameChar, "head"))
00366             { 
00367                 ret = parseSmilHead (reader);
00368                 if (ret != 1)
00369                 {
00370                     char error[STRLEN];
00371                     snprintf (error, sizeof (error), "Failed to parse SMIL file: %s", daisydata->smilPos->anchor);
00372                     report (error, REP_WARNING);
00373                     return -1;
00374                 }               
00375             }
00376             /* parse tag if <body> */
00377             else if (!strcmp (nameChar, "body"))
00378             { 
00379                 ret = parseSmilBody (reader, daisydata);
00380                 if (ret != 1)
00381                 {
00382                     char error[STRLEN];
00383                     snprintf (error, sizeof (error), "Failed to parse SMIL file: %s", daisydata->smilPos->anchor);
00384                     report (error, REP_WARNING);
00385                     return -1;
00386                 }               
00387             }
00388             ret = xmlTextReaderRead (reader);
00389             if (ret != 1) return ret;
00390             name = xmlTextReaderConstName (reader);
00391             if (name == NULL) return 0;
00392             nameChar = tolowercase((const char *)name);
00393             if (nameChar == NULL) return 0; 
00394             type = xmlTextReaderNodeType(reader);
00395         }
00396         xmlFreeTextReader (reader);
00397         if (ret != 1)
00398         {
00399             char error[STRLEN];
00400             snprintf (error, sizeof (error), "Failed to parse SMIL file: %s", daisydata->smilPos->anchor);
00401             report (error, REP_WARNING);
00402             return -1;
00403         }
00404     }
00405     return ret;
00406 }
00407 
00408 
00409 /* ************************************************** *
00410  *          Static function definitions               *
00411  * ************************************************** */
00412 
00413 
00419 static int parseSmilHead(xmlTextReaderPtr reader)
00420 {
00421     const xmlChar *name = NULL;
00422     char *nameChar = NULL;
00423     int ret, type;
00424     ret = type = 1;
00425     
00426     /* Attributes:
00427      * id
00428      * class
00429      * title
00430      * xml:lang
00431      */
00432     
00433     if (DEBUG_NCXSMILPARSER) report ("Parsing SMIL <HEAD>", REP_DEBUG);
00434     type = xmlTextReaderNodeType(reader);
00435     if (type != 1) 
00436     {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <HEAD>", REP_ERROR);
00437         return -1;
00438     }
00439     ret = xmlTextReaderRead (reader);
00440     if (ret != 1) return ret;
00441     name = xmlTextReaderConstName (reader);
00442     if (name == NULL) return 0;
00443     nameChar = tolowercase((const char *)name);
00444     if (nameChar == NULL) return 0;
00445     
00446     /* while we haven't reached the head-close tag */
00447     while (strcmp (nameChar, "head"))
00448     {   
00449         /* parse tag if <layout> ? */
00450         if (!strcmp (nameChar, "layout")) ret = parseSmilLayout (reader);
00451         /* parse tag if <meta> * */
00452         else if (!strcmp (nameChar, "meta")) ret = parseSmilMeta (reader);
00453         /* parse tag if <customAttributes> ? */
00454         else if (!strcmp (nameChar, "customAttributes")) ret = parseSmilCustomAttributes (reader);
00455         if (ret != 1) return ret;
00456         ret = xmlTextReaderRead (reader);
00457         if (ret != 1) return ret;
00458         name = xmlTextReaderConstName (reader);
00459         if (name == NULL) return 0;
00460         nameChar = tolowercase((const char *)name);
00461         if (nameChar == NULL) return 0;
00462     }
00463     type = xmlTextReaderNodeType(reader);
00464     if (type != 15) 
00465     {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <HEAD>, endtag expected", REP_ERROR);
00466         return -1;
00467     }       
00468     return ret;
00469 }
00470 
00476 static int parseSmilLayout (xmlTextReaderPtr reader)
00477 {
00478     const xmlChar *name = NULL;
00479     char *nameChar = NULL;
00480     int ret, type;
00481     ret = type = 1;
00482     
00483     /* Attributes:
00484      * id
00485      * class
00486      * title
00487      * xml:lang
00488      */
00489     if (DEBUG_NCXSMILPARSER) report ("Parsing SMIL <LAYOUT>", REP_DEBUG);
00490     type = xmlTextReaderNodeType(reader);
00491     if (type != 1) 
00492     {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <LAYOUT>", REP_ERROR);
00493         return -1;
00494     }
00495     ret = xmlTextReaderRead (reader);
00496     if (ret != 1) return ret;
00497     name = xmlTextReaderConstName (reader);
00498     if (name == NULL) return 0;
00499     nameChar = tolowercase((const char *)name);
00500     if (nameChar == NULL) return 0;
00501     
00502     /* while we haven't reached the layout-close tag */
00503     while (strcmp (nameChar, "layout"))
00504     {   
00505         /* parse tag if <region> + */
00506         if (!strcmp (nameChar, "region")) ret = parseSmilRegion (reader);       
00507         if (ret != 1) return ret;
00508         ret = xmlTextReaderRead (reader);
00509         if (ret != 1) return ret;
00510         name = xmlTextReaderConstName (reader);
00511         if (name == NULL) return 0;
00512         nameChar = tolowercase((const char *)name);
00513         if (nameChar == NULL) return 0;
00514     }   
00515     type = xmlTextReaderNodeType(reader);
00516     if (type != 15) 
00517     {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <LAYOUT>, endtag expected", REP_ERROR);
00518         return -1;
00519     }
00520     return ret;
00521 }
00522 
00528 static int parseSmilMeta (xmlTextReaderPtr reader)
00529 {
00530     int ret = 1;
00531     
00532     /* Attributes:
00533      * content
00534      * name
00535      */
00536     if (DEBUG_NCXSMILPARSER) report ("Parsing SMIL <META>", REP_DEBUG);
00537     ret = xmlTextReaderRead (reader);
00538     return ret; 
00539 }
00540 
00546 static int parseSmilRegion (xmlTextReaderPtr reader)
00547 {
00548     int ret = 1;
00549     
00550     /* Attributes:
00551      * id
00552      * bottom
00553      * left
00554      * right
00555      * top
00556      * height
00557      * width
00558      * fit
00559      * backgroundColor
00560      * showBackground
00561      * z-index
00562      * xml:lang
00563      */
00564     if (DEBUG_NCXSMILPARSER) report ("Parsing SMIL <REGION>", REP_DEBUG);
00565     ret = xmlTextReaderRead (reader);
00566     return ret; 
00567 }
00568 
00569 
00575 static int parseSmilCustomAttributes (xmlTextReaderPtr reader)
00576 {   
00577     const xmlChar *name = NULL;
00578     char *nameChar = NULL;
00579     int ret, type;
00580     ret = type = 1;
00581     
00582     /* Attributes:
00583      * id
00584      * class
00585      * title
00586      * xml:lang
00587      */
00588      
00589     if (DEBUG_NCXSMILPARSER) report ("Parsing SMIL <CUSTOMATTRIBUTES>", REP_DEBUG);
00590     type = xmlTextReaderNodeType(reader);
00591     if (type != 1) 
00592     {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <CUSTOMATTRIBUTES>", REP_ERROR);
00593         return -1;
00594     }
00595     ret = xmlTextReaderRead (reader);
00596     if (ret != 1) return ret;
00597     name = xmlTextReaderConstName (reader);
00598     if (name == NULL) return 0;
00599     nameChar = tolowercase((const char *)name);
00600     if (nameChar == NULL) return 0;
00601     
00602     /* while we haven't reached the customattributes-close tag */
00603     while (strcmp (nameChar, "customAttributes"))
00604     {   
00605         /* parse tag if <customTest> + */
00606         if (!strcmp (nameChar, "customTest")) ret = parseSmilCustomTest (reader);
00607         if (ret != 1) return ret;
00608         ret = xmlTextReaderRead (reader);
00609         if (ret != 1) return ret;
00610         name = xmlTextReaderConstName (reader);
00611         if (name == NULL) return 0;
00612         nameChar = tolowercase((const char *)name);
00613         if (nameChar == NULL) return 0;
00614     }
00615     type = xmlTextReaderNodeType(reader);
00616     if (type != 15) 
00617     {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <CUSTOMATTRIBUTES>, endtag expected", REP_ERROR);
00618         return -1;
00619     }   
00620     return ret;
00621 }
00622 
00623 
00629 static int parseSmilCustomTest (xmlTextReaderPtr reader)
00630 {   
00631     int ret;
00632     /* Attributes:
00633      * id
00634      * class
00635      * title
00636      * xml:lang
00637      * defaultState
00638      * override
00639      */
00640 
00641     if (DEBUG_NCXSMILPARSER) report ("Parsing SMIL <CUSTOMTEST>", REP_DEBUG);
00642     ret = xmlTextReaderRead (reader);
00643     return ret; 
00644 }
00645 
00646 
00653 static int parseSmilBody (xmlTextReaderPtr reader, struct DaisyData *daisydata)
00654 {
00655     const xmlChar *name = NULL;
00656     char *nameChar = NULL;
00657     int ret, type;
00658     ret = type = 1;
00659 
00660     /* Attributes:
00661      * id
00662      * class
00663      * title
00664      * xml:lang
00665      */
00666 
00667     if (DEBUG_NCXSMILPARSER) report ("Parsing SMIL <BODY>", REP_DEBUG);
00668     type = xmlTextReaderNodeType(reader);
00669     if (type != 1) 
00670     {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <BODY>", REP_ERROR);
00671         return -1;
00672     }
00673     ret = xmlTextReaderRead (reader);
00674     if (ret != 1) return ret;
00675     name = xmlTextReaderConstName (reader);
00676     if (name == NULL) return 0;
00677     nameChar = tolowercase((const char *)name);
00678     if (nameChar == NULL) return 0;
00679     
00680     /* while we haven't reached that body close tag */
00681     while (strcmp (nameChar, "body"))
00682     {   
00683         /* parse tag if <par> + */
00684         if (!strcmp (nameChar, "par")) ret = parseSmilPar (reader, daisydata);
00685         /* parse tag if <seq> + */
00686         else if (!strcmp (nameChar, "seq")) ret = parseSmilSeq (reader, daisydata);
00687         /* parse tag if <text> + */
00688         else if (!strcmp (nameChar, "text")) ret = parseSmilText (reader, daisydata, NULL);
00689         /* parse tag if <audio> + */
00690         else if (!strcmp (nameChar, "audio")) ret = parseSmilAudio (reader, NULL);
00691         /* parse tag if <img> + */
00692         else if (!strcmp (nameChar, "img")) ret = parseSmilImg (reader, NULL);
00693         /* parse tag if <a> + */
00694         else if (!strcmp (nameChar, "a")) ret = parseSmilA (reader, daisydata);
00695             
00696         if (ret != 1) return ret;
00697         ret = xmlTextReaderRead (reader);
00698         if (ret != 1)  return ret;
00699         name = xmlTextReaderConstName (reader);
00700         if (name == NULL) return 0;
00701         nameChar = tolowercase((const char *)name);
00702         if (nameChar == NULL) return 0;
00703     }
00704     type = xmlTextReaderNodeType(reader);
00705     if (type != 15) 
00706     {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <BODY>, endtag expected", REP_ERROR);
00707         return -1;
00708     }   
00709     return ret;
00710 }
00711 
00718 static int parseSmilSeq (xmlTextReaderPtr reader, struct DaisyData *daisydata)
00719 {
00720     const xmlChar *name = NULL;
00721     char *nameChar = NULL;
00722     int ret, type;
00723     ret = type = 1;
00724 
00725     /* Attributes:
00726      * id
00727      * class
00728      * customTest
00729      * dur
00730      * xml:lang
00731      * end
00732      * fill
00733      */
00734      
00735     if (DEBUG_NCXSMILPARSER) report ("Parsing SMIL <SEQ>", REP_DEBUG);
00736     type = xmlTextReaderNodeType(reader);
00737     if (type != 1) 
00738     {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <SEQ>", REP_ERROR);
00739         return -1;
00740     }
00741     ret = xmlTextReaderRead (reader);
00742     if (ret != 1) return ret;
00743     name = xmlTextReaderConstName (reader);
00744     if (name == NULL) return 0;
00745     nameChar = tolowercase((const char *)name);
00746     if (nameChar == NULL) return 0;
00747     /* while we haven't reached the seq endtag */
00748     while (type != 15 && strcmp (nameChar, "seq"))
00749     {   
00750         /* parse tag if <par> + */
00751         if (!strcmp (nameChar, "par")) ret = parseSmilPar (reader, daisydata);
00752         /* parse tag if <seq> + */
00753         else if (!strcmp (nameChar, "seq")) ret = parseSmilSeq (reader, daisydata);
00754         /* parse tag if <text> + */
00755         else if (!strcmp (nameChar, "text")) ret = parseSmilText (reader, daisydata, NULL);
00756         /* parse tag if <audio> + */
00757         else if (!strcmp (nameChar, "audio")) ret = parseSmilAudio (reader, NULL);
00758         /* parse tag if <img> + */
00759         else if (!strcmp (nameChar, "img")) ret = parseSmilImg (reader, NULL);
00760         /* parse tag if <a> + */
00761         else if (!strcmp (nameChar, "a")) ret = parseSmilA (reader, daisydata);
00762         if (ret != 1) return ret;
00763         ret = xmlTextReaderRead (reader);
00764         if (ret != 1) return ret;
00765         name = xmlTextReaderConstName (reader);
00766         if (name == NULL) return 0;     
00767         nameChar = tolowercase((const char *)name);
00768         if (nameChar == NULL) return 0;
00769         type = xmlTextReaderNodeType(reader);
00770     }   
00771     return ret;
00772 }
00773 
00780 static int parseSmilPar (xmlTextReaderPtr reader, struct DaisyData *daisydata)
00781 {
00782     const xmlChar *name = NULL;
00783     char *nameChar = NULL;
00784     xmlChar *tmp = NULL;
00785     int ret = 1, type = 1;
00786     
00787     /* Attributes:
00788      * id
00789      * class
00790      * customTest
00791      * xml:lang
00792      */
00793 
00794     if (DEBUG_NCXSMILPARSER) report ("Parsing SMIL <PAR>", REP_DEBUG);
00795     type = xmlTextReaderNodeType(reader);
00796     if (type != 1) 
00797         {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <PAR>", REP_ERROR);
00798             return -1;
00799         }
00800     if(daisydata->smilAlreadyfound == 0)
00801     {
00802         /* go to the fragment identifier point and parses from there. */
00803         tmp = xmlTextReaderGetAttribute (reader, (xmlChar *)"id");      
00804         /* if the par tag has an id attribute, read it and check wheter it matches the fragmentid.
00805            if there is none id attribute, set alreadyfound to 1 and parse the whole smilfile */
00806         if (tmp != NULL) 
00807         {   
00808             if (daisydata->smilPos->fragmentIdentifier != NULL)
00809             {   
00810                 if (!strcmp ((char *)tmp, daisydata->smilPos->fragmentIdentifier))
00811                     daisydata->smilAlreadyfound = 1; 
00812             }
00813         }
00814         else
00815             daisydata->smilAlreadyfound = 1;
00816         /* should we continue parsing if the id tag is missing?
00817            the way it is now, if the id tag is missing we parse from here and out  */
00818         if (tmp != NULL) xmlFree (tmp); tmp = NULL;
00819     }
00820     if (daisydata->smilAlreadyfound == 1)
00821     {
00822         struct Node *tmpNode;
00823         ret = xmlTextReaderRead (reader);
00824         if (ret != 1) return ret;
00825         name = xmlTextReaderConstName (reader);
00826         if (name == NULL) return 0;
00827         nameChar = tolowercase((const char *)name);
00828         if (nameChar == NULL) return 0;
00829         
00830         /* adding a new node to the linked list*/
00831         tmpNode = addNewNode(daisydata);
00832         
00833         /* while we haven't reached the par-end tag */
00834         while (strcmp (nameChar, "par"))
00835         {   
00836             /* parse tag if <seq> + */
00837             if (!strcmp (nameChar, "seq")) ret = parseSmilSeq (reader, daisydata);
00838             /* parse tag if <text> + */
00839             else if (!strcmp (nameChar, "text")) ret = parseSmilText (reader, daisydata, tmpNode);
00840             /* parse tag if <audio> + */
00841             else if (!strcmp (nameChar, "audio")) ret = parseSmilAudio (reader, tmpNode);
00842             /* parse tag if <img> + */
00843             else if (!strcmp (nameChar, "img")) ret = parseSmilImg (reader, tmpNode);
00844             /* parse tag if <a> + */
00845             else if (!strcmp (nameChar, "a")) ret = parseSmilA (reader, daisydata);
00846             if (ret != 1) return ret;
00847             ret = xmlTextReaderRead (reader);
00848             if (ret != 1) return ret;
00849             name = xmlTextReaderConstName (reader);
00850             if (name == NULL) return 0;
00851             nameChar = tolowercase((const char *)name);
00852             if (nameChar == NULL) return 0;
00853         }
00854     }
00855     else 
00856     {   ret = xmlTextReaderRead (reader);
00857         if (ret != 1) return ret;
00858         name = xmlTextReaderConstName (reader);
00859         if (name == NULL) return 0;
00860         nameChar = tolowercase((const char *)name);
00861         if (nameChar == NULL) return 0;
00862         while (strcmp (nameChar, "par"))
00863         {   
00864             ret = xmlTextReaderRead (reader);
00865             if (ret != 1) return ret;
00866             name = xmlTextReaderConstName (reader);
00867             if (name == NULL) return 0;
00868             nameChar = tolowercase((const char *)name);
00869             if (nameChar == NULL) return 0;
00870         }   
00871     }
00872     type = xmlTextReaderNodeType(reader);
00873     if (type != 15) 
00874     {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <PAR>, endtag expected", REP_ERROR);
00875         return -1;
00876     }
00877     return ret;
00878 }
00879 
00880 
00888 static int parseSmilText (xmlTextReaderPtr reader, struct DaisyData *daisydata, struct Node *tmpNode)
00889 {
00890     xmlChar *attr = NULL;
00891     const xmlChar *name = NULL;
00892     char *nameChar = NULL, *textsrc = NULL, *fragment = NULL, *tmp = NULL;
00893     int ret = 1;
00894     
00895     /* Attributes:
00896      * id
00897      * src
00898      * type
00899      * region
00900      * xml:lang
00901      */
00902      
00903     if (DEBUG_NCXSMILPARSER) report ("Parsing SMIL <TEXT>", REP_DEBUG);
00904     name = xmlTextReaderConstName (reader);
00905     if (name == NULL) return 0;
00906     nameChar = tolowercase((const char *)name);
00907     if (nameChar == NULL) return 0;
00908     if (!strcmp (nameChar, "#text")) {
00909         ret = xmlTextReaderRead (reader);
00910         if (ret != 1) return ret;
00911     }
00912     attr = xmlTextReaderGetAttribute(reader, (xmlChar *)"src"); 
00913     
00914     /* splits the text on #.
00915        get the anchor url  */
00916     textsrc = strtok ((char *)attr, "#");
00917     
00918     if (DEBUG_NCXSMILPARSER)
00919     {
00920         char error[STRLEN];
00921         snprintf (error, sizeof (error), "Parsing SMIL <TEXT> XMLFile: %s", textsrc);
00922         report (error, REP_DEBUG);
00923     }
00924     
00925     /* storing the filename of the text file */
00926     tmp = (char *) malloc (strlen (textsrc)+1);
00927     strcpy (tmp, textsrc);
00928     tmpNode->textfilename = tmp;
00929     
00930     /* get the fragment identifier */
00931     fragment = strtok (NULL, "#");
00932     
00933     if (DEBUG_NCXSMILPARSER)
00934     {
00935         char error[STRLEN];
00936         snprintf (error, sizeof (error), "Parsing SMIL <TEXT> Fragment Identifier: %s", fragment);
00937         report (error, REP_DEBUG);
00938     }
00939     
00940     /* storing the fragment identifier for the passage in the text file */
00941     tmp = (char *) malloc (strlen (fragment)+1);
00942     strcpy (tmp, fragment);
00943     tmpNode->fragmentIdentifier = tmp;
00944     
00945     /* parse text file */
00946     if (parseXML(daisydata, textsrc, fragment, tmpNode) != 0)
00947     {
00948         if (DEBUG_NCXSMILPARSER)
00949         {
00950             char error[STRLEN];
00951             snprintf (error, sizeof (error), "Failed to parse XML file: %s", tmpNode->textfilename);
00952             report (error, REP_WARNING);
00953         }
00954         return -1;
00955     }
00956     
00957     if (attr != NULL) xmlFree (attr);
00958     return ret;
00959 }
00960 
00967 static int parseSmilAudio (xmlTextReaderPtr reader, struct Node *tmpNode)
00968 {
00969     xmlChar *tmp = NULL;
00970     char *tempChar = NULL;
00971     int ret = 1;
00972 
00973     /* Attributes:
00974      * id
00975      * src
00976      * type
00977      * clipBegin
00978      * clipEnd
00979      * region
00980      * xml:lang
00981      */
00982     /* fetch the url anchor to the audio file */
00983     tmp = xmlTextReaderGetAttribute (reader, (xmlChar *)"src");
00984     if (tmp != NULL)
00985     {
00986         tempChar = (char *) malloc (strlen((char *)tmp)+1);
00987         strcpy(tempChar, (char *)tmp);
00988         tmpNode->audiofilename = tempChar;
00989         
00990         if (DEBUG_NCXSMILPARSER)
00991         {
00992             char error[STRLEN];
00993             snprintf (error, sizeof (error), "Parsing SMIL <AUDIO> Audiofile: %s", tmp);
00994             report (error, REP_DEBUG);
00995         }
00996         
00997         if (tmp != NULL) xmlFree (tmp); tmp = NULL;
00998         
00999         /* fetch clipBegin */
01000         tmp = xmlTextReaderGetAttribute (reader, (xmlChar *)"clipBegin");
01001         if (tmp != NULL)
01002         {
01003             tempChar = getTime((char *)tmp);
01004             tmpNode->audiofilename = tempChar;
01005             
01006             if (DEBUG_NCXSMILPARSER)
01007             {
01008                 char error[STRLEN];
01009                 snprintf (error, sizeof (error), "Parsing SMIL <AUDIO> clipBegin: %s", tempChar);
01010                 report (error, REP_DEBUG);
01011             }
01012             
01013             if (tmp != NULL) xmlFree (tmp); tmp = NULL;
01014             
01015             /* fetch clipEnd */
01016             tmp = xmlTextReaderGetAttribute (reader, (xmlChar *)"clipEnd");
01017             if (tmp != NULL)
01018             {
01019                 tempChar = getTime((char *)tmp);
01020                 tmpNode->audiofilename = tempChar;
01021                 
01022                 if (DEBUG_NCXSMILPARSER)
01023                 {
01024                     char error[STRLEN];
01025                     snprintf (error, sizeof (error), "Parsing SMIL <AUDIO> clipEnd: %s", tempChar);
01026                     report (error, REP_DEBUG);
01027                 }
01028             }
01029         }
01030         else
01031         {
01032             ret = 0;
01033         }
01034     }
01035     else
01036     {
01037         ret = 0;    
01038     }
01039     if (tmp != NULL) xmlFree (tmp); tmp = NULL; 
01040     return ret;
01041 }
01042 
01043 
01050 static int parseSmilImg (xmlTextReaderPtr reader, struct Node *tmpNode)
01051 {
01052     xmlChar *tmp = NULL;
01053     char *tempChar = NULL;
01054     int ret = 1;
01055     
01056     /* Attributes:
01057      * id
01058      * src
01059      * type
01060      * region
01061      * xml:lang
01062      */
01063      
01064     if (DEBUG_NCXSMILPARSER) report ("Parsing SMIL <IMG>", REP_DEBUG);
01065     /* fetch the url anchor to the audio file */
01066     tmp = xmlTextReaderGetAttribute (reader, (xmlChar *)"src");
01067     if (tmp != NULL)
01068     {
01069         tempChar = (char *) malloc (strlen((char *)tmp)+1);
01070         strcpy(tempChar, (char *)tmp);
01071         
01072         /* store the image in the node */
01073         tmpNode->image = tempChar;
01074     
01075         if (DEBUG_NCXSMILPARSER)
01076         {
01077             char error[STRLEN];
01078             snprintf (error, sizeof (error), "Parsing SMIL <IMG> Imagefile: %s", tmp);
01079             report (error, REP_DEBUG);
01080         }   
01081     }
01082     else
01083     {
01084         ret = 0;    
01085     }
01086     if (tmp != NULL) xmlFree (tmp); tmp = NULL;
01087     return ret;
01088 }
01089 
01090 
01097 static int parseSmilA (xmlTextReaderPtr reader, struct DaisyData *daisydata)
01098 {
01099     const xmlChar *name = NULL;
01100     char *nameChar = NULL;
01101     int ret, type;
01102     ret = type = 1;
01103     
01104     /* Attributes:
01105      * id
01106      * class
01107      * title
01108      * xml:lang
01109      * href
01110      * external
01111      */
01112      
01113     if (DEBUG_NCXSMILPARSER) report ("Parsing SMIL <A>", REP_DEBUG);
01114     type = xmlTextReaderNodeType(reader);
01115     if (type != 1) 
01116     {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <AT>", REP_ERROR);
01117         return -1;
01118     }
01119     ret = xmlTextReaderRead (reader);
01120     if (ret != 1) return ret;
01121     name = xmlTextReaderConstName (reader);
01122     if (name == NULL) return 0;
01123     nameChar = tolowercase((const char *)name);
01124     if (nameChar == NULL) return 0;
01125     
01126     /* while we haven't reached the a-close tag */
01127     while (strcmp (nameChar, "a"))
01128     {   
01129         /* parse tag if <par> * */
01130         if (!strcmp (nameChar, "par")) ret = parseSmilPar (reader, daisydata);
01131         /* parse tag if <seq> * */
01132         else if (!strcmp (nameChar, "seq")) ret = parseSmilSeq (reader, daisydata);
01133         /* parse tag if <text> * */
01134         else if (!strcmp (nameChar, "text")) ret = parseSmilText (reader, daisydata, NULL);
01135         /* parse tag if <audio> * */
01136         else if (!strcmp (nameChar, "audio")) ret = parseSmilAudio (reader, NULL);
01137         /* parse tag if <img> * */
01138         else if (!strcmp (nameChar, "img")) ret = parseSmilImg (reader, NULL);      
01139         if (ret != 1) return ret;
01140         ret = xmlTextReaderRead (reader);
01141         if (ret != 1) return ret;
01142         name = xmlTextReaderConstName (reader);
01143         if (name == NULL) return 0;
01144         nameChar = tolowercase((const char *)name);
01145         if (nameChar == NULL) return 0;
01146     }
01147     type = xmlTextReaderNodeType(reader);
01148     if (type != 15) 
01149     {   if (DEBUG_NCXPARSER) report ("Failed parsing SMIL <A>, endtag expected", REP_ERROR);
01150         return -1;
01151     }   
01152     return ret;
01153 }
01154 /************************ END of *.smil parser code ************************/
01155 
01156 /********************** Code for parsing a XML files **********************/
01157 
01158 
01167 static int parseXML (struct DaisyData *daisydata, char* filename, char* fragment, struct Node *tmpNode)
01168 {
01169     const xmlChar *name = NULL;
01170     char *nameChar = NULL;
01171     int ret = 1;
01172     xmlTextReaderPtr reader = NULL; 
01173     
01174     char tmp[STRLEN];
01175     snprintf (tmp, sizeof (tmp), "%s%s", daisydata->path, filename);
01176 
01177     /* open the xml file */
01178     reader = xmlReaderForFile (tmp, NULL, 0); 
01179     if (reader == NULL)
01180     {
01181         char error[STRLEN];
01182         snprintf (error, sizeof (error), "Failed to open XML file: %s", filename);
01183         report (error, REP_WARNING);
01184         return -1;
01185     }
01186     else 
01187     {
01188         if (DEBUG_NCXXML)
01189         {
01190             char error[STRLEN];
01191             snprintf (error, sizeof (error), "Parsing XML file: %s", filename);
01192             report (error, REP_DEBUG);
01193         }
01194         
01195         ret = xmlTextReaderRead(reader);
01196         if (ret != 1)
01197         {
01198             char error[STRLEN];
01199             snprintf (error, sizeof (error), "Failed to parse XML file: %s", filename);
01200             report (error, REP_WARNING);
01201             return -1;
01202         }
01203         while(ret == 1)
01204         {   
01205             /* Attributes:
01206              */
01207              
01208             name = xmlTextReaderConstName (reader);
01209             if (name == NULL) return 0;
01210             nameChar = tolowercase((const char *)name);
01211             if (nameChar == NULL) return 0;
01212             /* parse tag if <head> */
01213             if (!strcmp (nameChar, "head"))
01214             {
01215                 ret = parseXMLHead (reader);
01216                 if (ret != 1)
01217                 {
01218                     char error[STRLEN];
01219                     snprintf (error, sizeof (error), "Failed to parse XML file: %s", filename);
01220                     report (error, REP_WARNING);
01221                     return -1;
01222                 }
01223             }
01224             /* parse tag if <book> */
01225             else if (!strcmp (nameChar, "book"))
01226             {
01227                 ret = parseXMLBook (reader, fragment, tmpNode);
01228                 xmlFreeTextReader (reader);
01229                 if (ret == 1) return 0;
01230                 else return ret;
01231             }
01232             ret = xmlTextReaderRead (reader);
01233         }
01234         xmlFreeTextReader (reader);
01235         if (ret != 0)
01236         {
01237             char error[STRLEN];
01238             snprintf (error, sizeof (error), "Failed to parse XML file: %s", filename);
01239             report (error, REP_WARNING);
01240             return -1;
01241         }
01242     }
01243     return ret;
01244 }
01245 
01251 static int parseXMLHead (xmlTextReaderPtr reader)
01252 {
01253     const xmlChar *name = NULL;
01254     char *nameChar = NULL;
01255     int ret, type;
01256     ret = type = 1;
01257     
01258     /* Attributes:
01259      */
01260     if (DEBUG_NCXXML) report ("Parsing XML <HEAD>", REP_DEBUG);
01261     type = xmlTextReaderNodeType(reader);
01262     if (type != 1) 
01263     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <HEAD>", REP_ERROR);
01264         return -1;
01265     }
01266     ret = xmlTextReaderRead (reader);
01267     if (ret != 1) return ret;
01268     name = xmlTextReaderConstName (reader);
01269     if (name == NULL) return 0;
01270     nameChar = tolowercase((const char *)name);
01271     if (nameChar == NULL) return 0;
01272     
01273     /* while we haven't reached the head-close tag */
01274     while (strcmp (nameChar, "head"))
01275     {   
01276         /* parse tag if <title> */
01277         if (!strcmp (nameChar, "title"))
01278         {
01279             ret = parseXMLTitle (reader);
01280             if (ret != 1) return ret;
01281         }
01282         /* parse tag if <meta> */
01283         else if (!strcmp (nameChar, "meta"))
01284         {
01285             ret = parseXMLMeta (reader);
01286             if (ret != 1) return ret;
01287         }
01288         ret = xmlTextReaderRead (reader);
01289         if (ret != 1) return ret;
01290         name = xmlTextReaderConstName (reader);
01291         if (name == NULL) return 0;
01292         nameChar = tolowercase((const char *)name);
01293         if (nameChar == NULL) return 0;
01294     }
01295     type = xmlTextReaderNodeType(reader);
01296     if (type != 15) 
01297     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <HEAD>, endtag expected", REP_ERROR);
01298         return -1;
01299     }   
01300     return ret;
01301 }
01302 
01308 static int parseXMLTitle (xmlTextReaderPtr reader)
01309 {
01310     xmlChar *value = NULL;
01311     char *tmp = NULL;
01312     int ret = 1;
01313     /* Attributes:
01314      */
01315     if (DEBUG_NCXXML) report ("Parsing XML <TITLE>", REP_DEBUG);
01316     ret = xmlTextReaderRead (reader);
01317     if (ret != 1) return ret;
01318     
01319     /* if the title tag has a value  */
01320     if (xmlTextReaderHasValue (reader)){
01321         value = xmlTextReaderValue (reader);
01322         tmp =  removewhitespaces ((char *)value);
01323     /*  printf ("\t-%s-",tmp);
01324         TODO: The title is never used.. */
01325     }
01326     if(value != NULL) 
01327         xmlFree(value); value = NULL;
01328     if(tmp != NULL)
01329         free(tmp); tmp = NULL;
01330     ret = xmlTextReaderRead (reader);
01331     return ret;
01332 }
01333 
01339 static int parseXMLMeta (xmlTextReaderPtr reader){
01340     int ret = 1;
01341     /* Attributes:
01342      */
01343     if (DEBUG_NCXXML) report ("Parsing XML <META>", REP_DEBUG);
01344     ret = xmlTextReaderRead (reader);
01345     return ret; 
01346 }
01347 
01355 static int parseXMLBook (xmlTextReaderPtr reader, char *fragment, struct Node *tmpNode)
01356 {
01357     const xmlChar *name = NULL;
01358     char *nameChar = NULL;
01359     int ret, type;
01360     ret = type = 1;
01361     
01362     /* Attributes:
01363      */
01364     if (DEBUG_NCXXML) report ("Parsing XML <BOOK>", REP_DEBUG);
01365     type = xmlTextReaderNodeType(reader);
01366     if (type != 1) 
01367     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <BOOK>", REP_ERROR);
01368         return -1;
01369     }
01370     ret = xmlTextReaderRead (reader);
01371     if (ret != 1) return ret;
01372     name = xmlTextReaderConstName (reader);
01373     if (name == NULL) return 0;
01374     nameChar = tolowercase((const char *)name);
01375     if (nameChar == NULL) return 0;
01376     
01377     /* while we haven't reached the book-close tag */
01378     while (strcmp (nameChar, "book"))
01379     {   
01380         /* parse tag if <frontmatter> */
01381         if (!strcmp (nameChar, "frontmatter"))
01382         {
01383             ret = parseXMLFrontmatter (reader);
01384             if (ret != 1) return ret;
01385         }
01386         /* parse tag if <bodymatter> */
01387         else if (!strcmp (nameChar, "bodymatter"))
01388         {
01389             ret = parseXMLBodymatter (reader, fragment, tmpNode);
01390             if (ret == 1) return ret;
01391             /*if (ret != 1) return ret; */
01392         }
01393         /* parse tag if <rearmatter> */
01394         else if (!strcmp (nameChar, "rearmatter"))
01395         {
01396             ret = parseXMLRearmatter (reader);
01397             if (ret != 1) return ret;
01398         }
01399         ret = xmlTextReaderRead (reader);
01400         if (ret != 1) return ret;
01401         name = xmlTextReaderConstName (reader);
01402         if (name == NULL) return 0;
01403         nameChar = tolowercase((const char *)name);
01404         if (nameChar == NULL) return 0;
01405     }
01406     type = xmlTextReaderNodeType(reader);
01407     if (type != 15) 
01408     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <BOOK>, endtag expected", REP_ERROR);
01409         return -1;
01410     }   
01411     return ret;
01412 }
01413 
01414 
01420 static int parseXMLFrontmatter (xmlTextReaderPtr reader)
01421 {
01422     const xmlChar *name = NULL; 
01423     char *nameChar = NULL;
01424     int ret, type;
01425     ret = type = 1;
01426     
01427     /* Attributes:
01428      */
01429     if (DEBUG_NCXXML) report ("Parsing XML <FRONTMATTER>", REP_DEBUG);
01430     type = xmlTextReaderNodeType(reader);
01431     if (type != 1) 
01432     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <FRONTMATTER>", REP_ERROR);
01433         return -1;
01434     }
01435     ret = xmlTextReaderRead (reader);
01436     if (ret != 1) return ret;
01437     name = xmlTextReaderConstName (reader);
01438     if (name == NULL) return 0;
01439     nameChar = tolowercase((const char *)name);
01440     if (nameChar == NULL) return 0;
01441     
01442     /* while we haven't reached the frontmatter-close tag */
01443     while (strcmp (nameChar, "frontmatter"))
01444     {   
01445         /* parse tag if <doctitle> */
01446         if (!strcmp (nameChar, "doctitle"))
01447         {
01448             ret = parseXMLDoctitle (reader);
01449             if (ret != 1) return ret;
01450         }
01451         /* parse tag if <docauthor> */
01452         else if (!strcmp (nameChar, "docauthor"))
01453         {
01454             ret = parseXMLDocauthor (reader);
01455             if (ret != 1) return ret;
01456         }
01457         /* parse tag if <covertitle> */
01458         else if (!strcmp (nameChar, "covertitle"))
01459         {
01460             ret = parseXMLCovertitle (reader);
01461             if (ret != 1) return ret;
01462         }
01463         /* parse tag if <level> */
01464         else if (!strcmp (nameChar, "level"))
01465         {
01466             ret = parseXMLLevel (reader);
01467             if (ret != 1) return ret;
01468         }
01469         /* parse tag if <levelx> */
01470         else if (!strcmp (nameChar, "level1") || !strcmp (nameChar, "level2") || !strcmp (nameChar, "level3")
01471          || !strcmp (nameChar, "level4") || !strcmp (nameChar, "level5") || !strcmp (nameChar, "level6"))
01472         {
01473             ret = parseXMLLevel1 (reader, NULL, NULL);
01474             if (ret != 1) return ret;
01475         }
01476         ret = xmlTextReaderRead (reader);
01477         if (ret != 1) return ret;
01478         name = xmlTextReaderConstName (reader);
01479         if (name == NULL) return 0;
01480         nameChar = tolowercase((const char *)name);
01481         if (nameChar == NULL) return 0;
01482     }
01483     type = xmlTextReaderNodeType(reader);
01484     if (type != 15) 
01485     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <FRONTMATTER>, endtag expected", REP_ERROR);
01486         return -1;
01487     }   
01488     return ret;
01489 }
01490 
01491 
01498 static int parseXMLBodymatter (xmlTextReaderPtr reader, char* fragment, struct Node *tmpNode)
01499 {
01500     const xmlChar *name = NULL; 
01501     char *nameChar = NULL;
01502     int ret, type;
01503     ret = type = 1;
01504     
01505     /* Attributes:
01506      */
01507     if (DEBUG_NCXXML) report ("Parsing XML <BODYMATTER>", REP_DEBUG);
01508     type = xmlTextReaderNodeType(reader);
01509     if (type != 1) 
01510     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <BODYMATTER>", REP_ERROR);
01511         return -1;
01512     }
01513     ret = xmlTextReaderRead (reader);
01514     if (ret != 1) return ret;
01515     name = xmlTextReaderConstName (reader);
01516     if (name == NULL) return 0;
01517     nameChar = tolowercase((const char *)name);
01518     if (nameChar == NULL) return 0;
01519     
01520     /* while we haven't reached the bodymatter-close tag */
01521     while (strcmp (nameChar, "bodymatter"))
01522     {   
01523         /* parse tag if <level> */
01524         if (!strcmp (nameChar, "level"))
01525         {
01526             ret = parseXMLLevel (reader);
01527             if (ret != 1) return ret;
01528         }
01529         /* parse tag if <level1> */
01530         else if (!strcmp (nameChar, "level1"))
01531         {
01532             ret = parseXMLLevel1 (reader, fragment, tmpNode);
01533             if (ret == 1) return ret;
01534         /*  if (ret != 1) return ret; */
01535         }
01536         ret = xmlTextReaderRead (reader);
01537         if (ret != 1) return ret;
01538         name = xmlTextReaderConstName (reader);
01539         if (name == NULL) return 0;
01540         nameChar = tolowercase((const char *)name);
01541         if (nameChar == NULL) return 0;
01542     }
01543     type = xmlTextReaderNodeType(reader);
01544     if (type != 15) 
01545     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <BODYMATTER>, endtag expected", REP_ERROR);
01546         return -1;
01547     }   
01548     return ret;
01549 }
01550 
01551 
01557 static int parseXMLRearmatter (xmlTextReaderPtr reader)
01558 {
01559     const xmlChar *name = NULL; 
01560     char *nameChar = NULL;
01561     int ret, type;
01562     ret = type = 1;
01563     /* Attributes:
01564      */
01565     if (DEBUG_NCXXML) report ("Parsing XML <REARMATTER>", REP_DEBUG);
01566     type = xmlTextReaderNodeType(reader);
01567     if (type != 1) 
01568     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <REARMATTER>", REP_ERROR);
01569         return -1;
01570     }
01571     ret = xmlTextReaderRead (reader);
01572     if (ret != 1) return ret;
01573     name = xmlTextReaderConstName (reader);
01574     if (name == NULL) return 0;
01575     nameChar = tolowercase((const char *)name);
01576     if (nameChar == NULL) return 0;
01577     
01578     /* while we haven't reached the rearmatter-close tag */
01579     while (strcmp (nameChar, "rearmatter"))
01580     {   
01581         /* parse tag if <level> */
01582         if (!strcmp (nameChar, "level"))
01583         {
01584             ret = parseXMLLevel (reader);
01585             if (ret != 1) return ret;
01586         }
01587         /* parse tag if <level1> */
01588         else if (!strcmp (nameChar, "level1"))
01589         {
01590             ret = parseXMLLevel1 (reader, NULL, NULL);
01591             if (ret != 1) return ret;
01592         }
01593         ret = xmlTextReaderRead (reader);
01594         if (ret != 1) return ret;
01595         name = xmlTextReaderConstName (reader);
01596         if (name == NULL) return 0;
01597         nameChar = tolowercase((const char *)name);
01598         if (nameChar == NULL) return 0;
01599     }
01600     type = xmlTextReaderNodeType(reader);
01601     if (type != 15) 
01602     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <REARMATTER>, endtag expected", REP_ERROR);
01603         return -1;
01604     }   
01605     return ret;
01606 }
01607 
01608 
01614 static int parseXMLDoctitle (xmlTextReaderPtr reader)
01615 {   
01616     const xmlChar *name = NULL;
01617     xmlChar *value = NULL;
01618     char *nameChar = NULL, * tmp = NULL;
01619     int ret = 1, type = 1;
01620     
01621     /* Attributes:*/
01622     if (DEBUG_NCXXML) report ("Parsing XML <DOCTITLE>", REP_DEBUG);
01623     type = xmlTextReaderNodeType(reader);
01624     if (type != 1) 
01625     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <DOCTITLE>", REP_ERROR);
01626         return -1;
01627     }
01628     ret = xmlTextReaderRead (reader);
01629     if (ret != 1) return ret;
01630     name = xmlTextReaderConstName (reader);
01631     if (name == NULL) return 0;
01632     nameChar = tolowercase((const char *)name);
01633     if (nameChar == NULL) return 0;
01634     
01635     /* if the title tag has a value  */
01636     if (xmlTextReaderHasValue (reader)){
01637         value = xmlTextReaderValue (reader);
01638         tmp =  removewhitespaces ((char *)value);
01639         /* printf ("\t-%s-",tmp);
01640           TODO: The title is never used.. */
01641     }
01642     if(value != NULL) 
01643         xmlFree(value); value = NULL;
01644     if(tmp != NULL)
01645         free(tmp); tmp = NULL;
01646     while (strcmp (nameChar, "doctitle"))
01647     {
01648         ret = xmlTextReaderRead (reader);
01649         if (ret != 1) return ret;
01650         name = xmlTextReaderConstName (reader);
01651         if (name == NULL) return 0;
01652         nameChar = tolowercase((const char *)name);
01653         if (nameChar == NULL) return 0;
01654     }
01655     type = xmlTextReaderNodeType(reader);
01656     if (type != 15) 
01657     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <DOCTITLE>, endtag expected", REP_ERROR);
01658         return -1;
01659     }
01660     return ret;
01661 }
01662 
01663 
01669 static int parseXMLDocauthor (xmlTextReaderPtr reader)
01670 {
01671     const xmlChar *name = NULL;
01672     xmlChar *value = NULL;
01673     char *nameChar = NULL, *tmp = NULL;
01674     int ret =1 , type = 1;
01675     
01676     /* Attributes: */
01677     if (DEBUG_NCXXML) report ("Parsing XML <DOCAUTHOR>", REP_DEBUG);
01678     type = xmlTextReaderNodeType(reader);
01679     if (type != 1) 
01680     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <DOCAUTHOR>", REP_ERROR);
01681         return -1;
01682     }
01683     ret = xmlTextReaderRead (reader);
01684     if (ret != 1) return ret;
01685     name = xmlTextReaderConstName (reader);
01686     if (name == NULL) return 0;
01687     nameChar = tolowercase((const char *)name);
01688     if (nameChar == NULL) return 0;
01689     
01690     /* if the author tag has a value  */
01691     if (xmlTextReaderHasValue (reader)){
01692         value = xmlTextReaderValue (reader);
01693         tmp = removewhitespaces ((char *)value);
01694         /* printf ("\t-%s-",tmp);
01695           TODO: The title is never used.. */
01696     }
01697     if(value != NULL) 
01698         xmlFree(value); value = NULL;
01699     if(tmp != NULL) free(tmp); tmp = NULL;
01700     while (strcmp (nameChar, "docauthor"))
01701     {
01702         ret = xmlTextReaderRead (reader);
01703         if (ret != 1) return ret;
01704         name = xmlTextReaderConstName (reader);
01705         if (name == NULL) return 0;
01706         nameChar = tolowercase((const char *)name);
01707         if (nameChar == NULL) return 0;
01708     }
01709     type = xmlTextReaderNodeType(reader);
01710     if (type != 15) 
01711     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <DOCAUTHOR>, endtag expected", REP_ERROR);
01712         return -1;
01713     }   
01714     return ret;
01715 }
01716 
01717 
01723 static int parseXMLLevel (xmlTextReaderPtr reader)
01724 {
01725     const xmlChar *name = NULL; 
01726     char *nameChar = NULL;
01727     int ret = 1, type = 1;
01728     
01729     /* Attributes:
01730      * depth
01731      * class
01732      */
01733     if (DEBUG_NCXXML) report ("Parsing XML <LEVEL>", REP_DEBUG);
01734     type = xmlTextReaderNodeType(reader);
01735     if (type != 1) 
01736     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <LEVEL>", REP_ERROR);
01737         return -1;
01738     }
01739     ret = xmlTextReaderRead (reader);
01740     if (ret != 1) return ret;
01741     name = xmlTextReaderConstName (reader);
01742     if (name == NULL) return 0;
01743     nameChar = tolowercase((const char *)name);
01744     if (nameChar == NULL) return 0;
01745     type = xmlTextReaderNodeType(reader);
01746     if (type != 15) 
01747     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <LEVEL>, endtag expected", REP_ERROR);
01748         return -1;
01749     }   
01750     return ret;
01751 }
01752 
01753 
01761 static int parseXMLLevel1 (xmlTextReaderPtr reader, char* fragment, struct Node *tmpNode)
01762 {
01763     xmlChar *value = NULL, *tmp = NULL;
01764     const xmlChar *name = NULL; 
01765     char *nameChar = NULL;
01766     int ret = 1, type = 1;
01767 
01768 /*Level 1 may contain following childs..
01769   <level1>
01770     <pagenum>...</pagenum>
01771     <h1>...</h1>
01772     <level2>...</level2>
01773     <doctitle>...</doctitle>
01774     <docauthor>...</docauthor>
01775     <covertitle>...</covertitle>
01776     <bridgehead>...</bridgehead>
01777     <p>...</p>
01778     <list>...</list>
01779     <dl>...</dl>
01780     <div>...</div>
01781     <blockquote>...</blockquote>
01782     <img>...</img>
01783     <imggroup>...</imggroup>
01784     <poem>...</poem>
01785     <linegroup>...</linegroup>
01786     <byline>...</byline>
01787     <dateline>...</dateline>
01788     <epigraph>...</epigraph>
01789     <table>...</table>
01790     <address>...</address>
01791     <line>...</line>
01792     <author>...</author>
01793     <prodnote>...</prodnote>
01794     <sidebar>...</sidebar>
01795     <note>...</note>
01796     <a>...</a>
01797     <cite>...</cite>
01798     <samp>...</samp>
01799     <kbd>...</kbd>
01800 </level1>
01801 */
01802 
01803     if (DEBUG_NCXXML) report ("Parsing XML <LEVEL1>", REP_DEBUG);
01804     type = xmlTextReaderNodeType(reader);
01805     if (type != 1) 
01806     {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <LEVEL1>", REP_ERROR);
01807         return -1;
01808     }
01809     
01810     /* if the fragment = NULL - we are in frontmatter */
01811     if (fragment == NULL)
01812     {   
01813         ret = xmlTextReaderRead (reader);
01814         if (ret != 1) return ret;
01815         if(xmlTextReaderHasValue (reader))
01816         {
01817             value = xmlTextReaderValue (reader);
01818             tmp =  (xmlChar *)removewhitespaces ((char *)value);
01819             /* printf ("\t-%s-",tmp);
01820               TODO: The title is never used.. */
01821         }
01822     }
01823     /* we are in bodymatter */
01824     else
01825     {   
01826         /* check wheter it is the level1 tag who got the id we request */
01827         tmp = xmlTextReaderGetAttribute (reader, (xmlChar *)"id");
01828         ret = xmlTextReaderRead (reader);
01829         if (tmp != NULL)
01830         {   
01831             if (!strcmp ((char *)tmp, fragment))
01832             {
01833                 while(!xmlTextReaderHasValue (reader))
01834                 {
01835                     ret = xmlTextReaderRead (reader);
01836                     
01837                     /* check wheter we have come to a new tag with id,
01838                      * if that is the case, the level1 tag didn't have
01839                      * any valid values 
01840                      */
01841                     tmp = xmlTextReaderGetAttribute (reader, (xmlChar *)"id");
01842                     if (tmp != NULL) return 0;
01843                 }
01844                 value = xmlTextReaderValue (reader);
01845                 tmp =  (xmlChar *)removewhitespaces ((char *)value);
01846                 if(tmp != NULL) tmpNode->textPassage = (char *)tmp;
01847                 if(value != NULL) xmlFree(value); value = NULL; 
01848                 /* printf ("\n%s\n",tmp); TODO store chatper title? */
01849                 return 1;   /* return succesful reading  */
01850             }
01851         }
01852         name = xmlTextReaderConstName (reader);
01853         if (name == NULL) return 0;
01854         nameChar = tolowercase((const char *)name);
01855         if (nameChar == NULL) return 0;
01856         
01857         /* while we haven't reached the level1-endtag */
01858         while (strcmp (nameChar, "level1"))
01859         {   
01860             tmp = xmlTextReaderGetAttribute (reader, (xmlChar *)"id");
01861             if (tmp != NULL)
01862             {   
01863                 /*
01864                 name = xmlTextReaderConstName (reader);
01865                 if (name == NULL) return 0;
01866                 if (!strcmp (name, "p"))
01867                 {
01868                 */
01869                     if (!strcmp ((char *)tmp, fragment))
01870                     {   ret = xmlTextReaderRead (reader);
01871                         if(xmlTextReaderHasValue (reader))
01872                         {
01873                             value = xmlTextReaderValue (reader);
01874                             tmp =  (xmlChar *)removewhitespaces ((char *)value);
01875                             if(tmp != NULL) tmpNode->textPassage = (char *)tmp;
01876                             if(value != NULL) xmlFree(value); value = NULL;                                     
01877                             return 1;   
01878                         }
01879                         else
01880                         {
01881                             if(value != NULL) xmlFree(value); value = NULL;                                     
01882                             if(tmp != NULL) xmlFree(tmp); tmp = NULL;                                                               
01883                             return 0;
01884                         }
01885                     }
01886                 /*}*/               
01887             }
01888             ret = xmlTextReaderRead (reader);
01889             if (ret != 1) return ret;       
01890         }
01891         type = xmlTextReaderNodeType(reader);
01892         if (type != 15) 
01893         {   if (DEBUG_NCXPARSER) report ("Failed parsing XML <LEVEL1>, endtag expected", REP_ERROR);
01894             return -1;
01895         }           
01896     }
01897     if(value != NULL) 
01898         xmlFree(value); value = NULL;
01899     if(tmp != NULL)
01900         xmlFree(tmp); tmp = NULL;
01901     ret = xmlTextReaderRead (reader);
01902     return ret;
01903 }
01904 
01905 
01911 static int parseXMLCovertitle (xmlTextReaderPtr reader)
01912 {
01913     const xmlChar *name = NULL; 
01914     char *nameChar = NULL;
01915     int ret = 1;
01916     /* Attributes:
01917      */
01918     if (DEBUG_NCXXML) report ("Parsing XML <COVERTITLE>", REP_DEBUG);
01919     ret = xmlTextReaderRead (reader);
01920     if (ret != 1) return ret;
01921     name = xmlTextReaderConstName (reader);
01922     if (name == NULL) return 0; 
01923     nameChar = tolowercase((const char *)name);
01924     if (nameChar == NULL) return 0;
01925     return ret;
01926 }
01927 
01928 /*
01929  * Fetches the textvalue inside html-tags.
01930  * E.g: ....<b>text</b>...
01931  *
01932  * @param reader XmlTextReader.
01933  *
01934  * Return the textvalue.
01935  */
01936 /*static char *parseXMLHtmltag (xmlTextReaderPtr reader)
01937 {
01938     xmlChar *value = NULL;
01939     value = NULL;
01940     char *tempChar = NULL;
01941     
01942     xmlTextReaderRead (reader);
01943     TODO: hva om det ikke er flere noder etter denne?
01944     
01945     if (xmlTextReaderHasValue (reader))
01946     {
01947         value = xmlTextReaderValue (reader);
01948         tempChar = removewhitespaces(value);
01949         xmlFree (value);
01950     }
01951     return tempChar;    
01952 }
01953 */
01954 
01955 
01956 /************************* END of XML parser code *************************/
01957 
01958 
01959 #endif

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