Index: AbstractUnit.java
===================================================================
--- AbstractUnit.java	(revision 467)
+++ AbstractUnit.java	(revision 468)
@@ -3,6 +3,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.List;
+import org.apache.commons.io.FileUtils;
 
 import org.ini4j.Ini;
 import org.slf4j.Logger;
@@ -13,273 +14,281 @@
 import org.gvsig.scripting.ScriptingScript;
 import org.gvsig.scripting.ScriptingUnit;
 
-public abstract class AbstractUnit implements ScriptingUnit, Unit{
+public abstract class AbstractUnit implements ScriptingUnit, Unit {
 
-	private static final Logger logger = LoggerFactory.getLogger(AbstractUnit.class);
-	
-	protected DefaultScriptingManager manager;
-	
-	protected String id;
-	protected String name = null;
-	protected String description;
-	protected String createdBy;
-	protected String version;
-	protected ScriptingFolder parent;
-	protected String typename;
-	
-	public AbstractUnit(ScriptingFolder parent, String typename, ScriptingManager manager, String id){
-		this.parent = parent;
-		this.manager = (DefaultScriptingManager) manager;
-		this.typename = typename;
-		this.id = id;
-	}
-	
-	public String getTypeName() {
-		return typename;
-	}
-	
-	/* (non-Javadoc)
-	 * @see org.gvsig.scripting.impl.Unit#load(org.gvsig.scripting.ScriptingFolder, java.lang.String)
-	 */
-	public abstract void load(ScriptingFolder folder, String id);
-	
-        public String toString(){
-		if( this.getName() == null ) {
-			return "("+this.getClass().getSimpleName()+")";
-		}
-		return this.getName();
-	}
-	
-	protected void setParent(ScriptingFolder parent) {
-		this.parent = parent;
-	}
-	
-	protected ScriptingManager getManager() {
-		return this.manager;
-	}
-	
-	protected File getFileResource(String extension){
-		return new File(this.getParent().getFile(),this.id + extension).getAbsoluteFile();
-	}
-	
-	/* (non-Javadoc)
-	 * @see org.gvsig.scripting.impl.Unit#getDescription()
-	 */
-	public String getDescription() {
-		return this.description;
-	}
+    private static final Logger logger = LoggerFactory.getLogger(AbstractUnit.class);
 
-	public String getCreatedBy() {
-		return this.createdBy;
-	}
-	
-	public String getVersion() {
-		return this.version;
-	}
-	
-	/* (non-Javadoc)
-	 * @see org.gvsig.scripting.impl.Unit#getId()
-	 */
-	public String getId() {
-		return this.id;
-	}
+    protected DefaultScriptingManager manager;
 
-	/* (non-Javadoc)
-	 * @see org.gvsig.scripting.impl.Unit#getName()
-	 */
-	public String getName() {
-		if (this.name == null){
-			return this.id;
-		}
-		return this.name;
-	}
-	
-	/* (non-Javadoc)
-	 * @see org.gvsig.scripting.impl.Unit#setDescription(java.lang.String)
-	 */
-	public void setDescription(String description) {
-		this.description = description;		
-	}
+    protected String id;
+    protected String name = null;
+    protected String description;
+    protected String createdBy;
+    protected String version;
+    protected ScriptingFolder parent;
+    protected String typename;
 
-	public void setCreatedBy(String createdBy) {
-		this.createdBy = createdBy;	
-	}
-	
-	public void setVersion(String version) {
-		this.version = version;	
-	}
-	
-	/* (non-Javadoc)
-	 * @see org.gvsig.scripting.impl.Unit#setId(java.lang.String)
-	 */
-	public void setId(String id) {
-		if(this instanceof ScriptingFolder){
-			this.id = id;
-		} else {
-			this.id = FilenameUtils.getBaseName(id);
-		}
-	}
+    public AbstractUnit(ScriptingFolder parent, String typename, ScriptingManager manager, String id) {
+        this.parent = parent;
+        this.manager = (DefaultScriptingManager) manager;
+        this.typename = typename;
+        this.id = id;
+    }
 
-	/* (non-Javadoc)
-	 * @see org.gvsig.scripting.impl.Unit#setName(java.lang.String)
-	 */
-	public void setName(String name) {
-		this.name = name;		
-	}
+    public String getTypeName() {
+        return typename;
+    }
 
-	/* (non-Javadoc)
-	 * @see org.gvsig.scripting.impl.Unit#getParent()
-	 */
-	public ScriptingFolder getParent() {
-		return this.parent;
-	}
+    /* (non-Javadoc)
+     * @see org.gvsig.scripting.impl.Unit#load(org.gvsig.scripting.ScriptingFolder, java.lang.String)
+     */
+    public abstract void load(ScriptingFolder folder, String id);
 
-	/* (non-Javadoc)
-	 * @see org.gvsig.scripting.impl.Unit#move(org.gvsig.scripting.ScriptingFolder)
-	 */
-	
-	private boolean moveFiles(ScriptingFolder folder, String id){
-		
-		if(manager.validateUnitId(folder, id)){
-			String oldId = this.getId();
-			String fileName = null;
-			String s[] = null;
-			String extension = null;
+    public String toString() {
+        if (this.getName() == null) {
+            return "(" + this.getClass().getSimpleName() + ")";
+        }
+        return this.getName();
+    }
+
+    protected void setParent(ScriptingFolder parent) {
+        this.parent = parent;
+    }
+
+    public ScriptingManager getManager() {
+        return this.manager;
+    }
+
+    protected File getFileResource(String extension) {
+        return new File(this.getParent().getFile(), this.id + extension).getAbsoluteFile();
+    }
+
+    /* (non-Javadoc)
+     * @see org.gvsig.scripting.impl.Unit#getDescription()
+     */
+    public String getDescription() {
+        return this.description;
+    }
+
+    public String getCreatedBy() {
+        return this.createdBy;
+    }
+
+    public String getVersion() {
+        return this.version;
+    }
+
+    /* (non-Javadoc)
+     * @see org.gvsig.scripting.impl.Unit#getId()
+     */
+    public String getId() {
+        return this.id;
+    }
+
+    /* (non-Javadoc)
+     * @see org.gvsig.scripting.impl.Unit#getName()
+     */
+    public String getName() {
+        if (this.name == null) {
+            return this.id;
+        }
+        return this.name;
+    }
+
+    /* (non-Javadoc)
+     * @see org.gvsig.scripting.impl.Unit#setDescription(java.lang.String)
+     */
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    @Override
+    public void setCreatedBy(String createdBy) {
+        this.createdBy = createdBy;
+    }
+
+    @Override
+    public void setVersion(String version) {
+        this.version = version;
+    }
+
+    /* (non-Javadoc)
+     * @see org.gvsig.scripting.impl.Unit#setId(java.lang.String)
+     */
+    @Override
+    public void setId(String id) {
+        if (this instanceof ScriptingFolder) {
+            this.id = id;
+        } else {
+            this.id = FilenameUtils.getBaseName(id);
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.gvsig.scripting.impl.Unit#setName(java.lang.String)
+     */
+    @Override
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /* (non-Javadoc)
+     * @see org.gvsig.scripting.impl.Unit#getParent()
+     */
+    public ScriptingFolder getParent() {
+        return this.parent;
+    }
+
+    /* (non-Javadoc)
+     * @see org.gvsig.scripting.impl.Unit#move(org.gvsig.scripting.ScriptingFolder)
+     */
+    private boolean moveFiles(ScriptingFolder folder, String id) {
+
+        if (manager.validateUnitId(folder, id)) {
+            String oldId = this.getId();
+            String fileName = null;
+            String s[] = null;
+            String extension = null;
             List<ScriptingUnit> units = this.getParent().getUnits();
-			for(int i=0; i<units.size();i++){
-				fileName = (units.get(i)).getId();
-				s = fileName.split("\\.");
-				extension = "";
-				if (s.length>1){
-					extension ="." +  s[s.length-1];
-					fileName = fileName.substring(0,fileName.length()-extension.length());
-				}
-				if(extension.equals("") && this instanceof ScriptingScript){
-					extension = manager.getExtensionByLanguage(((ScriptingScript)this).getLangName());
-				}
-				if(fileName.equals(oldId)){
-					//renombramos fichero
-					File f = new File(this.getParent().getFile(),fileName+extension);
-					File fDest = new File(folder.getFile(),id+extension);
-					if(this instanceof ScriptingScript){
-						String code = DefaultScriptingManager.getStringFromFile(f.getPath());
-						((ScriptingScript)this).setCode(code);
-					}
-					f.renameTo(fDest);
+            for (int i = 0; i < units.size(); i++) {
+                fileName = (units.get(i)).getId();
+                s = fileName.split("\\.");
+                extension = "";
+                if (s.length > 1) {
+                    extension = "." + s[s.length - 1];
+                    fileName = fileName.substring(0, fileName.length() - extension.length());
+                }
+                if (extension.equals("") && this instanceof ScriptingScript) {
+                    extension = manager.getExtensionOfLanguage(((ScriptingScript) this).getLangName());
+                }
+                if (fileName.equals(oldId)) {
+                    //renombramos fichero
+                    File f = new File(this.getParent().getFile(), fileName + extension);
+                    File fDest = new File(folder.getFile(), id + extension);
+                    if (this instanceof ScriptingScript) {
+                        String code;
+                        try {
+                            code = FileUtils.readFileToString(f);
+                            ((ScriptingScript) this).setCode(code);
+                        } catch (IOException ex) {
+                            
+                        }
+                    }
+                    f.renameTo(fDest);
 
-					File fInf = new File(this.getParent().getFile(),fileName+".inf");
-					if(fInf.exists()){
-						File fInfDest = new File(folder.getFile(),id+".inf");
-						fInf.renameTo(fInfDest);
-					}
-					File fDialog = new File(this.getParent().getFile(),fileName+".dlg");
-					if(fDialog.exists()){
-						File fDialogDest = new File(folder.getFile(),id+".dlg");
-						fDialog.renameTo(fDialogDest);
-					}
-				}	
-			}
-			this.setParent(folder);
-			this.setId(id);
-			return true;
-		}
-		return false;
-	}
-	
-	public boolean move(ScriptingFolder target) {
-		return moveFiles(target, this.getId());
-	}
+                    File fInf = new File(this.getParent().getFile(), fileName + ".inf");
+                    if (fInf.exists()) {
+                        File fInfDest = new File(folder.getFile(), id + ".inf");
+                        fInf.renameTo(fInfDest);
+                    }
+                    File fDialog = new File(this.getParent().getFile(), fileName + ".dlg");
+                    if (fDialog.exists()) {
+                        File fDialogDest = new File(folder.getFile(), id + ".dlg");
+                        fDialog.renameTo(fDialogDest);
+                    }
+                }
+            }
+            this.setParent(folder);
+            this.setId(id);
+            return true;
+        }
+        return false;
+    }
 
-	/* (non-Javadoc)
-	 * @see org.gvsig.scripting.impl.Unit#rename(java.lang.String)
-	 */
-	public boolean rename(String newId) {
-		return moveFiles(this.getParent(), newId);
-	}
-	
-	private String toStringNotNull(String s) {
-		if( s==null ) {
-			return "";
-		}
-		return s;
-	}
-	
-	protected void save(Ini prefs){
-		prefs.put("Unit", "type",toStringNotNull(this.getTypeName()));
-		prefs.put("Unit", "name",this.getName());
-		prefs.put("Unit", "description",toStringNotNull(this.getDescription()));
-		prefs.put("Unit", "createdBy",toStringNotNull(this.getCreatedBy()));
-		prefs.put("Unit", "version",toStringNotNull(this.getVersion()));
-		
-		try {
-			prefs.store();
-		} catch (IOException e) {
-			File f = prefs.getFile();
-			String fname = (f==null)? "(null)":f.getAbsolutePath();
-			logger.warn("Can't save inf file '"+fname+"'.");
-		}
+    public boolean move(ScriptingFolder target) {
+        return moveFiles(target, this.getId());
+    }
 
-	}
-	
-	protected void loadInf(Ini prefs){
-		String typename = getInfString(prefs,"Unit","type", this.getTypeName());
-		if( !this.getTypeName().equalsIgnoreCase(typename) ) {
-			File f = prefs.getFile();
-			String fname = (f==null)? "(null)":f.getAbsolutePath();
-			logger.warn("inconsistent type in inf file '"+fname+"'. Curent type '"+this.getTypeName()+"', type from inf file '"+typename+"'.");
-		}
-		this.setName(getInfString(prefs,"Unit","name", this.getName()));
-		this.setDescription(getInfString(prefs,"Unit","description", null));
-		this.setCreatedBy(getInfString(prefs,"Unit","createdBy", null));
-		this.setVersion(getInfString(prefs,"Unit","version", null));
+    /* (non-Javadoc)
+     * @see org.gvsig.scripting.impl.Unit#rename(java.lang.String)
+     */
+    public boolean rename(String newId) {
+        return moveFiles(this.getParent(), newId);
+    }
 
-	}
-	
-	protected Object getInfValue(Ini prefs, String section, String option, Object defaultValue){
-		Object r = prefs.get(section,option);
-		if (r==null) {
-			return defaultValue;
-		} else {
-			return r;
-		}
-	}
-	
-	private String getInfString(Ini prefs, String section, String option, Object defaultValue) {
-		String s = (String) getInfValue(prefs, section, option, defaultValue);
-		if( s!=null && s.trim().length()<1) {
-			return null;
-		}
-		return s;
-	}
-	
-	protected void console_println(String s) {
-		// When running from the composer messages of stdout shows in the console tab.
-		logger.info(s);
-		System.out.println(s);
-	}
+    private String toStringNotNull(String s) {
+        if (s == null) {
+            return "";
+        }
+        return s;
+    }
 
-	public void create(ScriptingFolder folder, String id) {
-		this.setParent(folder);
-		this.setId(id);
+    protected void save(Ini prefs) {
+        prefs.put("Unit", "type", toStringNotNull(this.getTypeName()));
+        prefs.put("Unit", "name", this.getName());
+        prefs.put("Unit", "description", toStringNotNull(this.getDescription()));
+        prefs.put("Unit", "createdBy", toStringNotNull(this.getCreatedBy()));
+        prefs.put("Unit", "version", toStringNotNull(this.getVersion()));
 
-		File file = new File(folder.getFile(),id +".inf");
-		try {
-			file.createNewFile();
-		} catch (IOException e) {
-			logger.warn("Can't create inf file in '"+file.getAbsolutePath()+"'.",e);
-		}
-	}
+        try {
+            prefs.store();
+        } catch (IOException e) {
+            File f = prefs.getFile();
+            String fname = (f == null) ? "(null)" : f.getAbsolutePath();
+            logger.warn("Can't save inf file '" + fname + "'.");
+        }
 
-	public File getFile() {
-		if( this.getId()==null ) {
-			return null;
-		}
-		if( this.getParent()==null ) {
-			return null;
-		}
-		if( this.getParent().getFile()==null ) {
-			return null;
-		}
-		return new File(getParent().getFile(),this.getId()+".inf");
-	}
+    }
+
+    protected void loadInf(Ini prefs) {
+        String typename = getInfString(prefs, "Unit", "type", this.getTypeName());
+        if (!this.getTypeName().equalsIgnoreCase(typename)) {
+            File f = prefs.getFile();
+            String fname = (f == null) ? "(null)" : f.getAbsolutePath();
+            logger.warn("inconsistent type in inf file '" + fname + "'. Curent type '" + this.getTypeName() + "', type from inf file '" + typename + "'.");
+        }
+        this.setName(getInfString(prefs, "Unit", "name", this.getName()));
+        this.setDescription(getInfString(prefs, "Unit", "description", null));
+        this.setCreatedBy(getInfString(prefs, "Unit", "createdBy", null));
+        this.setVersion(getInfString(prefs, "Unit", "version", null));
+
+    }
+
+    protected Object getInfValue(Ini prefs, String section, String option, Object defaultValue) {
+        Object r = prefs.get(section, option);
+        if (r == null) {
+            return defaultValue;
+        } else {
+            return r;
+        }
+    }
+
+    private String getInfString(Ini prefs, String section, String option, Object defaultValue) {
+        String s = (String) getInfValue(prefs, section, option, defaultValue);
+        if (s != null && s.trim().length() < 1) {
+            return null;
+        }
+        return s;
+    }
+
+    protected void console_println(String s) {
+        // When running from the composer messages of stdout shows in the console tab.
+        logger.info(s);
+        System.out.println(s);
+    }
+
+    public void create(ScriptingFolder folder, String id) {
+        this.setParent(folder);
+        this.setId(id);
+
+        File file = new File(folder.getFile(), id + ".inf");
+        try {
+            file.createNewFile();
+        } catch (IOException e) {
+            logger.warn("Can't create inf file in '" + file.getAbsolutePath() + "'.", e);
+        }
+    }
+
+    public File getFile() {
+        if (this.getId() == null) {
+            return null;
+        }
+        if (this.getParent() == null) {
+            return null;
+        }
+        if (this.getParent().getFile() == null) {
+            return null;
+        }
+        return new File(getParent().getFile(), this.getId() + ".inf");
+    }
 }
