001    /*
002      Copyright (C) 2003 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    
024    /**
025     * Generation state for acc configuration code genaration. It
026     * memorizes if a class or attribute block is opened or not.
027     */
028    public class AccGenState {
029       /** Is a "class" block opened ? */
030       boolean classOpened = false;
031       /** Is an "attribute" block opened ? */
032       boolean fieldOpened = false;
033       Writer output;
034    
035       public AccGenState(Writer output) {
036          this.output = output;
037       }
038    
039       /**
040        * Start a "class" block
041        */
042       void openClass(Class cl) 
043          throws IOException
044       {
045          if (!classOpened) {
046             output.write("class "+cl.getGenerationFullName()+" {\n");
047             classOpened = true;
048          }
049       }
050       /**
051        * End a "class" block if any
052        */
053       void closeClass() 
054          throws IOException
055       {
056          if (classOpened) {
057             classOpened = false;
058             write("}\n");
059          }
060       }
061    
062       /**
063        * Start a "attribute" block
064        */
065       void openField(Class cl, Field field) 
066          throws IOException
067       {
068          openClass(cl);
069          if (!fieldOpened) {
070             write("attribute "+field.getGenerationName()+" {\n");
071             fieldOpened = true;
072          }
073       }
074    
075       /**
076        * Start a "attribute" block
077        */
078       void openRole(Class cl, RelationRole role) 
079          throws IOException
080       {
081          openClass(cl);
082          if (!fieldOpened) {
083             write("attribute "+role.getGenerationName()+" {\n");
084             fieldOpened = true;
085          }
086       }
087    
088       /**
089        * Start a "attribute" block
090        */
091       void openMethod(Class cl, Method method) 
092          throws IOException
093       {
094          openClass(cl);
095          if (!fieldOpened) {
096             write("method \""+method.getUniqueName()+"\" {\n");
097             fieldOpened = true;
098          }
099       }
100       /**
101        * End a "attribute" block if any
102        */
103       void closeMember() 
104          throws IOException
105       {
106          if (fieldOpened) {
107             fieldOpened = false;
108             write("}\n");
109          }
110       }
111    
112       /**
113        * Write some text to output with correct indentation
114        * @param text text to write to output
115        */
116       void write(String text) throws IOException {
117          if (classOpened)
118             output.write("    ");
119          if (fieldOpened)
120             output.write("    ");
121          output.write(text);
122       }
123    }