1
// $Id: DtdParser.C,v 1.1 2000/06/08 06:08:58 peticolas Exp $
2
// Copyright (C) 1997 ISOGEN International Corp. and TechnoTeacher, Inc.
3
// All Rights Reserved.
6
// This file and its associated materials are copyrighted material of
7
// ISOGEN International Corp. (ISOGEN) and TechnoTeacher,
8
// Inc. (TechnoTeacher). License to copy and use this file and its
9
// associated materials is granted to everyone, free of charge, with
10
// the following restrictions: (1) The ISOGEN and TechnoTeacher
11
// copyright statement must be maintained in any copies. (2) New
12
// materials derived from these materials must indicate their source,
13
// including references to the ISOGEN and TechnoTeacher web sites
14
// (www.isogen.com and www.techno.com). (3) These materials may not be
15
// sold in any form without the express written permission of ISOGEN
16
// and TechnoTeacher. [However, feel free to sell things you create
17
// from these materials as long as the things you create are truly
18
// different in function--we want to encourage people to learn from
19
// these materials and benefit from having learned--we just don't want
20
// others to sell what we're giving away.]
24
#include "DtdParser.h"
28
#include "CodingSystemKit.h"
30
#include "PosixStorage.h"
32
#include "WinInetStorage.h"
34
#include "URLStorage.h"
36
#include "LiteralStorage.h"
37
#include "NotationStorage.h"
38
#include "ExtendEntityManager.h"
39
#include "SOEntityCatalog.h"
41
#include "ErrorCountEventHandler.h"
43
#ifndef SP_DEFAULT_ENCODING
45
#define SP_DEFAULT_ENCODING SP_T("WINDOWS")
47
#define SP_DEFAULT_ENCODING SP_T("IS8859-1")
49
#endif /* not SP_DEFAULT_ENCODING */
51
#ifndef SGML_SEARCH_PATH_DEFAULT
52
#define SGML_SEARCH_PATH_DEFAULT SP_T("")
55
#ifndef SGML_CATALOG_FILES_DEFAULT
56
#define SGML_CATALOG_FILES_DEFAULT SP_T("")
57
#endif /* not SGML_CATALOG_FILES_DEFAULT */
60
namespace SP_NAMESPACE {
63
#ifdef SP_MSDOS_FILENAMES
64
const Char FILE_SEP = ';';
66
const Char FILE_SEP = ':';
70
DtdParser( const char * requiredInternalCode)
71
: internalCharsetIsDocCharset_( 1),
73
mapCatalogDocument_(0),
76
initCodingSystem( requiredInternalCode);
81
setMessenger( Messenger * mgr)
83
mgr_ = mgr ? mgr : &nullMgr_;
86
class DtdParserEventHandler
87
: public ErrorCountEventHandler
90
DtdParserEventHandler( Messenger *);
91
void message( MessageEvent *);
96
DtdParserEventHandler::
97
DtdParserEventHandler( Messenger * mgr)
103
DtdParserEventHandler::
104
message( MessageEvent * event)
106
// ignore ParserMessages::documentEndProlog and ParserMessages::documentElementUndefined
107
if ( event->message().type->number() != 45 && event->message().type->number() != 319) {
108
mgr_->dispatchMessage( event->message());
110
ErrorCountEventHandler::message( event);
115
parseDtd( const StringC & sysid)
117
DtdParserEventHandler handler( mgr_);
119
SgmlParser::Params sdParams;
120
sdParams.sysid = convertInput( SP_T( "<LITERAL> "));
121
sdParams.entityManager = entityManager().pointer();
122
sdParams.options = &options_;
123
sdParser_.init( sdParams);
124
sdParser_.parseAll( handler, handler.cancelPtr());
126
SgmlParser::Params params;
127
params.sysid = sysid;
128
params.entityType = SgmlParser::Params::dtd;
129
// unnecessary, since params.doctypeName starts out empty anyway.
130
// params.doctypeName = convertInput( SP_T( ""));
131
params.entityManager = entityManager().pointer();
132
params.options = &options_;
133
params.parent = &sdParser_;
134
parser_.init( params);
135
parser_.parseAll( handler, handler.cancelPtr());
137
Ptr< Dtd> dtd( parser_.baseDtd());
138
delete dtd->removeElementType( params.doctypeName);
144
stringMatches( const SP_TCHAR * s, const char * key)
146
for (; *key != '\0'; s++, key++) {
147
if (*s != tolower(*key) && *s != toupper(*key))
155
initCodingSystem( const char * requiredInternalCode)
157
const char *name = requiredInternalCode;
161
const SP_TCHAR *internalCode = tgetenv(SP_T("SP_SYSTEM_CHARSET"));
164
for (size_t i = 0; i < 255; i++) {
165
buf[i] = internalCode[i];
172
if (requiredInternalCode)
173
internalCharsetIsDocCharset_ = 0;
175
const SP_TCHAR *useInternal = tgetenv(SP_T("SP_CHARSET_FIXED"));
177
&& (stringMatches(useInternal, "YES")
178
|| stringMatches(useInternal, "1")))
179
internalCharsetIsDocCharset_ = 0;
181
#endif /* SP_MULTI_BYTE */
182
codingSystemKit_ = CodingSystemKit::make(name);
183
const SP_TCHAR *codingName = tgetenv(internalCharsetIsDocCharset_
185
: SP_T("SP_ENCODING"));
187
codingSystem_ = lookupCodingSystem(codingName);
189
if (!codingSystem_ && !internalCharsetIsDocCharset_)
190
codingSystem_ = lookupCodingSystem(SP_DEFAULT_ENCODING);
193
#ifndef SP_WIDE_SYSTEM
194
|| codingSystem_->fixedBytesPerChar() > 1
197
codingSystem_ = codingSystemKit_->identityCodingSystem();
202
lookupCodingSystem( const AppChar * codingName)
204
#define MAX_CS_NAME 50
205
if (tcslen(codingName) < MAX_CS_NAME) {
206
char buf[MAX_CS_NAME];
208
for (i = 0; codingName[i] != SP_T('\0'); i++) {
209
SP_TUCHAR c = codingName[i];
210
#ifdef SP_WIDE_SYSTEM
211
if (c > (unsigned char)-1)
217
return codingSystemKit_->makeCodingSystem(buf, internalCharsetIsDocCharset_);
224
convertInput( const SP_TCHAR * s)
226
#ifdef SP_WIDE_SYSTEM
227
StringC str(s, wcslen(s));
229
StringC str(codingSystem()->convertIn(s));
231
for (size_t i = 0; i < str.size(); i++)
239
makeSystemId( int nFiles, AppChar * const * files, StringC & result)
241
Vector<StringC> filenames(nFiles == 0 ? 1 : nFiles);
243
for (i = 0; i < nFiles; i++)
244
filenames[i] = convertInput(tcscmp(files[i], SP_T("-")) == 0
248
filenames[0] = convertInput(SP_T("<OSFD>0"));
249
return entityManager()->mergeSystemIds(filenames,
256
Ptr< ExtendEntityManager> &
260
if (!entityManager_.isNull())
261
return entityManager_;
262
PosixStorageManager *sm
263
= new PosixStorageManager("OSFILE",
265
#ifndef SP_WIDE_SYSTEM
270
for (i = 0; i < searchDirs_.size(); i++)
271
sm->addSearchDir(convertInput(searchDirs_[i]));
273
const AppChar *e = tgetenv(SP_T("SGML_SEARCH_PATH"));
275
e = SGML_SEARCH_PATH_DEFAULT;
277
StringC str(convertInput(e));
281
if (i == str.size() || str[i] == FILE_SEP) {
282
sm->addSearchDir(StringC(str.data() + start,
294
entityManager_ = ExtendEntityManager::make(sm,
296
inputCodingSystemKit(),
297
internalCharsetIsDocCharset_);
299
entityManager_->registerStorageManager(new WinInetStorageManager("URL"));
301
entityManager_->registerStorageManager(new URLStorageManager("URL"));
303
entityManager_->registerStorageManager(new LiteralStorageManager("LITERAL"));
304
entityManager_->registerStorageManager(new NotationStorageManager("CLSID"));
305
entityManager_->registerStorageManager(new NotationStorageManager("MIMETYPE"));
307
for (i = 0; i < catalogSysids_.size(); i++)
308
// filenames specified on command-line must exist
309
v.push_back(convertInput(catalogSysids_[i]));
311
const AppChar *e = tgetenv(SP_T("SGML_CATALOG_FILES"));
313
e = SGML_CATALOG_FILES_DEFAULT;
315
StringC str(convertInput(e));
319
if (i == str.size() || str[i] == FILE_SEP) {
320
v.push_back(StringC(str.data() + start,
331
entityManager_->setCatalogManager(SOCatalogManager::make(v,
332
catalogSysids_.size(),
336
return entityManager_;