/xmlbench/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/xmlbench/trunk
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*  contentmodel.c - Content Models from !ELEMENT declarations.
    Copyright (c) 2008, Robert D. Cameron and Dan Lin.
    Licensed to the public under the Open Software License 3.0.
    Licensed to International Characters, Inc., under the Academic
    Free License 3.0.
*/

#include "contentmodel.h"
#include "xml_error.h"

CM_Any::CM_Any(): ContentModel() {
	cm_type = cm_Any;
}

CM_Empty::CM_Empty() : ContentModel() {
	cm_type = cm_Empty;
}

CM_Mixed::CM_Mixed() : ContentModel() {
	cm_type = cm_Mixed;
}

CM_RegExp::CM_RegExp() : ContentModel() {
	cm_type = cm_RegExp;
}

CRE_Star::CRE_Star(Content_RE * s) : Content_RE() {
	subCM = s;
	matches_empty = true;
}

CRE_Plus::CRE_Plus(Content_RE * s) : Content_RE() {
	subCM = s;
	matches_empty = s->matches_empty;
}

CRE_Opt::CRE_Opt(Content_RE * s) : Content_RE() {
	subCM = s;
	matches_empty = true;
}
CRE_Name::CRE_Name(int id) : Content_RE() {
	elemID = id;
	matches_empty = false;
}

CRE_Seq::CRE_Seq() : Content_RE() {
}

CRE_Choice::CRE_Choice() : Content_RE() {
}

void CRE_Seq :: Compile() {
	 matches_empty = true;
	 for(int i=0; i< subCMs.size(); i++){
		if (!(subCMs[i]->matches_empty)){
			matches_empty = false;
			return;
		}
	}
}
void CRE_Name :: Set_Follow_Map(symbol_set_t * transition_map) {
/*	printf("stateID: %d\n",stateID);
	for(int i=0;i<follow_map.size();i++)
		printf("%d ",follow_map[i]);
	printf("\n");*/
	transition_map[stateID] = follow_map;
}

void CRE_Star :: Set_Follow_Map(symbol_set_t * transition_map) {
	hash_map<int, int >::iterator j;
	for (j=follow_map.begin(); j!=follow_map.end(); j++) {
		subCM->follow_map[j->first]=j->second;
	}
	for (j=subCM->first_map.begin(); j!=subCM->first_map.end(); j++) {
		if (subCM->follow_map[j->first] != 0) {
			ContentModelError();			
		}
		subCM->follow_map[j->first]=j->second;
	}
	subCM->Set_Follow_Map(transition_map);
}

void CRE_Plus :: Set_Follow_Map(symbol_set_t * transition_map) {
	hash_map<int, int >::iterator j;
	for (j=follow_map.begin(); j!=follow_map.end(); j++) 
		subCM->follow_map[j->first]=j->second;
	for (j=subCM->first_map.begin(); j!=subCM->first_map.end(); j++) {
		if (subCM->follow_map[j->first] != 0) {
			ContentModelError();
		}
		subCM->follow_map[j->first]=j->second;
	}
	subCM->Set_Follow_Map(transition_map);
}

void CRE_Opt :: Set_Follow_Map(symbol_set_t * transition_map) {
	subCM->follow_map =  follow_map;
	subCM->Set_Follow_Map(transition_map);
}

void CRE_Choice :: Set_Follow_Map(symbol_set_t * transition_map) {
	 for(int i=0; i< subCMs.size(); i++){
	 	subCMs[i]->follow_map =  follow_map;
	 	subCMs[i]->Set_Follow_Map(transition_map);
	 }
}

void CRE_Seq :: Set_Follow_Map(symbol_set_t * transition_map) {
	hash_map<int, int >::iterator itr;
	subCMs[subCMs.size()-1]->follow_map = follow_map;
	for (int i=subCMs.size()-1; i >= 1; i--) {
		// Set the follow_map for item i-1 to include first_map for i;
		for (itr=subCMs[i]->first_map.begin(); itr!=subCMs[i]->first_map.end(); itr++) {
			if (subCMs[i-1]->follow_map[itr->first] != 0) {
				ContentModelError();
			}
			subCMs[i-1]->follow_map[itr->first]=itr->second;
		}
		// If item i matches empty, set follow_map for item i-1 to include 
		// follow_map for i.
 		if (subCMs[i]->matches_empty) {
 			for (itr=subCMs[i]->follow_map.begin(); itr!=subCMs[i]->follow_map.end(); itr++) { 
				if (subCMs[i-1]->follow_map[itr->first] != 0) {
					ContentModelError();
				}
				subCMs[i-1]->follow_map[itr->first]=itr->second;
 			}
 		}
	}
	for (int j = 0; j < subCMs.size(); j++) 
		subCMs[j]->Set_Follow_Map(transition_map);
}



void CRE_Choice :: Compile() {
	 matches_empty = false;
	 for(int i=0; i< subCMs.size(); i++){
		if (subCMs[i]->matches_empty)
			matches_empty = true;
	}
}

int CRE_Name :: Set_IDs(int ID_base) {
	stateID = ++ID_base;
	return stateID;
}

int CRE_Seq :: Set_IDs(int ID_base) {
	int b = ID_base;
	for(int i=0; i< subCMs.size(); i++){
	 	b = subCMs[i]->Set_IDs(b);
	}
	return b;
}

int CRE_Choice :: Set_IDs(int ID_base) {
	int b = ID_base;
	for(int i=0; i< subCMs.size(); i++){
	 	b = subCMs[i]->Set_IDs(b);
	}
	return b;
}

int CRE_Star :: Set_IDs(int ID_base) {
	return subCM->Set_IDs(ID_base);
}

int CRE_Plus :: Set_IDs(int ID_base) {
	return subCM->Set_IDs(ID_base);
}

int CRE_Opt :: Set_IDs(int ID_base) {
	return subCM->Set_IDs(ID_base);
}


void CRE_Name :: Set_First_Map() {
	first_map[elemID]=stateID;
}


void CRE_Seq :: Set_First_Map() {
	for(int i=0; i< subCMs.size(); i++){
		subCMs[i]->Set_First_Map();
	}
	for(int i=0; i< subCMs.size(); i++){	
		int fm_size = first_map.size();
		symbol_set_t s_i = subCMs[i]->first_map;
		int s_i_size = s_i.size();
		
		hash_map<int, int >::iterator j;
		for (j=s_i.begin(); j!=s_i.end(); j++) first_map[j->first]=j->second;
		
//		if (first_map.size() < fm_size + s_i_size) {
//			ContentModelError();
//		}
		if (!(subCMs[i]->matches_empty)){
			return;
		}
	}
}

void CRE_Choice :: Set_First_Map() {
	for(int i=0; i< subCMs.size(); i++){
		subCMs[i]->Set_First_Map();
		int fm_size = first_map.size();
		symbol_set_t s_i = subCMs[i]->first_map;
		int s_i_size = s_i.size();
		
		hash_map<int, int >::iterator j;
		for (j=s_i.begin(); j!=s_i.end(); j++) first_map[j->first]=j->second;
		
//		if (first_map.size() < fm_size + s_i_size) {
//			ContentModelError();
//		}
	}
}

void CRE_Star :: Set_First_Map() {
	subCM->Set_First_Map();
	first_map=subCM->first_map;
}

void CRE_Plus :: Set_First_Map() {
	subCM->Set_First_Map();
	first_map=subCM->first_map;
}

void CRE_Opt :: Set_First_Map() {
	subCM->Set_First_Map();
	first_map=subCM->first_map;
}