| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Component; |
| 11 |
import net.sf.saxon.expr.instruct.GlobalVariable; |
| 12 |
import net.sf.saxon.expr.instruct.NamedTemplate; |
| 13 |
import net.sf.saxon.expr.instruct.UserFunction; |
| 14 |
import net.sf.saxon.om.StandardNames; |
| 15 |
import net.sf.saxon.om.StructuredQName; |
| 16 |
import net.sf.saxon.pattern.NameTest; |
| 17 |
import net.sf.saxon.pattern.QNameTest; |
| 18 |
import net.sf.saxon.trans.*; |
| 19 |
|
| 20 |
import java.util.List; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class XSLExpose extends XSLAcceptExpose { |
| 26 |
|
| 27 |
protected void checkCompatibility(SymbolicName name, Visibility declared, Visibility exposed) throws XPathException { |
| 28 |
if (!isCompatible(declared, exposed)) { |
| 29 |
String code = "XTSE3010"; |
| 30 |
compileError("The " + name + " is declared as " + declared.show() + " and cannot be exposed as " + exposed.show(), code); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public static boolean isCompatible(Visibility declared, Visibility exposed) { |
| 35 |
if (declared == null || declared == exposed) { |
| 36 |
return true; |
| 37 |
} |
| 38 |
switch (declared) { |
| 39 |
case PUBLIC: |
| 40 |
return exposed == Visibility.PUBLIC || exposed == Visibility.PRIVATE || |
| 41 |
exposed == Visibility.FINAL || exposed == Visibility.HIDDEN; |
| 42 |
case ABSTRACT: |
| 43 |
return exposed == Visibility.ABSTRACT || exposed == Visibility.HIDDEN; |
| 44 |
case FINAL: |
| 45 |
return exposed == Visibility.PRIVATE || |
| 46 |
exposed == Visibility.FINAL || exposed == Visibility.HIDDEN; |
| 47 |
default: |
| 48 |
return false; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
@Override |
| 63 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 64 |
PrincipalStylesheetModule psm = getPrincipalStylesheetModule(); |
| 65 |
Visibility exposedVisibility = getVisibility(); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
for (ComponentTest test : getExplicitComponentTests()) { |
| 73 |
QNameTest nameTest = test.getQNameTest(); |
| 74 |
if (nameTest instanceof NameTest) { |
| 75 |
StructuredQName qName = ((NameTest) nameTest).getMatchingNodeName(); |
| 76 |
int kind = test.getComponentKind(); |
| 77 |
SymbolicName sName = kind == StandardNames.XSL_FUNCTION ? |
| 78 |
new SymbolicName.F(((NameTest) nameTest).getMatchingNodeName(), test.getArity()) : |
| 79 |
new SymbolicName(kind, ((NameTest) nameTest).getMatchingNodeName()); |
| 80 |
boolean found = false; |
| 81 |
switch(kind) { |
| 82 |
case StandardNames.XSL_TEMPLATE: { |
| 83 |
NamedTemplate template = psm.getNamedTemplate(qName); |
| 84 |
found = template != null; |
| 85 |
if (found) { |
| 86 |
Visibility declared = template.getDeclaredVisibility(); |
| 87 |
checkCompatibility(template.getSymbolicName(), declared, exposedVisibility); |
| 88 |
|
| 89 |
} |
| 90 |
break; |
| 91 |
} |
| 92 |
case StandardNames.XSL_VARIABLE: { |
| 93 |
SourceBinding binding = psm.getGlobalVariableBinding(qName); |
| 94 |
if (binding != null && !(binding.getSourceElement() instanceof XSLGlobalParam)) { |
| 95 |
found = true; |
| 96 |
} |
| 97 |
if (found) { |
| 98 |
GlobalVariable var = ((XSLGlobalVariable) binding.getSourceElement()).getCompiledVariable(); |
| 99 |
Visibility declared = var.getDeclaredVisibility(); |
| 100 |
checkCompatibility(var.getSymbolicName(), declared, getVisibility()); |
| 101 |
|
| 102 |
} |
| 103 |
break; |
| 104 |
} |
| 105 |
case StandardNames.XSL_ATTRIBUTE_SET: { |
| 106 |
List<ComponentDeclaration> declarations = psm.getAttributeSetDeclarations(qName); |
| 107 |
found = declarations != null && !declarations.isEmpty(); |
| 108 |
if (found) { |
| 109 |
Visibility declared = declarations.get(0).getSourceElement().getDeclaredVisibility(); |
| 110 |
checkCompatibility(sName, declared, getVisibility()); |
| 111 |
} |
| 112 |
break; |
| 113 |
} |
| 114 |
case StandardNames.XSL_MODE: |
| 115 |
Mode mode = psm.getRuleManager().obtainMode(qName, false); |
| 116 |
found = mode != null; |
| 117 |
if (found) { |
| 118 |
checkCompatibility(sName, mode.getDeclaredVisibility(), getVisibility()); |
| 119 |
} |
| 120 |
if (getVisibility() == Visibility.ABSTRACT) { |
| 121 |
|
| 122 |
compileError("The visibility of a mode cannot be abstract"); |
| 123 |
} |
| 124 |
break; |
| 125 |
case StandardNames.XSL_FUNCTION: |
| 126 |
StylesheetPackage pack = psm.getStylesheetPackage(); |
| 127 |
if (test.getArity() == -1) { |
| 128 |
|
| 129 |
for (int i = 0; i <= pack.getMaxFunctionArity(); i++) { |
| 130 |
sName = new SymbolicName.F(((NameTest) nameTest).getMatchingNodeName(), i); |
| 131 |
Component fn = pack.getComponent(sName); |
| 132 |
if (fn != null) { |
| 133 |
found = true; |
| 134 |
UserFunction userFunction = (UserFunction)fn.getActor(); |
| 135 |
checkCompatibility(sName, userFunction.getDeclaredVisibility(), getVisibility()); |
| 136 |
} |
| 137 |
} |
| 138 |
} else { |
| 139 |
Component fn = pack.getComponent(sName); |
| 140 |
found = fn != null; |
| 141 |
if (found) { |
| 142 |
UserFunction userFunction = (UserFunction) fn.getActor(); |
| 143 |
checkCompatibility(sName, userFunction.getDeclaredVisibility(), getVisibility()); |
| 144 |
} |
| 145 |
} |
| 146 |
break; |
| 147 |
} |
| 148 |
if (!found) { |
| 149 |
compileError("No " + sName.toString() + " exists in the containing package", "XTSE3020"); |
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
} |