Model Access

A model access is a component that transforms a model from an external representation, e.g. XML or JSON, to an internal representation, typically an object model that is held in the memory of a computer. The model access does not hold any additional logic like for instance for the validation of the model content. The only thing a model access ensures is: When the transformation process succeeds, the model syntactically represents a valid model.

The open source components for generator development include some model access components like for instance for reading Excel sheets or models that were created with the Virtual Developer Modeler.

A model access has to implement the interface org.jenerateit.modelaccess.ModelAccessI. Normally, you extend the abstract class org.jenerateit.modelaccess.MessageProviderModelAccess and overwrite the following method:

protected abstract Collection<?>
    loadElements(InputStream input, ModelAccessOptions options) throws ModelAccessException;

As an example, the following code reads Java properties files and returns a collection of instances of java.util.Properties.

public class PropertiesModelAccess extends MessageProviderModelAccess {

...

/* (non-Javadoc)
 * @see org.jenerateit.modelaccess.MessageProviderModelAccess#loadElements(java.io.InputStream, org.jenerateit.modelaccess.ModelAccessOptions)
 */
@Override
protected Collection<?> loadElements(InputStream inputStream,
        ModelAccessOptions modelAccessOptions) throws ModelAccessException {

    try {
        List<Object> result = new ArrayList<>();
        final ZipInputStream zis = new ZipInputStream(inputStream);
        ZipEntry entry = null;
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.getName().toLowerCase().endsWith(".properties")) {
                Properties properties = new Properties();
                String simpleName = entry.getName();
                if (simpleName.contains("/")) simpleName = entry.getName().substring(entry.getName().lastIndexOf("/")+1);
                simpleName = simpleName.replace(".properties", "");

                // --- overwriting the close() method in order to do nothing since the given zip input stream will be closed by the caller
                properties.load(new BufferedInputStream(zis) {
                    @Override
                    public void close() throws IOException {}
                });
                properties.put("name", simpleName);
                result.add(properties);
            } else {
                // ignore this entry since it doesn't represent a properties file
            }
            zis.closeEntry();
        }
        return result;

    } catch (IOException ex) {
        throw new ModelAccessException("Error while reading ZIP input stream content", ex);

    } catch (Throwable th) {
        throw new ModelAccessException("Error while creating the Properties object from ZIP input stream", th);
    }
}

...