001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.generation.jorm;
027:
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Set;
033:
034: import org.objectweb.jorm.api.PException;
035: import org.objectweb.jorm.compiler.api.JormCompilerConfigurator;
036: import org.objectweb.jorm.compiler.api.JormCompilerParameter;
037: import org.objectweb.jorm.compiler.lib.JormCompiler;
038: import org.objectweb.jorm.metainfo.api.Class;
039: import org.objectweb.jorm.metainfo.api.ClassProject;
040: import org.objectweb.jorm.metainfo.api.CompositeName;
041: import org.objectweb.jorm.metainfo.api.GenClassRef;
042: import org.objectweb.jorm.metainfo.api.Manager;
043: import org.objectweb.jorm.metainfo.api.Mapping;
044: import org.objectweb.jorm.metainfo.api.NameDef;
045: import org.objectweb.jorm.metainfo.api.TypedElement;
046: import org.objectweb.jorm.util.io.lib.DirJavaExplorer;
047: import org.objectweb.speedo.api.ExceptionHelper;
048: import org.objectweb.speedo.api.SpeedoException;
049: import org.objectweb.speedo.api.SpeedoProperties;
050: import org.objectweb.speedo.generation.api.SpeedoXMLError;
051: import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent;
052: import org.objectweb.speedo.lib.Personality;
053: import org.objectweb.speedo.metadata.SpeedoClass;
054: import org.objectweb.speedo.metadata.SpeedoField;
055: import org.objectweb.speedo.metadata.SpeedoIdentity;
056: import org.objectweb.speedo.metadata.SpeedoModifier;
057: import org.objectweb.speedo.metadata.SpeedoPackage;
058: import org.objectweb.speedo.metadata.SpeedoXMLDescriptor;
059: import org.objectweb.util.monolog.api.BasicLevel;
060:
061: /**
062: * Generates the JORM Files corresponding to a set of Java Classes.
063: * <p>See <a href="http://www.objectweb.org/jorm">http://www.objectweb.org/jorm</a>for more information.</p>
064: * @author S.Chassande-Barrioz
065: */
066: public class JormGenerator extends AbstractGeneratorComponent {
067: public final static String LOGGER_NAME = SpeedoProperties.LOGGER_NAME
068: + ".generation.jorm";
069: /**
070: * Jorm classes intended for JORM generation
071: */
072: protected JormCompiler jormcompiler;
073:
074: public JormGenerator(Personality p) {
075: super (p);
076: }
077:
078: // IMPLEMENTATION OF THE GeneratorComponent INTERFACE //
079: //----------------------------------------------------//
080:
081: public boolean init() throws SpeedoException {
082: if (scp.getXmldescriptor().isEmpty()) {
083: return false;
084: }
085: logger = scp.loggerFactory.getLogger(LOGGER_NAME);
086: jormcompiler = (JormCompiler) scp.generatorsContext
087: .get("jormcompiler");
088: JormCompilerParameter jcp = jormcompiler.getCompilerParameter();
089: jcp.setProjectName(scp.projectName);
090: jcp.setBindingInheritance("%p%c");
091: jcp.setClassMappingInheritance("%p%cHome");
092: jcp.setOutput(scp.output);
093: jcp.setBindingAbstract(true);
094: jcp.setDtdLocations(scp.dtdLocations);
095: jcp.setInputFiles(scp.jorm);
096: DirJavaExplorer pe = new DirJavaExplorer();
097: pe.setLogger(scp.loggerFactory
098: .getLogger("org.objectweb.jorm.io.pathexplorer"));
099: pe.addPath(scp.jormclasspath);
100: jcp.setClasspath(pe);
101: jcp.setGenerateBinding(false);
102: jcp.setGeneratedWithMapperPackage(false);
103: return true;
104: }
105:
106: public String getTitle() {
107: return "Generating Jorm files...";
108: }
109:
110: /**
111: * Performs the JORM objects' generation.
112: */
113: public void process() throws SpeedoException {
114: if (scp.getXmldescriptor().isEmpty()) {
115: return;
116: }
117: if (logger.isLoggable(BasicLevel.DEBUG)) {
118: Manager m = jormcompiler.getMIManager();
119: for (Iterator it = m.getClasses().iterator(); it.hasNext();) {
120: logger.log(BasicLevel.DEBUG, "Class : "
121: + ((Class) it.next()).getFQName());
122: }
123: for (Iterator it = m.getCompositeNames().iterator(); it
124: .hasNext();) {
125: logger.log(BasicLevel.DEBUG, "CompositeName : "
126: + ((CompositeName) it.next()).getFQName());
127: }
128: }
129: try {
130: // Generate the .pd files
131: if (scp.generateNeededJormFile) {
132: jormcompiler.generateJormFiles(null);
133: }
134: //Launch the jorm generation
135: Collection gfs = jormcompiler.generateFiles(null);
136: if (logger.isLoggable(BasicLevel.DEBUG))
137: for (Iterator it = gfs.iterator(); it.hasNext();)
138: logger.log(BasicLevel.DEBUG, "Generated files: "
139: + it.next());
140:
141: //Register the meta information to serialize for each xml descriptor
142: for (Iterator itDesc = scp.getXmldescriptor().values()
143: .iterator(); itDesc.hasNext();) {
144: addSerializeJormMI((SpeedoXMLDescriptor) itDesc.next());
145: }
146: } catch (PException e) {
147: throw new SpeedoException("Error during Jorm generation", e);
148: }
149: }
150:
151: private void addSerializeJormMI(SpeedoXMLDescriptor desc)
152: throws SpeedoException {
153: // Calculate the list of the Jorm Meta Object which must be serialized
154: //Set mos = new HashSet(desc.jdoPackage.size());
155: for (Iterator itPack = desc.packages.values().iterator(); itPack
156: .hasNext();) {
157: SpeedoPackage sp = (SpeedoPackage) itPack.next();
158: for (Iterator itclass = sp.classes.values().iterator(); itclass
159: .hasNext();) {
160: SpeedoClass jdoClass = (SpeedoClass) itclass.next();
161: if (jdoClass.jormclass == null)
162: throw new SpeedoException(
163: "The Jorm meta information of the jdo descriptor "
164: + desc.xmlFile
165: + " is not complete: class "
166: + jdoClass.name
167: + " has not been found");
168: // The class meta object
169: addMOClass(jdoClass.jormclass, desc.mos);
170:
171: // Add the CompositeName which could be used in the class name def
172: for (Iterator it = jdoClass.jormclass.getNameDefs()
173: .iterator(); it.hasNext();) {
174: NameDef nd = (NameDef) it.next();
175: if (nd.isNameRef()) {
176: desc.mos
177: .add(nd.getNameRef().getCompositeName());
178: }
179: }
180:
181: //The compositeName used for the reference will be added by the
182: // name definition of the referenced class.
183: }
184: }
185: }
186:
187: private void addMOClass(Class mo, Set mos) {
188: mos.add(mo);
189: for (Iterator it = mo.getSuperClasses().iterator(); it
190: .hasNext();) {
191: addMOClass((Class) it.next(), mos);
192: }
193: }
194: }
|