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 / form_model_gui.js < prev    next >
Encoding:
Text File  |  2004-07-12  |  4.8 KB  |  137 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. // CVS $Id: form_model_gui.js,v 1.3 2004/04/12 14:05:09 tim Exp $
  19.  
  20. function form_model_gui(form) {
  21.  
  22.     var locale = determineLocale();
  23.     var model = form.getModel();
  24.     form.locale = locale;
  25.  
  26.     // get the documentURI parameter from the sitemap which
  27.     // contains the location of the file to be edited
  28.     var documentURI = cocoon.parameters["documentURI"];
  29.  
  30.     // parse the document to a DOM-tree
  31.     var document = loadDocument(documentURI);
  32.  
  33.     // bind the document data to the form
  34.     form.load(document);
  35.  
  36.     // show the form
  37.     form.showForm("form_model_gui-display-pipeline");
  38.     print("submitId = " + form.submitId);
  39.     if (form.isValid) {
  40.       print("Form is valid");  
  41.     } else {
  42.       print("Form is not valid");
  43.     }
  44.  
  45.     // bind the form's data back to the document
  46.     form.save(document);
  47.  
  48.     // save the DOM-tree back to an XML file, the makeTargetURI
  49.     // function makes a modified filename so that the
  50.     // original document is not overwritten
  51.     saveDocument(document, makeTargetURI(documentURI));
  52.  
  53.     // also store the form as a request attribute as the XSP isn't flow-aware
  54.     cocoon.request.setAttribute("form_model_gui", form.getWidget());
  55.     cocoon.sendPage("form_model_gui-success-pipeline.xsp");
  56. }
  57.  
  58. function determineLocale() {
  59.     var localeParam = cocoon.request.get("locale");
  60.     if (localeParam != null && localeParam.length > 0) {
  61.         return Packages.org.apache.cocoon.i18n.I18nUtils.parseLocale(localeParam);
  62.     }
  63.     return null;
  64. }
  65.  
  66. /**
  67.  * Translate source path into target path so we keep a clean source XML.
  68.  */
  69. function makeTargetURI(path) {
  70.     var sfx = ".xml";
  71.     var newSfx = "-result.xml";
  72.     var newPath = path;
  73.     if (path.match(/^.*\.xml$/)) {
  74.         newPath = path.substring(0, path.length - ".xml".length);
  75.     }
  76.     return newPath + newSfx;
  77. }
  78.  
  79. function loadDocument(uri) {
  80.     var parser = null;
  81.     var source = null;
  82.     var resolver = null;
  83.     try {
  84.         parser = cocoon.getComponent(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
  85.         resolver = cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
  86.         source = resolver.resolveURI(uri);
  87.         var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
  88.         is.setSystemId(source.getURI());
  89.         return parser.parseDocument(is);
  90.     } finally {
  91.         if (source != null)
  92.             resolver.release(source);
  93.         cocoon.releaseComponent(parser);
  94.         cocoon.releaseComponent(resolver);
  95.     }
  96. }
  97.  
  98. function saveDocument(document, uri) {
  99.     var source = null;
  100.     var resolver = null;
  101.     var outputStream = null;
  102.     try {
  103.         resolver = cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
  104.         source = resolver.resolveURI(uri);
  105.  
  106.         var tf = Packages.javax.xml.transform.TransformerFactory.newInstance();
  107.  
  108.         if (source instanceof Packages.org.apache.excalibur.source.ModifiableSource
  109.             && tf.getFeature(Packages.javax.xml.transform.sax.SAXTransformerFactory.FEATURE)) {
  110.  
  111.             outputStream = source.getOutputStream();
  112.             var transformerHandler = tf.newTransformerHandler();
  113.             var transformer = transformerHandler.getTransformer();
  114.             transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.INDENT, "true");
  115.             transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.METHOD, "xml");
  116.             transformerHandler.setResult(new Packages.javax.xml.transform.stream.StreamResult(outputStream));
  117.  
  118.             var streamer = new Packages.org.apache.cocoon.xml.dom.DOMStreamer(transformerHandler);
  119.             streamer.stream(document);
  120.         } else {
  121.             throw new Packages.org.apache.cocoon.ProcessingException("Cannot write to source " + uri);
  122.         }
  123.     } finally {
  124.         if (source != null)
  125.             resolver.release(source);
  126.         cocoon.releaseComponent(resolver);
  127.         if (outputStream != null) {
  128.             try {
  129.                 outputStream.flush();
  130.                 outputStream.close();
  131.             } catch (error) {
  132.                 cocoon.log.error("Could not flush/close outputstream: " + error);
  133.             }
  134.         }
  135.     }
  136. }
  137.