6 function createXSLT(xsl) {
7 var p = new XSLTProcessor();
8 p.importStylesheet(xsl);
12 var xml2htmlStylesheet = null;
14 // Wykonuje block z załadowanymi arkuszami stylów
15 function withStylesheets(code_block, onError)
17 if (!xml2htmlStylesheet) {
18 $.blockUI({message: 'Ładowanie arkuszy stylów...'});
20 url: STATIC_URL + 'xsl/wl2html_client.xsl',
22 success: function(data) {
23 xml2htmlStylesheet = createXSLT(data);
37 function xml2html(options) {
38 withStylesheets(function() {
39 var xml = options.xml.replace(/\/\s+/g, '<br />');
40 var parser = new DOMParser();
41 var serializer = new XMLSerializer();
42 var doc = parser.parseFromString(xml, 'text/xml');
43 var error = $('parsererror', doc);
45 if (error.length == 0) {
46 doc = xml2htmlStylesheet.transformToFragment(doc, document);
47 console.log(doc.firstChild);
49 if(doc.firstChild === null) {
50 options.error("Błąd w przetwarzaniu XML.");
54 error = $('parsererror', doc);
57 if (error.length > 0 && options.error) {
58 options.error(error.text());
60 options.success(doc.firstChild);
62 }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
65 /* USEFULL CONSTANTS */
66 const ELEMENT_NODE = 1;
67 const ATTRIBUTE_NODE = 2;
69 const CDATA_SECTION_NODE = 4;
70 const ENTITY_REFERENCE_NODE = 5;
71 const ENTITY_NODE = 6;
72 const PROCESSING_INSTRUCTION_NODE = 7;
73 const COMMENT_NODE = 8;
74 const DOCUMENT_NODE = 9;
75 const DOCUMENT_TYPE_NODE = 10;
76 const DOCUMENT_FRAGMENT_NODE = 11;
77 const NOTATION_NODE = 12;
78 const XATTR_RE = /^x-attr-name-(.*)$/;
85 // namespaces not listed here will be assigned random names
86 "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
87 "http://purl.org/dc/elements/1.1/": "dc",
88 "http://www.w3.org/XML/1998/namespace": "xml"
92 * PADDING for pretty-printing
95 dramat_wierszowany_l: 4,
96 dramat_wierszowany_lp: 4,
97 dramat_wspolczesny: 4,
105 naglowek_rozdzial: 4,
133 "rdf:Description": 1,
136 function getPadding(name) {
138 if(name.match(/^dc:.*$/))
142 return PADDING[name];
147 function HTMLSerializer() {
153 HTMLSerializer.prototype._prepare = function() {
156 // XML namespace is implicit
157 this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
163 HTMLSerializer.prototype._pushElement = function(element) {
170 HTMLSerializer.prototype._pushChildren = function(element) {
171 for(var i = element.childNodes.length-1; i >= 0; i--)
172 this._pushElement(element.childNodes.item(i));
175 HTMLSerializer.prototype._pushTagEnd = function(tagName) {
182 HTMLSerializer.prototype._verseBefore = function(node) {
183 var prev = node.previousSibling;
185 while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
186 prev = prev.previousSibling;
189 return (prev !== null) && prev.hasAttribute('x-verse');
192 HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
198 self._pushElement(rootElement);
200 self._pushChildren(rootElement);
202 while(self.stack.length > 0) {
203 var token = self.stack.pop();
205 if(token.type === ELEM_END) {
206 self.result += "</" + token.tagName + ">";
207 for(var padding = getPadding(token.tagName); padding > 0; padding--) {
213 if(token.type === NS_END) {
214 self._unassignNamespace(token.namespace);
219 switch(token.node.nodeType) {
221 if(token.node.hasAttribute('x-pass-thru')) {
222 self._pushChildren(token.node);
226 if(!token.node.hasAttribute('x-node'))
229 var xnode = token.node.getAttribute('x-node');
231 if(xnode === 'wers') {
233 if(self._verseBefore(token.node))
234 self.result += '/\n';
235 self._pushChildren(token.node);
239 if(xnode === 'out-of-flow-text') {
240 self._pushChildren(token.node);
244 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
245 self.result += '/\n';
248 self._serializeElement(token.node);
251 self.result += token.node.nodeValue;
260 * TODO: this doesn't support prefix redefinitions
262 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
263 this.nsMap[nsData.uri] = undefined;
266 HTMLSerializer.prototype._assignNamespace = function(uri) {
269 return ({"prefix": "", "uri": "", "fresh": false});
272 if(this.nsMap[uri] === undefined) {
273 // this prefix hasn't been defined yet in current context
274 var prefix = NAMESPACES[uri];
276 if (prefix === undefined) { // not predefined
277 prefix = "ns" + this.nsCounter;
281 this.nsMap[uri] = prefix;
289 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
292 HTMLSerializer.prototype._join = function(prefix, name) {
294 return prefix + ":" + name;
298 HTMLSerializer.prototype._rjoin = function(prefix, name) {
300 return prefix + ":" + name;
304 HTMLSerializer.prototype._serializeElement = function(node) {
307 var ns = node.getAttribute('x-ns');
309 var newNamespaces = [];
311 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
314 newNamespaces.push(nsData);
321 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
323 /* retrieve attributes */
324 var attributeIDs = [];
325 for (var i = 0; i < node.attributes.length; i++) {
326 var attr = node.attributes.item(i);
328 // check if name starts with "x-attr-name"
329 var m = attr.name.match(XATTR_RE);
331 attributeIDs.push(m[1]);
335 if (getPadding(tagName))
338 self.result += '<' + tagName;
340 $.each(attributeIDs, function() {
341 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
344 newNamespaces.push(nsData);
351 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
352 self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
355 /* print new namespace declarations */
356 $.each(newNamespaces, function() {
357 self.result += " " + self._rjoin("xmlns", this.prefix);
358 self.result += '="' + this.uri + '"';
361 if (node.childNodes.length > 0) {
363 self._pushTagEnd(tagName);
364 self._pushChildren(node);
371 function html2text(params) {
373 var s = new HTMLSerializer();
374 params.success( s.serialize(params.element, params.stripOuter) );
376 params.error("Nie udało się zserializować tekstu:" + e)