| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.style; |
| 9 | |
| 10 | import net.sf.saxon.expr.instruct.GlobalContextRequirement; |
| 11 | import net.sf.saxon.om.AxisInfo; |
| 12 | import net.sf.saxon.pattern.SameNameTest; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.tree.iter.AxisIterator; |
| 15 | |
| 16 | /** |
| 17 | * An xsl:global-context-item declaration in the stylesheet |
| 18 | */ |
| 19 | public class XSLGlobalContextItem extends XSLContextItem { |
| 20 | |
| 21 | public boolean isDeclaration() { |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Check that the stylesheet element is valid. This is called once for each element, after |
| 27 | * the entire tree has been built. As well as validation, it can perform first-time |
| 28 | * initialisation. The default implementation does nothing; it is normally overriden |
| 29 | * in subclasses. |
| 30 | * |
| 31 | * @param decl the declaration to be validated |
| 32 | * @throws net.sf.saxon.trans.XPathException |
| 33 | * if any error is found during validation |
| 34 | */ |
| 35 | @Override |
| 36 | public void validate(ComponentDeclaration decl) throws XPathException { |
| 37 | AxisIterator prior = iterateAxis(AxisInfo.PRECEDING_SIBLING, new SameNameTest(this)); |
| 38 | if (prior.next() != null) { |
| 39 | compileError("xsl:global-context-item must not appear twice within the same stylesheet module", "XTSE3087"); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Method supplied by declaration elements to add themselves to a stylesheet-level index |
| 45 | * |
| 46 | * @param decl the Declaration being indexed. (This corresponds to the StyleElement object |
| 47 | * except in cases where one module is imported several times with different precedence.) |
| 48 | * @param top the outermost XSLStylesheet element |
| 49 | * @throws net.sf.saxon.trans.XPathException |
| 50 | * if any error is encountered |
| 51 | */ |
| 52 | @Override |
| 53 | public void index(ComponentDeclaration decl, PrincipalStylesheetModule top) throws XPathException { |
| 54 | prepareAttributes(); |
| 55 | GlobalContextRequirement req = new GlobalContextRequirement(); |
| 56 | req.setMayBeOmitted(isMayBeOmitted()); |
| 57 | req.setAbsentFocus(isAbsentFocus()); |
| 58 | req.addRequiredItemType(getRequiredContextItemType()); |
| 59 | try { |
| 60 | top.getStylesheetPackage().setContextItemRequirements(req); |
| 61 | } catch (XPathException e) { |
| 62 | e.setLocation(decl.getSourceElement()); |
| 63 | throw e; |
| 64 | } |
| 65 | } |
| 66 | } |