| 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.serialize; |
| 9 | |
| 10 | import net.sf.saxon.event.Receiver; |
| 11 | import net.sf.saxon.lib.SerializerFactory; |
| 12 | import net.sf.saxon.om.StructuredQName; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | |
| 15 | import java.util.*; |
| 16 | |
| 17 | /** |
| 18 | * This class represents a set of named character maps. Each character map in the set is identified by a unique |
| 19 | * QName. |
| 20 | */ |
| 21 | |
| 22 | public class CharacterMapIndex implements Iterable<CharacterMap> { |
| 23 | |
| 24 | private HashMap<StructuredQName, CharacterMap> index = new HashMap<StructuredQName, CharacterMap>(10); |
| 25 | |
| 26 | public CharacterMapIndex() { |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Get the character map with a given name |
| 31 | * @param name the name of the required character map |
| 32 | * @return the requested character map if present, or null otherwise |
| 33 | */ |
| 34 | |
| 35 | public CharacterMap getCharacterMap(StructuredQName name) { |
| 36 | return index.get(name); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Add a character map with a given name |
| 41 | * @param name the name of the character map |
| 42 | * @param charMap the character map to be added |
| 43 | */ |
| 44 | |
| 45 | public void putCharacterMap(StructuredQName name, CharacterMap charMap) { |
| 46 | index.put(name, charMap); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get an iterator over all the character maps in the index |
| 51 | * @return an iterator over the character maps |
| 52 | */ |
| 53 | |
| 54 | public Iterator<CharacterMap> iterator() { |
| 55 | return index.values().iterator(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Ask if the character map index is empty |
| 60 | * @return true if the character map index contains no character maps |
| 61 | */ |
| 62 | |
| 63 | public boolean isEmpty() { |
| 64 | return index.isEmpty(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Copy this character map index |
| 69 | * @return a new character map index with the same contents. The character maps themselves are |
| 70 | * immutable and do not need to be copied |
| 71 | */ |
| 72 | |
| 73 | public CharacterMapIndex copy() { |
| 74 | CharacterMapIndex copy = new CharacterMapIndex(); |
| 75 | copy.index = new HashMap<>(this.index); |
| 76 | return copy; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Make a CharacterMapExpander to handle the character map definitions in the serialization |
| 81 | * properties. |
| 82 | * <p>This method is intended for internal use only.</p> |
| 83 | * |
| 84 | * @param useMaps the expanded use-character-maps property: a space-separated list of names |
| 85 | * of character maps to be used, each one expressed as an expanded-QName in Clark notation |
| 86 | * (that is, {uri}local-name). |
| 87 | * @param next the next receiver in the pipeline |
| 88 | * @param sf the SerializerFactory - used to create a CharacterMapExpander. This callback |
| 89 | * is provided so that a user-defined SerializerFactory can customize the result of this function, |
| 90 | * for example by returning a subclass of the standard CharacterMapExpander. |
| 91 | * @return a CharacterMapExpander if one is required, or null if not (for example, if the |
| 92 | * useMaps argument is an empty string). |
| 93 | * @throws net.sf.saxon.trans.XPathException |
| 94 | * if a name in the useMaps property cannot be resolved to a declared |
| 95 | * character map. |
| 96 | */ |
| 97 | |
| 98 | public CharacterMapExpander makeCharacterMapExpander( |
| 99 | String useMaps, Receiver next, SerializerFactory sf) throws XPathException { |
| 100 | CharacterMapExpander characterMapExpander = null; |
| 101 | List<CharacterMap> characterMaps = new ArrayList<CharacterMap>(5); |
| 102 | StringTokenizer st = new StringTokenizer(useMaps, " \t\n\r", false); |
| 103 | while (st.hasMoreTokens()) { |
| 104 | String expandedName = st.nextToken(); |
| 105 | StructuredQName qName = StructuredQName.fromClarkName(expandedName); |
| 106 | CharacterMap map = getCharacterMap(qName); |
| 107 | if (map == null) { |
| 108 | throw new XPathException("Character map '" + expandedName + "' has not been defined", "SEPM0016"); |
| 109 | } |
| 110 | characterMaps.add(map); |
| 111 | } |
| 112 | if (!characterMaps.isEmpty()) { |
| 113 | characterMapExpander = sf.newCharacterMapExpander(next); |
| 114 | if (characterMaps.size() == 1) { |
| 115 | characterMapExpander.setCharacterMap(characterMaps.get(0)); |
| 116 | } else { |
| 117 | characterMapExpander.setCharacterMap(new CharacterMap(characterMaps)); |
| 118 | } |
| 119 | } |
| 120 | return characterMapExpander; |
| 121 | } |
| 122 | |
| 123 | } |
| 124 |