1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *     http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 /*
  21  * $Id: DOMAdapter.java,v 1.2.4.1 2005/09/06 06:07:28 pvedula Exp $
  22  */
  23 
  24 package com.sun.org.apache.xalan.internal.xsltc.dom;
  25 
  26 import com.sun.org.apache.xalan.internal.xsltc.DOM;
  27 import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM;
  28 import com.sun.org.apache.xalan.internal.xsltc.StripFilter;
  29 import com.sun.org.apache.xalan.internal.xsltc.TransletException;
  30 import com.sun.org.apache.xml.internal.dtm.DTM;
  31 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  32 import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
  33 import java.util.Map;
  34 import org.w3c.dom.Node;
  35 import org.w3c.dom.NodeList;
  36 
  37 /**
  38  * @author Jacek Ambroziak
  39  * @author Morten Jorgensen
  40  */
  41 public final class DOMAdapter implements DOM {
  42 
  43     // Mutually exclusive casting of DOM interface to known implementations
  44     private DOMEnhancedForDTM _enhancedDOM;
  45 
  46     private DOM _dom;
  47 
  48     private String[] _namesArray;
  49     private String[] _urisArray;
  50     private int[]    _typesArray;
  51     private String[] _namespaceArray;
  52 
  53     // Cached mappings
  54     private short[] _mapping = null;
  55     private int[]   _reverse = null;
  56     private short[] _NSmapping = null;
  57     private short[] _NSreverse = null;
  58 
  59     private StripFilter _filter = null;
  60 
  61     private int _multiDOMMask;
  62 
  63     public DOMAdapter(DOM dom,
  64                       String[] namesArray,
  65                       String[] urisArray,
  66                       int[] typesArray,
  67                       String[] namespaceArray) {
  68         if (dom instanceof DOMEnhancedForDTM){
  69             _enhancedDOM = (DOMEnhancedForDTM) dom;
  70         }
  71 
  72         _dom = dom;
  73         _namesArray = namesArray;
  74         _urisArray = urisArray;
  75         _typesArray = typesArray;
  76         _namespaceArray = namespaceArray;
  77     }
  78 
  79     public void setupMapping(String[] names, String[] urisArray,
  80                              int[] typesArray, String[] namespaces) {
  81         _namesArray = names;
  82         _urisArray = urisArray;
  83         _typesArray = typesArray;
  84         _namespaceArray = namespaces;
  85     }
  86 
  87     public String[] getNamesArray() {
  88         return _namesArray;
  89     }
  90 
  91     public String[] getUrisArray() {
  92         return _urisArray;
  93     }
  94 
  95     public int[] getTypesArray() {
  96         return _typesArray;
  97     }
  98 
  99     public String[] getNamespaceArray() {
 100         return _namespaceArray;
 101     }
 102 
 103     public DOM getDOMImpl() {
 104         return _dom;
 105     }
 106 
 107     private short[] getMapping() {
 108         if (_mapping == null) {
 109             if (_enhancedDOM != null) {
 110                 _mapping = _enhancedDOM.getMapping(_namesArray, _urisArray,
 111                                                    _typesArray);
 112             }
 113         }
 114         return _mapping;
 115     }
 116 
 117     private int[] getReverse() {
 118         if (_reverse == null) {
 119             if (_enhancedDOM != null) {
 120                 _reverse = _enhancedDOM.getReverseMapping(_namesArray,
 121                                                           _urisArray,
 122                                                           _typesArray);
 123             }
 124         }
 125         return _reverse;
 126     }
 127 
 128     private short[] getNSMapping() {
 129         if (_NSmapping == null) {
 130             if (_enhancedDOM != null) {
 131                 _NSmapping = _enhancedDOM.getNamespaceMapping(_namespaceArray);
 132             }
 133         }
 134         return _NSmapping;
 135     }
 136 
 137     private short[] getNSReverse() {
 138         if (_NSreverse == null) {
 139             if (_enhancedDOM != null) {
 140                 _NSreverse = _enhancedDOM
 141                                   .getReverseNamespaceMapping(_namespaceArray);
 142             }
 143         }
 144         return _NSreverse;
 145     }
 146 
 147     /**
 148       * Returns singleton iterator containg the document root
 149       */
 150     public DTMAxisIterator getIterator() {
 151         return _dom.getIterator();
 152     }
 153 
 154     public String getStringValue() {
 155         return _dom.getStringValue();
 156     }
 157 
 158     public DTMAxisIterator getChildren(final int node) {
 159         if (_enhancedDOM != null) {
 160             return _enhancedDOM.getChildren(node);
 161         }
 162         else {
 163             DTMAxisIterator iterator = _dom.getChildren(node);
 164             return iterator.setStartNode(node);
 165         }
 166     }
 167 
 168     public void setFilter(StripFilter filter) {
 169         _filter = filter;
 170     }
 171 
 172     public DTMAxisIterator getTypedChildren(final int type) {
 173         final int[] reverse = getReverse();
 174 
 175         if (_enhancedDOM != null) {
 176             return _enhancedDOM.getTypedChildren(reverse[type]);
 177         }
 178         else {
 179             return _dom.getTypedChildren(type);
 180         }
 181     }
 182 
 183     public DTMAxisIterator getNamespaceAxisIterator(final int axis,
 184                                                     final int ns) {
 185         return _dom.getNamespaceAxisIterator(axis, getNSReverse()[ns]);
 186     }
 187 
 188     public DTMAxisIterator getAxisIterator(final int axis) {
 189         if (_enhancedDOM != null) {
 190             return _enhancedDOM.getAxisIterator(axis);
 191         }
 192         else {
 193             return _dom.getAxisIterator(axis);
 194         }
 195     }
 196 
 197     public DTMAxisIterator getTypedAxisIterator(final int axis,
 198                                                 final int type) {
 199         final int[] reverse = getReverse();
 200         if (_enhancedDOM != null) {
 201             return _enhancedDOM.getTypedAxisIterator(axis, reverse[type]);
 202         } else {
 203             return _dom.getTypedAxisIterator(axis, type);
 204         }
 205     }
 206 
 207     public int getMultiDOMMask() {
 208         return _multiDOMMask;
 209     }
 210 
 211     public void setMultiDOMMask(int mask) {
 212         _multiDOMMask = mask;
 213     }
 214 
 215     public DTMAxisIterator getNthDescendant(int type, int n,
 216                                             boolean includeself) {
 217         return _dom.getNthDescendant(getReverse()[type], n, includeself);
 218     }
 219 
 220     public DTMAxisIterator getNodeValueIterator(DTMAxisIterator iterator,
 221                                                 int type, String value,
 222                                                 boolean op) {
 223         return _dom.getNodeValueIterator(iterator, type, value, op);
 224     }
 225 
 226     public DTMAxisIterator orderNodes(DTMAxisIterator source, int node) {
 227         return _dom.orderNodes(source, node);
 228     }
 229 
 230     public int getExpandedTypeID(final int node) {
 231         final short[] mapping = getMapping();
 232         final int type;
 233         if (_enhancedDOM != null) {
 234             type = mapping[_enhancedDOM.getExpandedTypeID2(node)];
 235         }
 236         else {
 237                 if(null != mapping)
 238                 {
 239                 type = mapping[_dom.getExpandedTypeID(node)];
 240                 }
 241                 else
 242                 {
 243                         type = _dom.getExpandedTypeID(node);
 244                 }
 245         }
 246         return type;
 247     }
 248 
 249     public int getNamespaceType(final int node) {
 250         return getNSMapping()[_dom.getNSType(node)];
 251     }
 252 
 253     public int getNSType(int node) {
 254         return _dom.getNSType(node);
 255     }
 256 
 257     public int getParent(final int node) {
 258         return _dom.getParent(node);
 259     }
 260 
 261     public int getAttributeNode(final int type, final int element) {
 262         return _dom.getAttributeNode(getReverse()[type], element);
 263     }
 264 
 265     public String getNodeName(final int node) {
 266         if (node == DTM.NULL) {
 267             return "";
 268         }
 269         return _dom.getNodeName(node);
 270     }
 271 
 272     public String getNodeNameX(final int node)
 273     {
 274         if (node == DTM.NULL) {
 275             return "";
 276         }
 277         return _dom.getNodeNameX(node);
 278     }
 279 
 280     public String getNamespaceName(final int node)
 281     {
 282         if (node == DTM.NULL) {
 283             return "";
 284         }
 285         return _dom.getNamespaceName(node);
 286     }
 287 
 288     public String getStringValueX(final int node)
 289     {
 290         if (_enhancedDOM != null) {
 291             return _enhancedDOM.getStringValueX(node);
 292         }
 293         else {
 294             if (node == DTM.NULL) {
 295                 return "";
 296             }
 297             return _dom.getStringValueX(node);
 298         }
 299     }
 300 
 301     public void copy(final int node, SerializationHandler handler)
 302         throws TransletException
 303     {
 304         _dom.copy(node, handler);
 305     }
 306 
 307     public void copy(DTMAxisIterator nodes,SerializationHandler handler)
 308         throws TransletException
 309     {
 310         _dom.copy(nodes, handler);
 311     }
 312 
 313     public String shallowCopy(final int node, SerializationHandler handler)
 314         throws TransletException
 315     {
 316         if (_enhancedDOM != null) {
 317             return _enhancedDOM.shallowCopy(node, handler);
 318         }
 319         else {
 320             return _dom.shallowCopy(node, handler);
 321         }
 322     }
 323 
 324     public boolean lessThan(final int node1, final int node2)
 325     {
 326         return _dom.lessThan(node1, node2);
 327     }
 328 
 329     public void characters(final int textNode, SerializationHandler handler)
 330       throws TransletException
 331     {
 332         if (_enhancedDOM != null) {
 333             _enhancedDOM.characters(textNode, handler);
 334         }
 335         else {
 336             _dom.characters(textNode, handler);
 337         }
 338     }
 339 
 340     public Node makeNode(int index)
 341     {
 342         return _dom.makeNode(index);
 343     }
 344 
 345     public Node makeNode(DTMAxisIterator iter)
 346     {
 347         return _dom.makeNode(iter);
 348     }
 349 
 350     public NodeList makeNodeList(int index)
 351     {
 352         return _dom.makeNodeList(index);
 353     }
 354 
 355     public NodeList makeNodeList(DTMAxisIterator iter)
 356     {
 357         return _dom.makeNodeList(iter);
 358     }
 359 
 360     public String getLanguage(int node)
 361     {
 362         return _dom.getLanguage(node);
 363     }
 364 
 365     public int getSize()
 366     {
 367         return _dom.getSize();
 368     }
 369 
 370     public void setDocumentURI(String uri)
 371     {
 372         if (_enhancedDOM != null) {
 373             _enhancedDOM.setDocumentURI(uri);
 374         }
 375     }
 376 
 377     public String getDocumentURI()
 378     {
 379         if (_enhancedDOM != null) {
 380             return _enhancedDOM.getDocumentURI();
 381         }
 382         else {
 383             return "";
 384         }
 385     }
 386 
 387     public String getDocumentURI(int node)
 388     {
 389         return _dom.getDocumentURI(node);
 390     }
 391 
 392     public int getDocument()
 393     {
 394         return _dom.getDocument();
 395     }
 396 
 397     public boolean isElement(final int node)
 398     {
 399         return(_dom.isElement(node));
 400     }
 401 
 402     public boolean isAttribute(final int node)
 403     {
 404         return(_dom.isAttribute(node));
 405     }
 406 
 407     public int getNodeIdent(int nodeHandle)
 408     {
 409         return _dom.getNodeIdent(nodeHandle);
 410     }
 411 
 412     public int getNodeHandle(int nodeId)
 413     {
 414         return _dom.getNodeHandle(nodeId);
 415     }
 416 
 417     /**
 418      * Return a instance of a DOM class to be used as an RTF
 419      */
 420     public DOM getResultTreeFrag(int initSize, int rtfType)
 421     {
 422         if (_enhancedDOM != null) {
 423             return _enhancedDOM.getResultTreeFrag(initSize, rtfType);
 424         }
 425         else {
 426             return _dom.getResultTreeFrag(initSize, rtfType);
 427         }
 428     }
 429 
 430     /**
 431      * Return a instance of a DOM class to be used as an RTF
 432      */
 433     public DOM getResultTreeFrag(int initSize, int rtfType,
 434                                  boolean addToManager)
 435     {
 436         if (_enhancedDOM != null) {
 437             return _enhancedDOM.getResultTreeFrag(initSize, rtfType,
 438                                                   addToManager);
 439         }
 440         else {
 441             return _dom.getResultTreeFrag(initSize, rtfType, addToManager);
 442         }
 443     }
 444 
 445 
 446     /**
 447      * Returns a SerializationHandler class wrapped in a SAX adapter.
 448      */
 449     public SerializationHandler getOutputDomBuilder()
 450     {
 451         return _dom.getOutputDomBuilder();
 452     }
 453 
 454     public String lookupNamespace(int node, String prefix)
 455         throws TransletException
 456     {
 457         return _dom.lookupNamespace(node, prefix);
 458     }
 459 
 460     public String getUnparsedEntityURI(String entity) {
 461         return _dom.getUnparsedEntityURI(entity);
 462     }
 463 
 464     public Map<String, Integer> getElementsWithIDs() {
 465         return _dom.getElementsWithIDs();
 466     }
 467 }