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 || token.node.hasAttribute('data-pass-thru')) {
223 self._pushChildren(token.node);
227 if(!token.node.hasAttribute('x-node'))
230 var xnode = token.node.getAttribute('x-node');
232 if(xnode === 'wers') {
234 if(self._verseBefore(token.node))
235 self.result += '/\n';
236 self._pushChildren(token.node);
240 if(xnode === 'out-of-flow-text') {
241 self._pushChildren(token.node);
245 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
246 self.result += '/\n';
249 self._serializeElement(token.node);
252 self.result += token.node.nodeValue;
261 * TODO: this doesn't support prefix redefinitions
263 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
264 this.nsMap[nsData.uri] = undefined;
267 HTMLSerializer.prototype._assignNamespace = function(uri) {
270 return ({"prefix": "", "uri": "", "fresh": false});
273 if(this.nsMap[uri] === undefined) {
274 // this prefix hasn't been defined yet in current context
275 var prefix = NAMESPACES[uri];
277 if (prefix === undefined) { // not predefined
278 prefix = "ns" + this.nsCounter;
282 this.nsMap[uri] = prefix;
290 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
293 HTMLSerializer.prototype._join = function(prefix, name) {
295 return prefix + ":" + name;
299 HTMLSerializer.prototype._rjoin = function(prefix, name) {
301 return prefix + ":" + name;
305 HTMLSerializer.prototype._serializeElement = function(node) {
308 var ns = node.getAttribute('x-ns');
310 var newNamespaces = [];
312 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
315 newNamespaces.push(nsData);
322 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
324 /* retrieve attributes */
325 var attributeIDs = [];
326 for (var i = 0; i < node.attributes.length; i++) {
327 var attr = node.attributes.item(i);
329 // check if name starts with "x-attr-name"
330 var m = attr.name.match(XATTR_RE);
332 attributeIDs.push(m[1]);
336 if (getPadding(tagName))
339 self.result += '<' + tagName;
341 $.each(attributeIDs, function() {
342 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
345 newNamespaces.push(nsData);
352 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
353 self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
356 /* print new namespace declarations */
357 $.each(newNamespaces, function() {
358 self.result += " " + self._rjoin("xmlns", this.prefix);
359 self.result += '="' + this.uri + '"';
362 if (node.childNodes.length > 0) {
364 self._pushTagEnd(tagName);
365 self._pushChildren(node);
372 function html2text(params) {
374 var s = new HTMLSerializer();
375 params.success( s.serialize(params.element, params.stripOuter) );
377 params.error("Nie udało się zserializować tekstu:" + e)