View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2002-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * 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: DOMWSFilter.java,v 1.2.4.1 2005/09/06 06:14:31 pvedula Exp $
22   */
23  package com.sun.org.apache.xalan.internal.xsltc.dom;
24  
25  import com.sun.org.apache.xalan.internal.xsltc.DOM;
26  import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM;
27  import com.sun.org.apache.xalan.internal.xsltc.StripFilter;
28  import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
29  import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
30  import com.sun.org.apache.xml.internal.dtm.DTM;
31  import com.sun.org.apache.xml.internal.dtm.DTMWSFilter;
32  
33  /**
34   * A wrapper class that adapts the
35   * {@link com.sun.org.apache.xml.internal.dtm.DTMWSFilter DTMWSFilter} interface to the XSLTC
36   * DOM {@link com.sun.org.apache.xalan.internal.xsltc.StripFilter StripFilter} interface.
37   */
38  public class DOMWSFilter implements DTMWSFilter {
39  
40      private AbstractTranslet m_translet;
41      private StripFilter m_filter;
42  
43      // The Hashtable for DTM to mapping array
44      private Hashtable m_mappings;
45  
46      // Cache the DTM and mapping that are used last time
47      private DTM m_currentDTM;
48      private short[] m_currentMapping;
49  
50      /**
51       * Construct an adapter connecting the <code>DTMWSFilter</code> interface
52       * to the <code>StripFilter</code> interface.
53       *
54       * @param translet A translet that also implements the StripFilter
55       * interface.
56       *
57       * @see com.sun.org.apache.xml.internal.dtm.DTMWSFilter
58       * @see com.sun.org.apache.xalan.internal.xsltc.StripFilter
59       */
60      public DOMWSFilter(AbstractTranslet translet) {
61          m_translet = translet;
62          m_mappings = new Hashtable();
63  
64          if (translet instanceof StripFilter) {
65              m_filter = (StripFilter) translet;
66          }
67      }
68  
69      /**
70       * Test whether whitespace-only text nodes are visible in the logical
71       * view of <code>DTM</code>. Normally, this function
72       * will be called by the implementation of <code>DTM</code>;
73       * it is not normally called directly from
74       * user code.
75       *
76       * @param node int handle of the node.
77       * @param dtm the DTM that owns this node
78       * @return one of <code>NOTSTRIP</code>, <code>STRIP</code> or
79       * <code>INHERIT</code>.
80       */
81      public short getShouldStripSpace(int node, DTM dtm) {
82          if (m_filter != null && dtm instanceof DOM) {
83              DOM dom = (DOM)dtm;
84              int type = 0;
85  
86              if (dtm instanceof DOMEnhancedForDTM) {
87                  DOMEnhancedForDTM mappableDOM = (DOMEnhancedForDTM)dtm;
88  
89                  short[] mapping;
90                  if (dtm == m_currentDTM) {
91                      mapping = m_currentMapping;
92                  }
93                  else {
94                      mapping = (short[])m_mappings.get(dtm);
95                      if (mapping == null) {
96                          mapping = mappableDOM.getMapping(
97                                       m_translet.getNamesArray(),
98                                       m_translet.getUrisArray(),
99                                       m_translet.getTypesArray());
100                         m_mappings.put(dtm, mapping);
101                         m_currentDTM = dtm;
102                         m_currentMapping = mapping;
103                     }
104                 }
105 
106                 int expType = mappableDOM.getExpandedTypeID(node);
107 
108                 // %OPT% The mapping array does not have information about all the
109                 // exptypes. However it does contain enough information about all names
110                 // in the translet's namesArray. If the expType does not fall into the
111                 // range of the mapping array, it means that the expType is not for one
112                 // of the recognized names. In this case we can just set the type to -1.
113                 if (expType >= 0 && expType < mapping.length)
114                   type = mapping[expType];
115                 else
116                   type = -1;
117 
118             }
119             else {
120                 return INHERIT;
121             }
122 
123             if (m_filter.stripSpace(dom, node, type)) {
124                 return STRIP;
125             } else {
126                 return NOTSTRIP;
127             }
128         } else {
129             return NOTSTRIP;
130         }
131     }
132 }