KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > om > StrippedDocument


1 package net.sf.saxon.om;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.style.StandardNames;
4 import net.sf.saxon.pattern.NodeKindTest;
5 import net.sf.saxon.event.Stripper;
6
7 /**
8   * A StrippedDocument represents a view of a real Document in which selected
9  * whitespace text nodes are treated as having been stripped.
10   */

11
12 public class StrippedDocument extends StrippedNode implements DocumentInfo {
13
14     private Stripper stripper;
15     private boolean preservesSpace;
16
17     public StrippedDocument(DocumentInfo doc, Stripper stripper) {
18         this.node = doc;
19         this.parent = null;
20         this.docWrapper = this;
21         this.stripper = stripper;
22         preservesSpace = findPreserveSpace(doc);
23     }
24
25     /**
26      * Create a wrapped node within this document
27      */

28
29     public StrippedNode wrap(NodeInfo node) {
30         return makeWrapper(node, this, null);
31     }
32
33     /**
34      * Get the document's stripper
35      */

36
37     public Stripper getStripper() {
38         return stripper;
39     }
40
41     /**
42      * Get the configuration previously set using setConfiguration
43      */

44
45     public Configuration getConfiguration() {
46         return node.getConfiguration();
47     }
48
49     /**
50     * Get the name pool used for the names in this document
51     */

52
53     public NamePool getNamePool() {
54         return node.getNamePool();
55     }
56
57     /**
58     * Get the unique document number
59     */

60
61     public int getDocumentNumber() {
62         return node.getDocumentNumber();
63     }
64
65     /**
66     * Get the element with a given ID, if any
67     * @param id the required ID value
68     * @return the element with the given ID value, or null if there is none.
69     */

70
71     public NodeInfo selectID(String JavaDoc id) {
72         NodeInfo n = ((DocumentInfo)node).selectID(id);
73         if (n==null) {
74             return null;
75         } else {
76             return makeWrapper(n, this, null);
77         }
78     }
79
80     /**
81     * Get the unparsed entity with a given name
82     * @param name the name of the entity
83     */

84
85     public String JavaDoc[] getUnparsedEntity(String JavaDoc name) {
86         return ((DocumentInfo)node).getUnparsedEntity(name);
87     }
88
89     /**
90      * Determine whether the wrapped document contains any xml:space="preserve" attributes. If it
91      * does, we will look for them when stripping individual nodes. It's more efficient to scan
92      * the document in advance checking for xml:space attributes than to look for them every time
93      * we hit a whitespace text node.
94      */

95
96     private static boolean findPreserveSpace(DocumentInfo doc) {
97         AxisIterator iter = doc.iterateAxis(Axis.DESCENDANT, NodeKindTest.ELEMENT);
98         while (true) {
99             NodeInfo node = (NodeInfo)iter.next();
100             if (node == null) {
101                 return false;
102             }
103             String JavaDoc val = node.getAttributeValue(StandardNames.XML_SPACE);
104             if ("preserve".equals(val)) {
105                 return true;
106             }
107         }
108     }
109
110     /**
111      * Does the stripped document contain any xml:space="preserve" attributes?
112      */

113
114     public boolean containsPreserveSpace() {
115         return preservesSpace;
116     }
117
118
119 }
120
121 //
122
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
123
// you may not use this file except in compliance with the License. You may obtain a copy of the
124
// License at http://www.mozilla.org/MPL/
125
//
126
// Software distributed under the License is distributed on an "AS IS" basis,
127
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
128
// See the License for the specific language governing rights and limitations under the License.
129
//
130
// The Original Code is: all this file.
131
//
132
// The Initial Developer of the Original Code is
133
// Michael H. Kay.
134
//
135
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
136
//
137
// Contributor(s): none.
138
//
139
Popular Tags