1 /**
  2  * @fileoverview A class representing the document node of the
  3  * XDM interface.
  4  */
  5 
  6 goog.provide('xrx.node.Document');
  7 
  8 
  9 
 10 goog.require('xrx.node');
 11 goog.require('xrx.node.Element');
 12 goog.require('xrx.node.Text');
 13 goog.require('xrx.token');
 14 goog.require('xrx.xpath.NodeSet');
 15 
 16 
 17 
 18 /** 
 19  * @constructor 
 20  */
 21 xrx.node.Document = function(pilot, instance) {
 22   goog.base(this, pilot, xrx.node.DOCUMENT, new xrx.token.Root(), instance);
 23 };
 24 goog.inherits(xrx.node.Document, xrx.node);
 25 
 26 xrx.node.Document.prototype.accAttributes = function() {};
 27 xrx.node.Document.prototype.accBaseUri = function() {};
 28 xrx.node.Document.prototype.accChildren = function() {};
 29 xrx.node.Document.prototype.accDocumentUri = function() {};
 30 xrx.node.Document.prototype.accIsId = function() {};
 31 xrx.node.Document.prototype.accIsIdrefs = function() {};
 32 xrx.node.Document.prototype.accNamespaceNodes = function() {};
 33 xrx.node.Document.prototype.accNilled = function() {};
 34 xrx.node.Document.prototype.accNodeKind = function() {};
 35 xrx.node.Document.prototype.accNodeName = function() {};
 36 xrx.node.Document.prototype.accParent = function() {};
 37 xrx.node.Document.prototype.accStringValue = function() {};
 38 xrx.node.Document.prototype.accTypeName = function() {};
 39 xrx.node.Document.prototype.accTypedValue = function() {};
 40 xrx.node.Document.prototype.accUnparsedEntityPublicId = function() {};
 41 xrx.node.Document.prototype.accUnparsedEntitySystemId = function() {};
 42 
 43 /**
 44  * @private
 45  */
 46 xrx.node.prototype.forward = function() {
 47   var node = this;
 48   var stream = node.pilot_.stream();
 49   var label = node.label().clone();
 50   var first = true;
 51   var lastTag;
 52   if (label.length() === 0) label.child();
 53   
 54   stream.rowStartTag = function(offset, length1, length2) {
 55     if (first) {
 56       first = false;
 57     } else if (lastTag === xrx.token.START_TAG) {
 58       label.child();
 59     } else {
 60       label.nextSibling();
 61     }
 62     node.nodeElement(new xrx.token.StartEmptyTag(label.clone(), offset, length1));
 63 
 64     if (length1 !== length2) {
 65       var lbl = label.clone();
 66       lbl.push0();
 67       node.nodeText(new xrx.token.NotTag(lbl.clone(), 
 68           offset + length1, length2 - length1));
 69     }
 70     lastTag = xrx.token.START_TAG;
 71   };
 72   
 73   stream.rowEndTag = function(offset, length1, length2) {
 74     if (lastTag !== xrx.token.START_TAG) label.parent();
 75     if (length1 !== length2) {
 76       var lbl = label.clone();
 77       node.nodeText(new xrx.token.NotTag(lbl.clone(), 
 78           offset + length1, length2 - length1));
 79     }
 80     lastTag = xrx.token.END_TAG;
 81   };
 82   
 83   stream.rowEmptyTag = function(offset, length1, length2) {
 84     if (first) {
 85       first = false;
 86     } else if (lastTag === xrx.token.START_TAG) {
 87       label.child();
 88     } else {
 89       label.nextSibling();
 90     }
 91     node.nodeElement(new xrx.token.StartEmptyTag(label.clone(), offset, length1));
 92     if (length1 !== length2) {
 93       var lbl = label.clone();
 94       node.nodeText(new xrx.token.NotTag(lbl.clone(), 
 95           offset + length1, length2 - length1));
 96     }
 97     lastTag = xrx.token.END_TAG;
 98   };
 99   
100   stream.forward(node.offset());
101 };
102 
103 
104 
105 /**
106  * @private
107  */
108 xrx.node.prototype.backward = function() {
109   var node = this;
110   var stream = node.pilot_.stream();
111   var label = node.label().clone();
112   var lastTag = xrx.token.START_TAG;
113   
114   stream.rowStartTag = function(offset, length1, length2) {
115     if (lastTag !== xrx.token.END_TAG) label.parent();
116     node.nodeElement(new xrx.token.StartEmptyTag(label.clone(), offset, length1));
117 
118     if (length1 !== length2) {
119       var lbl = label.clone();
120       lbl.push0();
121       node.nodeText(new xrx.token.NotTag(lbl.clone(), 
122           offset + length1, length2 - length1));
123     }
124     lastTag = xrx.token.START_TAG;
125   };
126   
127   stream.rowEndTag = function(offset, length1, length2) {
128     lastTag === xrx.token.END_TAG ? label.child() : label.precedingSibling();
129     if (length1 !== length2) {
130       var lbl = label.clone();
131       node.nodeText(new xrx.token.NotTag(lbl.clone(), 
132           offset + length1, length2 - length1));
133     }
134     lastTag = xrx.token.END_TAG;
135   };
136   
137   stream.rowEmptyTag = function(offset, length1, length2) {
138     lastTag === xrx.token.END_TAG ? label.child() : label.precedingSibling();
139     node.nodeElement(new xrx.token.StartEmptyTag(label.clone(), offset, length1));
140     if (length1 !== length2) {
141       var lbl = label.clone();
142       node.nodeText(new xrx.token.NotTag(lbl.clone(), 
143           offset + length1, length2 - length1));
144     }
145     lastTag = xrx.token.START_TAG;
146   };
147   stream.backward(node.offset());
148 };
149 
150 
151 /**
152  * @private
153  */
154 xrx.node.prototype.find = function(test, axisTest, reverse) {
155   var nodeset = new xrx.xpath.NodeSet();
156   var pilot = this.pilot_;
157   var instance = this.instance_;
158   var elmnt = null;
159 
160   this.nodeElement = function(token) {
161     elmnt = new xrx.node.Element(pilot, token, instance);
162     if (axisTest.call(this.label(), token.label()) && test.matches(elmnt)) {
163       reverse ? nodeset.unshift(elmnt) : nodeset.add(elmnt);
164     }
165   };
166 
167   this.nodeText = function(token) {
168     var txt = new xrx.node.Text(pilot, token, elmnt, instance);
169     if (axisTest.call(this.label(), token.label()) && test.matches(txt)) {
170       reverse ? nodeset.unshift(txt) : nodeset.add(txt);
171     }
172   };
173   
174   reverse ? this.backward() : this.forward();
175   return nodeset;
176 };
177 
178 
179 
180 /** 
181  * @overwrite
182  */
183 xrx.node.Document.prototype.getAncestorNodes = function() {
184   return new xrx.xpath.NodeSet();
185 };
186 
187 
188 
189 /** 
190  * @overwrite
191  */
192 xrx.node.Document.prototype.getAttributeNodes = function() {
193   return new xrx.xpath.NodeSet();
194 };
195 
196 
197 
198 /** 
199  * @overwrite
200  */
201 xrx.node.Document.prototype.xml = function() {
202   return xrx.model.getComponent(this.instance_).xml();
203 };