1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.tika.parser.odf;
18
19 import java.io.IOException;
20 import java.io.StringReader;
21
22 import org.apache.tika.sax.ContentHandlerDecorator;
23 import org.xml.sax.Attributes;
24 import org.xml.sax.ContentHandler;
25 import org.xml.sax.InputSource;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.helpers.AttributesImpl;
28
29
30
31
32
33
34
35 public class NSNormalizerContentHandler extends ContentHandlerDecorator {
36
37 private static final String OLD_NS =
38 "http://openoffice.org/2000/";
39
40 private static final String NEW_NS =
41 "urn:oasis:names:tc:opendocument:xmlns:";
42
43 private static final String DTD_PUBLIC_ID =
44 "-//OpenOffice.org//DTD OfficeDocument 1.0//EN";
45
46 public NSNormalizerContentHandler(ContentHandler handler) {
47 super(handler);
48 }
49
50 private String mapOldNS(String ns) {
51 if (ns != null && ns.startsWith(OLD_NS)) {
52 return NEW_NS + ns.substring(OLD_NS.length()) + ":1.0";
53 } else {
54 return ns;
55 }
56 }
57
58 @Override
59 public void startElement(
60 String namespaceURI, String localName, String qName,
61 Attributes atts) throws SAXException {
62 AttributesImpl natts = new AttributesImpl();
63 for (int i = 0; i < atts.getLength(); i++) {
64 natts.addAttribute(
65 mapOldNS(atts.getURI(i)), atts.getLocalName(i),
66 atts.getQName(i), atts.getType(i), atts.getValue(i));
67 }
68 super.startElement(mapOldNS(namespaceURI), localName, qName, atts);
69 }
70
71 @Override
72 public void endElement(String namespaceURI, String localName, String qName)
73 throws SAXException {
74 super.endElement(mapOldNS(namespaceURI), localName, qName);
75 }
76
77 @Override
78 public void startPrefixMapping(String prefix, String uri)
79 throws SAXException {
80 super.startPrefixMapping(prefix, mapOldNS(uri));
81 }
82
83
84
85
86
87 @Override
88 public InputSource resolveEntity(String publicId, String systemId)
89 throws IOException, SAXException {
90 if ((systemId != null && systemId.toLowerCase().endsWith(".dtd"))
91 || DTD_PUBLIC_ID.equals(publicId)) {
92 return new InputSource(new StringReader(""));
93 } else {
94 return super.resolveEntity(publicId, systemId);
95 }
96 }
97
98 }