File: ProcessContextImpl.java

package info (click to toggle)
libxt-java 0.19991105-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,908 kB
  • ctags: 2,762
  • sloc: java: 12,823; makefile: 52; xml: 46
file content (489 lines) | stat: -rw-r--r-- 16,287 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
package com.jclark.xsl.tr;

import com.jclark.xsl.om.*;
import com.jclark.xsl.expr.VariantExpr;
import com.jclark.xsl.expr.Variant;
import com.jclark.xsl.expr.VariantBase;
import com.jclark.xsl.expr.StringVariant;
import com.jclark.xsl.expr.NumberVariant;
import com.jclark.xsl.expr.ExtensionContext;
import com.jclark.xsl.expr.CloneableNodeIterator;
import com.jclark.xsl.expr.CloneableNodeIteratorImpl;
import com.jclark.xsl.expr.SingleNodeIterator;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Hashtable;
import java.util.Vector;

class ProcessContextImpl implements ProcessContext {
  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;
  }

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

  private final SheetDetails sheet;
  private final ParameterSet params;
  private Node root;
  private Hashtable variableValueTable = new Hashtable();
  private Name evalGlobalVariableName = null;
  private Name[] actionNames = new Name[OPEN_ACTION_INIT_SIZE];
  private Node[] actionNodes = new Node[OPEN_ACTION_INIT_SIZE];
  private int[] actionImportLevels = null;
  private int[] actionForEachLevels = null;
  private int nOpenActions = 0;
  private VariableBindings localVariables;
  private Name[] currentParamNames = null;
  private Variant[] currentParamValues = null;
  private int position = 1;
  private int lastPosition = 1;
  private NodeIterator currentIter = null;
  private Hashtable extensionTable = new Hashtable();
  private Hashtable documentTable = new Hashtable();
  private XMLProcessor parser;
  private Hashtable attributeSetInUseTable = new Hashtable();
  private Hashtable nameAliasTable;
  private Hashtable namespacePrefixMapAliasTable;
  private NameTable nameTable;
  private Hashtable objectTable = new Hashtable();
  private int nResultFragmentNodes = 0;

  ProcessContextImpl(SheetDetails sheet,
		     Node root,
		     XMLProcessor parser,
		     ParameterSet params) {
    this.sheet = sheet;
    this.root = root;
    this.parser = parser;
    this.params = params;
    if (sheet.haveNamespaceAliases()) {
      nameAliasTable = new Hashtable();
      namespacePrefixMapAliasTable = new Hashtable();
    }
    else {
      nameAliasTable = null;
      namespacePrefixMapAliasTable = null;
    }
    nameTable = root.getNamespacePrefixMap().getNameTable();
  }

  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;
    try {
      getAction(name, node).invoke(this, node, result);
    }
    finally {
      --nOpenActions;
    }
  }

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

  public void applyImports(Node node, Result result) throws XSLException {
    if (actionForEachLevels != null
	&& actionForEachLevels[nOpenActions - 1] > 0)
      throw new XSLException("xsl:apply-templates inside xsl:for-each", node);
    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);
    }
    sheet.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;
    VariableInfo info = sheet.getGlobalVariableInfo(name);
    if (info == null)
      return null;
    Object obj = params.getParameter(name);
    if (obj != null) {
      value = VariantBase.create(obj).makePermanent();
      variableValueTable.put(obj, value);
      return value;
    }
    // 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 = info.getExpr().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) throws XSLException {
    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 = sheet.createExtensionContext(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) {
    return sheet.getSystemProperty(name);
  }

  public Node getCurrent(Node node) {
    return node;
  }

  public void useAttributeSet(Name name, Node node, Result result) throws XSLException {
    try {
      Action action = sheet.getAttributeSet(name);
      if (action == null)
	return;
      boolean[] inUse = (boolean[])attributeSetInUseTable.get(name);
      if (inUse == null) {
	inUse = new boolean[1];
	attributeSetInUseTable.put(name, inUse);
      }
      if (inUse[0])
	throw new XSLException("circular attribute set usage", node);
      inUse[0] = true;
      try {
	action.invoke(this, node, result);
      }
      finally {
	inUse[0] = false;
      }
    }
    catch (ClassCastException e) {
    }
  }

  public NodeIterator getDocument(URL baseURL, String uriRef)
    throws XSLException {
    int fragmentIndex = uriRef.indexOf('#');
    String fragment = null;
    if (fragmentIndex >= 0) {
      fragment = uriRef.substring(fragmentIndex + 1);
      uriRef = uriRef.substring(0, fragmentIndex);
    }
    try {
      // Handling of empty relative specs is broken on JDK 1.2 for
      // the file protocol, so workaround this.
      URL url = uriRef.length() == 0 ? baseURL : new URL(baseURL, uriRef);
      Node node = (Node)documentTable.get(url);
      if (node == null) {
	node = parser.load(url,
			   documentTable.size() + 1 + nResultFragmentNodes,
			   sheet.getSourceLoadContext(),
			   root.getNamespacePrefixMap().getNameTable());
	documentTable.put(url, node);
      }
      return new SingleNodeIterator(node);
    }
    catch (MalformedURLException e) {
      throw new XSLException(e);
    }
    catch (IOException e) {
      throw new XSLException(e);
    }
  }

  public Name unaliasName(Name name) {
    if (nameAliasTable == null)
      return name;
    Name unaliasedName = (Name)nameAliasTable.get(name);
    if (unaliasedName == null) {
      String ns = sheet.getNamespaceAlias(name.getNamespace());
      if (ns == null)
	unaliasedName = name;
      else
	unaliasedName = nameTable.createName(name.toString(), ns);
    }
    nameAliasTable.put(name, unaliasedName);
    return unaliasedName;
  }
  
  public NamespacePrefixMap unaliasNamespacePrefixMap(NamespacePrefixMap map) {
    if (namespacePrefixMapAliasTable == null)
      return map;
    NamespacePrefixMap unaliasedMap
      = (NamespacePrefixMap)namespacePrefixMapAliasTable.get(map);
    if (unaliasedMap == null) {
      unaliasedMap = map;
      String ns = sheet.getNamespaceAlias(map.getDefaultNamespace());
      if (ns != null)
	unaliasedMap = unaliasedMap.bindDefault(ns);
      int size = map.getSize();
      for (int i = 0; i < size; i++) {
	ns = sheet.getNamespaceAlias(map.getNamespace(i));
	if (ns != null)
	  unaliasedMap = unaliasedMap.bind(map.getPrefix(i), ns);
      }
    }
    namespacePrefixMapAliasTable.put(map, unaliasedMap);
    return unaliasedMap;
  }

  public void put(Object key, Object value) {
    objectTable.put(key, value);
  }

  public Object get(Object key) {
    return objectTable.get(key);
  }

  public Node getTree(Variant variant) throws XSLException {
    if (!(variant instanceof ResultFragmentVariant))
      return null;
    return ((ResultFragmentVariant)variant).getTree(this);
  }

  public Result createNodeResult(Node baseNode, Node[] rootNodeRef) throws XSLException {
    return parser.createResult(baseNode,
			       documentTable.size() + ++nResultFragmentNodes,
			       sheet.getSourceLoadContext(),
			       rootNodeRef);
  }
}