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