Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / nsgmls / Lpd.C
1 /* $XConsortium: Lpd.C /main/1 1996/07/29 16:56:26 cde-hp $ */
2 // Copyright (c) 1994 James Clark
3 // See the file COPYING for copying permission.
4
5 #ifdef __GNUG__
6 #pragma implementation
7 #endif
8 #include "splib.h"
9 #include "Lpd.h"
10
11 #ifdef SP_NAMESPACE
12 namespace SP_NAMESPACE {
13 #endif
14
15 Lpd::Lpd(const StringC &name, Type type, const Location &location,
16          const Ptr<Dtd> &sourceDtd)
17 : name_(new StringResource<Char>(name)), type_(type), location_(location),
18   active_(0), sourceDtd_(sourceDtd)
19 {
20 }
21
22 Lpd::~Lpd()
23 {
24 }
25
26 SimpleLpd::SimpleLpd(const StringC &name, const Location &location,
27                      const Ptr<Dtd> &sourceDtd)
28 : Lpd(name, simpleLink, location, sourceDtd)
29 {
30 }
31
32 ResultElementSpec::ResultElementSpec()
33 : elementType(0)
34 {
35 }
36
37 void ResultElementSpec::swap(ResultElementSpec &to)
38 {
39   attributeList.swap(to.attributeList);
40   {
41     const ElementType *tem = to.elementType;
42     to.elementType = elementType;
43     elementType = tem;
44   }
45 }
46
47 SourceLinkRule::SourceLinkRule()
48 : uselink_(0), postlink_(0), postlinkRestore_(0)
49 {
50 }
51
52 void SourceLinkRule::swap(SourceLinkRule &to)
53 {
54   linkAttributes_.swap(to.linkAttributes_);
55   resultElementSpec_.swap(to.resultElementSpec_);
56   {
57     const LinkSet *tem = to.uselink_;
58     to.uselink_ = uselink_;
59     uselink_ = tem;
60   }
61   {
62     const LinkSet *tem = to.postlink_;
63     to.postlink_ = postlink_;
64     postlink_ = tem;
65   }
66   {
67     Boolean tem = to.postlinkRestore_;
68     to.postlinkRestore_ = postlinkRestore_;
69     postlinkRestore_ = tem;
70   }
71 }
72
73 SourceLinkRuleResource::SourceLinkRuleResource()
74 {
75 }
76
77 LinkSet::LinkSet(const StringC &name, const Dtd *dtd)
78 : Named(name), defined_(0), linkRules_(dtd ? dtd->nElementTypeIndex() : 0)
79 {
80 }
81
82 void LinkSet::addLinkRule(const ElementType *element,
83                           const ConstPtr<SourceLinkRuleResource> &rule)
84 {
85   linkRules_[element->index()].push_back(rule);
86 }
87
88 void LinkSet::addImplied(const ElementType *element, AttributeList &attributes)
89 {
90   impliedSourceLinkRules_.resize(impliedSourceLinkRules_.size() + 1);
91   ResultElementSpec &result = impliedSourceLinkRules_.back();
92   result.elementType = element;
93   result.attributeList = attributes;
94 }
95
96 Boolean LinkSet::impliedResultAttributes(const ElementType *resultType,
97                                          const AttributeList *&attributes)
98 {
99   for (size_t i = 0; i < impliedSourceLinkRules_.size(); i++)
100     if (impliedSourceLinkRules_[i].elementType == resultType) {
101       attributes = &impliedSourceLinkRules_[i].attributeList;
102       return 1;
103     }
104   return 0;
105 }
106
107 size_t LinkSet::nLinkRules(const ElementType *e) const
108 {
109   if (e->index() >= linkRules_.size())
110     return 0;
111   return linkRules_[e->index()].size();
112 }
113
114 IdLinkRule::IdLinkRule()
115 {
116 }
117
118 Boolean IdLinkRule::isAssociatedWith(const ElementType *e) const
119 {
120   for (size_t i = 0; i < assocElementTypes_.size(); i++)
121     if (assocElementTypes_[i] == e)
122       return 1;
123   return 0;
124 }
125
126 void IdLinkRule::setAssocElementTypes(Vector<const ElementType *> &v)
127 {
128   v.swap(assocElementTypes_);
129 }
130
131 void IdLinkRule::swap(IdLinkRule &to)
132 {
133   SourceLinkRule::swap(to);
134   assocElementTypes_.swap(to.assocElementTypes_);
135 }
136
137 IdLinkRuleGroup::IdLinkRuleGroup(const StringC &name)
138 : Named(name)
139 {
140 }
141
142 void IdLinkRuleGroup::addLinkRule(IdLinkRule &rule)
143 {
144   linkRules_.resize(linkRules_.size() + 1);
145   rule.swap(linkRules_.back());
146 }
147
148 ComplexLpd::ComplexLpd(const StringC &name, Type type,
149                        const Location &location,
150                        const Syntax &syntax,
151                        const Ptr<Dtd> &sourceDtd,
152                        const Ptr<Dtd> &resultDtd)
153 : Lpd(name, type, location, sourceDtd), resultDtd_(resultDtd),
154   hadIdLinkSet_(0), nAttributeDefinitionList_(0),
155   initialLinkSet_(syntax.rniReservedName(Syntax::rINITIAL),
156                   sourceDtd.pointer()),
157   emptyLinkSet_(syntax.rniReservedName(Syntax::rEMPTY),
158                 sourceDtd.pointer()),
159   linkAttributeDefs_(sourceDtd.isNull() ? 0 : sourceDtd->nElementTypeIndex())
160 {
161 }
162
163 IdLinkRuleGroup *ComplexLpd::lookupCreateIdLink(const StringC &id)
164 {
165   IdLinkRuleGroup *group = idLinkTable_.lookup(id);
166   if (!group) {
167     group = new IdLinkRuleGroup(id);
168     idLinkTable_.insert(group);
169   }
170   return group;
171 }
172
173
174
175 #ifdef SP_NAMESPACE
176 }
177 #endif