2 * Copyright (C) 2005 Casper S. Hornstrup
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define MAX_PATH _MAX_PATH
28 # include <sys/stat.h>
29 # define MAX_PATH PATH_MAX
34 #include "exception.h"
41 #define getcwd _getcwd
44 static const char* WS
= " \t\r\n";
45 static const char* WSEQ
= " =\t\r\n";
47 string working_directory
;
49 XMLIncludes::~XMLIncludes()
51 for ( size_t i
= 0; i
< this->size(); i
++ )
56 InitWorkingDirectory()
58 // store the current directory for path calculations
59 working_directory
.resize ( _MAX_PATH
);
60 working_directory
[0] = 0;
61 getcwd ( &working_directory
[0], working_directory
.size() );
62 working_directory
.resize ( strlen ( working_directory
.c_str() ) );
73 return _filelengthi64 ( _fileno(f
) );
75 struct stat64 file_stat
;
76 if ( fstat64(fileno(f
), &file_stat
) != 0 )
78 return file_stat
.st_size
;
84 if ( !working_directory
.size() )
85 InitWorkingDirectory();
86 string
s ( working_directory
);
87 const char* p
= strtok ( &s
[0], "/\\" );
92 p
= strtok ( NULL
, "/\\" );
96 Path::Path ( const Path
& cwd
, const string
& file
)
98 string
s ( cwd
.Fixup ( file
, false ) );
99 const char* p
= strtok ( &s
[0], "/\\" );
103 path
.push_back ( p
);
104 p
= strtok ( NULL
, "/\\" );
109 Path::Fixup ( const string
& file
, bool include_filename
) const
111 if ( strchr ( "/\\", file
[0] )
113 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
120 vector
<string
> pathtmp ( path
);
122 const char* prev
= strtok ( &tmp
[0], "/\\" );
123 const char* p
= strtok ( NULL
, "/\\" );
126 if ( !strcmp ( prev
, "." ) )
128 else if ( !strcmp ( prev
, ".." ) )
130 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
132 if ( pathtmp
.size() > 1 )
134 if ( pathtmp
.size() )
136 pathtmp
.resize ( pathtmp
.size() - 1 );
139 pathtmp
.push_back ( prev
);
141 p
= strtok ( NULL
, "/\\" );
143 if ( include_filename
)
144 pathtmp
.push_back ( prev
);
146 // reuse tmp variable to return recombined path
148 for ( size_t i
= 0; i
< pathtmp
.size(); i
++ )
150 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
162 Path::RelativeFromWorkingDirectory ()
165 for ( size_t i
= 0; i
< path
.size(); i
++ )
167 out
+= "/" + path
[i
];
169 return RelativeFromWorkingDirectory ( out
);
173 Path::RelativeFromWorkingDirectory ( const string
& path
)
175 vector
<string
> vwork
, vpath
, vout
;
176 Path::Split ( vwork
, working_directory
, true );
177 Path::Split ( vpath
, path
, true );
179 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
180 // not possible to do relative across different drive letters
181 if ( vwork
[0] != vpath
[0] )
185 while ( i
< vwork
.size() && i
< vpath
.size() && vwork
[i
] == vpath
[i
] )
187 if ( i
< vwork
.size() )
189 // path goes above our working directory, we will need some ..'s
190 for ( size_t j
= 0; j
< i
; j
++ )
191 vout
.push_back ( ".." );
193 while ( i
< vpath
.size() )
194 vout
.push_back ( vpath
[i
++] );
196 // now merge vout into a string again
197 string out
= vout
[0];
198 for ( i
= 1; i
< vout
.size(); i
++ )
200 out
+= "/" + vout
[i
];
206 Path::Split ( vector
<string
>& out
,
211 const char* prev
= strtok ( &s
[0], "/\\" );
212 const char* p
= strtok ( NULL
, "/\\" );
216 out
.push_back ( prev
);
218 p
= strtok ( NULL
, "/\\" );
221 out
.push_back ( prev
);
236 XMLFile::open(const string
& filename_
)
239 FILE* f
= fopen ( filename_
.c_str(), "rb" );
242 unsigned long len
= (unsigned long)filelen(f
);
244 fread ( &_buf
[0], 1, len
, f
);
248 _filename
= filename_
;
253 // next_token() moves the pointer to next token, which may be
254 // an xml element or a text element, basically it's a glorified
255 // skipspace, normally the user of this class won't need to call
258 XMLFile::next_token()
260 _p
+= strspn ( _p
, WS
);
264 XMLFile::next_is_text()
270 XMLFile::more_tokens()
275 // get_token() is used to return a token, and move the pointer
278 XMLFile::get_token(string
& token
)
281 if ( !strncmp ( _p
, "<!--", 4 ) )
283 tokend
= strstr ( _p
, "-->" );
289 else if ( !strncmp ( _p
, "<?", 2 ) )
291 tokend
= strstr ( _p
, "?>" );
297 else if ( *_p
== '<' )
299 tokend
= strchr ( _p
, '>' );
307 tokend
= strchr ( _p
, '<' );
310 while ( tokend
> _p
&& isspace(tokend
[-1]) )
315 token
= string ( _p
, tokend
-_p
);
322 XMLFile::Location() const
325 const char* p
= strchr ( _buf
.c_str(), '\n' );
326 while ( p
&& p
< _p
)
329 p
= strchr ( p
+1, '\n' );
331 return ssprintf ( "%s(%i)",_filename
.c_str(), line
);
334 XMLAttribute::XMLAttribute()
338 XMLAttribute::XMLAttribute(const string
& name_
,
339 const string
& value_
)
340 : name(name_
), value(value_
)
344 XMLAttribute::XMLAttribute ( const XMLAttribute
& src
)
345 : name(src
.name
), value(src
.value
)
350 XMLAttribute
& XMLAttribute::operator = ( const XMLAttribute
& src
)
357 XMLElement::XMLElement ( XMLFile
* xmlFile
,
358 const string
& location
)
359 : xmlFile ( xmlFile
),
360 location ( location
),
361 parentElement ( NULL
)
365 XMLElement::~XMLElement()
368 for ( i
= 0; i
< attributes
.size(); i
++ )
369 delete attributes
[i
];
370 for ( i
= 0; i
< subElements
.size(); i
++ )
371 delete subElements
[i
];
375 XMLElement::AddSubElement ( XMLElement
* e
)
377 subElements
.push_back ( e
);
378 e
->parentElement
= this;
382 // This function takes a single xml tag ( i.e. beginning with '<' and
383 // ending with '>', and parses out it's tag name and constituent
385 // Return Value: returns true if you need to look for a </tag> for
386 // the one it just parsed...
388 XMLElement::Parse(const string
& token
,
391 const char* p
= token
.c_str();
392 assert ( *p
== '<' );
394 p
+= strspn ( p
, WS
);
396 // check if this is a comment
397 if ( !strncmp ( p
, "!--", 3 ) )
401 return false; // never look for end tag to a comment
404 end_tag
= ( *p
== '/' );
408 p
+= strspn ( p
, WS
);
410 const char* end
= strpbrk ( p
, WS
);
413 end
= strpbrk ( p
, "/>" );
416 name
= string ( p
, end
-p
);
418 p
+= strspn ( p
, WS
);
419 while ( *p
!= '>' && *p
!= '/' )
421 end
= strpbrk ( p
, WSEQ
);
424 end
= strpbrk ( p
, "/>" );
427 string
attribute ( p
, end
-p
), value
;
429 p
+= strspn ( p
, WS
);
433 p
+= strspn ( p
, WS
);
435 if ( strchr ( "\"'", *p
) )
438 end
= strchr ( p
, quote
);
442 end
= strpbrk ( p
, WS
);
446 end
= strchr ( p
, '>' );
448 if ( end
[-1] == '/' )
451 value
= string ( p
, end
-p
);
453 if ( quote
&& *p
== quote
)
455 p
+= strspn ( p
, WS
);
457 else if ( name
[0] != '!' )
459 throw XMLSyntaxErrorException ( location
,
460 "attributes must have values" );
462 attributes
.push_back ( new XMLAttribute ( attribute
, value
) );
464 return !( *p
== '/' ) && !end_tag
;
468 XMLElement::GetAttribute ( const string
& attribute
,
471 // this would be faster with a tree-based container, but our attribute
472 // lists are likely to stay so short as to not be an issue.
473 for ( size_t i
= 0; i
< attributes
.size(); i
++ )
475 if ( attribute
== attributes
[i
]->name
)
476 return attributes
[i
];
480 throw RequiredAttributeNotFoundException ( location
,
488 XMLElement::GetAttribute ( const string
& attribute
,
489 bool required
) const
491 // this would be faster with a tree-based container, but our attribute
492 // lists are likely to stay so short as to not be an issue.
493 for ( size_t i
= 0; i
< attributes
.size(); i
++ )
495 if ( attribute
== attributes
[i
]->name
)
496 return attributes
[i
];
500 throw RequiredAttributeNotFoundException ( location
,
508 // This function reads a "token" from the file loaded in XMLFile
509 // if it finds a tag that is non-singular, it parses sub-elements and/or
510 // inner text into the XMLElement that it is building to return.
511 // Return Value: an XMLElement allocated via the new operator that contains
512 // it's parsed data. Keep calling this function until it returns NULL
515 XMLParse ( XMLFile
& f
,
516 XMLIncludes
* includes
,
518 bool* pend_tag
= NULL
)
521 if ( !f
.get_token(token
) )
523 bool end_tag
, is_include
= false;
525 while ( token
[0] != '<'
526 || !strncmp ( token
.c_str (), "<!--", 4 )
527 || !strncmp ( token
.c_str (), "<?", 2 ) )
529 if ( token
[0] != '<' )
530 throw XMLSyntaxErrorException ( f
.Location (),
531 "expecting xml tag, not '%s'",
533 if ( !f
.get_token(token
) )
537 XMLElement
* e
= new XMLElement ( &f
,
539 bool bNeedEnd
= e
->Parse ( token
, end_tag
);
541 if ( e
->name
== "xi:include" && includes
)
544 att
= e
->GetAttribute ( "href", true );
546 string
includeFile ( path
.Fixup ( att
->value
, true ) );
547 string
topIncludeFile ( Path::RelativeFromWorkingDirectory ( includeFile
) );
548 includes
->push_back ( new XMLInclude ( e
, path
, topIncludeFile
) );
559 throw XMLSyntaxErrorException ( f
.Location (),
560 "end tag '%s' not expected",
566 bool bThisMixingErrorReported
= false;
567 while ( f
.more_tokens () )
569 if ( f
.next_is_text () )
571 if ( !f
.get_token ( token
) || token
.size () == 0 )
573 throw InvalidBuildFileException (
575 "internal tool error - get_token() failed when more_tokens() returned true" );
578 if ( e
->subElements
.size() && !bThisMixingErrorReported
)
580 throw XMLSyntaxErrorException ( f
.Location (),
581 "mixing of inner text with sub elements" );
582 bThisMixingErrorReported
= true;
584 if ( strchr ( token
.c_str (), '>' ) )
586 throw XMLSyntaxErrorException ( f
.Location (),
587 "invalid symbol '>'" );
589 if ( e
->value
.size() > 0 )
591 throw XMLSyntaxErrorException ( f
.Location (),
592 "multiple instances of inner text" );
593 e
->value
+= " " + token
;
600 XMLElement
* e2
= XMLParse ( f
, is_include
? NULL
: includes
, path
, &end_tag
);
603 throw InvalidBuildFileException (
605 "end of file found looking for end tag" );
610 if ( e
->name
!= e2
->name
)
613 throw XMLSyntaxErrorException ( f
.Location (),
614 "end tag name mismatch" );
620 if ( e
->value
.size () > 0 && !bThisMixingErrorReported
)
622 throw XMLSyntaxErrorException ( f
.Location (),
623 "mixing of inner text with sub elements" );
624 bThisMixingErrorReported
= true;
626 e
->AddSubElement ( e2
);
633 XMLReadFile ( XMLFile
& f
, XMLElement
& head
, XMLIncludes
& includes
, const Path
& path
)
637 XMLElement
* e
= XMLParse ( f
, &includes
, path
);
640 head
.AddSubElement ( e
);
645 XMLLoadInclude ( XMLInclude
& include
,
646 XMLIncludes
& includes
)
649 att
= include
.e
->GetAttribute("href", true);
652 string
file ( include
.path
.Fixup(att
->value
, true) );
653 string
top_file ( Path::RelativeFromWorkingDirectory ( file
) );
654 include
.e
->attributes
.push_back ( new XMLAttribute ( "top_href", top_file
) );
655 XMLFile
* fInc
= new XMLFile();
656 if ( !fInc
->open ( file
) )
658 include
.fileExists
= false;
659 // look for xi:fallback element
660 for ( size_t i
= 0; i
< include
.e
->subElements
.size (); i
++ )
662 XMLElement
* e2
= include
.e
->subElements
[i
];
663 if ( e2
->name
== "xi:fallback" )
665 // now look for xi:include below...
666 for ( i
= 0; i
< e2
->subElements
.size (); i
++ )
668 XMLElement
* e3
= e2
->subElements
[i
];
669 if ( e3
->name
== "xi:include" )
671 att
= e3
->GetAttribute ( "href", true );
673 string
includeFile ( include
.path
.Fixup ( att
->value
, true ) );
674 string
topIncludeFile ( Path::RelativeFromWorkingDirectory ( includeFile
) );
675 XMLInclude
* fallbackInclude
= new XMLInclude ( e3
, include
.path
, topIncludeFile
);
676 return XMLLoadInclude ( *fallbackInclude
, includes
);
679 throw InvalidBuildFileException (
681 "<xi:fallback> must have a <xi:include> sub-element" );
689 include
.fileExists
= true;
690 XMLElement
* new_e
= new XMLElement ( fInc
,
691 include
.e
->location
);
692 new_e
->name
= "xi:included";
693 Path
path2 ( include
.path
, att
->value
);
694 XMLReadFile ( *fInc
, *new_e
, includes
, path2
);
700 XMLLoadFile ( const string
& filename
,
702 XMLIncludes
& includes
)
704 XMLFile
* f
= new XMLFile();
706 if ( !f
->open ( filename
) )
707 throw FileNotFoundException ( filename
);
709 XMLElement
* head
= new XMLElement ( f
,
712 XMLReadFile ( *f
, *head
, includes
, path
);
714 for ( size_t i
= 0; i
< includes
.size (); i
++ )
716 XMLElement
* e
= includes
[i
]->e
;
717 XMLElement
* e2
= XMLLoadInclude ( *includes
[i
], includes
);
720 throw FileNotFoundException (
721 ssprintf ( "%s (referenced from %s)",
722 e
->GetAttribute ( "top_href", true )->value
.c_str (),
723 f
->Location ().c_str () ) );
725 XMLElement
* parent
= e
->parentElement
;
726 XMLElement
** parent_container
= NULL
;
730 throw Exception ( "internal tool error: xi:include doesn't have a parent" );
733 for ( size_t j
= 0; j
< parent
->subElements
.size (); j
++ )
735 if ( parent
->subElements
[j
] == e
)
737 parent_container
= &parent
->subElements
[j
];
741 if ( !parent_container
)
744 throw Exception ( "internal tool error: couldn't find xi:include in parent's sub-elements" );
747 // replace inclusion tree with the imported tree
748 e2
->parentElement
= e
->parentElement
;
750 e2
->attributes
= e
->attributes
;
751 *parent_container
= e2
;
752 e
->attributes
.resize ( 0 );