📄 sgmlseventhandler.cxx
字号:
// Copyright (c) 1994 James Clark// See the file COPYING for copying permission.#include "config.h"#include "SgmlsEventHandler.h"#include "SgmlParser.h"#include "ParserOptions.h"#include "Entity.h"#include "Notation.h"#include "Attribute.h"#include "ExtendEntityManager.h"#include "StorageManager.h"#include "macros.h"#ifdef SP_NAMESPACEnamespace SP_NAMESPACE {#endifconst char dataCode = '-';const char piCode = '?';const char conformingCode = 'C';const char appinfoCode = '#';const char startElementCode = '(';const char endElementCode = ')';const char referenceEntityCode = '&';const char attributeCode = 'A';const char dataAttributeCode = 'D';const char linkAttributeCode = 'a';const char defineNotationCode = 'N';const char defineExternalEntityCode = 'E';const char defineInternalEntityCode = 'I';const char defineSubdocEntityCode = 'S';const char defineExternalTextEntityCode = 'T';const char pubidCode = 'p';const char sysidCode = 's';const char startSubdocCode = '{';const char endSubdocCode = '}';const char fileCode = 'f';const char locationCode = 'L';const char includedElementCode = 'i';const char emptyElementCode = 'e';const OutputCharStream::Newline nl = OutputCharStream::newline;const char space = ' ';const Char re = '\r';inlinevoid SgmlsEventHandler::startData(){ if (!haveData_) { os() << dataCode; haveData_ = 1; }}inlinevoid SgmlsEventHandler::flushData(){ if (haveData_) { os() << nl; haveData_ = 0; }}inlinevoid SgmlsEventHandler::outputLocation(const Location &loc){ if (outputLine_) outputLocation1(loc);}SgmlsEventHandler::SgmlsEventHandler(const SgmlParser *parser, OutputCharStream *os, Messenger *messenger, unsigned outputFlags): SgmlsSubdocState(parser), os_(os), messenger_(messenger), outputLine_((outputFlags & outputLine) != 0), outputEntity_((outputFlags & outputEntity) != 0), outputId_((outputFlags & outputId) != 0), outputNotationSysid_((outputFlags & outputNotationSysid) != 0), outputIncluded_((outputFlags & outputIncluded) != 0), outputNonSgml_((outputFlags & outputNonSgml) != 0), outputEmpty_((outputFlags & outputEmpty) != 0), haveData_(0), lastSos_(0){ os_->setEscaper(escape);}SgmlsEventHandler::~SgmlsEventHandler(){ flushData(); if (errorCount() == 0) os() << conformingCode << nl; delete os_;}void SgmlsEventHandler::message(MessageEvent *event){ messenger_->dispatchMessage(event->message()); ErrorCountEventHandler::message(event);}void SgmlsEventHandler::appinfo(AppinfoEvent *event){ const StringC *str; if (event->literal(str)) { outputLocation(event->location()); flushData(); os() << appinfoCode; outputString(*str); os() << nl; } delete event;}void SgmlsEventHandler::endProlog(EndPrologEvent *event){ if (outputEntity_) { flushData(); const Dtd &dtd = event->dtd(); Dtd::ConstEntityIter iter(dtd.generalEntityIter()); for (;;) { const Entity *entity = iter.next().pointer(); if (!entity) break; defineEntity(entity); } } if (!event->lpdPointer().isNull()) { linkProcess_.init(event->lpdPointer()); haveLinkProcess_ = 1; flushData(); } for (size_t i = 0; i < event->simpleLinkNames().size(); i++) { flushData(); attributes(event->simpleLinkAttributes()[i], linkAttributeCode, &event->simpleLinkNames()[i]); } delete event;}void SgmlsEventHandler::entityDefaulted(EntityDefaultedEvent *event){ if (outputEntity_) { flushData(); defineEntity(event->entityPointer().pointer()); } delete event;}void SgmlsEventHandler::uselink(UselinkEvent *event){ linkProcess_.uselink(event->linkSet(), event->restore(), event->lpd().pointer()); delete event;}void SgmlsEventHandler::sgmlDecl(SgmlDeclEvent *event){ sd_ = event->sdPointer(); syntax_ = event->instanceSyntaxPointer(); // FIXME which syntax? delete event;}void SgmlsEventHandler::data(DataEvent *event){ outputLocation(event->location()); startData(); outputString(event->data(), event->dataLength()); delete event;}void SgmlsEventHandler::sdataEntity(SdataEntityEvent *event){ outputLocation(event->location()); startData(); os() << "\\|"; outputString(event->data(), event->dataLength()); os() << "\\|"; delete event;}void SgmlsEventHandler::pi(PiEvent *event){ outputLocation(event->location()); flushData(); os() << piCode; outputString(event->data(), event->dataLength()); os() << nl; delete event;}void SgmlsEventHandler::nonSgmlChar(NonSgmlCharEvent *event){ if (outputNonSgml_) { outputLocation(event->location()); startData(); os() << "\\%" << (unsigned long)event->character() << ';'; } delete event;}void SgmlsEventHandler::startElement(StartElementEvent *event){ flushData(); currentLocation_ = event->location(); if (haveLinkProcess_) { const AttributeList *linkAttributes; const ResultElementSpec *resultElementSpec; linkProcess_.startElement(event->elementType(), event->attributes(), event->location(), *this, // Messenger & linkAttributes, resultElementSpec); if (linkAttributes) attributes(*linkAttributes, linkAttributeCode, &linkProcess_.name()); } attributes(event->attributes(), attributeCode, 0); currentLocation_.clear(); if (outputIncluded_ && event->included()) os() << includedElementCode << nl; if (outputEmpty_ && event->mustOmitEnd()) os() << emptyElementCode << nl; outputLocation(event->location()); os() << startElementCode << event->name() << nl; delete event;}void SgmlsEventHandler::attributes(const AttributeList &attributes, char code, const StringC *ownerName){ size_t nAttributes = attributes.size(); for (size_t i = 0; i < nAttributes; i++) { const Text *text; const StringC *string; const AttributeValue *value = attributes.value(i); if (value) { switch (value->info(text, string)) { case AttributeValue::implied: startAttribute(attributes.name(i), code, ownerName); os() << "IMPLIED" << nl; break; case AttributeValue::tokenized: { const char *typeString = "TOKEN"; const AttributeSemantics *semantics = attributes.semantics(i); if (semantics) { ConstPtr<Notation> notation = semantics->notation(); if (!notation.isNull()) { defineNotation(notation.pointer()); typeString = "NOTATION"; } else { size_t nEntities = semantics->nEntities(); if (nEntities) { typeString = "ENTITY"; if (!outputEntity_) for (size_t i = 0; i < nEntities; i++) { const Entity *entity = semantics->entity(i).pointer(); if (!markEntity(entity)) defineEntity(entity); } } } } if (outputId_ && attributes.id(i)) typeString = "ID"; startAttribute(attributes.name(i), code, ownerName); os() << typeString << space << *string << nl; } break; case AttributeValue::cdata: { startAttribute(attributes.name(i), code, ownerName); os() << "CDATA "; TextIter iter(*text); TextItem::Type type; const Char *p; size_t length; const Location *loc; while (iter.next(type, p, length, loc)) switch (type) { case TextItem::data: case TextItem::cdata: outputString(p, length); break; case TextItem::sdata: os() << "\\|"; outputString(p, length); os() << "\\|"; break; case TextItem::nonSgml: if (outputNonSgml_) os() << "\\%" << (unsigned long)*p << ';'; break; default: break; } os() << nl; } break; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -