]> CRI, Mines Paris - PSL - PlinnDocument.git/blobdiff - Products/PlinnDocument/skins/xml_io.js
eggification
[PlinnDocument.git] / Products / PlinnDocument / skins / xml_io.js
diff --git a/Products/PlinnDocument/skins/xml_io.js b/Products/PlinnDocument/skins/xml_io.js
new file mode 100644 (file)
index 0000000..bfd7cf0
--- /dev/null
@@ -0,0 +1,132 @@
+// (c) BenoĆ®t PIN 2006-2007
+// http://plinn.org
+// Licence GPL
+
+function XMLExport() {
+       this.domDoc = Sarissa.getDomDocument('http://plinn.org/namespaces/plinn_document/1.0', 'plinn');
+       this.rootNode = this.domDoc.documentElement;
+}
+
+XMLExport.prototype.getXML = function() {
+       this.exportDocument();
+       var s = new XMLSerializer();
+       XML_OUTPUT.value = s.serializeToString(this.domDoc);
+};
+
+
+XMLExport.prototype.exportDocument = function() {
+       this.exportRectangles(LAYER_MANAGER.space, this.rootNode);
+};
+
+XMLExport.prototype.exportRectangles = function(baseObj, baseNode) {
+       var doc = this.domDoc;
+       var childs = baseObj.childNodes;
+       
+       for(var i = 0 ; i < childs.length ; i++) {
+               rectObj = childs[i].rectangle;
+               if (!rectObj)
+                       continue;
+               
+               // rectangle
+               var rectEl = doc.createElement("rectangle");
+               rectEl.setAttribute("width", rectObj.width);
+               rectEl.setAttribute("height", rectObj.height);
+               rectEl.setAttribute("elementKey", rectObj.elementKey);
+               rectEl.setAttribute("ddOptions", rectObj.ddOptions);
+               rectEl.setAttribute("ratio", rectObj.ratio);
+               rectEl.setAttribute("visibility", rectObj.style.visibility);
+               
+               // upperLeftCorner
+               var ulc = doc.createElement("upperLeftCorner");
+               var point = doc.createElement("point");
+               point.setAttribute("x", rectObj.upperLeftCorner.x);
+               point.setAttribute("y", rectObj.upperLeftCorner.y);
+               ulc.appendChild(point);
+               rectEl.appendChild(ulc);
+               
+               // raw data
+               var rdata = doc.createElement("rawData");
+               if (rectObj.getRawData) {
+                       var rawEl = doc.createTextNode(rectObj.getRawData());
+                       rdata.appendChild(rawEl);
+               }
+               rectEl.appendChild(rdata);
+               
+               baseNode.appendChild(rectEl);
+               
+               this.exportRectangles(rectObj.node, rectEl);
+       }
+};
+
+function XMLImport(url, root_container) {      
+       this.root_container = root_container;
+       var thisImporter = this;
+       var req = new XMLHttpRequest();
+       
+       req.onreadystatechange = function() {
+               if(req.readyState == 4)
+                       thisImporter.constructDocument(req);
+       }
+       req.open("GET", url, true);
+       req.send(null);
+}
+
+XMLImport.prototype.constructDocument = function(req) {
+       var rootNode = req.responseXML.documentElement;
+       var layerElements = rootNode.childNodes;
+       initLayerManager(this.root_container, true);
+       var layerElement;
+       for (var i = 0 ; i < layerElements.length ; i++) {
+               layerElement = layerElements[i];
+               if (i==0) { // initialize LAYER_MANAGER from first layer data
+                       LAYER_MANAGER.defaultLayerWidth = parseInt(layerElement.getAttribute("width"));
+                       LAYER_MANAGER.defaultLayerHeight = parseInt(layerElement.getAttribute("height"));
+                       LAYER_MANAGER.addLayer("relative");
+               }
+               else
+                       LAYER_MANAGER.addLayer();
+               
+               // common part
+               if (layerElement.getAttribute("visibility") == "hidden")
+                       LAYER_MANAGER.toggleLayerVisibility();
+
+               this.constructRectangles(CURRENT_LAYER, layerElement)
+       }
+};
+
+XMLImport.prototype.constructRectangles = function(baseObj, baseNode) {
+       var rectangleElements = baseNode.childNodes;
+       var rectE, rect, ulcE, ulc, rawDataE, rawData, putFunc;
+       
+       for (var i = 0 ; i < rectangleElements.length ; i ++) {
+               rectE = rectangleElements[i];
+               if (rectE.nodeName != "rectangle")
+                       continue;
+               ulcE = rectE.childNodes[0].childNodes[0];
+               rawDataE = rectE.childNodes[1]
+               
+               ulc = new Point( parseInt(ulcE.getAttribute("x")), parseInt(ulcE.getAttribute("y")) )
+               rect = new Rectangle(ulc,
+                                                        parseInt(rectE.getAttribute("width")),
+                                                        parseInt(rectE.getAttribute("height")), 
+                                                        rectE.getAttribute("elementKey"), 
+                                                        parseInt(rectE.getAttribute("ddOptions")),
+                                                        parseFloat(rectE.getAttribute("ratio")));
+
+               putFunc = ELEMENTS_POOL[rectE.getAttribute("elementKey")]["putData"]
+               if (putFunc)
+                       putFunc.apply(rect, [rawDataE.childNodes[0].nodeValue]);
+
+               rect.draw(baseObj);
+       }
+};
+
+/* utils */
+function _plinnDocumentBeforeSubmit() {
+       with (GLOBAL_DD_CONTROLER.ddEventCaptureElmt) {
+               onmousedown=null;
+               onmousemouse=null;
+               onmouseup=null;
+       }
+       new XMLExport().getXML();
+}
\ No newline at end of file