| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.instruct.ResultDocument; |
| 11 |
import net.sf.saxon.expr.parser.Location; |
| 12 |
import net.sf.saxon.lib.NamespaceConstant; |
| 13 |
import net.sf.saxon.lib.SaxonOutputKeys; |
| 14 |
import net.sf.saxon.om.*; |
| 15 |
import net.sf.saxon.pattern.NodeKindTest; |
| 16 |
import net.sf.saxon.regex.UnicodeString; |
| 17 |
import net.sf.saxon.trans.Err; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 20 |
import net.sf.saxon.tree.util.Navigator; |
| 21 |
import net.sf.saxon.type.Type; |
| 22 |
import net.sf.saxon.z.IntHashMap; |
| 23 |
|
| 24 |
import java.util.Arrays; |
| 25 |
import java.util.HashSet; |
| 26 |
import java.util.Properties; |
| 27 |
import java.util.Set; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class SerializationParamsHandler { |
| 34 |
|
| 35 |
public static final String NAMESPACE = NamespaceConstant.OUTPUT; |
| 36 |
Properties properties; |
| 37 |
CharacterMap characterMap; |
| 38 |
Location locator; |
| 39 |
|
| 40 |
public SerializationParamsHandler() {} |
| 41 |
|
| 42 |
public SerializationParamsHandler(Properties props) { |
| 43 |
this.properties = props; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public void setLocator(Location locator) { |
| 52 |
this.locator = locator; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public void setSerializationParams(NodeInfo node) throws XPathException { |
| 64 |
if (properties == null) { |
| 65 |
properties = new Properties(); |
| 66 |
} |
| 67 |
if (node.getNodeKind() == Type.DOCUMENT) { |
| 68 |
node = Navigator.getOutermostElement(node.getTreeInfo()); |
| 69 |
} |
| 70 |
if (node.getNodeKind() != Type.ELEMENT) { |
| 71 |
throw new XPathException("Serialization params: node must be a document or element node"); |
| 72 |
} |
| 73 |
if (!node.getLocalPart().equals("serialization-parameters")) { |
| 74 |
throw new XPathException("Serialization params: element name must be 'serialization-parameters"); |
| 75 |
} |
| 76 |
if (!node.getURI().equals(NAMESPACE)) { |
| 77 |
throw new XPathException("Serialization params: element namespace must be " + NAMESPACE); |
| 78 |
} |
| 79 |
restrictAttributes(node); |
| 80 |
Set<NodeName> nodeNames = new HashSet<>(); |
| 81 |
AxisIterator kids = node.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 82 |
NodeInfo child; |
| 83 |
while ((child = kids.next()) != null) { |
| 84 |
if (!nodeNames.add(NameOfNode.makeName(child))) { |
| 85 |
throw new XPathException("Duplicated serialization parameter " + child.getDisplayName(), "SEPM0019"); |
| 86 |
} |
| 87 |
String lname = child.getLocalPart(); |
| 88 |
String uri = child.getURI(); |
| 89 |
if (uri.isEmpty()) { |
| 90 |
throw new XPathException("Serialization parameter " + lname + " is in no namespace", "SEPM0017"); |
| 91 |
} |
| 92 |
if (NamespaceConstant.OUTPUT.equals(uri)) { |
| 93 |
uri = ""; |
| 94 |
} |
| 95 |
if ("".equals(uri) && lname.equals("use-character-maps")) { |
| 96 |
restrictAttributes(child); |
| 97 |
AxisIterator gKids = child.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 98 |
NodeInfo gChild; |
| 99 |
IntHashMap<String> map = new IntHashMap<>(); |
| 100 |
while ((gChild = gKids.next()) != null) { |
| 101 |
restrictAttributes(gChild, "character", "map-string"); |
| 102 |
if (!(gChild.getURI().equals(NAMESPACE) && gChild.getLocalPart().equals("character-map"))) { |
| 103 |
if (gChild.getURI().equals(NAMESPACE) || gChild.getURI().isEmpty()) { |
| 104 |
throw new XPathException("Invalid child of use-character-maps: " + gChild.getDisplayName(), "SEPM0017"); |
| 105 |
} |
| 106 |
} |
| 107 |
String ch = getAttribute(gChild, "character"); |
| 108 |
String str = getAttribute(gChild, "map-string"); |
| 109 |
UnicodeString chValue = UnicodeString.makeUnicodeString(ch); |
| 110 |
|
| 111 |
if (chValue.uLength() != 1) { |
| 112 |
throw new XPathException("In the serialization parameters, the value of @character in the character map " + |
| 113 |
"must be a single Unicode character", "SEPM0017"); |
| 114 |
} |
| 115 |
int code = chValue.uCharAt(0); |
| 116 |
String prev = map.put(code, str); |
| 117 |
if (prev != null) { |
| 118 |
throw new XPathException("In the serialization parameters, the character map contains two entries for the character \\u" + |
| 119 |
Integer.toHexString(65536+code).substring(1), "SEPM0018"); |
| 120 |
} |
| 121 |
} |
| 122 |
characterMap = new CharacterMap(NameOfNode.makeName(node).getStructuredQName(), map); |
| 123 |
} else { |
| 124 |
restrictAttributes(child, "value"); |
| 125 |
String value = getAttribute(child, "value"); |
| 126 |
try { |
| 127 |
ResultDocument.setSerializationProperty(properties, uri, lname, value, |
| 128 |
new InscopeNamespaceResolver(child), false, node.getConfiguration()); |
| 129 |
} catch (XPathException err) { |
| 130 |
if ("XQST0109".equals(err.getErrorCodeLocalPart()) || "SEPM0016".equals(err.getErrorCodeLocalPart())) { |
| 131 |
if ("".equals(uri)) { |
| 132 |
XPathException e2 = new XPathException("Unknown serialization parameter " + |
| 133 |
Err.depict(child), "SEPM0017"); |
| 134 |
e2.setLocator(locator); |
| 135 |
throw e2; |
| 136 |
} |
| 137 |
|
| 138 |
} else { |
| 139 |
throw err; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
private static void restrictAttributes(NodeInfo element, String... allowedNames) throws XPathException { |
| 155 |
element.iterateAxis(AxisInfo.ATTRIBUTE).forEachOrFail(att -> { |
| 156 |
if ("".equals(att.getURI()) && Arrays.binarySearch(allowedNames, att.getLocalPart()) < 0) { |
| 157 |
throw new XPathException("In serialization parameters, attribute @" + att.getLocalPart() + |
| 158 |
" must not appear on element " + element.getDisplayName(), "SEPM0017"); |
| 159 |
} |
| 160 |
}); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
private static String getAttribute(NodeInfo element, String localName) throws XPathException { |
| 173 |
String value = element.getAttributeValue("", localName); |
| 174 |
if (value == null) { |
| 175 |
throw new XPathException("In serialization parameters, attribute @" + localName + |
| 176 |
" is missing on element " + element.getDisplayName()); |
| 177 |
} |
| 178 |
return value; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public SerializationProperties getSerializationProperties() { |
| 188 |
CharacterMapIndex index = new CharacterMapIndex(); |
| 189 |
if (characterMap != null) { |
| 190 |
index.putCharacterMap(new StructuredQName("", "", "charMap"), characterMap); |
| 191 |
properties.put(SaxonOutputKeys.USE_CHARACTER_MAPS, "charMap"); |
| 192 |
} |
| 193 |
return new SerializationProperties(properties, index); |
| 194 |
} |
| 195 |
|
| 196 |
public CharacterMap getCharacterMap() { |
| 197 |
return characterMap; |
| 198 |
} |
| 199 |
} |
| 200 |
|