| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.dom; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.lib.NamespaceConstant; |
| 12 |
import net.sf.saxon.om.AxisInfo; |
| 13 |
import net.sf.saxon.om.GenericTreeInfo; |
| 14 |
import net.sf.saxon.om.NodeInfo; |
| 15 |
import net.sf.saxon.pattern.NodeKindTest; |
| 16 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 17 |
import org.w3c.dom.*; |
| 18 |
|
| 19 |
import java.net.URI; |
| 20 |
import java.net.URISyntaxException; |
| 21 |
import java.util.*; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class DocumentWrapper extends GenericTreeInfo { |
| 31 |
|
| 32 |
protected boolean domLevel3; |
| 33 |
public final Node docNode; |
| 34 |
private Map<String, NodeInfo> idIndex = null; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public DocumentWrapper(Node doc, String baseURI, Configuration config) { |
| 45 |
super(config); |
| 46 |
if (doc.getNodeType() != Node.DOCUMENT_NODE && doc.getNodeType() != Node.DOCUMENT_FRAGMENT_NODE) { |
| 47 |
throw new IllegalArgumentException("Node must be a DOM Document or DocumentFragment"); |
| 48 |
} |
| 49 |
if (config.getExternalObjectModel(doc.getClass()) == null) { |
| 50 |
throw new IllegalArgumentException( |
| 51 |
"Node class " + doc.getClass().getName() + " is not recognized in this Saxon configuration"); |
| 52 |
} |
| 53 |
domLevel3 = true; |
| 54 |
this.docNode = doc; |
| 55 |
setRootNode(wrap(doc)); |
| 56 |
setSystemId(baseURI); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public DOMNodeWrapper wrap(Node node) { |
| 70 |
return DOMNodeWrapper.makeWrapper(node, this); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public void setDOMLevel(int level) { |
| 90 |
if (!(level == 2 || level == 3)) { |
| 91 |
throw new IllegalArgumentException("DOM Level must be 2 or 3"); |
| 92 |
} |
| 93 |
domLevel3 = level == 3; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public int getDOMLevel() { |
| 103 |
return domLevel3 ? 3 : 2; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public NodeInfo selectID(String id, boolean getParent) { |
| 118 |
synchronized (docNode) { |
| 119 |
Node node = ((DOMNodeWrapper)getRootNode()).node; |
| 120 |
if (node instanceof Document) { |
| 121 |
Node el = ((Document) node).getElementById(id); |
| 122 |
if (el != null) { |
| 123 |
return wrap(el); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if (idIndex != null) { |
| 128 |
return idIndex.get(id); |
| 129 |
} else { |
| 130 |
idIndex = new HashMap<>(); |
| 131 |
AxisIterator iter = getRootNode().iterateAxis(AxisInfo.DESCENDANT, NodeKindTest.ELEMENT); |
| 132 |
NodeInfo e; |
| 133 |
while ((e = iter.next()) != null) { |
| 134 |
String xmlId = e.getAttributeValue(NamespaceConstant.XML, "id"); |
| 135 |
if (xmlId != null) { |
| 136 |
idIndex.put(xmlId, e); |
| 137 |
} |
| 138 |
} |
| 139 |
return idIndex.get(id); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public Iterator<String> getUnparsedEntityNames() { |
| 155 |
synchronized (docNode) { |
| 156 |
Node node = ((DOMNodeWrapper) getRootNode()).node; |
| 157 |
if (node instanceof Document) { |
| 158 |
DocumentType docType = ((Document) node).getDoctype(); |
| 159 |
if (docType == null) { |
| 160 |
List<String> ls = Collections.emptyList(); |
| 161 |
return ls.iterator(); |
| 162 |
} |
| 163 |
NamedNodeMap map = docType.getEntities(); |
| 164 |
if (map == null) { |
| 165 |
List<String> ls = Collections.emptyList(); |
| 166 |
return ls.iterator(); |
| 167 |
} |
| 168 |
List<String> names = new ArrayList<>(map.getLength()); |
| 169 |
for (int i = 0; i < map.getLength(); i++) { |
| 170 |
Entity e = (Entity) map.item(i); |
| 171 |
if (e.getNotationName() != null) { |
| 172 |
|
| 173 |
names.add(e.getLocalName()); |
| 174 |
} |
| 175 |
} |
| 176 |
return names.iterator(); |
| 177 |
} else { |
| 178 |
return null; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public String[] getUnparsedEntity(String name) { |
| 197 |
synchronized (docNode) { |
| 198 |
Node node = ((DOMNodeWrapper) getRootNode()).node; |
| 199 |
if (node instanceof Document) { |
| 200 |
DocumentType docType = ((Document) node).getDoctype(); |
| 201 |
if (docType == null) { |
| 202 |
return null; |
| 203 |
} |
| 204 |
NamedNodeMap map = docType.getEntities(); |
| 205 |
if (map == null) { |
| 206 |
return null; |
| 207 |
} |
| 208 |
Entity entity = (Entity) map.getNamedItem(name); |
| 209 |
if (entity == null || entity.getNotationName() == null) { |
| 210 |
|
| 211 |
return null; |
| 212 |
} |
| 213 |
String systemId = entity.getSystemId(); |
| 214 |
try { |
| 215 |
URI systemIdURI = new URI(systemId); |
| 216 |
if (!systemIdURI.isAbsolute()) { |
| 217 |
String base = getRootNode().getBaseURI(); |
| 218 |
if (base != null) { |
| 219 |
systemIdURI = new URI(base).resolve(systemIdURI); |
| 220 |
systemId = systemIdURI.toString(); |
| 221 |
} else { |
| 222 |
|
| 223 |
} |
| 224 |
} |
| 225 |
} catch (URISyntaxException err) { |
| 226 |
|
| 227 |
} |
| 228 |
return new String[]{systemId, entity.getPublicId()}; |
| 229 |
} else { |
| 230 |
return null; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
} |
| 237 |
|