| 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.event; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.Location; |
| 11 | import net.sf.saxon.om.*; |
| 12 | import net.sf.saxon.trans.XPathException; |
| 13 | import net.sf.saxon.type.SchemaType; |
| 14 | import net.sf.saxon.type.SimpleType; |
| 15 | |
| 16 | /** |
| 17 | * This class is a filter that passes all Receiver events through unchanged, |
| 18 | * except that it changes namecodes to allow for the source and the destination |
| 19 | * using different NamePools. This is necessary when a stylesheet has been constructed |
| 20 | * as a general document (e.g. as the result of a transformation) and is passed to |
| 21 | * newTemplates() to be compiled as a stylesheet. |
| 22 | * |
| 23 | * <p>The type annotations of nodes passed through this filter must be built-in types |
| 24 | * in the XSD namespace, because user-defined types belong to a specific Configuration |
| 25 | * and cannot readily be transferred. In practice the class is used only for untyped trees.</p> |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | public class NamePoolConverter extends ProxyReceiver { |
| 30 | |
| 31 | NamePool oldPool; |
| 32 | NamePool newPool; |
| 33 | |
| 34 | /** |
| 35 | * Constructor |
| 36 | * |
| 37 | * @param next the next receiver in the pipeline |
| 38 | * @param oldPool the old namepool |
| 39 | * @param newPool th new namepool |
| 40 | */ |
| 41 | |
| 42 | public NamePoolConverter(Receiver next, NamePool oldPool, NamePool newPool) { |
| 43 | super(next); |
| 44 | this.oldPool = oldPool; |
| 45 | this.newPool = newPool; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Output element start tag |
| 50 | */ |
| 51 | |
| 52 | public void startElement(NodeName name, SchemaType type, Location location, int properties) throws XPathException { |
| 53 | checkType(type); |
| 54 | int fp = newPool.allocateFingerprint(name.getURI(), name.getLocalPart()); |
| 55 | nextReceiver.startElement(new CodedName(fp, name.getPrefix(), newPool), type, location, properties); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Handle an attribute |
| 60 | */ |
| 61 | |
| 62 | public void attribute(NodeName name, SimpleType type, CharSequence value, Location location, int properties) |
| 63 | throws XPathException { |
| 64 | checkType(type); |
| 65 | int fp = newPool.allocateFingerprint(name.getURI(), name.getLocalPart()); |
| 66 | nextReceiver.attribute(new CodedName(fp, name.getPrefix(), newPool), type, value, location, properties); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Check that the schema type of a node is a built-in type |
| 71 | * @param type the type to be checked |
| 72 | * @throws UnsupportedOperationException if the type is a user-defined type |
| 73 | */ |
| 74 | |
| 75 | private void checkType(SchemaType type) { |
| 76 | if ((type.getFingerprint() & NamePool.USER_DEFINED_MASK) != 0) { |
| 77 | throw new UnsupportedOperationException("Cannot convert a user-typed node to a different name pool"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | } |
| 82 |