home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / bindings.js < prev    next >
Encoding:
Text File  |  2004-07-12  |  9.9 KB  |  283 lines

  1. /*
  2.  * Copyright 1999-2004 The Apache Software Foundation.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. cocoon.load("resource://org/apache/cocoon/forms/flow/javascript/Form.js");
  17.  
  18. /** 
  19.  * Disclaimer: 
  20.  *   To make this flowscript quite generic we've applied some typical javascript 
  21.  *   hacks here and there. Don't let them overwhelm you, the purpose of this tutorial
  22.  *   is _not_ to get into the depths of Cocoon Forms and flowscript. Instead you should 
  23.  *   focus on the effects of the applied definitions in the binding files:
  24.  *   see ../forms/bindings/*-bind.xml.  And how to shape your backend-models:
  25.  *    - for JS see here: createJSBeanfor*() functions
  26.  *    - for Java see the org.apache.cocoon.forms.samples.bindings.* classes
  27.  *    - for XML see ../forms/bindings/*-data.xml.
  28.  *   
  29.  * In the same area of genericity we are not using the FormsTemplateTransformer 
  30.  *   since that would require to provide an extra template file to every sample.
  31.  *
  32.  * To add more binding-tutorial-samples follow these steps:
  33.  *  - decide on a sample-code (e.g. '01value')
  34.  *  - provide a Cocoon Forms formDefinition ../forms/bindings/{sampleCode}-def.xml
  35.  *  - provide a Cocoon Forms formBinding    ../forms/bindings/{sampleCode}-bind.xml
  36.  *  - provide the 3 types of backend-models:
  37.  *    - for JS (javascript) you add here a function createJSBeanFor{sampleCode}()
  38.  *      and let it return the js object to bind to
  39.  *    - for Java you create an actual Java class that gets instantiated and returned
  40.  *      by a function createJavaBeanFor{sampleCode}() over here
  41.  *    - for XML you create a simple XML file over at
  42.  *      ../forms/bindings/{sampleCode}-data.xml
  43.  *  - finally you add a link into the ../welcome.xml page to link to your new sample      
  44.  */
  45.  
  46.  
  47.  
  48.  
  49. /**
  50.  *  Generic entry-function for all binding samples.  This uses the 
  51.  *   'sample-code' and the 'backend-type' to effectively select the form 
  52.  *   and specific backend-model to use.
  53.  */
  54. function bindingSample(sampleCode, backendType) {
  55.     // sample-code holds the number-and-name of this binding sample in the tutorial
  56.     if (cocoon.parameters["sample-code"] != undefined) {
  57.         sampleCode = cocoon.parameters["sample-code"];
  58.     }
  59.     
  60.     // backend-type holds one of 'JS', 'Java' or 'XML' to indicate 
  61.     // the type of backend to use.
  62.     if (cocoon.parameters["backend-type"] != undefined) {
  63.         backendType = cocoon.parameters["backend-type"];
  64.     }
  65.  
  66.     // all back-end models are bound to essentially the same form, using the same binding!
  67.     var form = createFormForSample(sampleCode);
  68.  
  69.     // the beack-end model itself however depends on sample and type. 
  70.     var bean = createBeanForSample(backendType, sampleCode);
  71.     
  72.     // loads the backend-bean into the form
  73.     form.load(bean);
  74.  
  75.     // wait for user to submit the form correctly    
  76.     form.showForm("binding.form." + sampleCode);
  77.     
  78.     // saves the form into the backend-bean
  79.     form.save(bean);
  80.     var bizData = new Object();
  81.     //hack the XML content to a string for display
  82.     if (backendType.equals("XML")) {
  83.         bean = {xml: serializeNode(bean)};
  84.     }
  85.     bizData["bean"] = bean;
  86.     bizData["backendType"] = backendType;
  87.     bizData["sampleCode"] = sampleCode;
  88.  
  89.     cocoon.sendPage("binding.done", bizData);
  90. }
  91.  
  92. /** 
  93.  * Uses internal cocoon utility class to convert the DOM Node to it's 
  94.  * String representation .
  95.  */
  96. function serializeNode(node) {
  97.     return Packages.org.apache.cocoon.xml.XMLUtils.serializeNodeToXML(node);
  98. }
  99.  
  100. /** 
  101.  * Creates the form for this sample. And automatically creates the accompanied 
  102.  * binding.
  103.  */
  104. function createFormForSample(sampleCode) {
  105.     var form = new Form("forms/binding/" + sampleCode + "-def.xml");
  106.     form.createBinding("forms/binding/" + sampleCode +"-bind.xml");
  107.     return form;
  108. }
  109.  
  110. /** 
  111.  * Creates the Bean of the desired type for this sample.
  112.  */
  113. function createBeanForSample(backendType, sampleCode) {
  114.     if (backendType.equals("XML")) {
  115.         return createXMLBean(sampleCode);
  116.     } else {
  117.         var factoryFunction = "create" + backendType + "BeanFor" + sampleCode;
  118.         print("Using the bean returned by function " + factoryFunction + "()");
  119.         return this[factoryFunction].apply();
  120.     }
  121. }
  122.  
  123. /** 
  124.  * Finds the sample specific XML file to bind to and parses it into a DOM Document.
  125.  */
  126. function createXMLBean(sampleCode) {
  127.     // note if you want to toss around with the XML model (e.g. sample 02lenient)
  128.     // then you should do that by editing the files: ../forms/binding/*-data.xml
  129.  
  130.     var uri = "forms/binding/" + sampleCode +"-data.xml";
  131.     print("Using the XML data file at " + uri);
  132.  
  133.     var parser = null;
  134.     var source = null;
  135.     var resolver = null;
  136.     try {
  137.         parser = cocoon.getComponent(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
  138.         resolver = cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
  139.         source = resolver.resolveURI(uri);
  140.         var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
  141.         is.setSystemId(source.getURI());
  142.         //Note: we immediately narrow down to the root-element here to avoid
  143.         // needing to wrap js and Java beans in a silly 'root'
  144.         return parser.parseDocument(is).getDocumentElement();
  145.     } finally {
  146.         if (source != null)
  147.             resolver.release(source);
  148.         cocoon.releaseComponent(parser);
  149.         cocoon.releaseComponent(resolver);
  150.     }
  151. }    
  152.  
  153. /**
  154.  * Creates the JS Bean for sample '01value'
  155.  */ 
  156. function createJSBeanFor01value() {
  157.     var bean;
  158.     bean = new Object();
  159.     bean.simple = "Simple";
  160.     bean.readOnly = "Read-Only";
  161.     bean.writeOnly = "Write-Only";
  162.     bean.diffIn = "Diff-in/out";
  163.     // diffOut doesn't need to exist, binding will create it.
  164.     bean.onUpdate = "On Update";
  165.     bean.updateCount = 0;
  166.     bean.bool = true;
  167.     bean.date = "19700605";
  168.     bean.other = "This field is not involved in the form.";
  169.     return bean;
  170. }
  171.  
  172. /**
  173.  * Creates the Java Bean for sample '01value'
  174.  */ 
  175. function createJavaBeanFor01value() {
  176.     return new Packages.org.apache.cocoon.forms.samples.bindings.ValuesBean();
  177. }
  178.  
  179. /**
  180.  * Creates the JS Bean for sample '02lenient'
  181.  */ 
  182. function createJSBeanFor02lenient() {
  183.     var bean = new Object();
  184.     var contexts = ["one","two","three"];      
  185.     for(var i=0; i<contexts.length; i++) {
  186.         bean[contexts[i]] = new Object();
  187.         // using javascript beans seem to survive even non lenient binding
  188.         // so you can do here what you want, the bean itself seems to be leninet?
  189.         //bean[contexts[i]]["breakingField"] = "present";
  190.     }
  191.     return bean;
  192. }
  193.  
  194. /**
  195.  * Creates the Java Bean for sample '02lenient'
  196.  */ 
  197. function createJavaBeanFor02lenient() {
  198.     var bean = new Packages.java.util.HashMap();
  199.     // to see the runtime effect of non-lenient binding
  200.     // remove/replace the 'one' in the following list:
  201.     var contexts = ["one","two","three"];      // only the 'one' context is required by non-lenient binding
  202.         for(var i=0; i<contexts.length; i++) {
  203.         // to see the runtime effect of non-lenient binding
  204.         // swap the following 2 lines from comment to code 
  205.             var subBean = new Packages.org.apache.cocoon.forms.samples.bindings.LenientOKBean("init");
  206. //            var subBean = new Packages.org.apache.cocoon.forms.samples.bindings.LenientNotOKBean("init");
  207.         
  208.             // the NotOkBean does not have a getBreakingField() required by the non-lenient binding
  209.  
  210.             bean.put(contexts[i], subBean);
  211.     }
  212.     return bean;
  213. }
  214.  
  215.  
  216. /**
  217.  * Creates the JS Bean for sample '03aggregate'
  218.  */ 
  219. function createJSBeanFor03aggregate() {
  220.     var bean = new Object();
  221.     bean["match-combined"] = new Object();
  222.     bean["match-combined"].split = new Object();
  223.     bean["match-combined"].split.day = "7";
  224.     bean["match-combined"].split.month = "4";
  225.     bean["match-combined"].split.year = "2004";
  226.  
  227.     bean["match-split"] = new Object();    
  228.     bean["match-split"].combined = "07/04/2004";
  229.     bean["match-split"].split = new Object();
  230.     return bean;
  231. }
  232.  
  233. /**
  234.  * Creates the Java Bean for sample '03aggregate'
  235.  */ 
  236. function createJavaBeanFor03aggregate() {
  237.     var bean = new Packages.java.util.HashMap();
  238.     bean.put("match-combined", new Packages.org.apache.cocoon.forms.samples.bindings.DateWrapper("07","04","2004"));
  239.     bean.put("match-split", new Packages.org.apache.cocoon.forms.samples.bindings.DateWrapper("07","04","2004"));
  240.     return bean;
  241. }
  242.  
  243. /**
  244.  * sample conversion methods used by the javascript binding in '05custom'
  245.  */
  246.  
  247. function doLoadConversion(backendValue, delimiter){
  248.   var presuffix = "" + delimiter + delimiter;
  249.   var result = "";
  250.   if (backendValue.startsWith(presuffix) 
  251.       && backendValue.endsWith(presuffix)
  252.       && backendValue.length() >= 4) {
  253.     result = backendValue.substring(2,backendValue.length() - 2);
  254.   }
  255.   return result;
  256. }
  257.  
  258. function doSaveConversion(formValue, delimiter){
  259.   var result = "" + delimiter + delimiter + formValue + delimiter + delimiter;
  260.   return result;
  261. }
  262.  
  263. /**
  264.  * Creates the JS Bean for sample '05custom'
  265.  */ 
  266. function createJSBeanFor05custom() {
  267.     var bean = {"jswrap-value": "--wrapped value--",
  268.                 "custom-value": "**custom value**",
  269.                 "config-value": "[[config value]]"};
  270.     return bean;
  271. }
  272.  
  273. /**
  274.  * Creates the Java Bean for sample '05custom'
  275.  */ 
  276. function createJavaBeanFor05custom() {
  277.     var bean = new Packages.java.util.HashMap();
  278.     bean.put("jswrap-value", "--wrapped value--");
  279.     bean.put("custom-value", "**custom value**");
  280.     bean.put("config-value", "[[config value]]");
  281.     return bean;
  282. }
  283.