KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > dom > DOMWSFilter


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 org.apache.xalan.xsltc.dom;
20
21 import org.apache.xalan.xsltc.DOM;
22 import org.apache.xalan.xsltc.DOMEnhancedForDTM;
23 import org.apache.xalan.xsltc.StripFilter;
24 import org.apache.xalan.xsltc.runtime.AbstractTranslet;
25 import org.apache.xalan.xsltc.runtime.Hashtable;
26 import org.apache.xml.dtm.DTM;
27 import org.apache.xml.dtm.DTMWSFilter;
28
29 /**
30  * A wrapper class that adapts the
31  * {@link org.apache.xml.dtm.DTMWSFilter DTMWSFilter} interface to the XSLTC
32  * DOM {@link org.apache.xsltc.StripFilter StripFilter} interface.
33  */

34 public class DOMWSFilter implements DTMWSFilter {
35
36     private AbstractTranslet m_translet;
37     private StripFilter m_filter;
38     
39     // The Hashtable for DTM to mapping array
40
private Hashtable m_mappings;
41     
42     // Cache the DTM and mapping that are used last time
43
private DTM m_currentDTM;
44     private short[] m_currentMapping;
45
46     /**
47      * Construct an adapter connecting the <code>DTMWSFilter</code> interface
48      * to the <code>StripFilter</code> interface.
49      *
50      * @param translet A translet that also implements the StripFilter
51      * interface.
52      *
53      * @see org.apache.xml.dtm.DTMWSFilter
54      * @see org.apache.xsltc.StripFilter
55      */

56     public DOMWSFilter(AbstractTranslet translet) {
57         m_translet = translet;
58         m_mappings = new Hashtable();
59
60         if (translet instanceof StripFilter) {
61             m_filter = (StripFilter) translet;
62         }
63     }
64
65     /**
66      * Test whether whitespace-only text nodes are visible in the logical
67      * view of <code>DTM</code>. Normally, this function
68      * will be called by the implementation of <code>DTM</code>;
69      * it is not normally called directly from
70      * user code.
71      *
72      * @param node int handle of the node.
73      * @param dtm the DTM that owns this node
74      * @return one of <code>NOTSTRIP</code>, <code>STRIP</code> or
75      * <code>INHERIT</code>.
76      */

77     public short getShouldStripSpace(int node, DTM dtm) {
78         if (m_filter != null && dtm instanceof DOM) {
79             DOM dom = (DOM)dtm;
80             int type = 0;
81
82             if (dtm instanceof DOMEnhancedForDTM) {
83                 DOMEnhancedForDTM mappableDOM = (DOMEnhancedForDTM)dtm;
84                 
85                 short[] mapping;
86                 if (dtm == m_currentDTM) {
87                     mapping = m_currentMapping;
88                 }
89                 else {
90                     mapping = (short[])m_mappings.get(dtm);
91                     if (mapping == null) {
92                         mapping = mappableDOM.getMapping(
93                                      m_translet.getNamesArray(),
94                                      m_translet.getUrisArray(),
95                                      m_translet.getTypesArray());
96                         m_mappings.put(dtm, mapping);
97                         m_currentDTM = dtm;
98                         m_currentMapping = mapping;
99                     }
100                 }
101                 
102                 int expType = mappableDOM.getExpandedTypeID(node);
103                 
104                 // %OPT% The mapping array does not have information about all the
105
// exptypes. However it does contain enough information about all names
106
// in the translet's namesArray. If the expType does not fall into the
107
// range of the mapping array, it means that the expType is not for one
108
// of the recognized names. In this case we can just set the type to -1.
109
if (expType >= 0 && expType < mapping.length)
110                   type = mapping[expType];
111                 else
112                   type = -1;
113                 
114             }
115             else {
116                 return INHERIT;
117             }
118
119             if (m_filter.stripSpace(dom, node, type)) {
120                 return STRIP;
121             } else {
122                 return NOTSTRIP;
123             }
124         } else {
125             return NOTSTRIP;
126         }
127     }
128 }
129
Popular Tags