//Source file: H:\\TEMP\\generated\\ispuml\\mdaTransformation\\model\\factory\\MDRModelFactory.java
package modTransf.model.factory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Iterator;
import javax.jmi.xmi.XmiReader;
import javax.jmi.xmi.XmiWriter;
import javax.jmi.xmi.MalformedXMIException;
import org.netbeans.api.mdr.MDRManager;
import org.netbeans.api.mdr.MDRepository;
import org.netbeans.api.mdr.CreationFailedException;
import org.openide.util.Lookup;
import javax.jmi.reflect.RefPackage;
import javax.jmi.model.ModelPackage;
import javax.jmi.model.MofPackage;
/**
* Implementation of the MM Helper for MDR.
* The metamodel is stored in the MDR repository under the MM Uri.
* The mm filename is used to load the metamodel from an xmi specification if
* needed.
*/
public class MDRModelFactory extends ModelFactoryBase
{
/** MDR repository */
protected static MDRepository rep;
/** XMI reader */
public static XmiReader reader;
/** XMI reader */
public static XmiWriter writer;
/** A counter used to postfix the instance names */
public static int nameCounter=0;
/** The name of this mm in the MDR repository */
//protected String metaModelMdrName;
/** The name of the file containing the xmi description of the mm */
//protected String metaModelFileName;
/** The name of the main extent in the mm (ex: UML in the UML1.4 mm)*/
protected String metaModelMainExtentName;
/**
* Static constructor.
*/
/* static {
init();
};
*/
/**
* Default constructor.
*/
public MDRModelFactory()
{
try {
init();
}
catch( Error er )
{
System.out.println("Can't initialize the MDR manager. Should be done manually.");
}
}
/**
* init static instance of MDR.
*/
public void init()
{
if(rep==null)
{
MDRManager mngr = MDRManager.getDefault();
if( mngr != null )
rep = mngr.getDefaultRepository();
if( rep == null )
{
// Try to create it manually
System.out.println("Try to Create the MDRRepository manually.");
//mngr = new org.netbeans.mdr.NBMDRManagerImpl();
rep = new org.netbeans.mdr.NBMDRepositoryImpl();
}
if(rep == null )
{
throw new Error("Can't get the default MDRRepository.");
}
} // end if
}
/**
* Constructor.
*/
public MDRModelFactory( String mmUri, String mmFilename, String metaModelMainExtentName,
String mmEngineAdaptor)
{
super(mmUri, mmFilename, mmEngineAdaptor);
setMetaModelMainExtentName(metaModelMainExtentName);
}
/**
* @return Object
*/
public Object createEmptyNativeModel()
throws ModelFactoryException
{
try
{
return createContainer();
}
catch( ModelFactoryException ex)
{
throw ex;
}
catch( Exception ex)
{
throw new ModelFactoryException(ex);
}
}
/**
* Release the model container.
* Called when the application don't need to use the model anymore.
* The container implementation can collect and free all the model resources.
* This implementation delete the JMI container.
* @return Object
*/
public void releaseNativeModel(Object container)
{
((RefPackage)container).refDelete();
}
/**
* Read model from the specified reader and store it into the container .
* @param input
* @param container
*/
public void readNativeModel(InputStream input, Object container)
throws IOException, ModelFactoryException
{
try
{
if( reader == null )
reader = lookupXmiReader();
reader.read( input, null, (RefPackage)container );
}
catch(MalformedXMIException ex)
{
throw new ModelFactoryException(ex);
}
}
/**
* Read model from the specified filename and store it into the container .
* The container should be adapted to the model to be read.
* @param filereader
* @param container
*/
public void readNativeModel(String fileName, Object container)
throws IOException, ModelFactoryException
{
try {
if( reader == null )
reader = lookupXmiReader();
String uri = lookupFileURI(fileName);
//System.out.println(" readModel("+ uri+")");
reader.read( uri, (RefPackage)container );
}
catch(MalformedXMIException ex)
{
throw new ModelFactoryException(ex);
}
}
/**
* Write the model contained in the container to the specified writer.
* Do not close the stream
* @param filewriter
* @param container
*/
public void writeNativeModel(FileOutputStream out, Object container)
throws IOException, ModelFactoryException
{
writeNativeModel( (OutputStream)out, container);
}
/**
* Write the model contained in the container to the specified writer.
* Do not close the stream
* @param filewriter
* @param container
*/
public void writeNativeModel(OutputStream out, Object container)
throws IOException, ModelFactoryException
{
if( writer == null )
writer = lookupXmiWriter();
writer.write( out, (RefPackage)container, null );
}
/**
* Write the model contained in the container to the specified filename.
* @param filewriter
* @param container
*/
public void writeNativeModel(String filename, Object container)
throws IOException, ModelFactoryException
{
//System.out.println("Container type=" + container );
if( writer == null )
writer = lookupXmiWriter();
FileOutputStream out = new FileOutputStream(filename);
writer.write( out, (RefPackage)container, null );
out.close();
}
/**
* Lookup the xmi writer.
* @return XmiWriter
*/
protected XmiWriter lookupXmiWriter()
{
XmiWriter writer = (XmiWriter) Lookup.getDefault().lookup(XmiWriter.class);
if( writer == null )
{
writer = new org.netbeans.lib.jmi.xmi.XMIWriterImpl();
}
return writer;
}
/**
* Lookup the xmi reader.
* @return XmiWriter
*/
protected XmiReader lookupXmiReader()
{
XmiReader reader = (XmiReader) Lookup.getDefault().lookup(XmiReader.class);
if( reader == null )
{
reader = new org.netbeans.lib.jmi.xmi.SAXReader();
}
return reader;
}
/**
* Create the container for the specified metamodel.
* @param modelContainerName The name of the model container in the repository
* @param metamodelContainerName The name of the metamodel container in the repository
* @param metamodelFile The metamodel xmi file name.
* @param mmMainExtentName The metamodel main extent name.
* @return
*/
protected RefPackage createContainer()
throws CreationFailedException, Exception
{
RefPackage container = null;
String modelName = generateModelName();
// Look for a container name which is not already in use.
// Try each incremented name.
try {
container = getRep().getExtent(modelName);
while(container != null)
{
modelName = generateModelName();
container = getRep().getExtent(modelName);
}
}
catch(org.netbeans.mdr.util.DebugException ex)
{ // Pass it.
}
// Get or create the metamodel
MofPackage metamodel = loadMetamodel();
// Create a container from the metamodel
container = getRep().createExtent( modelName, metamodel );
return container;
}
private String generateModelName()
{
return mmURI + "Instance" + nameCounter++;
}
/**
* Load or find the specified metamodel extent.
* @param metamodelContainerName
* @param metamodelFile
* @param mmMainExtentName
* @return
*/
protected MofPackage loadMetamodel()
throws CreationFailedException, Exception
{
if(mmURI==null )
{
throw new ModelFactoryException( "Metamodel URI should be specified" );
}
if(metaModelMainExtentName == null )
{
throw new ModelFactoryException( "Metamodel main extend name should be specified for '" + mmURI + "'." );
}
// Is the metamodel container already loaded ?
ModelPackage metamodelContainer = (ModelPackage)getRep().getExtent( mmURI );
if( metamodelContainer == null )
{ // Create mof instance
metamodelContainer = (ModelPackage)getRep().createExtent( mmURI );
}
// Get the metamodel main entry package
MofPackage metamodel = getMetamodelMainPackage( metamodelContainer, metaModelMainExtentName );
if( metamodel == null )
{ // load the mm in the container
readNativeModel( mmFilename, metamodelContainer );
// Get the main entry
metamodel = getMetamodelMainPackage( metamodelContainer, metaModelMainExtentName );
}
return metamodel;
}
/**
* Get the specified package from the container.
* Return the package or null if not found
* @param metamodelContainer
* @param mmMainExtentName
* @return
*/
protected MofPackage getMetamodelMainPackage( ModelPackage metamodelContainer, String mmMainExtentName)
{
for (Iterator it = metamodelContainer.getMofPackage().refAllOfClass().iterator(); it.hasNext();) {
MofPackage pkg = (MofPackage) it.next();
if (pkg.getContainer() == null && mmMainExtentName.equals(pkg.getName())) {
return pkg;
}
}
return null;
}
/**
* Lookup for the file in the different locations:
* As a file
* as a resource
* Return the URI.toString form.
* @param filename String
* @return InputStream
*/
protected String lookupFileURI( String filename )
throws FileNotFoundException
{
URL url;
// Try to get the input as a regular file
File file = new File(filename);
if( file.exists() )
return file.toURI().toString();
// try as a resource
url = this.getClass().getResource(filename);
if(url!=null)
return url.toString();
url = System.class.getClassLoader().getSystemResource(filename);
if(url!=null)
return url.toString();
// Not found :-(
throw new FileNotFoundException( "Can't find file '" + filename + "'.");
}
/**
* Check and print the container status (empty or not)
* @param container
*/
protected void checkContainerStatus( RefPackage container )
{
if( container.refAllAssociations() != null || container.refAllClasses() != null )
System.out.println("Container '" + container +"' is NOT empty" );
else
System.out.println("Container '" + container +"' is empty" );
}
/**
*
* @return
*/
public String getMetaModelMainExtentName()
{
return metaModelMainExtentName;
}
/**
*
* @param metaModelMainExtentName
*/
public void setMetaModelMainExtentName(String metaModelMainExtentName)
{
this.metaModelMainExtentName = metaModelMainExtentName;
}
protected MDRepository getRep()
{
init();
return rep;
}
}