| 1 | /* |
|---|
| 2 | * Copyright 2007-2008, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | package org.docx4j.jaxb; |
|---|
| 22 | |
|---|
| 23 | import java.util.HashMap; |
|---|
| 24 | import java.util.Iterator; |
|---|
| 25 | import java.util.Map; |
|---|
| 26 | |
|---|
| 27 | import javax.xml.XMLConstants; |
|---|
| 28 | import javax.xml.namespace.NamespaceContext; |
|---|
| 29 | import javax.xml.xpath.XPath; |
|---|
| 30 | import javax.xml.xpath.XPathFactory; |
|---|
| 31 | |
|---|
| 32 | import org.apache.commons.lang.text.StrTokenizer; |
|---|
| 33 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * NamespacePrefixMappings, required for JAXB, and XPath. |
|---|
| 37 | * |
|---|
| 38 | * The intent is to define the namespace prefix mappings in a |
|---|
| 39 | * single place. |
|---|
| 40 | * |
|---|
| 41 | * This class implements NamespaceContext, so it can be used as follows: |
|---|
| 42 | * |
|---|
| 43 | * XPathFactory factory = XPathFactory.newInstance(); |
|---|
| 44 | * XPath xPath = factory.newXPath(); |
|---|
| 45 | * xPath.setNamespaceContext(new NamespacePrefixMappings()); |
|---|
| 46 | * |
|---|
| 47 | * For JAXB, NamespacePrefixMapper (for RI) and NamespacePrefixMapperSunInternal |
|---|
| 48 | * (for Java 6) both refer to this class. |
|---|
| 49 | * |
|---|
| 50 | * @author jharrop |
|---|
| 51 | * |
|---|
| 52 | */ |
|---|
| 53 | public class NamespacePrefixMappings implements NamespaceContext { |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Returns a preferred prefix for the given namespace URI. |
|---|
| 58 | * |
|---|
| 59 | * @param namespaceUri |
|---|
| 60 | * The namespace URI for which the prefix needs to be found. |
|---|
| 61 | * Never be null. "" is used to denote the default namespace. |
|---|
| 62 | * @param suggestion |
|---|
| 63 | * When the content tree has a suggestion for the prefix |
|---|
| 64 | * to the given namespaceUri, that suggestion is passed as a |
|---|
| 65 | * parameter. Typically this value comes from QName.getPrefix() |
|---|
| 66 | * to show the preference of the content tree. This parameter |
|---|
| 67 | * may be null, and this parameter may represent an already |
|---|
| 68 | * occupied prefix. |
|---|
| 69 | * @param requirePrefix |
|---|
| 70 | * If this method is expected to return non-empty prefix. |
|---|
| 71 | * When this flag is true, it means that the given namespace URI |
|---|
| 72 | * cannot be set as the default namespace. |
|---|
| 73 | * |
|---|
| 74 | * @return |
|---|
| 75 | * null if there's no preferred prefix for the namespace URI. |
|---|
| 76 | * In this case, the system will generate a prefix for you. |
|---|
| 77 | * |
|---|
| 78 | * Otherwise the system will try to use the returned prefix, |
|---|
| 79 | * but generally there's no guarantee if the prefix will be |
|---|
| 80 | * actually used or not. |
|---|
| 81 | * |
|---|
| 82 | * return "" to map this namespace URI to the default namespace. |
|---|
| 83 | * Again, there's no guarantee that this preference will be |
|---|
| 84 | * honored. |
|---|
| 85 | * |
|---|
| 86 | * If this method returns "" when requirePrefix=true, the return |
|---|
| 87 | * value will be ignored and the system will generate one. |
|---|
| 88 | */ |
|---|
| 89 | protected static String getPreferredPrefixStatic(String namespaceUri, String suggestion, boolean requirePrefix) { |
|---|
| 90 | |
|---|
| 91 | if (namespaceUri.equals(Namespaces.NS_WORD12)) { |
|---|
| 92 | return "w"; |
|---|
| 93 | } |
|---|
| 94 | if (namespaceUri.equals(Namespaces.PKG_XML)) { |
|---|
| 95 | return "pkg"; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | if (namespaceUri.equals("http://schemas.openxmlformats.org/presentationml/2006/main")) { |
|---|
| 99 | return "p"; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | if (namespaceUri.equals("http://schemas.openxmlformats.org/officeDocument/2006/custom-properties")) { |
|---|
| 103 | return "prop"; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | if (namespaceUri.equals("http://schemas.openxmlformats.org/officeDocument/2006/extended-properties")) { |
|---|
| 107 | return "properties"; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | if (namespaceUri.equals("http://schemas.openxmlformats.org/package/2006/metadata/core-properties")) { |
|---|
| 111 | return "cp"; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | if (namespaceUri.equals("http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes")) { |
|---|
| 116 | return "vt"; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | if (namespaceUri.equals("http://schemas.openxmlformats.org/package/2006/relationships")) { |
|---|
| 120 | return "rel"; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | if (namespaceUri.equals(Namespaces.RELATIONSHIPS_OFFICEDOC)) { |
|---|
| 124 | return "r"; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | // DrawingML |
|---|
| 128 | if (namespaceUri.equals("http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing")) { |
|---|
| 129 | return "wp"; |
|---|
| 130 | } |
|---|
| 131 | if (namespaceUri.equals("http://schemas.openxmlformats.org/drawingml/2006/chart")) { |
|---|
| 132 | return "c"; |
|---|
| 133 | } |
|---|
| 134 | if (namespaceUri.equals("http://schemas.openxmlformats.org/drawingml/2006/main")) { |
|---|
| 135 | return "a"; |
|---|
| 136 | } |
|---|
| 137 | if (namespaceUri.equals("http://schemas.openxmlformats.org/drawingml/2006/picture")) { |
|---|
| 138 | return "pic"; |
|---|
| 139 | } |
|---|
| 140 | if (namespaceUri.equals("http://schemas.openxmlformats.org/drawingml/2006/diagram")) { |
|---|
| 141 | return "dgm"; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | if (namespaceUri.equals("http://schemas.microsoft.com/office/drawing/2008/diagram")) { |
|---|
| 145 | return "dsp"; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | if (namespaceUri.equals("urn:schemas-microsoft-com:office:office")) { |
|---|
| 149 | return "o"; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | if (namespaceUri.equals("urn:schemas-microsoft-com:vml")) { |
|---|
| 153 | return "v"; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | if (namespaceUri.equals("http://schemas.microsoft.com/office/word/2003/auxHint")) { |
|---|
| 157 | return "WX"; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | if (namespaceUri.equals("http://schemas.microsoft.com/aml/2001/core")) { |
|---|
| 161 | return "aml"; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | if (namespaceUri.equals("urn:schemas-microsoft-com:office:word")) { |
|---|
| 165 | return "w10"; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | if (namespaceUri.equals("http://schemas.openxmlformats.org/officeDocument/2006/math")) { |
|---|
| 169 | return "m"; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | if (namespaceUri.equals("http://www.w3.org/2001/XMLSchema-instance")) { |
|---|
| 173 | return "xsi"; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | if (namespaceUri.equals("http://purl.org/dc/elements/1.1/")) { |
|---|
| 177 | return "dc"; |
|---|
| 178 | } |
|---|
| 179 | if (namespaceUri.equals("http://purl.org/dc/terms/")) { |
|---|
| 180 | return "dcterms"; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | if (namespaceUri.equals("http://www.w3.org/XML/1998/namespace")) { |
|---|
| 184 | return "xml"; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | if (namespaceUri.equals("http://schemas.openxmlformats.org/officeDocument/2006/customXml")) { |
|---|
| 188 | return "ds"; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | // OpenDoPE |
|---|
| 192 | if (namespaceUri.equals("http://opendope.org/xpaths")) { |
|---|
| 193 | return "odx"; |
|---|
| 194 | } |
|---|
| 195 | if (namespaceUri.equals("http://opendope.org/conditions")) { |
|---|
| 196 | return "odc"; |
|---|
| 197 | } |
|---|
| 198 | if (namespaceUri.equals("http://opendope.org/components")) { |
|---|
| 199 | return "odi"; |
|---|
| 200 | } |
|---|
| 201 | if (namespaceUri.equals("http://opendope.org/questions")) { |
|---|
| 202 | return "odq"; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | if (namespaceUri.equals("http://opendope.org/SmartArt/DataHierarchy")) { |
|---|
| 206 | return "odgm"; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | // if (namespaceUri.equals("urn:schemas-microsoft-com:office:excel")) { |
|---|
| 210 | // return "?"; |
|---|
| 211 | // } |
|---|
| 212 | // if (namespaceUri.equals("http://schemas.openxmlformats.org/drawingml/2006/compatibility")) { |
|---|
| 213 | // return "?"; |
|---|
| 214 | // } |
|---|
| 215 | // if (namespaceUri.equals("http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas")) { |
|---|
| 216 | // return "?"; |
|---|
| 217 | // } |
|---|
| 218 | // if (namespaceUri.equals("http://schemas.openxmlformats.org/drawingml/2006/chartDrawing")) { |
|---|
| 219 | // return "?"; |
|---|
| 220 | // } |
|---|
| 221 | // if (namespaceUri.equals("http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing")) { |
|---|
| 222 | // return "?"; |
|---|
| 223 | // } |
|---|
| 224 | // if (namespaceUri.equals("http://schemas.openxmlformats.org/officeDocument/2006/bibliography")) { |
|---|
| 225 | // return "?"; |
|---|
| 226 | // } |
|---|
| 227 | // if (namespaceUri.equals("http://schemas.openxmlformats.org/schemaLibrary/2006/main")) { |
|---|
| 228 | // return "?"; |
|---|
| 229 | // } |
|---|
| 230 | |
|---|
| 231 | return suggestion; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | // ---------------------------------------------------- |
|---|
| 237 | // implement NamespaceContext, |
|---|
| 238 | // for use with for use with javax.xml.xpath |
|---|
| 239 | |
|---|
| 240 | public String getNamespaceURI(String prefix) { |
|---|
| 241 | |
|---|
| 242 | return getNamespaceURIStatic(prefix); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | protected static String getNamespaceURIStatic(String prefix) { |
|---|
| 246 | |
|---|
| 247 | // Pre-defined prefixes |
|---|
| 248 | if (prefix.equals("w")) |
|---|
| 249 | return Namespaces.NS_WORD12; |
|---|
| 250 | if (prefix.equals("r")) |
|---|
| 251 | return Namespaces.RELATIONSHIPS_OFFICEDOC; |
|---|
| 252 | if (prefix.equals("pkg")) |
|---|
| 253 | return Namespaces.PKG_XML; |
|---|
| 254 | |
|---|
| 255 | if (prefix.equals("p")) |
|---|
| 256 | return "http://schemas.openxmlformats.org/presentationml/2006/main"; |
|---|
| 257 | |
|---|
| 258 | if (prefix.equals("prop")) |
|---|
| 259 | return "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"; |
|---|
| 260 | |
|---|
| 261 | if (prefix.equals("properties")) |
|---|
| 262 | return "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"; |
|---|
| 263 | |
|---|
| 264 | if (prefix.equals("cp")) |
|---|
| 265 | return "http://schemas.openxmlformats.org/package/2006/metadata/core-properties"; |
|---|
| 266 | |
|---|
| 267 | if (prefix.equals("vt")) |
|---|
| 268 | return "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"; |
|---|
| 269 | |
|---|
| 270 | if (prefix.equals("rel")) |
|---|
| 271 | return "http://schemas.openxmlformats.org/package/2006/relationships"; |
|---|
| 272 | |
|---|
| 273 | // DrawingML |
|---|
| 274 | if (prefix.equals("wp")) |
|---|
| 275 | return "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"; |
|---|
| 276 | |
|---|
| 277 | if (prefix.equals("c")) |
|---|
| 278 | return "http://schemas.openxmlformats.org/drawingml/2006/chart"; |
|---|
| 279 | |
|---|
| 280 | if (prefix.equals("a")) |
|---|
| 281 | return "http://schemas.openxmlformats.org/drawingml/2006/main"; |
|---|
| 282 | |
|---|
| 283 | if (prefix.equals("pic")) |
|---|
| 284 | return "http://schemas.openxmlformats.org/drawingml/2006/picture"; |
|---|
| 285 | |
|---|
| 286 | if (prefix.equals("dgm")) |
|---|
| 287 | return "http://schemas.openxmlformats.org/drawingml/2006/diagram"; |
|---|
| 288 | |
|---|
| 289 | if (prefix.equals("dsp")) |
|---|
| 290 | return "http://schemas.microsoft.com/office/drawing/2008/diagram"; |
|---|
| 291 | |
|---|
| 292 | if (prefix.equals("o")) |
|---|
| 293 | return "urn:schemas-microsoft-com:office:office"; |
|---|
| 294 | |
|---|
| 295 | if (prefix.equals("v")) |
|---|
| 296 | return "urn:schemas-microsoft-com:vml"; |
|---|
| 297 | |
|---|
| 298 | if (prefix.equals("WX")) |
|---|
| 299 | return "http://schemas.microsoft.com/office/word/2003/auxHint"; |
|---|
| 300 | |
|---|
| 301 | if (prefix.equals("aml")) |
|---|
| 302 | return "http://schemas.microsoft.com/aml/2001/core"; |
|---|
| 303 | |
|---|
| 304 | if (prefix.equals("w10")) |
|---|
| 305 | return "urn:schemas-microsoft-com:office:word"; |
|---|
| 306 | |
|---|
| 307 | if (prefix.equals("m")) |
|---|
| 308 | return "http://schemas.openxmlformats.org/officeDocument/2006/math"; |
|---|
| 309 | |
|---|
| 310 | if (prefix.equals("xsi")) |
|---|
| 311 | return "http://www.w3.org/2001/XMLSchema-instance"; |
|---|
| 312 | |
|---|
| 313 | if (prefix.equals("dc")) |
|---|
| 314 | return "http://purl.org/dc/elements/1.1/"; |
|---|
| 315 | |
|---|
| 316 | if (prefix.equals("dcterms")) |
|---|
| 317 | return "http://purl.org/dc/terms/"; |
|---|
| 318 | |
|---|
| 319 | if (prefix.equals("xml")) |
|---|
| 320 | return "http://www.w3.org/XML/1998/namespace"; |
|---|
| 321 | |
|---|
| 322 | if (prefix.equals("ds")) |
|---|
| 323 | return "http://schemas.openxmlformats.org/officeDocument/2006/customXml"; |
|---|
| 324 | |
|---|
| 325 | // OpenDoPE |
|---|
| 326 | if (prefix.equals("odx")) |
|---|
| 327 | return "http://opendope.org/xpaths"; |
|---|
| 328 | if (prefix.equals("odc")) |
|---|
| 329 | return "http://opendope.org/conditions"; |
|---|
| 330 | if (prefix.equals("odi")) |
|---|
| 331 | return "http://opendope.org/components"; |
|---|
| 332 | if (prefix.equals("odq")) |
|---|
| 333 | return "http://opendope.org/questions"; |
|---|
| 334 | if (prefix.equals("odgm")) |
|---|
| 335 | return "http://opendope.org/SmartArt/DataHierarchy"; |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | // Registered prefixes |
|---|
| 339 | String result = namespaces.get(prefix); |
|---|
| 340 | if (result==null) { |
|---|
| 341 | return XMLConstants.NULL_NS_URI; |
|---|
| 342 | } else { |
|---|
| 343 | return result; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | public String getPrefix(String namespaceURI) { |
|---|
| 349 | |
|---|
| 350 | return getPreferredPrefixStatic(namespaceURI, null, false ); |
|---|
| 351 | |
|---|
| 352 | // if (namespaceURI.equals(Namespaces.NS_WORD12)) |
|---|
| 353 | // return "w"; |
|---|
| 354 | // else if (namespaceURI.equals(Namespaces.RELATIONSHIPS_OFFICEDOC)) |
|---|
| 355 | // return "r"; |
|---|
| 356 | // else if (namespaceURI.equals(Namespaces.PKG_XML)) |
|---|
| 357 | // return "pkg"; |
|---|
| 358 | // else return null; |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | public Iterator getPrefixes(String namespaceURI) { |
|---|
| 362 | return null; |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | private static Map<String, String> namespaces = new HashMap<String, String>(); |
|---|
| 366 | public static void registerPrefixMappings(String prefixMappings) { |
|---|
| 367 | // eg w:prefixMappings="xmlns:ns0='http://schemas.medchart'" |
|---|
| 368 | // according to the spec, whitespace is the delimiter |
|---|
| 369 | |
|---|
| 370 | if (prefixMappings==null || prefixMappings.equals("") ) return; |
|---|
| 371 | |
|---|
| 372 | // we get one of these each time we encounter a w:dataBinding |
|---|
| 373 | // element in a content control; pity it is not done just |
|---|
| 374 | // once! |
|---|
| 375 | |
|---|
| 376 | // first tokenise on space |
|---|
| 377 | StrTokenizer tokens = new StrTokenizer(prefixMappings); |
|---|
| 378 | while (tokens.hasNext() ) { |
|---|
| 379 | String token = tokens.nextToken(); |
|---|
| 380 | //log.debug("Got: " + token); |
|---|
| 381 | int pos = token.indexOf("="); |
|---|
| 382 | String prefix = token.substring(6, pos); // drop xmlns: |
|---|
| 383 | //log.debug("Got: " + prefix); |
|---|
| 384 | String uri = token.substring(pos+2, token.lastIndexOf("'")); |
|---|
| 385 | //log.debug("Got: " + uri); |
|---|
| 386 | namespaces.put(prefix, uri); |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | } |
|---|