cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anto...@apache.org
Subject svn commit: r178778 - /cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DynamicSelectionList.java /cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Field.java /cocoon/branches/BRANCH_2_1_X/status.xml
Date Fri, 27 May 2005 14:05:03 GMT
Author: antonio
Date: Fri May 27 07:05:02 2005
New Revision: 178778

URL: http://svn.apache.org/viewcvs?rev=178778&view=rev
Log:
Cforms block: Improved dynamic selection list performance inside repeaters.

Modified:
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DynamicSelectionList.java
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Field.java
    cocoon/branches/BRANCH_2_1_X/status.xml

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DynamicSelectionList.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DynamicSelectionList.java?rev=178778&r1=178777&r2=178778&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DynamicSelectionList.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DynamicSelectionList.java
Fri May 27 07:05:02 2005
@@ -16,22 +16,28 @@
 package org.apache.cocoon.forms.datatype;
 
 import org.xml.sax.ContentHandler;
+import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 import org.xml.sax.Attributes;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.excalibur.source.SourceResolver;
 import org.apache.excalibur.source.Source;
+import org.apache.excalibur.xml.sax.XMLizable;
 import org.apache.cocoon.ProcessingException;
 import org.apache.cocoon.components.source.SourceUtil;
 import org.apache.cocoon.forms.Constants;
 import org.apache.cocoon.forms.datatype.convertor.Convertor;
 import org.apache.cocoon.forms.datatype.convertor.DefaultFormatCache;
 import org.apache.cocoon.forms.datatype.convertor.ConversionResult;
+import org.apache.cocoon.forms.util.DomHelper;
 import org.apache.cocoon.xml.AttributesImpl;
 import org.apache.cocoon.xml.AbstractXMLPipe;
 import org.apache.cocoon.xml.XMLUtils;
 import org.apache.cocoon.xml.dom.DOMBuilder;
+import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 import java.io.IOException;
 import java.util.Locale;
@@ -55,6 +61,7 @@
         this.datatype = datatype;
         this.src = src;
         this.serviceManager = serviceManager;
+        this.cachedItemList = null;
     }
 
     public Datatype getDatatype() {
@@ -73,21 +80,112 @@
     }
 
     public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws
SAXException {
-        SourceResolver sourceResolver = null;
-        Source source = null;
+        if (cachedItemList != null) {
+            cachedItemList.generateSaxFragment(contentHandler, locale);
+        } else {
+            SourceResolver sourceResolver = null;
+            Source source = null;
+            try {
+                sourceResolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
+                source = sourceResolver.resolveURI(src);
+                generateSaxFragment(contentHandler, locale, source);
+            } catch (SAXException e) {
+                throw e;
+            } catch (Exception e) {
+                throw new SAXException("Error while generating selection list: " + e.getMessage(),
e);
+            } finally {
+                if (sourceResolver != null) {
+                    if (source != null)
+                        try { sourceResolver.release(source); } catch (Exception e) {}
+                    serviceManager.release(sourceResolver);
+                }
+            }
+        }
+    }
+    
+    private SelectionList cachedItemList;
+    
+    /**
+     * Prepare cache for the DynamicSelectionList
+     * THIS IS FOR INTERNAL USE ONLY. Subject to change without notice.  
+     */
+    public void prepareCache() throws SAXException {
         try {
-            sourceResolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
-            source = sourceResolver.resolveURI(src);
-            generateSaxFragment(contentHandler, locale, source);
-        } catch (SAXException e) {
-            throw e;
+            Element selectionListElement = readSelectionList();
+            cachedItemList = buildStaticList(selectionListElement);
         } catch (Exception e) {
             throw new SAXException("Error while generating selection list: " + e.getMessage(),
e);
+        }
+    }
+
+    private SelectionList buildStaticList(Element selectionListElement) throws Exception
{
+        StaticSelectionList selectionList = new StaticSelectionList(datatype);
+        Convertor convertor = null;
+        Convertor.FormatCache formatCache = new DefaultFormatCache();
+
+        NodeList children = selectionListElement.getChildNodes();
+        for (int i = 0; children.item(i) != null; i++) {
+            Node node = children.item(i);
+            if (convertor == null && node instanceof Element && Constants.DEFINITION_NS.equals(node.getNamespaceURI())
&& "convertor".equals(node.getLocalName())) {
+                Element convertorConfigElement = (Element)node;
+                try {
+                    convertor = datatype.getBuilder().buildConvertor(convertorConfigElement);
+                } catch (Exception e) {
+                    throw new SAXException("Error building convertor from convertor configuration
embedded in selection list XML.", e);
+                }
+            } else if (node instanceof Element && Constants.DEFINITION_NS.equals(node.getNamespaceURI())
&& "item".equals(node.getLocalName())) {
+                if (convertor == null) {
+                    convertor = datatype.getConvertor();
+                }
+                Element element = (Element)node;
+                String stringValue = element.getAttribute("value");
+                Object value;
+                if ("".equals(stringValue)) {
+                    // Empty value translates into the null object
+                    value = null;
+                } else {
+                    ConversionResult conversionResult = convertor.convertFromString(stringValue,
Locale.US, formatCache);
+                    if (!conversionResult.isSuccessful()) {
+                        throw new Exception("Could not convert the value \"" + stringValue
+
+                                            "\" to the type " + datatype.getDescriptiveName()
+
+                                            ", defined at " + DomHelper.getLocation(element));
+                    }
+                    value = conversionResult.getResult();
+                }
+
+                XMLizable label = null;
+                Element labelEl = DomHelper.getChildElement(element, Constants.DEFINITION_NS,
"label");
+                if (labelEl != null) {
+                    label = DomHelper.compileElementContent(labelEl);
+                }
+                selectionList.addItem(value, label);
+            }
+        }
+        return selectionList;
+    }
+
+    private Element readSelectionList() throws Exception {
+        SourceResolver resolver = null;
+        Source source = null;
+        try {
+            resolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
+            source = resolver.resolveURI(src);
+            InputSource inputSource = new InputSource(source.getInputStream());
+            inputSource.setSystemId(source.getURI());
+            Document document = DomHelper.parse(inputSource, this.serviceManager);
+            Element selectionListElement = document.getDocumentElement();
+            if (!Constants.DEFINITION_NS.equals(selectionListElement.getNamespaceURI()) ||
+                    !"selection-list".equals(selectionListElement.getLocalName())) {
+                throw new Exception("Expected a fd:selection-list element at " +
+                                    DomHelper.getLocation(selectionListElement));
+            }
+            return selectionListElement;
         } finally {
-            if (sourceResolver != null) {
-                if (source != null)
-                    try { sourceResolver.release(source); } catch (Exception e) {}
-                serviceManager.release(sourceResolver);
+            if (resolver != null) {
+                if (source != null) {
+                    resolver.release(source);
+                }
+                serviceManager.release(resolver);
             }
         }
     }

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Field.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Field.java?rev=178778&r1=178777&r2=178778&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Field.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Field.java
Fri May 27 07:05:02 2005
@@ -18,6 +18,7 @@
 import org.apache.cocoon.forms.Constants;
 import org.apache.cocoon.forms.FormContext;
 import org.apache.cocoon.forms.datatype.Datatype;
+import org.apache.cocoon.forms.datatype.DynamicSelectionList;
 import org.apache.cocoon.forms.datatype.SelectionList;
 import org.apache.cocoon.forms.datatype.convertor.ConversionResult;
 import org.apache.cocoon.forms.event.*;
@@ -434,6 +435,13 @@
 
         // generate selection list, if any
         if (selectionList != null) {
+            // If we are inside a repeater, Notify DynamicSelectionList to cache the item
list
+            if (selectionList instanceof DynamicSelectionList && this.getParent()
instanceof Repeater.RepeaterRow) {
+                Repeater r = (Repeater)this.getParent().getParent();
+                if (r.indexOf((Repeater.RepeaterRow)this.getParent()) == 0) {
+                    ((DynamicSelectionList)selectionList).prepareCache();
+                }
+            }
             selectionList.generateSaxFragment(contentHandler, locale);
         }
 

Modified: cocoon/branches/BRANCH_2_1_X/status.xml
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/status.xml?rev=178778&r1=178777&r2=178778&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/status.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/status.xml Fri May 27 07:05:02 2005
@@ -196,6 +196,9 @@
 
   <changes>
   <release version="@version@" date="@date@">
+    <<action dev="AG" type="update">
+      Cforms block: Improved dynamic selection list performance inside repeaters.
+    </action>
     <action dev="AG" type="update">
       Updated ant to 1.6.4, batik to 1.6, db-ojb to 1.0.3.
     </action>



Mime
View raw message