1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: DOMWSFilter.java,v 1.5 2004/02/16 22:54:59 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.dom;
  20. import com.sun.org.apache.xalan.internal.xsltc.DOM;
  21. import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM;
  22. import com.sun.org.apache.xalan.internal.xsltc.StripFilter;
  23. import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
  24. import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
  25. import com.sun.org.apache.xml.internal.dtm.DTM;
  26. import com.sun.org.apache.xml.internal.dtm.DTMWSFilter;
  27. /**
  28. * A wrapper class that adapts the
  29. * {@link com.sun.org.apache.xml.internal.dtm.DTMWSFilter DTMWSFilter} interface to the XSLTC
  30. * DOM {@link org.apache.xsltc.StripFilter StripFilter} interface.
  31. */
  32. public class DOMWSFilter implements DTMWSFilter {
  33. private AbstractTranslet m_translet;
  34. private StripFilter m_filter;
  35. // The Hashtable for DTM to mapping array
  36. private Hashtable m_mappings;
  37. // Cache the DTM and mapping that are used last time
  38. private DTM m_currentDTM;
  39. private short[] m_currentMapping;
  40. /**
  41. * Construct an adapter connecting the <code>DTMWSFilter</code> interface
  42. * to the <code>StripFilter</code> interface.
  43. *
  44. * @param translet A translet that also implements the StripFilter
  45. * interface.
  46. *
  47. * @see com.sun.org.apache.xml.internal.dtm.DTMWSFilter
  48. * @see org.apache.xsltc.StripFilter
  49. */
  50. public DOMWSFilter(AbstractTranslet translet) {
  51. m_translet = translet;
  52. m_mappings = new Hashtable();
  53. if (translet instanceof StripFilter) {
  54. m_filter = (StripFilter) translet;
  55. }
  56. }
  57. /**
  58. * Test whether whitespace-only text nodes are visible in the logical
  59. * view of <code>DTM</code>. Normally, this function
  60. * will be called by the implementation of <code>DTM</code>
  61. * it is not normally called directly from
  62. * user code.
  63. *
  64. * @param node int handle of the node.
  65. * @param dtm the DTM that owns this node
  66. * @return one of <code>NOTSTRIP</code>, <code>STRIP</code> or
  67. * <code>INHERIT</code>.
  68. */
  69. public short getShouldStripSpace(int node, DTM dtm) {
  70. if (m_filter != null && dtm instanceof DOM) {
  71. DOM dom = (DOM)dtm;
  72. int type = 0;
  73. if (dtm instanceof DOMEnhancedForDTM) {
  74. DOMEnhancedForDTM mappableDOM = (DOMEnhancedForDTM)dtm;
  75. short[] mapping;
  76. if (dtm == m_currentDTM) {
  77. mapping = m_currentMapping;
  78. }
  79. else {
  80. mapping = (short[])m_mappings.get(dtm);
  81. if (mapping == null) {
  82. mapping = mappableDOM.getMapping(
  83. m_translet.getNamesArray(),
  84. m_translet.getUrisArray(),
  85. m_translet.getTypesArray());
  86. m_mappings.put(dtm, mapping);
  87. m_currentDTM = dtm;
  88. m_currentMapping = mapping;
  89. }
  90. }
  91. int expType = mappableDOM.getExpandedTypeID(node);
  92. // %OPT% The mapping array does not have information about all the
  93. // exptypes. However it does contain enough information about all names
  94. // in the translet's namesArray. If the expType does not fall into the
  95. // range of the mapping array, it means that the expType is not for one
  96. // of the recognized names. In this case we can just set the type to -1.
  97. if (expType >= 0 && expType < mapping.length)
  98. type = mapping[expType];
  99. else
  100. type = -1;
  101. }
  102. else {
  103. return INHERIT;
  104. }
  105. if (m_filter.stripSpace(dom, node, type)) {
  106. return STRIP;
  107. } else {
  108. return NOTSTRIP;
  109. }
  110. } else {
  111. return NOTSTRIP;
  112. }
  113. }
  114. }