1 Clazz.declarePackage ("swingjs");
2 Clazz.load (["org.xml.sax.Parser", "$.XMLReader"], "swingjs.JSSAXParser", ["java.io.BufferedInputStream", "$.BufferedReader", "java.lang.Boolean", "java.util.Hashtable", "JU.AU", "$.PT", "$.Rdr", "org.xml.sax.SAXParseException", "swingjs.JSSAXAttributes", "$.JSSAXContentHandler", "$.JSToolkit", "swingjs.api.DOMNode"], function () {
3 c$ = Clazz.decorateAsClass (function () {
5 this.dtdHandler = null;
6 this.docHandler = null;
7 this.contentHandler = null;
8 this.errorHandler = null;
10 this.uniqueSeq = null;
12 this.tempChars = null;
14 Clazz.instantialize (this, arguments);
15 }, swingjs, "JSSAXParser", null, [org.xml.sax.Parser, org.xml.sax.XMLReader]);
16 Clazz.prepareFields (c$, function () {
17 this.tempChars = Clazz.newCharArray (1024, '\0');
19 Clazz.overrideMethod (c$, "setLocale",
21 }, "java.util.Locale");
22 Clazz.overrideMethod (c$, "setEntityResolver",
24 this.resolver = resolver;
25 }, "org.xml.sax.EntityResolver");
26 Clazz.overrideMethod (c$, "setDTDHandler",
28 this.dtdHandler = handler;
29 }, "org.xml.sax.DTDHandler");
30 Clazz.overrideMethod (c$, "setDocumentHandler",
32 this.docHandler = handler;
33 }, "org.xml.sax.DocumentHandler");
34 Clazz.overrideMethod (c$, "setErrorHandler",
36 this.errorHandler = handler;
37 }, "org.xml.sax.ErrorHandler");
38 Clazz.defineMethod (c$, "parse",
39 function (source, handler) {
40 this.setContentHandler (handler);
41 this.parseSource (source);
42 }, "org.xml.sax.InputSource,swingjs.JSSAXContentHandler");
43 Clazz.defineMethod (c$, "parse",
45 this.parseSource (source);
46 }, "org.xml.sax.InputSource");
47 Clazz.defineMethod (c$, "parseSource",
49 var rdr = source.getCharacterStream ();
50 var data = new Array (1);
52 var bs = source.getByteStream ();
53 if (!(Clazz.instanceOf (bs, java.io.BufferedInputStream))) bs = new java.io.BufferedInputStream (bs);
54 data[0] = JU.Rdr.fixUTF (JU.Rdr.getStreamAsBytes (bs, null));
56 if (!(Clazz.instanceOf (rdr, java.io.BufferedReader))) rdr = new java.io.BufferedReader (rdr);
57 JU.Rdr.readAllAsString (rdr, -1, false, data, 0);
59 this.parseDocument (this.parseXML (data[0]));
61 if (Clazz.exceptionOf (e, Exception)) {
67 }, "org.xml.sax.InputSource");
68 Clazz.defineMethod (c$, "parse",
71 this.parseDocument (this.parseXML (swingjs.JSToolkit.getFileContents (fileName)));
73 if (Clazz.exceptionOf (e, Exception)) {
80 Clazz.defineMethod (c$, "parseXMLString",
83 this.parseDocument (this.parseXML (data));
85 if (Clazz.exceptionOf (e, Exception)) {
92 Clazz.defineMethod (c$, "parseXML",
94 return swingjs.JSToolkit.getJQuery ().parseXML (this.removeProcessing (data));
96 Clazz.defineMethod (c$, "removeProcessing",
98 if (data.indexOf ("<?") >= 0) {
99 this.getUniqueSequence (data);
100 data = JU.PT.rep (JU.PT.rep (data, "<?", "<![CDATA[" + this.uniqueSeq), "?>", "]]>");
101 if (data.startsWith ("<!")) {
102 data = "<pre>" + data + "</pre>";
106 Clazz.defineMethod (c$, "getUniqueSequence",
109 while (data.indexOf ("<![CDATA[" + s) >= 0) s += "~";
113 Clazz.defineMethod (c$, "error",
115 var ee = new org.xml.sax.SAXParseException ("Invalid Document", null);
116 if (this.errorHandler == null) throw (ee);
117 else this.errorHandler.fatalError (ee);
119 Clazz.defineMethod (c$, "parseDocument",
121 if (this.docHandler == null && this.contentHandler == null) this.contentHandler = new swingjs.JSSAXContentHandler ();
122 this.ver2 = (this.contentHandler != null);
123 if (this.ver2) this.contentHandler.startDocument ();
124 else this.docHandler.startDocument ();
125 this.walkDOMTree (swingjs.api.DOMNode.getAttr (doc, "firstChild"), this.havePre);
126 if (this.ver2) this.contentHandler.endDocument ();
127 else this.docHandler.endDocument ();
128 }, "swingjs.api.DOMNode");
129 Clazz.defineMethod (c$, "walkDOMTree",
130 function (node, skipTag) {
131 var localName = (swingjs.api.DOMNode.getAttr (node, "localName"));
132 var qName = swingjs.api.DOMNode.getAttr (node, "nodeName");
133 var uri = swingjs.api.DOMNode.getAttr (node, "namespaceURI");
134 if (localName == null) {
135 var isText = "#text".equals (qName);
136 if (isText || "#cdata-section".equals (qName)) {
137 var data = swingjs.api.DOMNode.getAttr (node, "textContent");
138 if (isText || this.uniqueSeq == null || !data.startsWith (this.uniqueSeq)) {
139 var len = data.length;
140 var ch = this.tempChars;
141 if (len > ch.length) ch = this.tempChars = JU.AU.ensureLength (ch, len * 2);
142 for (var i = len; --i >= 0; ) ch[i] = data.charAt (i);
144 if (this.ver2) this.contentHandler.characters (ch, 0, len);
145 else this.docHandler.characters (ch, 0, len);
147 }data = data.substring (this.uniqueSeq.length);
148 var target = data + " ";
149 target = target.substring (0, target.indexOf (" "));
150 data = data.substring (target.length).trim ();
151 if (this.ver2) this.contentHandler.processingInstruction (target, data);
152 else this.docHandler.processingInstruction (target, data);
153 }} else if (!skipTag) {
154 var atts = new swingjs.JSSAXAttributes (node);
155 if (this.ver2) this.contentHandler.startElement (uri, localName, qName, atts);
156 else this.docHandler.startElement (localName, atts);
157 }node = swingjs.api.DOMNode.getAttr (node, "firstChild");
158 while (node != null) {
159 this.walkDOMTree (node, false);
160 node = swingjs.api.DOMNode.getAttr (node, "nextSibling");
162 if (localName == null || skipTag) return;
163 if (this.ver2) this.contentHandler.endElement (uri, localName, qName);
164 else this.docHandler.endElement (localName);
165 }, "swingjs.api.DOMNode,~B");
166 Clazz.overrideMethod (c$, "getFeature",
168 return (this.getProperty ("\1" + name) != null);
170 Clazz.overrideMethod (c$, "setFeature",
171 function (name, value) {
172 this.setProperty ("\1" + name, value ? Boolean.TRUE : null);
174 Clazz.overrideMethod (c$, "getProperty",
176 return (this.props == null ? null : this.props.get (name));
178 Clazz.overrideMethod (c$, "setProperty",
179 function (name, value) {
181 if (this.props != null) this.props.remove (name);
183 }if (this.props == null) this.props = new java.util.Hashtable ();
184 this.props.put (name, value);
186 Clazz.overrideMethod (c$, "getEntityResolver",
188 return this.resolver;
190 Clazz.overrideMethod (c$, "getDTDHandler",
192 return this.dtdHandler;
194 Clazz.overrideMethod (c$, "setContentHandler",
196 this.contentHandler = handler;
197 }, "org.xml.sax.ContentHandler");
198 Clazz.overrideMethod (c$, "getContentHandler",
200 return this.contentHandler;
202 Clazz.overrideMethod (c$, "getErrorHandler",
204 return this.errorHandler;