Search in sources :

Example 1 with NameBearer

use of gate.util.NameBearer in project gate-core by GateNLP.

the class PersistenceManager method getPersistentRepresentation.

/**
 * Recursively traverses the provided object and replaces it and all
 * its contents with the appropriate persistent equivalent classes.
 *
 * @param target the object to be analysed and translated into a
 *          persistent equivalent.
 * @return the persistent equivalent value for the provided target
 */
public static Serializable getPersistentRepresentation(Object target) throws PersistenceException {
    if (target == null)
        return null;
    // first check we don't have it already
    Persistence res = existingPersistentReplacements.get().getFirst().get(new ObjectHolder(target));
    if (res != null)
        return res;
    Class<? extends Object> type = target.getClass();
    Class<?> newType = getMostSpecificPersistentType(type);
    if (newType == null) {
        // no special handler
        if (target instanceof Serializable)
            return (Serializable) target;
        else
            throw new PersistenceException("Could not find a serialisable replacement for " + type);
    }
    // we have a new type; create the new object, populate and return it
    try {
        res = (Persistence) newType.newInstance();
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
    if (target instanceof NameBearer) {
        StatusListener sListener = (StatusListener) Gate.getListeners().get("gate.event.StatusListener");
        if (sListener != null) {
            sListener.statusChanged("Storing " + ((NameBearer) target).getName());
        }
    }
    res.extractDataFromSource(target);
    existingPersistentReplacements.get().getFirst().put(new ObjectHolder(target), res);
    return res;
}
Also used : Serializable(java.io.Serializable) PersistenceException(gate.persist.PersistenceException) StatusListener(gate.event.StatusListener) NameBearer(gate.util.NameBearer) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) PersistenceException(gate.persist.PersistenceException) GateRuntimeException(gate.util.GateRuntimeException) ResourceInstantiationException(gate.creole.ResourceInstantiationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) GateException(gate.util.GateException)

Aggregations

ResourceInstantiationException (gate.creole.ResourceInstantiationException)1 StatusListener (gate.event.StatusListener)1 PersistenceException (gate.persist.PersistenceException)1 GateException (gate.util.GateException)1 GateRuntimeException (gate.util.GateRuntimeException)1 NameBearer (gate.util.NameBearer)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1