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 / binding_example.js < prev    next >
Encoding:
Text File  |  2004-07-12  |  5.2 KB  |  144 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. // The purpose of the form2 example is to edit the contents of an XML file
  19. // or a Java bean through a Cocoon form
  20.  
  21. // the form2 function is not directly called by the sitemap but by
  22. // a generic "forms" function that instantiates the form based on
  23. // parameters passed from the sitemap (see above loaded forms.js file)
  24. function form2xml(form) {
  25.     // get the documentURI parameter from the sitemap which contains the
  26.     // location of the file to be edited
  27.     var documentURI = cocoon.parameters["documentURI"];
  28.  
  29.     // parse the document to a DOM-tree
  30.     var document = loadDocument(documentURI);
  31.  
  32.     // bind the document data to the form
  33.     form.load(document);
  34.  
  35.     // show the form to the user until it is validated successfully
  36.     form.showForm("form2-display-pipeline");
  37.  
  38.     // bind the form's data back to the document
  39.     form.save(document);
  40.  
  41.     // save the DOM-tree back to an XML file, the makeTargetURI
  42.     // function makes a modified filename so that the
  43.     // original document is not overwritten
  44.     saveDocument(document, makeTargetURI(documentURI));
  45.  
  46.     cocoon.sendPage("form2-success-pipeline");
  47. }
  48.  
  49. // bean variant of the binding sample
  50. function form2bean(form) {
  51.     var bean = new Packages.org.apache.cocoon.forms.samples.Form2Bean();
  52.  
  53.     // fill bean with some data to avoid users having to type to much
  54.     bean.setEmail("yourname@yourdomain.com");
  55.     bean.setIpAddress("10.0.0.1");
  56.     bean.setPhoneCountry("32");
  57.     bean.setPhoneZone("2");
  58.     bean.setPhoneNumber("123456");
  59.     bean.setBirthday(new java.util.Date());
  60.     bean.setSex(Packages.org.apache.cocoon.forms.samples.Sex.FEMALE);
  61.     var contact = new Packages.org.apache.cocoon.forms.samples.Contact();
  62.     contact.setId("1");
  63.     contact.setFirstName("Hermann");
  64.     bean.addContact(contact);
  65.     
  66.     form.load(bean);
  67.     form.showForm("form2-display-pipeline");
  68.     form.save(bean);
  69.  
  70.     cocoon.sendPage("form2bean-success-pipeline", { "form2bean": bean });
  71. }
  72.  
  73. /**
  74.  * Translate source path into target path so we keep a clean source XML.
  75.  */
  76. function makeTargetURI(path) {
  77.     var sfx = ".xml";
  78.     var newSfx = "-result.xml";
  79.     var newPath = path;
  80.     if (path.match(/^.*\.xml$/)) {
  81.         newPath = path.substring(0, path.length - ".xml".length);
  82.     }
  83.     return newPath + newSfx;
  84. }
  85.  
  86. function loadDocument(uri) {
  87.     var parser = null;
  88.     var source = null;
  89.     var resolver = null;
  90.     try {
  91.         parser = cocoon.getComponent(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
  92.         resolver = cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
  93.         source = resolver.resolveURI(uri);
  94.         var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
  95.         is.setSystemId(source.getURI());
  96.         return parser.parseDocument(is);
  97.     } finally {
  98.         if (source != null)
  99.             resolver.release(source);
  100.         cocoon.releaseComponent(parser);
  101.         cocoon.releaseComponent(resolver);
  102.     }
  103. }
  104.  
  105. function saveDocument(document, uri) {
  106.     var source = null;
  107.     var resolver = null;
  108.     var outputStream = null;
  109.     try {
  110.         resolver = cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
  111.         source = resolver.resolveURI(uri);
  112.  
  113.         var tf = Packages.javax.xml.transform.TransformerFactory.newInstance();
  114.  
  115.         if (source instanceof Packages.org.apache.excalibur.source.ModifiableSource
  116.             && tf.getFeature(Packages.javax.xml.transform.sax.SAXTransformerFactory.FEATURE)) {
  117.  
  118.             outputStream = source.getOutputStream();
  119.             var transformerHandler = tf.newTransformerHandler();
  120.             var transformer = transformerHandler.getTransformer();
  121.             transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.INDENT, "true");
  122.             transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.METHOD, "xml");
  123.             transformerHandler.setResult(new Packages.javax.xml.transform.stream.StreamResult(outputStream));
  124.  
  125.             var streamer = new Packages.org.apache.cocoon.xml.dom.DOMStreamer(transformerHandler);
  126.             streamer.stream(document);
  127.         } else {
  128.             throw new Packages.org.apache.cocoon.ProcessingException("Cannot write to source " + uri);
  129.         }
  130.     } finally {
  131.         if (source != null)
  132.             resolver.release(source);
  133.         cocoon.releaseComponent(resolver);
  134.         if (outputStream != null) {
  135.             try {
  136.                 outputStream.flush();
  137.                 outputStream.close();
  138.             } catch (error) {
  139.                 cocoon.log.error("Could not flush/close outputstream: " + error);
  140.             }
  141.         }
  142.     }
  143. }
  144.