001    /*
002      Copyright (C) 2003-2004 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.ide;
020    
021    import java.io.IOException;
022    import java.io.Writer;
023    import java.util.Iterator;
024    
025    /**
026     * rtti.acc generation plugin
027     */
028    
029    public class RttiPlugin extends AbstractPlugin {   
030    
031        public void genConfig(Writer output, Project project) 
032            throws IOException
033        {
034            // Generate "extended" types
035            Iterator enums = Projects.types.getExtendedTypes().iterator();
036            while (enums.hasNext()) {
037                ExtendedType extended = (ExtendedType)enums.next();
038                output.write("newVirtualClass "+extended.getName()+" "+
039                             extended.getRealType().getGenerationFullName()+";\n");
040            }
041    
042            super.genConfig(output,project);
043        }
044    
045        public void genFieldConfig(Writer output, Project project, 
046                                   Package pkg, Class cl, Field field) 
047            throws IOException 
048        {
049            if (field.isCalculated()) {
050                Getter getter = field.getGetter();
051                if (getter!=null) {
052                    state.openField(cl,field);
053                    output.write("        declareCalculatedField "+
054                                 getter.getGenerationName()+";\n");
055                } else {
056                    System.err.println("Calculated field "+field.getFullName()+
057                                       " does not have a getter");
058                }
059                Setter setter = field.getSetter();
060                if (setter!=null) {
061                    state.openField(cl,field);
062                    output.write("        setSetter "+
063                                 setter.getGenerationName()+";\n");
064                }         
065            }
066            if (field.getType() instanceof ExtendedType) {
067                state.openField(cl,field);
068                output.write("        setFieldType "+
069                             field.getType().getName()+";\n");
070            }
071        }   
072    
073        public void genRoleConfig(Writer output, Project project, 
074                                  Package pkg, Class cl, RelationRole role)
075            throws IOException 
076        {
077            // Repositories
078            String name = role.getGenerationName();
079            if (role.getEnd() instanceof Repository) {
080                Repository rep = (Repository)role.getEnd();
081                RelationRole itemsRole = (RelationRole)role.oppositeRole();
082                if (itemsRole!=null) {
083                    state.openClass(cl);
084                    output.write(
085                        "    defineRepository \""+rep.getGenerationName().toLowerCase()+"#0\" "+
086                        itemsRole.getGenerationFullName()+";\n");
087                }
088                
089            }
090    
091            // associations
092            if (role instanceof RelationRole) {
093                RelationRole relRole = (RelationRole)role;
094                if (((RelationLink)relRole.getLink()).getOrientation()
095                    == RelationLink.ORIENTATION_BOTH 
096                    && relRole.getLink().getStartRole()==relRole) 
097                {
098                    RelationRole opposite = (RelationRole)relRole.oppositeRole();
099                    state.openRole(cl,role);
100                    output.write("        declareAssociation "+
101                                 opposite.getGenerationFullName()+";\n");
102                }
103            }
104    
105            if (role.isNavigable()) {
106                RelationLink link = (RelationLink)role.getLink();
107                if (link.isCalculated()) {
108                    Method getter = role.getGetter();
109                    if (getter!=null) {
110                        state.openRole(cl,role);
111                        output.write("        declareCalculatedField "+
112                                     role.getGetter().getGenerationName()+";\n");
113                    } else {
114                        System.err.println("Calculated role "+role.getFullName()+
115                                           " does not have a getter");
116                    }
117                    state.openRole(cl,role);
118                    output.write("        setComponentType "+role.getEnd().getGenerationFullName()+";\n");
119                }
120                state.openRole(cl,role);
121                output.write("        setAggregation "+link.isAggregation()+";\n");
122                if (!role.isMultiple() && role.getCardinality().startsWith("0")) {
123                    state.openRole(cl,role);
124                    output.write("        setNullAllowed;\n");
125                }
126                Typed index = role.getPrimaryKey();
127                if (index!=null) {
128                    state.openRole(cl,role);
129                    output.write("        setIndexedField "+
130                                 index.getGenerationFullName()+";\n");
131                    output.write("        setRemover "+role.getRemoverName()+";\n");
132                }
133            }
134        }
135    
136    }