10 #include "exception.h"
17 #define getcwd _getcwd
20 static const char* WS
= " \t\r\n";
21 static const char* WSEQ
= " =\t\r\n";
23 string working_directory
;
31 XMLInclude ( XMLElement
* e_
, const Path
& path_
)
37 class XMLIncludes
: public vector
<XMLInclude
*>
42 for ( size_t i
= 0; i
< this->size(); i
++ )
48 InitWorkingDirectory()
50 // store the current directory for path calculations
51 working_directory
.resize ( _MAX_PATH
);
52 working_directory
[0] = 0;
53 getcwd ( &working_directory
[0], working_directory
.size() );
54 working_directory
.resize ( strlen ( working_directory
.c_str() ) );
65 return _filelengthi64 ( _fileno(f
) );
67 struct stat64 file_stat
;
68 if ( fstat64(fileno(f
), &file_stat
) != 0 )
70 return file_stat
.st_size
;
76 if ( !working_directory
.size() )
77 InitWorkingDirectory();
78 string
s ( working_directory
);
79 const char* p
= strtok ( &s
[0], "/\\" );
84 p
= strtok ( NULL
, "/\\" );
88 Path::Path ( const Path
& cwd
, const string
& file
)
90 string
s ( cwd
.Fixup ( file
, false ) );
91 const char* p
= strtok ( &s
[0], "/\\" );
96 p
= strtok ( NULL
, "/\\" );
101 Path::Fixup ( const string
& file
, bool include_filename
) const
103 if ( strchr ( "/\\", file
[0] )
105 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
112 vector
<string
> pathtmp ( path
);
114 const char* prev
= strtok ( &tmp
[0], "/\\" );
115 const char* p
= strtok ( NULL
, "/\\" );
118 if ( !strcmp ( prev
, "." ) )
120 else if ( !strcmp ( prev
, ".." ) )
122 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
124 if ( pathtmp
.size() > 1 )
126 if ( pathtmp
.size() )
128 pathtmp
.resize ( pathtmp
.size() - 1 );
131 pathtmp
.push_back ( prev
);
133 p
= strtok ( NULL
, "/\\" );
135 if ( include_filename
)
136 pathtmp
.push_back ( prev
);
138 // reuse tmp variable to return recombined path
140 for ( size_t i
= 0; i
< pathtmp
.size(); i
++ )
142 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
154 Path::RelativeFromWorkingDirectory ( const string
& path
)
156 vector
<string
> vwork
, vpath
, vout
;
157 Path::Split ( vwork
, working_directory
, true );
158 Path::Split ( vpath
, path
, true );
160 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
161 // not possible to do relative across different drive letters
162 if ( vwork
[0] != vpath
[0] )
166 while ( i
< vwork
.size() && i
< vpath
.size() && vwork
[i
] == vpath
[i
] )
168 if ( i
< vwork
.size() )
170 // path goes above our working directory, we will need some ..'s
171 for ( size_t j
= 0; j
< i
; j
++ )
172 vout
.push_back ( ".." );
174 while ( i
< vpath
.size() )
175 vout
.push_back ( vpath
[i
++] );
177 // now merge vout into a string again
179 for ( i
= 0; i
< vout
.size(); i
++ )
181 // this squirreliness is b/c win32 has drive letters and *nix doesn't...
193 Path::Split ( vector
<string
>& out
,
198 const char* prev
= strtok ( &s
[0], "/\\" );
199 const char* p
= strtok ( NULL
, "/\\" );
203 out
.push_back ( prev
);
205 p
= strtok ( NULL
, "/\\" );
208 out
.push_back ( prev
);
223 XMLFile::open(const string
& filename_
)
226 FILE* f
= fopen ( filename_
.c_str(), "rb" );
229 unsigned long len
= (unsigned long)filelen(f
);
231 fread ( &_buf
[0], 1, len
, f
);
235 _filename
= filename_
;
240 // next_token() moves the pointer to next token, which may be
241 // an xml element or a text element, basically it's a glorified
242 // skipspace, normally the user of this class won't need to call
245 XMLFile::next_token()
247 _p
+= strspn ( _p
, WS
);
251 XMLFile::next_is_text()
257 XMLFile::more_tokens()
262 // get_token() is used to return a token, and move the pointer
265 XMLFile::get_token(string
& token
)
268 if ( !strncmp ( _p
, "<!--", 4 ) )
270 tokend
= strstr ( _p
, "-->" );
276 else if ( !strncmp ( _p
, "<?", 2 ) )
278 tokend
= strstr ( _p
, "?>" );
284 else if ( *_p
== '<' )
286 tokend
= strchr ( _p
, '>' );
294 tokend
= strchr ( _p
, '<' );
297 while ( tokend
> _p
&& isspace(tokend
[-1]) )
302 token
= string ( _p
, tokend
-_p
);
309 XMLFile::Location() const
312 const char* p
= strchr ( _buf
.c_str(), '\n' );
313 while ( p
&& p
< _p
)
316 p
= strchr ( p
+1, '\n' );
318 return ssprintf ( "%s(%i)",_filename
.c_str(), line
);
321 XMLAttribute::XMLAttribute()
325 XMLAttribute::XMLAttribute(const string
& name_
,
326 const string
& value_
)
327 : name(name_
), value(value_
)
331 XMLAttribute::XMLAttribute ( const XMLAttribute
& src
)
332 : name(src
.name
), value(src
.value
)
337 XMLAttribute
& XMLAttribute::operator = ( const XMLAttribute
& src
)
344 XMLElement::XMLElement ( const string
& location_
)
345 : location(location_
),
350 XMLElement::~XMLElement()
353 for ( i
= 0; i
< attributes
.size(); i
++ )
354 delete attributes
[i
];
355 for ( i
= 0; i
< subElements
.size(); i
++ )
356 delete subElements
[i
];
360 XMLElement::AddSubElement ( XMLElement
* e
)
362 subElements
.push_back ( e
);
363 e
->parentElement
= this;
367 // This function takes a single xml tag ( i.e. beginning with '<' and
368 // ending with '>', and parses out it's tag name and constituent
370 // Return Value: returns true if you need to look for a </tag> for
371 // the one it just parsed...
373 XMLElement::Parse(const string
& token
,
376 const char* p
= token
.c_str();
377 assert ( *p
== '<' );
379 p
+= strspn ( p
, WS
);
381 // check if this is a comment
382 if ( !strncmp ( p
, "!--", 3 ) )
386 return false; // never look for end tag to a comment
389 end_tag
= ( *p
== '/' );
393 p
+= strspn ( p
, WS
);
395 const char* end
= strpbrk ( p
, WS
);
398 end
= strpbrk ( p
, "/>" );
401 name
= string ( p
, end
-p
);
403 p
+= strspn ( p
, WS
);
404 while ( *p
!= '>' && *p
!= '/' )
406 end
= strpbrk ( p
, WSEQ
);
409 end
= strpbrk ( p
, "/>" );
412 string
attribute ( p
, end
-p
), value
;
414 p
+= strspn ( p
, WS
);
418 p
+= strspn ( p
, WS
);
420 if ( strchr ( "\"'", *p
) )
423 end
= strchr ( p
, quote
);
427 end
= strpbrk ( p
, WS
);
431 end
= strchr ( p
, '>' );
433 if ( end
[-1] == '/' )
436 value
= string ( p
, end
-p
);
438 if ( quote
&& *p
== quote
)
440 p
+= strspn ( p
, WS
);
442 attributes
.push_back ( new XMLAttribute ( attribute
, value
) );
444 return !( *p
== '/' ) && !end_tag
;
448 XMLElement::GetAttribute ( const string
& attribute
,
451 // this would be faster with a tree-based container, but our attribute
452 // lists are likely to stay so short as to not be an issue.
453 for ( size_t i
= 0; i
< attributes
.size(); i
++ )
455 if ( attribute
== attributes
[i
]->name
)
456 return attributes
[i
];
460 throw RequiredAttributeNotFoundException ( location
,
468 XMLElement::GetAttribute ( const string
& attribute
,
469 bool required
) const
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 // This function reads a "token" from the file loaded in XMLFile
489 // if it finds a tag that is non-singular, it parses sub-elements and/or
490 // inner text into the XMLElement that it is building to return.
491 // Return Value: an XMLElement allocated via the new operator that contains
492 // it's parsed data. Keep calling this function until it returns NULL
496 XMLIncludes
* includes
,
498 bool* pend_tag
= NULL
)
501 if ( !f
.get_token(token
) )
503 bool end_tag
, is_include
= false;
505 while ( token
[0] != '<'
506 || !strncmp ( token
.c_str(), "<!--", 4 )
507 || !strncmp ( token
.c_str(), "<?", 2 ) )
509 if ( token
[0] != '<' )
510 throw XMLSyntaxErrorException ( f
.Location(),
511 "expecting xml tag, not '%s'",
513 if ( !f
.get_token(token
) )
517 XMLElement
* e
= new XMLElement ( f
.Location() );
518 bool bNeedEnd
= e
->Parse ( token
, end_tag
);
520 if ( e
->name
== "xi:include" && includes
)
522 includes
->push_back ( new XMLInclude ( e
, path
) );
533 throw XMLSyntaxErrorException ( f
.Location(),
534 "end tag '%s' not expected",
540 bool bThisMixingErrorReported
= false;
541 while ( f
.more_tokens() )
543 if ( f
.next_is_text() )
545 if ( !f
.get_token ( token
) || !token
.size() )
547 throw InvalidBuildFileException (
549 "internal tool error - get_token() failed when more_tokens() returned true" );
552 if ( e
->subElements
.size() && !bThisMixingErrorReported
)
554 throw XMLSyntaxErrorException ( f
.Location(),
555 "mixing of inner text with sub elements" );
556 bThisMixingErrorReported
= true;
558 if ( strchr ( token
.c_str(), '>' ) )
560 throw XMLSyntaxErrorException ( f
.Location(),
561 "invalid symbol '>'" );
563 if ( e
->value
.size() )
565 throw XMLSyntaxErrorException ( f
.Location(),
566 "multiple instances of inner text" );
567 e
->value
+= " " + token
;
574 XMLElement
* e2
= XMLParse ( f
, is_include
? NULL
: includes
, path
, &end_tag
);
577 throw InvalidBuildFileException (
579 "end of file found looking for end tag" );
584 if ( e
->name
!= e2
->name
)
587 throw XMLSyntaxErrorException ( f
.Location(),
588 "end tag name mismatch" );
594 if ( e
->value
.size() && !bThisMixingErrorReported
)
596 throw XMLSyntaxErrorException ( f
.Location(),
597 "mixing of inner text with sub elements" );
598 bThisMixingErrorReported
= true;
600 e
->AddSubElement ( e2
);
607 XMLReadFile ( XMLFile
& f
, XMLElement
& head
, XMLIncludes
& includes
, const Path
& path
)
611 XMLElement
* e
= XMLParse ( f
, &includes
, path
);
614 head
.AddSubElement ( e
);
619 XMLLoadInclude ( XMLElement
* e
, const Path
& path
, XMLIncludes
& includes
)
623 att
= e
->GetAttribute("href",true);
626 string
file ( path
.Fixup(att
->value
,true) );
627 string
top_file ( Path::RelativeFromWorkingDirectory ( file
) );
628 e
->attributes
.push_back ( new XMLAttribute ( "top_href", top_file
) );
630 if ( !fInc
.open ( file
) )
632 // look for xi:fallback element
633 for ( size_t i
= 0; i
< e
->subElements
.size(); i
++ )
635 XMLElement
* e2
= e
->subElements
[i
];
636 if ( e2
->name
== "xi:fallback" )
638 // now look for xi:include below...
639 for ( i
= 0; i
< e2
->subElements
.size(); i
++ )
641 XMLElement
* e3
= e2
->subElements
[i
];
642 if ( e3
->name
== "xi:include" )
644 return XMLLoadInclude ( e3
, path
, includes
);
647 throw InvalidBuildFileException (
649 "<xi:fallback> must have a <xi:include> sub-element" );
657 XMLElement
* new_e
= new XMLElement ( e
->location
);
658 new_e
->name
= "xi:included";
659 Path
path2 ( path
, att
->value
);
660 XMLReadFile ( fInc
, *new_e
, includes
, path2
);
666 XMLLoadFile ( const string
& filename
, const Path
& path
)
668 XMLIncludes includes
;
671 if ( !f
.open ( filename
) )
672 throw FileNotFoundException ( filename
);
674 XMLElement
* head
= new XMLElement("(virtual)");
676 XMLReadFile ( f
, *head
, includes
, path
);
678 for ( size_t i
= 0; i
< includes
.size(); i
++ )
680 XMLElement
* e
= includes
[i
]->e
;
681 XMLElement
* e2
= XMLLoadInclude ( includes
[i
]->e
, includes
[i
]->path
, includes
);
684 throw FileNotFoundException (
685 ssprintf("%s (referenced from %s)",
686 e
->GetAttribute("top_href",true)->value
.c_str(),
687 f
.Location().c_str() ) );
689 XMLElement
* parent
= e
->parentElement
;
690 XMLElement
** parent_container
= NULL
;
694 throw Exception ( "internal tool error: xi:include doesn't have a parent" );
697 for ( size_t j
= 0; j
< parent
->subElements
.size(); j
++ )
699 if ( parent
->subElements
[j
] == e
)
701 parent_container
= &parent
->subElements
[j
];
705 if ( !parent_container
)
708 throw Exception ( "internal tool error: couldn't find xi:include in parent's sub-elements" );
711 // replace inclusion tree with the imported tree
712 e2
->parentElement
= e
->parentElement
;
714 e2
->attributes
= e
->attributes
;
715 *parent_container
= e2
;
716 e
->attributes
.resize(0);