| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.functions.DocumentFn; |
| 12 |
import net.sf.saxon.om.AttributeCollection; |
| 13 |
import net.sf.saxon.om.DocumentURI; |
| 14 |
import net.sf.saxon.om.TreeInfo; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.tree.linked.DocumentImpl; |
| 17 |
import net.sf.saxon.tree.linked.ElementImpl; |
| 18 |
import net.sf.saxon.value.Whitespace; |
| 19 |
|
| 20 |
import javax.xml.transform.Source; |
| 21 |
import javax.xml.transform.URIResolver; |
| 22 |
import java.util.Map; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public abstract class XSLGeneralIncorporate extends StyleElement { |
| 31 |
|
| 32 |
private String href; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@Override |
| 42 |
public boolean isDeclaration() { |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public abstract boolean isImport(); |
| 54 |
|
| 55 |
public void prepareAttributes() throws XPathException { |
| 56 |
|
| 57 |
AttributeCollection atts = getAttributeList(); |
| 58 |
|
| 59 |
for (int a = 0; a < atts.getLength(); a++) { |
| 60 |
String f = atts.getQName(a); |
| 61 |
if (f.equals("href")) { |
| 62 |
href = Whitespace.trim(atts.getValue(a)); |
| 63 |
} else { |
| 64 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if (href == null) { |
| 69 |
reportAbsence("href"); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 74 |
validateInstruction(); |
| 75 |
} |
| 76 |
|
| 77 |
public void validateInstruction() { |
| 78 |
checkEmpty(); |
| 79 |
checkTopLevel(isImport() ? "XTSE0190" : "XTSE0170", false); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public StylesheetModule getIncludedStylesheet(StylesheetModule importer, int precedence) { |
| 92 |
|
| 93 |
if (href == null) { |
| 94 |
|
| 95 |
return null; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
try { |
| 102 |
PrincipalStylesheetModule psm = importer.getPrincipalStylesheetModule(); |
| 103 |
|
| 104 |
URIResolver resolver = getCompilation().getCompilerInfo().getURIResolver(); |
| 105 |
Configuration config = getConfiguration(); |
| 106 |
XSLStylesheet includedSheet; |
| 107 |
StylesheetModule incModule; |
| 108 |
|
| 109 |
DocumentURI key = DocumentFn.computeDocumentKey(href, getBaseURI(), getCompilation().getPackageData(), resolver, false); |
| 110 |
includedSheet = (XSLStylesheet)psm.getStylesheetDocument(key); |
| 111 |
if (includedSheet != null) { |
| 112 |
|
| 113 |
|
| 114 |
incModule = new StylesheetModule(includedSheet, precedence); |
| 115 |
incModule.setImporter(importer); |
| 116 |
|
| 117 |
|
| 118 |
if (checkForRecursion(importer, incModule.getRootElement())) { |
| 119 |
return null; |
| 120 |
} |
| 121 |
|
| 122 |
} else { |
| 123 |
|
| 124 |
Map<DocumentURI, TreeInfo> map = getCompilation().getStylesheetModules(); |
| 125 |
DocumentImpl includedDoc = (DocumentImpl)map.get(key); |
| 126 |
|
| 127 |
ElementImpl outermost = includedDoc.getDocumentElement(); |
| 128 |
|
| 129 |
if (outermost instanceof LiteralResultElement) { |
| 130 |
includedDoc = ((LiteralResultElement) outermost).makeStylesheet(false); |
| 131 |
outermost = includedDoc.getDocumentElement(); |
| 132 |
} |
| 133 |
|
| 134 |
if (!(outermost instanceof XSLStylesheet)) { |
| 135 |
String verb = this instanceof XSLImport ? "Imported" : "Included"; |
| 136 |
compileError(verb + " document " + href + " is not a stylesheet", "XTSE0165"); |
| 137 |
return null; |
| 138 |
} |
| 139 |
includedSheet = (XSLStylesheet) outermost; |
| 140 |
psm.putStylesheetDocument(key, includedSheet); |
| 141 |
|
| 142 |
incModule = new StylesheetModule(includedSheet, precedence); |
| 143 |
incModule.setImporter(importer); |
| 144 |
ComponentDeclaration decl = new ComponentDeclaration(incModule, includedSheet); |
| 145 |
includedSheet.validate(decl); |
| 146 |
|
| 147 |
if (includedSheet.validationError != null) { |
| 148 |
if (reportingCircumstances == REPORT_ALWAYS) { |
| 149 |
includedSheet.compileError(includedSheet.validationError); |
| 150 |
} else if (includedSheet.reportingCircumstances == REPORT_UNLESS_FORWARDS_COMPATIBLE |
| 151 |
|
| 152 |
) { |
| 153 |
includedSheet.compileError(includedSheet.validationError); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
incModule.spliceIncludes(); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
importer.setInputTypeAnnotations(includedSheet.getInputTypeAnnotationsAttribute() | |
| 163 |
incModule.getInputTypeAnnotations()); |
| 164 |
|
| 165 |
return incModule; |
| 166 |
|
| 167 |
} catch (XPathException err) { |
| 168 |
err.setErrorCode("XTSE0165"); |
| 169 |
err.setIsStaticError(true); |
| 170 |
compileError(err); |
| 171 |
return null; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
private boolean checkForRecursion(StylesheetModule importer, Source source) { |
| 176 |
StylesheetModule anc = importer; |
| 177 |
|
| 178 |
if (source.getSystemId() != null) { |
| 179 |
while (anc != null) { |
| 180 |
if (DocumentURI.normalizeURI(source.getSystemId()) |
| 181 |
.equals(DocumentURI.normalizeURI(anc.getRootElement().getSystemId()))) { |
| 182 |
compileError("A stylesheet cannot " + getLocalPart() + " itself", |
| 183 |
this instanceof XSLInclude ? "XTSE0180" : "XTSE0210"); |
| 184 |
return true; |
| 185 |
} |
| 186 |
anc = anc.getImporter(); |
| 187 |
} |
| 188 |
} |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) { |
| 193 |
|
| 194 |
|
| 195 |
} |
| 196 |
} |
| 197 |
|