JAL-1807 Bob
[jalviewjs.git] / site / j2s / swingjs / JSSAXParser.js
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 () {
4 this.resolver = null;
5 this.dtdHandler = null;
6 this.docHandler = null;
7 this.contentHandler = null;
8 this.errorHandler = null;
9 this.havePre = false;
10 this.uniqueSeq = null;
11 this.ver2 = false;
12 this.tempChars = null;
13 this.props = 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');
18 });
19 Clazz.overrideMethod (c$, "setLocale", 
20 function (locale) {
21 }, "java.util.Locale");
22 Clazz.overrideMethod (c$, "setEntityResolver", 
23 function (resolver) {
24 this.resolver = resolver;
25 }, "org.xml.sax.EntityResolver");
26 Clazz.overrideMethod (c$, "setDTDHandler", 
27 function (handler) {
28 this.dtdHandler = handler;
29 }, "org.xml.sax.DTDHandler");
30 Clazz.overrideMethod (c$, "setDocumentHandler", 
31 function (handler) {
32 this.docHandler = handler;
33 }, "org.xml.sax.DocumentHandler");
34 Clazz.overrideMethod (c$, "setErrorHandler", 
35 function (handler) {
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", 
44 function (source) {
45 this.parseSource (source);
46 }, "org.xml.sax.InputSource");
47 Clazz.defineMethod (c$, "parseSource", 
48  function (source) {
49 var rdr = source.getCharacterStream ();
50 var data =  new Array (1);
51 if (rdr == null) {
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));
55 } else {
56 if (!(Clazz.instanceOf (rdr, java.io.BufferedReader))) rdr =  new java.io.BufferedReader (rdr);
57 JU.Rdr.readAllAsString (rdr, -1, false, data, 0);
58 }try {
59 this.parseDocument (this.parseXML (data[0]));
60 } catch (e) {
61 if (Clazz.exceptionOf (e, Exception)) {
62 this.error (e);
63 } else {
64 throw e;
65 }
66 }
67 }, "org.xml.sax.InputSource");
68 Clazz.defineMethod (c$, "parse", 
69 function (fileName) {
70 try {
71 this.parseDocument (this.parseXML (swingjs.JSToolkit.getFileContents (fileName)));
72 } catch (e) {
73 if (Clazz.exceptionOf (e, Exception)) {
74 this.error (e);
75 } else {
76 throw e;
77 }
78 }
79 }, "~S");
80 Clazz.defineMethod (c$, "parseXMLString", 
81 function (data) {
82 try {
83 this.parseDocument (this.parseXML (data));
84 } catch (e) {
85 if (Clazz.exceptionOf (e, Exception)) {
86 this.error (e);
87 } else {
88 throw e;
89 }
90 }
91 }, "~S");
92 Clazz.defineMethod (c$, "parseXML", 
93  function (data) {
94 return swingjs.JSToolkit.getJQuery ().parseXML (this.removeProcessing (data));
95 }, "~S");
96 Clazz.defineMethod (c$, "removeProcessing", 
97  function (data) {
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>";
103 this.havePre = true;
104 }}return data;
105 }, "~S");
106 Clazz.defineMethod (c$, "getUniqueSequence", 
107  function (data) {
108 var s = "~";
109 while (data.indexOf ("<![CDATA[" + s) >= 0) s += "~";
110
111 this.uniqueSeq = s;
112 }, "~S");
113 Clazz.defineMethod (c$, "error", 
114  function (e) {
115 var ee =  new org.xml.sax.SAXParseException ("Invalid Document", null);
116 if (this.errorHandler == null) throw (ee);
117  else this.errorHandler.fatalError (ee);
118 }, "Exception");
119 Clazz.defineMethod (c$, "parseDocument", 
120  function (doc) {
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);
143
144 if (this.ver2) this.contentHandler.characters (ch, 0, len);
145  else this.docHandler.characters (ch, 0, len);
146 return;
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");
161 }
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", 
167 function (name) {
168 return (this.getProperty ("\1" + name) != null);
169 }, "~S");
170 Clazz.overrideMethod (c$, "setFeature", 
171 function (name, value) {
172 this.setProperty ("\1" + name, value ? Boolean.TRUE : null);
173 }, "~S,~B");
174 Clazz.overrideMethod (c$, "getProperty", 
175 function (name) {
176 return (this.props == null ? null : this.props.get (name));
177 }, "~S");
178 Clazz.overrideMethod (c$, "setProperty", 
179 function (name, value) {
180 if (value == null) {
181 if (this.props != null) this.props.remove (name);
182 return;
183 }if (this.props == null) this.props =  new java.util.Hashtable ();
184 this.props.put (name, value);
185 }, "~S,~O");
186 Clazz.overrideMethod (c$, "getEntityResolver", 
187 function () {
188 return this.resolver;
189 });
190 Clazz.overrideMethod (c$, "getDTDHandler", 
191 function () {
192 return this.dtdHandler;
193 });
194 Clazz.overrideMethod (c$, "setContentHandler", 
195 function (handler) {
196 this.contentHandler = handler;
197 }, "org.xml.sax.ContentHandler");
198 Clazz.overrideMethod (c$, "getContentHandler", 
199 function () {
200 return this.contentHandler;
201 });
202 Clazz.overrideMethod (c$, "getErrorHandler", 
203 function () {
204 return this.errorHandler;
205 });
206 });