001: package net.sf.saxon.om;
002:
003: import net.sf.saxon.Configuration;
004: import net.sf.saxon.style.StandardNames;
005: import net.sf.saxon.pattern.NodeKindTest;
006: import net.sf.saxon.event.Stripper;
007:
008: /**
009: * A StrippedDocument represents a view of a real Document in which selected
010: * whitespace text nodes are treated as having been stripped.
011: */
012:
013: public class StrippedDocument extends StrippedNode implements
014: DocumentInfo {
015:
016: private Stripper stripper;
017: private boolean preservesSpace;
018:
019: public StrippedDocument(DocumentInfo doc, Stripper stripper) {
020: this .node = doc;
021: this .parent = null;
022: this .docWrapper = this ;
023: this .stripper = stripper;
024: preservesSpace = findPreserveSpace(doc);
025: }
026:
027: /**
028: * Create a wrapped node within this document
029: */
030:
031: public StrippedNode wrap(NodeInfo node) {
032: return makeWrapper(node, this , null);
033: }
034:
035: /**
036: * Get the document's stripper
037: */
038:
039: public Stripper getStripper() {
040: return stripper;
041: }
042:
043: /**
044: * Get the configuration previously set using setConfiguration
045: */
046:
047: public Configuration getConfiguration() {
048: return node.getConfiguration();
049: }
050:
051: /**
052: * Get the name pool used for the names in this document
053: */
054:
055: public NamePool getNamePool() {
056: return node.getNamePool();
057: }
058:
059: /**
060: * Get the unique document number
061: */
062:
063: public int getDocumentNumber() {
064: return node.getDocumentNumber();
065: }
066:
067: /**
068: * Get the element with a given ID, if any
069: * @param id the required ID value
070: * @return the element with the given ID value, or null if there is none.
071: */
072:
073: public NodeInfo selectID(String id) {
074: NodeInfo n = ((DocumentInfo) node).selectID(id);
075: if (n == null) {
076: return null;
077: } else {
078: return makeWrapper(n, this , null);
079: }
080: }
081:
082: /**
083: * Get the unparsed entity with a given name
084: * @param name the name of the entity
085: */
086:
087: public String[] getUnparsedEntity(String name) {
088: return ((DocumentInfo) node).getUnparsedEntity(name);
089: }
090:
091: /**
092: * Determine whether the wrapped document contains any xml:space="preserve" attributes. If it
093: * does, we will look for them when stripping individual nodes. It's more efficient to scan
094: * the document in advance checking for xml:space attributes than to look for them every time
095: * we hit a whitespace text node.
096: */
097:
098: private static boolean findPreserveSpace(DocumentInfo doc) {
099: AxisIterator iter = doc.iterateAxis(Axis.DESCENDANT,
100: NodeKindTest.ELEMENT);
101: while (true) {
102: NodeInfo node = (NodeInfo) iter.next();
103: if (node == null) {
104: return false;
105: }
106: String val = node
107: .getAttributeValue(StandardNames.XML_SPACE);
108: if ("preserve".equals(val)) {
109: return true;
110: }
111: }
112: }
113:
114: /**
115: * Does the stripped document contain any xml:space="preserve" attributes?
116: */
117:
118: public boolean containsPreserveSpace() {
119: return preservesSpace;
120: }
121:
122: }
123:
124: //
125: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
126: // you may not use this file except in compliance with the License. You may obtain a copy of the
127: // License at http://www.mozilla.org/MPL/
128: //
129: // Software distributed under the License is distributed on an "AS IS" basis,
130: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
131: // See the License for the specific language governing rights and limitations under the License.
132: //
133: // The Original Code is: all this file.
134: //
135: // The Initial Developer of the Original Code is
136: // Michael H. Kay.
137: //
138: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
139: //
140: // Contributor(s): none.
141: //
|