cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 29715] New: - JavaSelectionListBuilder
Date Mon, 21 Jun 2004 14:25:43 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=29715>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=29715

JavaSelectionListBuilder

           Summary: JavaSelectionListBuilder
           Product: Cocoon 2
           Version: Current CVS 2.1
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: CocoonForms
        AssignedTo: dev@cocoon.apache.org
        ReportedBy: nsantos@electroplus.pt


Here goes a small contribution to the cforms block.

It's a selection list builder which returns a custom class selection
list. I use it to build from my DAO methods!

It's syntax its something like :

<fd:selection-list type="java" class="x.p.t.o.MyJavaSelectionList"
nullable="false"/>
-------------------------[ Start of AbstractJavaSelectionList ]----------------
/*
 * Copyright 1999-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.cocoon.forms.datatype;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
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.xml.AttributesImpl;
import org.apache.cocoon.xml.XMLUtils;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

/**
 * Abstract implementation of a JavaSelectionList
 *
 *  
 */
public abstract class AbstractJavaSelectionList implements JavaSelectionList,
Serviceable {     
        private HashMap attributes;

        /* (non-Javadoc)
         * @see
org.apache.cocoon.forms.datatype.JavaSelectionList#getAttribute(java.lang.String)
         */
        public String getAttribute(String name) {
                if( this.attributes==null){
                        return null;
                }
                return (String) this.attributes.get(name);
        }
        /* (non-Javadoc)
         * @see
org.apache.cocoon.forms.datatype.JavaSelectionList#removeAttribute(java.lang.String)
         */
        public void removeAttribute(String name) {
                if( this.attributes!=null){
                        this.attributes.remove(name);
                }

        }
        /* (non-Javadoc)
         * @see
org.apache.cocoon.forms.datatype.JavaSelectionList#setAttribute(java.lang.String,
java.lang.String)
         */
        public void setAttribute(String name, String value) {
                if( this.attributes==null){
                        this.attributes=new HashMap();
                }
                this.attributes.put(name, value);

        }
        protected Datatype datatype;
        private List items = new ArrayList();
        private boolean nullable = false;
        protected ServiceManager manager = null;
        private boolean rebuilding = true;

        /* (non-Javadoc)
         * @see
org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
         */
        public void service(ServiceManager manager) throws ServiceException {
                this.manager=manager;
        }
        
        
        
        /* (non-Javadoc)
         * @see org.apache.cocoon.forms.datatype.JavaSelectionList#isNullable()
         */
        public boolean isNullable() {           
                return this.nullable;
        }
        /* (non-Javadoc)
         * @see
org.apache.cocoon.forms.datatype.JavaSelectionList#setDatatype(org.apache.cocoon.forms.datatype.Datatype)
         */
        public void setDatatype(Datatype datatype) {
                this.datatype=datatype;
        }
        /* (non-Javadoc)
         * @see
org.apache.cocoon.forms.datatype.JavaSelectionList#setNullable(boolean)
         */
        public void setNullable(boolean nullable) {
                this.nullable=nullable;
        }       
        /*
         * (non-Javadoc)
         * 
         * @see org.apache.cocoon.forms.datatype.SelectionList#getDatatype()
         */
        public Datatype getDatatype() {
                return this.datatype;
        }

        public void rebuild() {
                this.rebuilding = true;
        }
        /*public void prebuild() {
                try {
                        this.items.clear();
                        this.build();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                this.rebuilding = false;
        }*/
        public void generateSaxFragment(ContentHandler contentHandler, Locale
locale)
                        throws SAXException {
                if (this.rebuilding)
                        try {
                                this.items.clear();
                                this.rebuilding = this.build();
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                Convertor.FormatCache formatCache = new DefaultFormatCache();
                contentHandler.startElement(Constants.INSTANCE_NS,
SELECTION_LIST_EL,
                                Constants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL,
                                XMLUtils.EMPTY_ATTRIBUTES);
                if (nullable) {
                        AttributesImpl voidAttrs = new AttributesImpl();
                        voidAttrs.addCDATAAttribute("value", "");
                        contentHandler.startElement(Constants.INSTANCE_NS, ITEM_EL,
                                        Constants.INSTANCE_PREFIX_COLON +
ITEM_EL, voidAttrs);
                        contentHandler.endElement(Constants.INSTANCE_NS, ITEM_EL,
                                        Constants.INSTANCE_PREFIX_COLON + ITEM_EL);
                }
                Iterator itemIt = items.iterator();
                while (itemIt.hasNext()) {
                        SelectionListItem item = (SelectionListItem) itemIt.next();
                        item.generateSaxFragment(contentHandler, locale,
formatCache);
                }
                contentHandler.endElement(Constants.INSTANCE_NS, SELECTION_LIST_EL,
                                Constants.INSTANCE_PREFIX_COLON +
SELECTION_LIST_EL);
                this.rebuilding = true;
        }

        /**
         *  
         */
        protected abstract boolean build() throws Exception;
        /**
         * Adds a new item to this selection list.
         * 
         * @param value
         *            a value of the correct type (i.e. the type with which this
         *            selectionlist is associated)
         * @param label
         *            a SAX-fragment such as a
         *            {@link org.apache.cocoon.xml.SaxBuffer}, can be null
         */
        protected void addItem(Object value, String label) {
                items.add(new SelectionListItem(value, label));
        }

        protected List getItems() {
                return items;
        }
        public Object getFirst() {
                if (items.isEmpty())
                        return null;
                SelectionListItem item = (SelectionListItem) items.get(0);
                return item.value;
        }

        private final class SelectionListItem {
                private final Object value;
                private final String label;

                public SelectionListItem(Object value, String label) {
                        this.value = value;
                        this.label = label;
                }

                public Object getValue() {
                        return value;
                }

                public void generateSaxFragment(ContentHandler contentHandler,
                                Locale locale, Convertor.FormatCache formatCache)
                                throws SAXException {
                        AttributesImpl itemAttrs = new AttributesImpl();
                        String stringValue;
                        if (this.value == null) {
                                stringValue = "";
                        } else {
                                stringValue =
datatype.getConvertor().convertToString(value,
                                                locale, formatCache);
                        }
                        itemAttrs.addCDATAAttribute("value", stringValue);
                        contentHandler.startElement(Constants.INSTANCE_NS, ITEM_EL,
                                        Constants.INSTANCE_PREFIX_COLON +
ITEM_EL, itemAttrs);
                        contentHandler.startElement(Constants.INSTANCE_NS, LABEL_EL,
                                        Constants.INSTANCE_PREFIX_COLON + LABEL_EL,
                                        XMLUtils.EMPTY_ATTRIBUTES);
                        if (label == null) {
                                contentHandler.characters(stringValue.toCharArray(),
0,
                                                stringValue.length());
                        } else {
                                contentHandler.characters(label.toCharArray(),
0, label
                                                .length());
                        }
                        contentHandler.endElement(Constants.INSTANCE_NS, LABEL_EL,
                                        Constants.INSTANCE_PREFIX_COLON + LABEL_EL);
                        contentHandler.endElement(Constants.INSTANCE_NS, ITEM_EL,
                                        Constants.INSTANCE_PREFIX_COLON + ITEM_EL);
                }
        }

}

---------------------------[ End of AbstractJavaSelectionList ]----------------
---------------------------[ Start of JavaSelectionList ]----------------------

/*
 * Copyright 1999-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.cocoon.forms.datatype;

/**
 * A selection list that takes its values from the custom java class itself.
 *
 *  
 */
public interface JavaSelectionList extends SelectionList{
        
        void setDatatype(Datatype datatype);
        
        boolean isNullable();
        
        void setNullable(boolean nullable);
        
        String getAttribute(String name);
        
        void setAttribute(String name, String value);
        
        void removeAttribute(String name);
}
---------------------------[ End of JavaSelectionList ]------------------------
---------------------[ Start of JavaSelectionListBuilder ]---------------------
/*
 * Copyright 1999-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.cocoon.forms.datatype;

import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.cocoon.components.LifecycleHelper;
import org.apache.cocoon.forms.util.DomHelper;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 * Builds {@link SelectionList}s from a JavaSelectionList class
 *
 *  
 */
public class JavaSelectionListBuilder extends AbstractLogEnabled
                implements
                        SelectionListBuilder,
                        Serviceable,
                        Contextualizable {

        private ServiceManager manager;
        private Context context;

        /*
         * (non-Javadoc)
         * 
         * @see
org.apache.cocoon.forms.datatype.SelectionListBuilder#build(org.w3c.dom.Element,
         *      org.apache.cocoon.forms.datatype.Datatype)
         */
        public SelectionList build(Element selectionListElement, Datatype datatype)
                        throws Exception {
                String className = DomHelper
                                .getAttribute(selectionListElement, "class");
                boolean nullable = DomHelper.getAttributeAsBoolean(
                                selectionListElement, "nullable", true);
                
                try {
                        Class clasz = Class.forName(className);
                        if (JavaSelectionList.class.isAssignableFrom(clasz)) {
                                JavaSelectionList list = (JavaSelectionList)
clasz.newInstance();
                                LifecycleHelper.setupComponent(list,getLogger(),this.context,
this.manager,null, null, true);
                                list.setDatatype(datatype);
                                list.setNullable(nullable);
                                NamedNodeMap attrs =
selectionListElement.getAttributes();
                                int size=attrs.getLength();
                                for( int i=0; i< size; i++){
                                        Node attr = attrs.item(i);
                                        String name=attr.getNodeName();
                                        //if( !"class".equals(name) &&
!"nullable".equals(name)){
                                                list.setAttribute(name,
attr.getNodeValue());
                                        //}
                                }
                                return list;
                        }
                        return new StaticSelectionList(datatype);
                } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                        throw e;
                } catch (InstantiationException e) {
                        e.printStackTrace();
                        throw e;
                } catch (IllegalAccessException e) {
                        e.printStackTrace();
                        throw e;
                }
        }

        /*
         * (non-Javadoc)
         * 
         * @see
org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
         */
        public void service(ServiceManager manager) throws ServiceException {
                this.manager = manager;

        }

        /*
         * (non-Javadoc)
         * 
         * @see
org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
         */
        public void contextualize(Context context) throws ContextException {
                this.context = context;

        }

}
-----------------------[ End of JavaSelectionListBuilder ]---------------------

Mime
View raw message