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 return Path::RelativeFromDirectory ( path
, working_directory
);
179 Path::RelativeFromDirectory (
181 const string
& base_directory
)
183 vector
<string
> vbase
, vpath
, vout
;
184 Path::Split ( vbase
, base_directory
, true );
185 Path::Split ( vpath
, path
, true );
187 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
188 // not possible to do relative across different drive letters
189 if ( vbase
[0] != vpath
[0] )
193 while ( i
< vbase
.size() && i
< vpath
.size() && vbase
[i
] == vpath
[i
] )
195 if ( vbase
.size() == vpath
.size() && i
== vpath
.size() )
197 if ( i
< vbase
.size() )
199 // path goes above our base directory, we will need some ..'s
200 for ( size_t j
= 0; j
< i
; j
++ )
201 vout
.push_back ( ".." );
203 while ( i
< vpath
.size() )
204 vout
.push_back ( vpath
[i
++] );
206 // now merge vout into a string again
207 string out
= vout
[0];
208 for ( i
= 1; i
< vout
.size(); i
++ )
210 out
+= "/" + vout
[i
];
216 Path::Split ( vector
<string
>& out
,
221 const char* prev
= strtok ( &s
[0], "/\\" );
222 const char* p
= strtok ( NULL
, "/\\" );
226 out
.push_back ( prev
);
228 p
= strtok ( NULL
, "/\\" );
231 out
.push_back ( prev
);
246 XMLFile::open(const string
& filename_
)
249 FILE* f
= fopen ( filename_
.c_str(), "rb" );
252 unsigned long len
= (unsigned long)filelen(f
);
254 fread ( &_buf
[0], 1, len
, f
);
258 _filename
= filename_
;
263 // next_token() moves the pointer to next token, which may be
264 // an xml element or a text element, basically it's a glorified
265 // skipspace, normally the user of this class won't need to call
268 XMLFile::next_token()
270 _p
+= strspn ( _p
, WS
);
274 XMLFile::next_is_text()
280 XMLFile::more_tokens()
285 // get_token() is used to return a token, and move the pointer
288 XMLFile::get_token(string
& token
)
291 if ( !strncmp ( _p
, "<!--", 4 ) )
293 tokend
= strstr ( _p
, "-->" );
299 else if ( !strncmp ( _p
, "<?", 2 ) )
301 tokend
= strstr ( _p
, "?>" );
307 else if ( *_p
== '<' )
309 tokend
= strchr ( _p
, '>' );
317 tokend
= strchr ( _p
, '<' );
320 while ( tokend
> _p
&& isspace(tokend
[-1]) )
325 token
= string ( _p
, tokend
-_p
);
332 XMLFile::Location() const
335 const char* p
= strchr ( _buf
.c_str(), '\n' );
336 while ( p
&& p
< _p
)
339 p
= strchr ( p
+1, '\n' );
341 return ssprintf ( "%s(%i)",_filename
.c_str(), line
);
344 XMLAttribute::XMLAttribute()
348 XMLAttribute::XMLAttribute(const string
& name_
,
349 const string
& value_
)
350 : name(name_
), value(value_
)
354 XMLAttribute::XMLAttribute ( const XMLAttribute
& src
)
355 : name(src
.name
), value(src
.value
)
360 XMLAttribute
& XMLAttribute::operator = ( const XMLAttribute
& src
)
367 XMLElement::XMLElement ( XMLFile
* xmlFile
,
368 const string
& location
)
369 : xmlFile ( xmlFile
),
370 location ( location
),
371 parentElement ( NULL
)
375 XMLElement::~XMLElement()
378 for ( i
= 0; i
< attributes
.size(); i
++ )
379 delete attributes
[i
];
380 for ( i
= 0; i
< subElements
.size(); i
++ )
381 delete subElements
[i
];
385 XMLElement::AddSubElement ( XMLElement
* e
)
387 subElements
.push_back ( e
);
388 e
->parentElement
= this;
392 // This function takes a single xml tag ( i.e. beginning with '<' and
393 // ending with '>', and parses out it's tag name and constituent
395 // Return Value: returns true if you need to look for a </tag> for
396 // the one it just parsed...
398 XMLElement::Parse(const string
& token
,
401 const char* p
= token
.c_str();
402 assert ( *p
== '<' );
404 p
+= strspn ( p
, WS
);
406 // check if this is a comment
407 if ( !strncmp ( p
, "!--", 3 ) )
411 return false; // never look for end tag to a comment
414 end_tag
= ( *p
== '/' );
418 p
+= strspn ( p
, WS
);
420 const char* end
= strpbrk ( p
, WS
);
423 end
= strpbrk ( p
, "/>" );
426 name
= string ( p
, end
-p
);
428 p
+= strspn ( p
, WS
);
429 while ( *p
!= '>' && *p
!= '/' )
431 end
= strpbrk ( p
, WSEQ
);
434 end
= strpbrk ( p
, "/>" );
437 string
attribute ( p
, end
-p
), value
;
439 p
+= strspn ( p
, WS
);
443 p
+= strspn ( p
, WS
);
445 if ( strchr ( "\"'", *p
) )
448 end
= strchr ( p
, quote
);
452 end
= strpbrk ( p
, WS
);
456 end
= strchr ( p
, '>' );
458 if ( end
[-1] == '/' )
461 value
= string ( p
, end
-p
);
463 if ( quote
&& *p
== quote
)
465 p
+= strspn ( p
, WS
);
467 else if ( name
[0] != '!' )
469 throw XMLSyntaxErrorException ( location
,
470 "attributes must have values" );
472 attributes
.push_back ( new XMLAttribute ( attribute
, value
) );
474 return !( *p
== '/' ) && !end_tag
;
478 XMLElement::GetAttribute ( const string
& attribute
,
481 // this would be faster with a tree-based container, but our attribute
482 // lists are likely to stay so short as to not be an issue.
483 for ( size_t i
= 0; i
< attributes
.size(); i
++ )
485 if ( attribute
== attributes
[i
]->name
)
486 return attributes
[i
];
490 throw RequiredAttributeNotFoundException ( location
,
498 XMLElement::GetAttribute ( const string
& attribute
,
499 bool required
) const
501 // this would be faster with a tree-based container, but our attribute
502 // lists are likely to stay so short as to not be an issue.
503 for ( size_t i
= 0; i
< attributes
.size(); i
++ )
505 if ( attribute
== attributes
[i
]->name
)
506 return attributes
[i
];
510 throw RequiredAttributeNotFoundException ( location
,
518 // This function reads a "token" from the file loaded in XMLFile
519 // if it finds a tag that is non-singular, it parses sub-elements and/or
520 // inner text into the XMLElement that it is building to return.
521 // Return Value: an XMLElement allocated via the new operator that contains
522 // it's parsed data. Keep calling this function until it returns NULL
525 XMLParse ( XMLFile
& f
,
526 XMLIncludes
* includes
,
528 bool* pend_tag
= NULL
)
531 if ( !f
.get_token(token
) )
533 bool end_tag
, is_include
= false;
535 while ( token
[0] != '<'
536 || !strncmp ( token
.c_str (), "<!--", 4 )
537 || !strncmp ( token
.c_str (), "<?", 2 ) )
539 if ( token
[0] != '<' )
540 throw XMLSyntaxErrorException ( f
.Location (),
541 "expecting xml tag, not '%s'",
543 if ( !f
.get_token(token
) )
547 XMLElement
* e
= new XMLElement ( &f
,
549 bool bNeedEnd
= e
->Parse ( token
, end_tag
);
551 if ( e
->name
== "xi:include" && includes
)
554 att
= e
->GetAttribute ( "href", true );
556 string
includeFile ( path
.Fixup ( att
->value
, true ) );
557 string
topIncludeFile ( Path::RelativeFromWorkingDirectory ( includeFile
) );
558 includes
->push_back ( new XMLInclude ( e
, path
, topIncludeFile
) );
569 throw XMLSyntaxErrorException ( f
.Location (),
570 "end tag '%s' not expected",
576 bool bThisMixingErrorReported
= false;
577 while ( f
.more_tokens () )
579 if ( f
.next_is_text () )
581 if ( !f
.get_token ( token
) || token
.size () == 0 )
583 throw InvalidBuildFileException (
585 "internal tool error - get_token() failed when more_tokens() returned true" );
588 if ( e
->subElements
.size() && !bThisMixingErrorReported
)
590 throw XMLSyntaxErrorException ( f
.Location (),
591 "mixing of inner text with sub elements" );
592 bThisMixingErrorReported
= true;
594 if ( strchr ( token
.c_str (), '>' ) )
596 throw XMLSyntaxErrorException ( f
.Location (),
597 "invalid symbol '>'" );
599 if ( e
->value
.size() > 0 )
601 throw XMLSyntaxErrorException ( f
.Location (),
602 "multiple instances of inner text" );
603 e
->value
+= " " + token
;
610 XMLElement
* e2
= XMLParse ( f
, is_include
? NULL
: includes
, path
, &end_tag
);
613 throw InvalidBuildFileException (
615 "end of file found looking for end tag" );
620 if ( e
->name
!= e2
->name
)
623 throw XMLSyntaxErrorException ( f
.Location (),
624 "end tag name mismatch" );
630 if ( e
->value
.size () > 0 && !bThisMixingErrorReported
)
632 throw XMLSyntaxErrorException ( f
.Location (),
633 "mixing of inner text with sub elements" );
634 bThisMixingErrorReported
= true;
636 e
->AddSubElement ( e2
);
643 XMLReadFile ( XMLFile
& f
, XMLElement
& head
, XMLIncludes
& includes
, const Path
& path
)
647 XMLElement
* e
= XMLParse ( f
, &includes
, path
);
650 head
.AddSubElement ( e
);
655 XMLLoadInclude ( XMLInclude
& include
,
656 XMLIncludes
& includes
)
659 att
= include
.e
->GetAttribute("href", true);
662 string
file ( include
.path
.Fixup(att
->value
, true) );
663 string
top_file ( Path::RelativeFromWorkingDirectory ( file
) );
664 include
.e
->attributes
.push_back ( new XMLAttribute ( "top_href", top_file
) );
665 XMLFile
* fInc
= new XMLFile();
666 if ( !fInc
->open ( file
) )
668 include
.fileExists
= false;
669 // look for xi:fallback element
670 for ( size_t i
= 0; i
< include
.e
->subElements
.size (); i
++ )
672 XMLElement
* e2
= include
.e
->subElements
[i
];
673 if ( e2
->name
== "xi:fallback" )
675 // now look for xi:include below...
676 for ( i
= 0; i
< e2
->subElements
.size (); i
++ )
678 XMLElement
* e3
= e2
->subElements
[i
];
679 if ( e3
->name
== "xi:include" )
681 att
= e3
->GetAttribute ( "href", true );
683 string
includeFile ( include
.path
.Fixup ( att
->value
, true ) );
684 string
topIncludeFile ( Path::RelativeFromWorkingDirectory ( includeFile
) );
685 XMLInclude
* fallbackInclude
= new XMLInclude ( e3
, include
.path
, topIncludeFile
);
686 return XMLLoadInclude ( *fallbackInclude
, includes
);
689 throw InvalidBuildFileException (
691 "<xi:fallback> must have a <xi:include> sub-element" );
699 include
.fileExists
= true;
700 XMLElement
* new_e
= new XMLElement ( fInc
,
701 include
.e
->location
);
702 new_e
->name
= "xi:included";
703 Path
path2 ( include
.path
, att
->value
);
704 XMLReadFile ( *fInc
, *new_e
, includes
, path2
);
710 XMLLoadFile ( const string
& filename
,
712 XMLIncludes
& includes
)
714 XMLFile
* f
= new XMLFile();
716 if ( !f
->open ( filename
) )
717 throw FileNotFoundException ( filename
);
719 XMLElement
* head
= new XMLElement ( f
,
722 XMLReadFile ( *f
, *head
, includes
, path
);
724 for ( size_t i
= 0; i
< includes
.size (); i
++ )
726 XMLElement
* e
= includes
[i
]->e
;
727 XMLElement
* e2
= XMLLoadInclude ( *includes
[i
], includes
);
730 throw FileNotFoundException (
731 ssprintf ( "%s (referenced from %s)",
732 e
->GetAttribute ( "top_href", true )->value
.c_str (),
733 f
->Location ().c_str () ) );
735 XMLElement
* parent
= e
->parentElement
;
736 XMLElement
** parent_container
= NULL
;
740 throw Exception ( "internal tool error: xi:include doesn't have a parent" );
743 for ( size_t j
= 0; j
< parent
->subElements
.size (); j
++ )
745 if ( parent
->subElements
[j
] == e
)
747 parent_container
= &parent
->subElements
[j
];
751 if ( !parent_container
)
754 throw Exception ( "internal tool error: couldn't find xi:include in parent's sub-elements" );
757 // replace inclusion tree with the imported tree
758 e2
->parentElement
= e
->parentElement
;
760 e2
->attributes
= e
->attributes
;
761 *parent_container
= e2
;
762 e
->attributes
.resize ( 0 );