File: SheetImpl.java

package info (click to toggle)
lib-xt-java 0.19990725-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,224 kB
  • ctags: 2,133
  • sloc: java: 9,665; makefile: 54; xml: 28
file content (1206 lines) | stat: -rw-r--r-- 40,735 bytes parent folder | download
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
package com.jclark.xsl.tr;

import com.jclark.xsl.om.*;
import com.jclark.xsl.expr.Pattern;
import com.jclark.xsl.expr.ExprParser;
import com.jclark.xsl.expr.StringExpr;
import com.jclark.xsl.expr.NodeSetExpr;
import com.jclark.xsl.expr.BooleanExpr;
import com.jclark.xsl.expr.VariantExpr;
import com.jclark.xsl.expr.Variant;
import com.jclark.xsl.expr.StringVariant;
import com.jclark.xsl.expr.NumberVariant;
import com.jclark.xsl.expr.VariableSet;
import com.jclark.xsl.expr.EmptyVariableSet;
import com.jclark.xsl.expr.ExtensionContext;
import com.jclark.xsl.expr.CloneableNodeIterator;
import com.jclark.xsl.expr.CloneableNodeIteratorImpl;
import com.jclark.xsl.util.Comparator;
import com.jclark.xsl.util.BilevelComparator;
import com.jclark.xsl.util.TextComparator;
import com.jclark.xsl.util.NumberComparator;
import com.jclark.xsl.util.ReverseComparator;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.util.StringTokenizer;
import java.util.Locale;
import java.text.Collator;

class SheetImpl implements Sheet, LoadContext {
  private
  interface TopLevelParser {
    void parse(Node node) throws XSLException, IOException;
  }

  private
  class IncludeParser implements TopLevelParser {
    public void parse(Node ruleNode) throws XSLException, IOException {
      Node sheetNode = parser.load(new URL(ruleNode.getURL(),
					   getRequiredAttribute(ruleNode,
								HREF)),
				   sheetLoadContext,
				   nameTable).getChildren().next();
      if (XSL_NAMESPACE.equals(sheetNode.getName().getNamespace())) {
	if (!XSL_STYLESHEET.equals(sheetNode.getName())
	    && !XSL_TRANSFORM.equals(sheetNode.getName()))
	  throw new XSLException("bad document element for stylesheet",
				 sheetNode);
	parseTopLevel(sheetNode);
      }
      else
	parseRootTemplate(sheetNode);
    }
  }

  private
  class ImportParser extends IncludeParser {
    public void parse(Node ruleNode) throws XSLException, IOException {
      Importance oldFirstImportImportance = firstImportImportance;
      firstImportImportance = currentImportance;
      super.parse(ruleNode);
      currentImportance = currentImportance.createHigher();
      firstImportImportance = oldFirstImportImportance;
    }
  }

  private
  class TemplateParser implements TopLevelParser {
    public void parse(Node defNode) throws XSLException {
      String name = defNode.getAttributeValue(NAME);
      Action contents = parseActions(defNode, emptyAction);
      if (name != null)
        namedTemplateTable.put(expandSourceElementTypeName(name, defNode), contents);
      String pattern = defNode.getAttributeValue(MATCH);
      if (pattern == null)
	return;
      String modeString = defNode.getAttributeValue(MODE);
      TemplateRuleSet ruleSet;
      if (modeString != null)
        ruleSet = getModeTemplateRuleSet(expandSourceElementTypeName(modeString, defNode));
      else
        ruleSet = templateRules;
      try {
        ruleSet.add(ExprParser.parsePattern(defNode, pattern),
	            currentImportance,
		    firstImportImportance,
		    Priority.create(defNode.getAttributeValue(PRIORITY)),
		    contents);
      }
      catch (NumberFormatException e) {
        throw new XSLException("invalid priority", defNode);
      }
    }
  }

  private
  class AttributeSetParser implements TopLevelParser {
    public void parse(Node defNode) throws XSLException {
      // FIXME
    }
  }

  private
  class KeyParser implements TopLevelParser {
    public void parse(Node defNode) throws XSLException {
      // FIXME
    }
  }

  private
  class LocaleParser implements TopLevelParser {
    public void parse(Node defNode) throws XSLException {
      // FIXME
    }
  }

  private
  class VariableTopLevelParser implements TopLevelParser {
    public void parse(Node defNode) throws XSLException {
      variableExprTable
	.put(expandSourceElementTypeName(getRequiredAttribute(defNode, NAME), defNode),
	     getVariantExpr(defNode));
    }
  }

  private
  class StripSpaceParser implements TopLevelParser {
    public void parse(Node node) throws XSLException {
      parse(node, Boolean.TRUE);
    }

    void parse(Node node, Boolean strip) throws XSLException {
      if (stripSourceTable == null)
	stripSourceTable = new Hashtable();
      StringTokenizer iter
	  = new StringTokenizer(getRequiredAttribute(node, ELEMENTS));
      while (iter.hasMoreElements())
	stripSourceTable.put(expandSourceElementTypeName((String)iter.nextElement(), node),
			     strip);
    }
  }

  private
  class PreserveSpaceParser extends StripSpaceParser {
    public void parse(Node node) throws XSLException {
      parse(node, Boolean.FALSE);
    }
  }

  private
  interface ActionParser {
    Action parse(Node node) throws XSLException;
  }

  private
  class ApplyTemplatesParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      NodeSetExpr expr = getSortNodeSetExpr(node, getNodeSetExpr(node));
      String modeString = node.getAttributeValue(MODE);
      Name modeName = null;
      if (modeString != null)
	modeName = expandSourceElementTypeName(modeString, node);
      return addParams(new ProcessAction(expr, modeName), node);
    }


    NodeSetExpr getNodeSetExpr(Node node) throws XSLException {
      String select = node.getAttributeValue(SELECT);
      if (select == null)
	return childrenExpr;
      return ExprParser.parseNodeSetExpr(node, select, currentLocalVariables);
    }

  }

  private
  class ForEachParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      return new ForEachAction(getSortNodeSetExpr(node,
					    ExprParser
					    .parseNodeSetExpr(node, getRequiredAttribute(node, SELECT), currentLocalVariables)),
			       parseActions(node, emptyAction));
    }
  }

  private
  class IfParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      return new IfAction(makeCondition(node),
			  parseActions(node, emptyAction),
			  emptyAction);
    }

    BooleanExpr makeCondition(Node node) throws XSLException {
      return ExprParser.parseBooleanExpr(node,
					 getRequiredAttribute(node, TEST),
					 currentLocalVariables);
    }
  }

  private
  class CopyParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      return new CopyAction(parseActions(node, emptyAction));
    }
  }

  private
  class CopyOfParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      return new CopyOfAction(ExprParser.parseVariantExpr(node,
							  getRequiredAttribute(node, SELECT),
							  currentLocalVariables));
    }
  }

  private
  class CommentParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      return new CommentAction(parseActions(node, emptyAction));
    }
  }

  private
  class ProcessingInstructionParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      return new ProcessingInstructionAction(
	ExprParser.parseAttributeValueTemplate(node,
					       getRequiredAttribute(node, NAME),
					       currentLocalVariables),
	parseActions(node, emptyAction));
    }
  }

  private
  class ElementParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      return new ElementAction(
	ExprParser.parseAttributeValueTemplate(node,
					       getRequiredAttribute(node, NAME),
					       currentLocalVariables),
	getNamespaceExpr(node),
	node.getNamespacePrefixMap(),
	parseActions(node, emptyAction));
    }

    StringExpr getNamespaceExpr(Node node) throws XSLException {
      String namespace = node.getAttributeValue(NAMESPACE);
      if (namespace == null)
	return null;
      return ExprParser.parseAttributeValueTemplate(node, namespace, currentLocalVariables);
    }
  }

  private
  class AttributeParser extends ElementParser {
    public Action parse(Node node) throws XSLException {
      return new AttributeAction(
	ExprParser.parseAttributeValueTemplate(node,
					       getRequiredAttribute(node, NAME),
					       currentLocalVariables),
	getNamespaceExpr(node),
	node.getNamespacePrefixMap(),
	parseActions(node, emptyAction));
    }
  }

  private
  class CallTemplateParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      
      return addParams(new InvokeAction(expandSourceElementTypeName(getRequiredAttribute(node, NAME), node),
					namedTemplateTable),
		       node);
    }
  }

  private
  class ChooseParser extends IfParser {
    public Action parse(Node node) throws XSLException {
      return parseChoices(node.getChildren());
    }
    public Action parseChoices(NodeIterator iter) throws XSLException {
      Node node = iter.next();
      if (node == null)
	return emptyAction;
      Name name = node.getName();
      if (XSL_WHEN.equals(name))
	return new IfAction(makeCondition(node),
			    parseActions(node, emptyAction),
			    parseChoices(iter));
      if (XSL_OTHERWISE.equals(name)) {
	Node next = iter.next();
	if (next != null)
	  throw new XSLException("unexpected element after otherwise",
				 next);
	return parseActions(node, emptyAction);
      }
      throw new XSLException("expected when or otherwise", node);
    }
  }

  private
  class TextParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      Node child = node.getChildren().next();
      if (child != null) {
	String data = child.getData();
	if (data == null || child.getFollowingSiblings().next() != null)
	  throw new XSLException("xsl:text must not contain elements", node);
	return new CharsAction(data);
      }
      else
	return emptyAction;
    }
  }

  private
  class ValueOfParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      return new ValueOfAction(ExprParser.parseStringExpr(node,
							  getRequiredAttribute(node, SELECT),
							  currentLocalVariables));
    }
  }

  private
  class NumberParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      NumberListFormatTemplate format = getNumberListFormatTemplate(node);
      String value = node.getAttributeValue(VALUE);
      if (value != null)
	return new ExprNumberAction(ExprParser.parseNumberExpr(node, value, currentLocalVariables), format);
      String level = getOptionalAttribute(node, LEVEL, "single");
      String countString = node.getAttributeValue(COUNT);
      Pattern count;
      if (countString == null)
	count = null;
      else
	count = ExprParser.parsePattern(node, countString);
      String fromString = node.getAttributeValue(FROM);
      Pattern from;
      if (fromString == null)
	from = null;
      else
	from = ExprParser.parsePattern(node, fromString);
      if (level.equals("any"))
	return new AnyLevelNumberAction(count, from, format);
      if (level.equals("multiple"))
	return new MultiLevelNumberAction(count, from, format);
      if (level.equals("single"))
	return new SingleLevelNumberAction(count, from, format);
      throw new XSLException("bad level", node);
    }
  }

  private
  class ApplyImportsParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      return new ApplyImportsAction();
    }
  }

  private
  class VariableActionParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      Name name = expandSourceElementTypeName(getRequiredAttribute(node, NAME), node);
      VariantExpr expr = getVariantExpr(node);
      // Must create the VariantExpr before adding to local variables.
      currentLocalVariables = new AddVariableSet(name, currentLocalVariables);
      nCurrentLocalVariables++;
      return makeAction(name, expr);
    }

    Action makeAction(Name name, VariantExpr expr) {
      return new BindLocalVariableAction(name, expr);
    }
  }

  private
  class ParamActionParser extends VariableActionParser {
    Action makeAction(Name name, VariantExpr expr) {
      return new BindLocalParamAction(name, expr);
    }
  }

  private
  class MessageParser implements ActionParser {
    public Action parse(Node node) throws XSLException {
      // FIXME do something more useful
      return new AppendAction();
    }
  }

  static final int OPEN_ACTION_INIT_SIZE = 2;
  static StringVariant emptyStringVariant = new StringVariant("");

  static final class VariableBindings {
    VariableBindings(Name name, Variant value, VariableBindings next) {
      this.name = name;
      this.value = value;
      this.next = next;
    }
    final VariableBindings next;
    final Variant value;
    final Name name;
  }

  class ProcessContextImpl implements ProcessContext {
    Node root;
    Hashtable variableValueTable = new Hashtable();
    Name evalGlobalVariableName = null;
    Name[] actionNames = new Name[OPEN_ACTION_INIT_SIZE];
    Node[] actionNodes = new Node[OPEN_ACTION_INIT_SIZE];
    int[] actionImportLevels = null;
    int[] actionForEachLevels = null;
    int nOpenActions = 0;
    VariableBindings localVariables;
    Name[] currentParamNames = null;
    Variant[] currentParamValues = null;
    int position = 1;
    int lastPosition = 1;
    NodeIterator currentIter = null;
    Hashtable extensionTable = new Hashtable();

    ProcessContextImpl(Node root) {
      this.root = root;
    }

    public void invoke(NodeIterator iter, Action action, Result result) throws XSLException {
      int savePosition = position;
      int saveLastPosition = lastPosition;
      NodeIterator saveCurrentIter = currentIter;
      currentIter = iter;
      position = 0;
      lastPosition = 0;
      if (actionForEachLevels == null)
	actionForEachLevels = new int[nOpenActions];
      else if (nOpenActions > actionForEachLevels.length) {
	int[] oldActionForEachLevels = actionForEachLevels;
	actionForEachLevels = new int[nOpenActions];
	System.arraycopy(oldActionForEachLevels, 0,
			 actionForEachLevels, 0,
			 oldActionForEachLevels.length);
      }
      actionForEachLevels[nOpenActions - 1]++;
      try {
	for (;;) {
	  // getLastPosition() may change the iterator, so use currentIter not iter
	  Node node = currentIter.next();
	  if (node == null)
	    break;
	  ++position;
	  action.invoke(this, node, result);
	}
      }
      finally {
	actionForEachLevels[nOpenActions - 1]--;
	position = savePosition;
	lastPosition = saveLastPosition;
	currentIter = saveCurrentIter;
      }
    }

    public void process(NodeIterator iter, Name modeName,
			Name[] paramNames, Variant[] paramValues,
			Result result)
      throws XSLException {
      int savePosition = position;
      int saveLastPosition = lastPosition;
      NodeIterator saveCurrentIter = currentIter;
      currentIter = iter;
      position = 0;
      lastPosition = 0;
      Name[] saveParamNames = currentParamNames;
      currentParamNames = paramNames;
      Variant[] saveParamValues = currentParamValues;
      currentParamValues = paramValues;
      boolean processSafely = paramValues == null;
      try {
	for (;;) {
	  // getLastPosition() may change the iterator, so use currentIter not iter
	  Node node = currentIter.next();
	  if (node == null)
	    break;
	  ++position;
	  if (processSafely)
	    processSafe(node, modeName, result);
	  else
	    processUnsafe(node, modeName, result);
	}
      }
      finally {
	position = savePosition;
	lastPosition = saveLastPosition;
	currentIter = saveCurrentIter;
	currentParamNames = saveParamNames;
	currentParamValues = saveParamValues;
      }
    }

    private void processUnsafe(Node node, Name name, Result result) throws XSLException {
      getAction(name, node).invoke(this, node, result);
    }

    void processSafe(Node node, Name name, Result result) throws XSLException {
      if (name == null) {
	for (int i = 0; i < nOpenActions; i++)
	  if (actionNames[i] == null && actionNodes[i].equals(node))
	    return; // loop detected
      }
      else {
	for (int i = 0; i < nOpenActions; i++)
	  if (name.equals(actionNames[i]) && actionNodes[i].equals(node))
	    return; // loop detected
      }
      if (nOpenActions == actionNames.length) {
	Name[] oldActionNames = actionNames;
	actionNames = new Name[nOpenActions * 2];
	System.arraycopy(oldActionNames, 0, actionNames, 0, nOpenActions);
	Node[] oldActionNodes = actionNodes;
	actionNodes = new Node[nOpenActions * 2];
	System.arraycopy(oldActionNodes, 0, actionNodes, 0, nOpenActions);
      }
      actionNames[nOpenActions] = name;
      actionNodes[nOpenActions] = node;
      ++nOpenActions;
      getAction(name, node).invoke(this, node, result);
      --nOpenActions;
    }

    private final Action getAction(Name name, Node node) throws XSLException {
      return getModeTemplateRuleSet(name).getAction(node, this);
    }

    public void applyImports(Node node, Result result) throws XSLException {
      if (actionForEachLevels != null
	  && actionForEachLevels[nOpenActions - 1] > 0)
	return;
      if (actionImportLevels == null)
	actionImportLevels = new int[nOpenActions];
      else if (nOpenActions > actionImportLevels.length) {
	int[] oldActionImportLevels = actionImportLevels;
	actionImportLevels = new int[nOpenActions];
	System.arraycopy(oldActionImportLevels, 0,
			 actionImportLevels, 0,
			 oldActionImportLevels.length);
      }
      getModeTemplateRuleSet(actionNames[nOpenActions - 1])
       .getImportAction(node,
			this,
			actionImportLevels[nOpenActions - 1]++)
       .invoke(this, node, result);
      actionImportLevels[nOpenActions - 1]--;
    }
 
    public final boolean hasAttribute(Vector nameList, Node node, String value) {
      int len = nameList.size();
      for (int i = 0; i < len; i++)
	if (value.equals(node.getAttributeValue((Name)nameList.elementAt(i))))
	  return true;
      return false;
    }

    public int getPosition() {
      return position;
    }

    public int getLastPosition() throws XSLException {
      if (lastPosition == 0) {
	lastPosition = position;
	for (NodeIterator iter = cloneCurrentIter(); iter.next() != null;)
	  lastPosition++;
      }
      return lastPosition;
    }
    
    private NodeIterator cloneCurrentIter() {
      if (!(currentIter instanceof CloneableNodeIterator))
	currentIter = new CloneableNodeIteratorImpl(currentIter);
      return (NodeIterator)((CloneableNodeIterator)currentIter).clone();
    }

    public Variant getGlobalVariableValue(Name name) throws XSLException {
      Variant value = (Variant)variableValueTable.get(name);
      if (value != null)
	return value;
      VariantExpr expr = (VariantExpr)variableExprTable.get(name);
      if (expr == null)
	return null;
      // Avoid possibility of infinite loop
      variableValueTable.put(name, emptyStringVariant);
      Name temp = evalGlobalVariableName;
      // set this so we can save it in a memento
      evalGlobalVariableName = name;
      try {
	value = expr.eval(root, this).makePermanent();
      }
      finally {
	evalGlobalVariableName = temp;
      }
      variableValueTable.put(name, value);
      return value;
    }

    public Variant getLocalVariableValue(Name name) {
      for (VariableBindings p = localVariables; p != null; p = p.next) {
	if (p.name.equals(name))
	  return p.value;
      }
      throw new Error("no such local variable");
    }

    public void bindLocalVariable(Name name, Variant value) {
      localVariables = new VariableBindings(name, value.makePermanent(), localVariables);
    }

    public void unbindLocalVariables(int n) {
      for (; n > 0; --n)
	localVariables = localVariables.next;
    }

    public void invokeWithParams(Action action, Name[] paramNames, Variant[] paramValues,
				 Node node, Result result) throws XSLException {
      Name[] saveParamNames = currentParamNames;
      currentParamNames = paramNames;
      Variant[] saveParamValues = currentParamValues;
      currentParamValues = paramValues;
      try {
	action.invoke(this, node, result);
      }
      finally {
	currentParamNames = saveParamNames;
	currentParamValues = saveParamValues;
      }
    }

    public Variant getParam(Name name) {
      if (currentParamNames != null) {
	for (int i = 0; i < currentParamNames.length; i++)
	  if (name.equals(currentParamNames[i]))
	    return currentParamValues[i];
      }
      return null;
    }

    public ProcessContext.Memento createMemento() {
      final VariableBindings rememberLocalVariables = localVariables;
      final int rememberPosition = position;
      final int rememberLastPosition = lastPosition;
      final NodeIterator rememberCurrentIter
	= lastPosition == 0 ? cloneCurrentIter() : null;
      final Name rememberEvalGlobalVariableName = evalGlobalVariableName;
      return new ProcessContext.Memento() {
	public void invoke(Action action, Node node, Result result) throws XSLException {
	  Name[] saveParamNames = currentParamNames;
	  currentParamNames = null;
	  Variant[] saveParamValues = currentParamValues;
	  currentParamValues = null;
	  int savePosition = position;
	  position = rememberPosition;
	  int saveLastPosition = lastPosition;
	  lastPosition = rememberLastPosition;
	  NodeIterator saveCurrentIter = currentIter;
	  currentIter = rememberCurrentIter;
	  VariableBindings saveLocalVariables = localVariables;
	  localVariables = rememberLocalVariables;
	  Object saveGlobalVariableValue = null;
	  if (rememberEvalGlobalVariableName != null) {
	    saveGlobalVariableValue
	      = variableValueTable.get(rememberEvalGlobalVariableName);
	    variableValueTable.put(rememberEvalGlobalVariableName,
				   emptyStringVariant);
	  }
	  try {
	    action.invoke(ProcessContextImpl.this, node, result);
	  }
	  finally {
	    currentParamNames = saveParamNames;
	    currentParamValues = saveParamValues;
	    localVariables = saveLocalVariables;
	    position = savePosition;
	    lastPosition = saveLastPosition;
	    currentIter = saveCurrentIter;
	    if (rememberEvalGlobalVariableName != null)
	      variableValueTable.put(rememberEvalGlobalVariableName,
				     saveGlobalVariableValue);
	  }
	}
      };
    }

    public ExtensionContext getExtensionContext(String namespace) throws XSLException {
      ExtensionContext extension = (ExtensionContext)extensionTable.get(namespace);
      if (extension == null) {
	extension = extensionHandler.createContext(namespace);
	if (extension == null) {
	  extension = new ExtensionContext() {
	    public boolean available(String name) {
	      return false;
	    }
	    public Object call(String name, Node currentNode, Object[] args) throws XSLException {
	      throw new XSLException("implementation of extension namespace not available");
	    }
	  };
	}
	extensionTable.put(namespace, extension);
      }
      return extension;
    }

    public Variant getSystemProperty(Name name) {
      if (name.equals(XSL_VERSION))
	return new NumberVariant(1.0);
      if (name.equals(XSL_VENDOR))
	return new StringVariant("James Clark");
      if (name.equals(XSL_VENDOR_URL))
	return new StringVariant("http://www.jclark.com/");
      return emptyStringVariant;
    }

  }


  private boolean stripSource = false;
  private Hashtable stripSourceTable = null;
  private boolean indentResult = false;
  private String resultNamespace = null;
  private TemplateRuleSet templateRules = new TemplateRuleSet(new BuiltinAction());
  private Hashtable modeTable = new Hashtable();
  private Hashtable namedTemplateTable = new Hashtable();
  private Hashtable variableExprTable = new Hashtable();
  private Vector idAttributes = new Vector();
  private static final Action emptyAction = new EmptyAction();
  private Hashtable topLevelTable = new Hashtable();
  private Hashtable actionTable = new Hashtable();
  Importance currentImportance = Importance.create();
  Importance firstImportImportance = Importance.create();
  static NodeSetExpr childrenExpr = ExprParser.getChildrenExpr();
  VariableSet currentLocalVariables = new EmptyVariableSet();
  int nCurrentLocalVariables = 0;
  XMLProcessor parser;
  Name XSL_WHEN;
  Name XSL_OTHERWISE;
  Name XSL_STYLESHEET;
  Name XSL_TRANSFORM;
  Name XSL_WITH_PARAM;
  Name XSL_SORT;
  Name XSL_FOR_EACH;
  Name XSL_VERSION;
  Name XSL_VENDOR;
  Name XSL_VENDOR_URL;
  Name HREF;
  Name MATCH;
  Name PRIORITY;
  Name SELECT;
  Name TEST;
  Name DEFAULT_SPACE;
  Name INDENT_RESULT;
  Name RESULT_NS;
  Name NAME;
  Name NAMESPACE;
  Name DEFAULT;
  Name VALUE;
  Name ELEMENTS;
  Name ATTRIBUTE;
  Name FROM;
  Name COUNT;
  Name LEVEL;
  Name FORMAT;
  Name LETTER_VALUE;
  Name GROUPING_SIZE;
  Name GROUPING_SEPARATOR;
  Name MODE;
  Name ORDER;
  Name LANG;
  Name CASE_ORDER;
  Name DATA_TYPE;
  LoadContext sheetLoadContext;
  NameTable nameTable;
  ExtensionHandler extensionHandler;
  
  SheetImpl(Node node, XMLProcessor parser, ExtensionHandler extensionHandler,
	    LoadContext sheetLoadContext, NameTable nameTable)
    throws IOException, XSLException {
    this.sheetLoadContext = sheetLoadContext;
    this.nameTable = nameTable;
    this.parser = parser;
    this.extensionHandler = extensionHandler;
    XSL_WHEN = xsl("when");
    XSL_OTHERWISE = xsl("otherwise");
    XSL_STYLESHEET = xsl("stylesheet");
    XSL_TRANSFORM = xsl("transform");
    XSL_WITH_PARAM = xsl("with-param");
    XSL_SORT = xsl("sort");
    XSL_FOR_EACH = xsl("for-each");
    XSL_VERSION = xsl("version");
    XSL_VENDOR = xsl("vendor");
    XSL_VENDOR_URL = xsl("vendor-url");
    HREF = nameTable.createName("href");
    SELECT = nameTable.createName("select");
    TEST = nameTable.createName("test");
    MATCH = nameTable.createName("match");
    PRIORITY = nameTable.createName("priority");
    DEFAULT_SPACE = nameTable.createName("default-space");
    INDENT_RESULT = nameTable.createName("indent-result");
    RESULT_NS = nameTable.createName("result-ns");
    NAME = nameTable.createName("name");
    NAMESPACE = nameTable.createName("namespace");
    DEFAULT = nameTable.createName("default");
    VALUE = nameTable.createName("value");
    ELEMENTS = nameTable.createName("elements");
    ATTRIBUTE = nameTable.createName("attribute");
    COUNT = nameTable.createName("count");
    LEVEL = nameTable.createName("level");
    FROM = nameTable.createName("from");
    FORMAT = nameTable.createName("format");
    LETTER_VALUE = nameTable.createName("letter-value");
    GROUPING_SIZE = nameTable.createName("grouping-size");
    GROUPING_SEPARATOR = nameTable.createName("grouping-separator");
    MODE = nameTable.createName("mode");
    ORDER = nameTable.createName("order");
    LANG = nameTable.createName("lang");
    DATA_TYPE = nameTable.createName("data-type");
    CASE_ORDER = nameTable.createName("case-order");
    
    topLevelTable.put(xsl("include"), new IncludeParser());
    topLevelTable.put(xsl("import"), new ImportParser());
    topLevelTable.put(xsl("template"), new TemplateParser());
    topLevelTable.put(xsl("attribute-set"), new AttributeSetParser());
    topLevelTable.put(xsl("locale"), new LocaleParser());
    topLevelTable.put(xsl("key"), new KeyParser());
    topLevelTable.put(xsl("variable"), new VariableTopLevelParser());
    topLevelTable.put(xsl("param"), new VariableTopLevelParser());
    topLevelTable.put(xsl("strip-space"), new StripSpaceParser());
    topLevelTable.put(xsl("preserve-space"), new PreserveSpaceParser());
    actionTable.put(xsl("text"), new TextParser());
    actionTable.put(xsl("apply-templates"), new ApplyTemplatesParser());
    actionTable.put(xsl("call-template"), new CallTemplateParser());
    actionTable.put(xsl("for-each"), new ForEachParser());
    actionTable.put(xsl("value-of"), new ValueOfParser());
    actionTable.put(xsl("number"), new NumberParser());
    actionTable.put(xsl("if"), new IfParser());
    actionTable.put(xsl("choose"), new ChooseParser());
    actionTable.put(xsl("copy"), new CopyParser());
    actionTable.put(xsl("copy-of"), new CopyOfParser());
    actionTable.put(xsl("comment"), new CommentParser());
    actionTable.put(xsl("processing-instruction"), new ProcessingInstructionParser());
    actionTable.put(xsl("element"), new ElementParser());
    actionTable.put(xsl("attribute"), new AttributeParser());
    actionTable.put(xsl("apply-imports"), new ApplyImportsParser());
    actionTable.put(xsl("variable"), new VariableActionParser());
    actionTable.put(xsl("param"), new ParamActionParser());
    actionTable.put(xsl("message"), new MessageParser());
    parseSheet(node);
  }

  public
  Result process(Node node, Result root) throws XSLException {
    root.start();
    new ProcessContextImpl(node).processSafe(node, null, root);
    root.end();
    return root;
  }

  public
  boolean getIndentResult() {
    return indentResult;
  }

  public
  String getResultNamespace() {
    return resultNamespace;
  }

  private
  void parseSheet(Node rootNode) throws XSLException, IOException {
    Node sheetNode = rootNode.getChildren().next();
    if (XSL_NAMESPACE.equals(sheetNode.getName().getNamespace())) {
      if (!XSL_STYLESHEET.equals(sheetNode.getName())
	  && !XSL_TRANSFORM.equals(sheetNode.getName()))
	throw new XSLException("bad document element for stylesheet",
			       sheetNode);
      if ("strip".equals(sheetNode.getAttributeValue(DEFAULT_SPACE)))
	stripSource = true;
      if ("yes".equals(sheetNode.getAttributeValue(INDENT_RESULT)))
	indentResult = true;
      resultNamespace = sheetNode.getAttributeValue(RESULT_NS);
      if (resultNamespace != null) {
	NamespacePrefixMap map = sheetNode.getNamespacePrefixMap();
	if (resultNamespace.equals(""))
	  resultNamespace = map.getDefaultNamespace();
	else
	  resultNamespace = map.getNamespace(resultNamespace);
      }
      parseTopLevel(sheetNode);
    }
    else
      parseRootTemplate(sheetNode);
    templateRules.compile();
    for (Enumeration iter = modeTable.elements(); iter.hasMoreElements();)
      ((TemplateRuleSet)iter.nextElement()).compile();
  }

  void parseTopLevel(Node sheetNode) throws XSLException, IOException {
    for (NodeIterator iter = sheetNode.getChildren();;) {
      Node node = iter.next();
      if (node == null)
	break;
      TopLevelParser parser = null;
      try {
	Name name = node.getName();
	if (name == null)
	  throw new XSLException("illegal data characters inside xsl:stylesheet", node);
	parser = (TopLevelParser)topLevelTable.get(name);
	if (parser == null && name.getNamespace() == null)
	  throw new XSLException("illegal top-level element", node);
      }
      catch (ClassCastException e) { }
      parser.parse(node);
    }
  }

  void parseRootTemplate(Node defNode) throws XSLException {
    templateRules.add(ExprParser.parsePattern(defNode, "/"),
		      currentImportance,
		      firstImportImportance,
		      null,
		      parseActions(defNode, emptyAction));
  }

  /* Parse the attributes on node as literal attributes and then
     parse the actions. */

  Action parseAttributesAndActions(Node node) throws XSLException {
    AppendAction sequence = null;
    for (NodeIterator iter = node.getAttributes();;) {
      Node att = iter.next();
      if (att == null)
	break;
      if (sequence == null)
	sequence = new AppendAction();
      String value = att.getData();
      Name name = unquoteName(att.getName(), node);
      if (value.indexOf('{') >= 0 || value.indexOf('}') >= 0)
	sequence.add(new TemplateAttributeAction(name,
						 ExprParser.parseAttributeValueTemplate(node, value, currentLocalVariables)));
      else
	sequence.add(new LiteralAttributeAction(name, value));
    }
    return parseActions(node, null, sequence);
  }

  Action parseActions(Node node, Action ifEmpty) throws XSLException {
    return parseActions(node, ifEmpty, null);
  }
  
  Action parseActions(Node node, Action ifEmpty, AppendAction sequence) throws XSLException {
    final VariableSet startLocalVariables = currentLocalVariables;
    final int nStartLocalVariables = nCurrentLocalVariables;
    NodeIterator iter = node.getChildren();
    node = iter.next();
    if (node == null) {
      if (sequence == null)
	return ifEmpty;
      else
	return sequence;
    }
    if (sequence == null)
      sequence = new AppendAction();
    do {
      switch (node.getType()) {
      case Node.TEXT:
	sequence.add(new CharsAction(node.getData()));
	break;
      case Node.ELEMENT:
	{
	  Name name = node.getName();
	  if (!XSL_NAMESPACE.equals(name.getNamespace()))
	    sequence.add(new LiteralElementAction(unquoteName(node.getName(), node),
					          unquoteNamespacePrefixMap(node.getNamespacePrefixMap()),
					          parseAttributesAndActions(node)));
	  else {
	    ActionParser actionParser = null;
	    try {
	      actionParser = (ActionParser)actionTable.get(name);
	    }
	    catch (ClassCastException e) { }
	    if (actionParser == null) {
	      if (name.equals(XSL_SORT)
		  && XSL_FOR_EACH.equals(node.getParent().getName()))
		;
	      else
		throw new XSLException("expected action not " + name, node);
	    }
	    else
	      sequence.add(actionParser.parse(node));
	  }
	}
      }
      node = iter.next();
    } while (node != null);
    // FIXME should use finally here
    if (nStartLocalVariables != nCurrentLocalVariables) {
      sequence.add(new UnbindLocalVariablesAction(nCurrentLocalVariables - nStartLocalVariables));
      nCurrentLocalVariables = nStartLocalVariables;
      currentLocalVariables = startLocalVariables;
    }
    return sequence;
  }

  String getRequiredAttribute(Node node, Name name) throws XSLException {
    String value = node.getAttributeValue(name);
    if (value == null)
      throw new XSLException("missing attribute \"" + name + "\"", node);
    return value;
  }

  String getOptionalAttribute(Node node, Name name, String dflt) {
    String value = node.getAttributeValue(name);
    return value == null ? dflt : value;
  }

  String getData(Node node) throws XSLException {
    node = node.getChildren().next();
    if (node == null)
      return "";
    String data = node.getData();
    if (data == null)
      throw new XSLException("only character data allowed", node);
    return data;
  }

  NumberListFormatTemplate getNumberListFormatTemplate(Node node) throws XSLException {
    NumberListFormatTemplate t = new NumberListFormatTemplate();
    String value = node.getAttributeValue(FORMAT);
    if (value != null) {
      StringExpr expr = ExprParser.parseAttributeValueTemplate(node, value, currentLocalVariables);
      String format = expr.constantValue();
      if (format != null)
  	t.setFormat(format);
      else
	t.setFormat(expr);
    }
    // FIXME should be able to use attribute value templates for these
    value = node.getAttributeValue(LANG);
    if (value != null)
      t.setLang(value);
    value = node.getAttributeValue(LETTER_VALUE);
    if (value != null)
      t.setLetterValue(value);
    value = node.getAttributeValue(GROUPING_SIZE);
    if (value != null) {
      try {
	t.setGroupingSize(Integer.parseInt(value));
      }
      catch (NumberFormatException e) { }
    }
    value = node.getAttributeValue(GROUPING_SEPARATOR);
    if (value != null)
      t.setGroupingSeparator(value);
    return t;
  }

  Action addParams(ParamAction action, Node node) throws XSLException {
    for (NodeIterator iter = node.getChildren();;) {
      node = iter.next();
      if (node == null)
	break;
      if (XSL_WITH_PARAM.equals(node.getName()))
	action.addParam(expandSourceElementTypeName(getRequiredAttribute(node, NAME), node),
			getVariantExpr(node));
    }
    return action;
  }

  NodeSetExpr getSortNodeSetExpr(Node node, NodeSetExpr expr) throws XSLException {
    ComparatorTemplate comparatorTemplate = null;
    for (NodeIterator iter = node.getChildren();;) {
      node = iter.next();
      if (node == null)
	break;
      if (XSL_SORT.equals(node.getName())) {
	Locale locale = Lang.getLocale(node.getAttributeValue(LANG));
	Comparator cmp;
	String dataType = node.getAttributeValue(DATA_TYPE);
	if ("number".equals(dataType))
	  cmp = new NumberComparator(locale);
	else {
	  int caseOrder = 0;
	  String caseOrderString = node.getAttributeValue(CASE_ORDER);
	  if ("upper-first".equals(caseOrderString))
	    caseOrder = TextComparator.UPPER_FIRST;
	  else if ("lower-first".equals(caseOrderString))
	    caseOrder = TextComparator.LOWER_FIRST;
	  cmp = TextComparator.create(locale, caseOrder);
	}
	if ("descending".equals(node.getAttributeValue(ORDER)))
	  cmp = new ReverseComparator(cmp);
	ComparatorTemplate templ 
	  = new NodeComparatorTemplate(cmp,
				       ExprParser.parseStringExpr(node,
								  getOptionalAttribute(node, SELECT, "."), currentLocalVariables));
	if (comparatorTemplate == null)
	  comparatorTemplate = templ;
	else
	  comparatorTemplate = new BilevelComparatorTemplate(comparatorTemplate, templ);
      }
    }
    if (comparatorTemplate == null)
      return expr;
    return new SortNodeSetExpr(expr, comparatorTemplate);
  }

  VariantExpr getVariantExpr(Node defNode) throws XSLException {
    String expr = defNode.getAttributeValue(SELECT);
    if (expr != null)
      return ExprParser.parseVariantExpr(defNode, expr, currentLocalVariables);
    return new ResultFragmentExpr(parseActions(defNode, emptyAction), extensionHandler);
  }

  TemplateRuleSet getModeTemplateRuleSet(Name modeName) {
    if (modeName == null)
      return templateRules;
    TemplateRuleSet ruleSet = (TemplateRuleSet)modeTable.get(modeName);
    if (ruleSet == null) {
      ruleSet = new TemplateRuleSet(new BuiltinAction(modeName));
      modeTable.put(modeName, ruleSet);
    }
    return ruleSet;
  }

  static final String XSL_NAMESPACE = "http://www.w3.org/XSL/Transform/1.0";

  private Name xsl(String name) {
    return nameTable.createName("xsl:" + name, XSL_NAMESPACE);
  }
  
  Name unquoteName(Name name, Node node) {
    String ns = unquoteNamespace(name.getNamespace());
    if (ns == null)
      return name;
    return nameTable.createName(name.toString(), ns);
  }

  static private NamespacePrefixMap unquoteNamespacePrefixMap(NamespacePrefixMap map) {
    String ns = map.getDefaultNamespace();
    if (XSL_NAMESPACE.equals(ns))
      map = map.unbindDefault();
    else {
      ns = unquoteNamespace(ns);
      if (ns != null)
	map = map.bindDefault(ns);
    }
    NamespacePrefixMap newMap = map;
    int size = map.getSize();
    for (int i = 0; i < size; i++) {
      ns = map.getNamespace(i);
      if (XSL_NAMESPACE.equals(ns))
	newMap = newMap.unbind(map.getPrefix(i));
      else {
	ns = unquoteNamespace(ns);
	if (ns != null)
	  newMap = newMap.bind(map.getPrefix(i), ns);
      }
    }
    return newMap;
  }

  /* If the namespace is a quoted namespace, return the unquoted namespace.
     Otherwise return null. */

  private static String unquoteNamespace(String ns) {
    if (ns != null && ns.startsWith("quote:"))
      return ns.substring(6);
    return null;
  }

  static Name expandSourceElementTypeName(String nameString, Node node) throws XSLException {
    return node.getNamespacePrefixMap().expandAttributeName(nameString, node);
  }

  public LoadContext getSourceLoadContext() {
    return this;
  }

  public boolean getStripSource(Name elementTypeName) {
    if (stripSourceTable != null) {
      Boolean b = (Boolean)stripSourceTable.get(elementTypeName);
      if (b != null)
	return b.booleanValue();
    }
    return stripSource;
  }

  public boolean getIncludeComments() {
    return true;
  }

  public boolean getIncludeProcessingInstructions() {
    return true;
  }

}