1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
// Copyright (c) 1994 James Clark
// See the file COPYING for copying permission.
#ifdef __GNUG__
#pragma implementation
#endif
#include "splib.h"
#include "LinkProcess.h"
// ParserState is used for access to parser messages
#include "ParserState.h"
#include "MessageArg.h"
#include "ParserMessages.h"
#ifdef SP_NAMESPACE
namespace SP_NAMESPACE {
#endif
LinkProcess::LinkProcess()
{
}
void LinkProcess::init(const ConstPtr<ComplexLpd> &lpd)
{
lpd_ = lpd;
open_.clear();
open_.insert(new LinkProcessOpenElement(lpd_->initialLinkSet()));
}
Boolean LinkProcess::startElement(const ElementType *element,
const AttributeList &attributes,
const Location &location,
Messenger &mgr,
const AttributeList *&linkAttributes,
const ResultElementSpec *&resultElementSpec)
{
if (lpd_.isNull()) {
linkAttributes = 0;
resultElementSpec = 0;
return 1;
}
const StringC *id = attributes.getId();
if (id) {
const IdLinkRuleGroup *p = lpd_->lookupIdLink(*id);
if (p) {
size_t selected;
if (p->nLinkRules() > 1) {
linkAttributes_.resize(p->nLinkRules());
for (size_t i = 0; i < linkAttributes_.size(); i++)
linkAttributes_[i] = &p->linkRule(i).attributes();
if (!selectLinkRule(linkAttributes_,
location,
selected))
return 0;
}
else
selected = 0;
const IdLinkRule &rule = p->linkRule(selected);
open_.insert(new LinkProcessOpenElement(open_.head()->current,
rule));
linkAttributes = &rule.attributes();
resultElementSpec = &rule.resultElementSpec();
if (!rule.isAssociatedWith(element)) {
mgr.setNextLocation(location);
mgr.message(ParserMessages::idlinkElementType,
StringMessageArg(element->name()),
StringMessageArg(*id));
}
return 1;
}
}
const LinkSet *currentLinkSet = open_.head()->current;
size_t nRules = currentLinkSet->nLinkRules(element);
if (nRules > 0) {
size_t selected;
if (nRules > 1) {
linkAttributes_.resize(nRules);
for (size_t i = 0; i < nRules; i++)
linkAttributes_[i]
= ¤tLinkSet->linkRule(element, i).attributes();
if (!selectLinkRule(linkAttributes_,
location,
selected))
return 0;
}
else
selected = 0;
const SourceLinkRule &rule = currentLinkSet->linkRule(element, selected);
open_.insert(new LinkProcessOpenElement(open_.head()->current,
rule));
linkAttributes = &rule.attributes();
resultElementSpec = &rule.resultElementSpec();
return 1;
}
// FIXME construct attributes from attribute definition list
linkAttributes = 0;
resultElementSpec = 0;
open_.insert(new LinkProcessOpenElement(open_.head()->current));
return 1;
}
void LinkProcess::endElement()
{
if (lpd_.isNull())
return;
LinkProcessOpenElement *top = open_.get();
if (top->post)
open_.head()->current = top->post;
else if (top->postRestore)
open_.head()->current = open_.head()->restore;
delete top;
}
void LinkProcess::uselink(const LinkSet *linkSet,
Boolean restore,
const Lpd *lpd)
{
if (lpd_.isNull())
return;
if (lpd != lpd_.pointer())
return;
if (restore)
open_.head()->current = open_.head()->restore;
else if (linkSet)
open_.head()->current = linkSet;
}
size_t LinkProcess::nImpliedLinkRules() const
{
if (!open_.head())
return 0;
return open_.head()->current->nImpliedLinkRules();
}
const ResultElementSpec &LinkProcess::impliedLinkRule(size_t i) const
{
return open_.head()->current->impliedLinkRule(i);
}
// Usually redefined by application.
Boolean LinkProcess::selectLinkRule(const Vector<const AttributeList *> &,
const Location &,
size_t &selected)
{
selected = 0;
return 1;
}
void LinkProcess::clear()
{
open_.clear();
lpd_.clear();
linkAttributes_.clear();
}
void LinkProcess::swap(LinkProcess &to)
{
open_.swap(to.open_);
lpd_.swap(to.lpd_);
linkAttributes_.swap(to.linkAttributes_);
}
LinkProcessOpenElement::LinkProcessOpenElement(const LinkSet *cur,
const SourceLinkRule &rule)
{
current = rule.uselink();
if (!current)
current = cur;
restore = cur;
post = rule.postlink();
postRestore = rule.postlinkRestore();
}
LinkProcessOpenElement::LinkProcessOpenElement(const LinkSet *cur)
{
restore = current = cur;
post = 0;
postRestore = 0;
}
#ifdef SP_NAMESPACE
}
#endif
|