001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library 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 GNU
012: * 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 library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.generation.mivisitor;
018:
019: import org.objectweb.speedo.api.SpeedoException;
020: import org.objectweb.speedo.api.SpeedoProperties;
021: import org.objectweb.speedo.generation.api.SpeedoCompilerParameter;
022: import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent;
023: import org.objectweb.speedo.lib.Personality;
024: import org.objectweb.speedo.metadata.SpeedoClass;
025: import org.objectweb.speedo.metadata.SpeedoExtension;
026: import org.objectweb.speedo.metadata.SpeedoField;
027: import org.objectweb.speedo.metadata.SpeedoIndex;
028: import org.objectweb.speedo.metadata.SpeedoPackage;
029: import org.objectweb.speedo.metadata.SpeedoTuple;
030: import org.objectweb.speedo.metadata.SpeedoXMLDescriptor;
031: import org.objectweb.speedo.sequence.lib.SpeedoSequence;
032: import org.objectweb.util.monolog.api.BasicLevel;
033:
034: /**
035: *
036: * @author S.Chassande-Barrioz
037: */
038: public class AbstractMetaInfoVisitor extends AbstractGeneratorComponent
039: implements MetaInfoVisitor {
040: public final static String LOGGER_NAME = SpeedoProperties.LOGGER_NAME
041: + ".generation.mivisitor";
042: /**
043: * The next MetaInfoVisitor instance. If the field is null that means there
044: * is no next. Then the current MetaInfoVisitor is the last.
045: */
046: MetaInfoVisitor mim;
047:
048: /**
049: * builds a MetaInfoVisitor which is the last of the chain
050: */
051: public AbstractMetaInfoVisitor(Personality p) {
052: super (p);
053: mim = null;
054: }
055:
056: /**
057: * builds a MetaInfoVisitor which is the last of the chain
058: */
059: public AbstractMetaInfoVisitor(MetaInfoVisitor mim, Personality p) {
060: super (p);
061: this .mim = mim;
062: }
063:
064: protected String getLoggerName() {
065: return LOGGER_NAME;
066: }
067:
068: public boolean init() throws SpeedoException {
069: logger = scp.loggerFactory.getLogger(getLoggerName());
070: debug = logger != null && logger.isLoggable(BasicLevel.DEBUG);
071: return !scp.getXmldescriptor().isEmpty();
072: }
073:
074: public void process() throws SpeedoException {
075: visitCompilerParameter(scp);
076: }
077:
078: // IMPLEMENTATION OF THE METHODS FROM THE MetaInfoVisitor INTERFACE //
079: //------------------------------------------------------------------//
080:
081: public MetaInfoVisitor getNext() {
082: return mim;
083: }
084:
085: public void setNext(MetaInfoVisitor next) {
086: mim = next;
087: }
088:
089: public void visitCompilerParameter(SpeedoCompilerParameter scp)
090: throws SpeedoException {
091: if (logger == null) {
092: setLogger(scp.loggerFactory.getLogger(getLoggerName()));
093: }
094: if (mim != null) {
095: mim.visitCompilerParameter(scp);
096: }
097: }
098:
099: public void visitXml(SpeedoXMLDescriptor xml)
100: throws SpeedoException {
101: if (mim != null) {
102: mim.visitXml(xml);
103: }
104: }
105:
106: public void visitPackage(SpeedoPackage sp) throws SpeedoException {
107: if (mim != null) {
108: mim.visitPackage(sp);
109: }
110: }
111:
112: public void visitClass(SpeedoClass sc) throws SpeedoException {
113: if (mim != null) {
114: mim.visitClass(sc);
115: }
116: }
117:
118: public void visitEndClass(SpeedoClass sc) throws SpeedoException {
119: if (mim != null) {
120: mim.visitEndClass(sc);
121: }
122: }
123:
124: public void visitSequence(SpeedoSequence ss, SpeedoPackage sp)
125: throws SpeedoException {
126: if (mim != null) {
127: mim.visitSequence(ss, sp);
128: }
129: }
130:
131: public void visitField(SpeedoField sf) throws SpeedoException {
132: if (mim != null) {
133: mim.visitField(sf);
134: }
135: }
136:
137: public void visitExtension(SpeedoExtension se)
138: throws SpeedoException {
139: if (mim != null) {
140: mim.visitExtension(se);
141: }
142: }
143:
144: public void visitIndex(SpeedoIndex si, SpeedoPackage sp)
145: throws SpeedoException {
146: //try to find the column name(s)
147: /*if (si.columnNames.isEmpty()) {
148: for (Iterator it = si.fieldNames.iterator(); it.hasNext(); ) {
149: String fieldName = (String) it.next();
150: //try to add the column name(s) to the index
151: SpeedoField sf = (SpeedoField) si.speedoClass.fields.get(fieldName);
152: if( sf.columns != null && sf.columns.length != 0) {
153: for(int i = 0; i < sf.columns.length; i++) {
154: si.columnNames.add(sf.columns[i].name);
155: }
156: } else {
157: //by default, it is the field name
158: si.columnNames.add(fieldName);
159: }
160: }
161: }
162: sp.xmlDescriptor.mos.add(si);*/
163: if (mim != null) {
164: mim.visitIndex(si, sp);
165: }
166: }
167:
168: public void visitEnd() {
169: if (mim != null) {
170: mim.visitEnd();
171: }
172: }
173:
174: /**
175: * Helper method able to find the SpeedoClass hosting a Speedo meta object.
176: * if the specified object is not a child (directly or not) of a SpeedoClass
177: * then a null value is returned.
178: * @param o is a child (directly or not) of SpeedoClass
179: * (supported types: SpeedoClass, SpeedoField, SpeedoTuple, SpeedoExtension)
180: */
181: protected static SpeedoClass getSpeedoClass(Object o) {
182: if (o instanceof SpeedoClass) {
183: return (SpeedoClass) o;
184: } else if (o instanceof SpeedoField) {
185: return ((SpeedoField) o).moClass;
186: } else if (o instanceof SpeedoTuple) {
187: return ((SpeedoTuple) o).moField.moClass;
188: } else if (o instanceof SpeedoExtension) {
189: return getSpeedoClass(((SpeedoExtension) o).owner);
190: } else {//SpeedoPackage, SpeedoXMLDescriptor, ...
191: return null;
192: }
193: }
194: }
|