eclipse plugin: Implement the persistance for the DependencyTreeBuilder

This commit is contained in:
Timotei Dolean 2011-07-06 16:51:07 +00:00
parent f603b37661
commit 0f80beb9d7
8 changed files with 117 additions and 22 deletions

View file

@ -8,6 +8,9 @@
*******************************************************************************/
package org.wesnoth.builder;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@ -29,15 +32,16 @@ import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.wesnoth.Logger;
import org.wesnoth.builder.WesnothProjectBuilder.WMLFilesComparator;
import org.wesnoth.projects.ProjectDependencyNode;
import org.wesnoth.utils.ListUtils;
import org.wesnoth.utils.ResourceUtils;
import org.wesnoth.wml.WMLMacroCall;
import org.wesnoth.wml.WMLRoot;
public class DependencyTreeBuilder
public class DependencyTreeBuilder implements Serializable
{
protected IProject project_;
private static final long serialVersionUID = 6007509520015856611L;
protected transient IProject project_;
protected int currentIndex_ = 0;
private ProjectDependencyNode parent_;
private ProjectDependencyNode previous_;
@ -50,12 +54,16 @@ public class DependencyTreeBuilder
project_ = project;
}
/**
* Create the whole dependency tree from scratch
*/
public void createDependencyTree()
{
// start creating the PDT (project dependency tree)
Queue<IContainer> containers = new LinkedBlockingDeque<IContainer>( );
parent_ = null;
tree_.clear( );
containers.add( project_ );
@ -182,7 +190,11 @@ public class DependencyTreeBuilder
}
}
private void addNode( IFile file )
/**
* Adds a new node to this tree
* @param file The file to add
*/
public void addNode( IFile file )
{
ProjectDependencyNode newNode = new ProjectDependencyNode( file, currentIndex_ );
currentIndex_ += ProjectDependencyNode.INDEX_STEP;
@ -207,4 +219,36 @@ public class DependencyTreeBuilder
tree_.put( file.getProjectRelativePath( ).toString( ), newNode );
previous_ = newNode;
}
/**
* Removes a node specified by the file
* @param file The file to remove from the tree
*/
public void removeNode( IFile file )
{
}
/**
* Deserializes this object from the input
* @param input The object input stream
* @throws IOException
* @throws ClassNotFoundException
*/
public void deserialize( ObjectInputStream input ) throws IOException, ClassNotFoundException
{
DependencyTreeBuilder tmp = (DependencyTreeBuilder) input.readObject( );
if ( tmp == null )
return;
this.currentIndex_ = tmp.currentIndex_;
this.tree_ = tmp.tree_;
this.previous_ = tmp.previous_;
this.parent_ = tmp.parent_;
// now, refill the dependency nodes
for ( ProjectDependencyNode node : tree_.values( ) ) {
node.file_ = project_.getFile( node.fileName_ );
}
}
}

View file

@ -6,7 +6,9 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.wesnoth.projects;
package org.wesnoth.builder;
import java.io.Serializable;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
@ -22,8 +24,10 @@ import org.wesnoth.Logger;
* Alternatively, a tree node is created
* when a new resource is added.
*/
public class ProjectDependencyNode
public class ProjectDependencyNode implements Serializable
{
private static final long serialVersionUID = -9140173740211465384L;
/**
* This integer represents the default step between 2 file indexes.
* Since int it's on 4 bytes, it can hold values between
@ -41,7 +45,8 @@ public class ProjectDependencyNode
private ProjectDependencyNode parent_;
private ProjectDependencyNode son_;
private IFile file_;
protected transient IFile file_;
protected String fileName_;
private int index_;
@ -50,6 +55,7 @@ public class ProjectDependencyNode
previous_ = next_ = parent_ = son_ = null;
file_ = file;
fileName_ = file.getProjectRelativePath( ).toString( );
setIndex( index );
}
@ -162,6 +168,6 @@ public class ProjectDependencyNode
@Override
public String toString()
{
return ( file_ == null ? "" : file_.getProjectRelativePath( ).toString( ) ) + "_" + index_;
return ( file_ == null ? "" : fileName_ ) + "_" + index_;
}
}

View file

@ -45,8 +45,8 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
protected void fullBuild(final IProgressMonitor monitor) throws CoreException
{
DependencyTreeBuilder treeBuilder = new DependencyTreeBuilder( getProject( ) );
treeBuilder.createDependencyTree( );
ProjectCache cache = ProjectUtils.getCacheForProject( getProject( ) );
cache.getDependencyTree( ).createDependencyTree( );
// getProject().accept(new ResourceVisitor(monitor));
}

View file

@ -215,7 +215,7 @@ public class PreprocessorUtils
}
/**
* Gets the temporary location where that file should be preprcessed
* Gets the temporary location where that file should be preprocessed
* @param file
* @return
*/
@ -237,8 +237,7 @@ public class PreprocessorUtils
*/
public String getDefinesLocation(IResource resource)
{
return WorkspaceUtils.getTemporaryFolder() +
resource.getProject().getName() +
return WorkspaceUtils.getProjectTemporaryFolder( resource.getProject( ) ) +
"/_MACROS_.cfg"; //$NON-NLS-1$
}

View file

@ -9,6 +9,11 @@
package org.wesnoth.projects;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
@ -17,10 +22,12 @@ import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.wesnoth.Constants;
import org.wesnoth.Logger;
import org.wesnoth.builder.DependencyTreeBuilder;
import org.wesnoth.preferences.Preferences;
import org.wesnoth.preprocessor.Define;
import org.wesnoth.preprocessor.PreprocessorUtils;
import org.wesnoth.utils.ResourceUtils;
import org.wesnoth.utils.WorkspaceUtils;
import org.wesnoth.wml.core.ConfigFile;
import org.wesnoth.wml.core.Variable;
@ -39,9 +46,11 @@ public class ProjectCache
private File wesnothFile_;
private File definesFile_;
private File treeCacheFile_;
private Map< String, ConfigFile > configFiles_;
private Map< String, Define > defines_;
private DependencyTreeBuilder dependTree_;
private IProject project_;
@ -52,13 +61,16 @@ public class ProjectCache
configFiles_ = new HashMap<String, ConfigFile>();
defines_ = new HashMap<String, Define>(0);
dependTree_ = new DependencyTreeBuilder( project_ );
propertiesTimetamp_ = 0;
properties_ = new DialogSettings("project"); //$NON-NLS-1$
wesnothFile_ = new File(project.getLocation().toOSString() +
"/.wesnoth"); //$NON-NLS-1$
definesFile_ = new File (PreprocessorUtils.getInstance().getTemporaryLocation(
project.getFile("_main.cfg")) + "/_MACROS_.cfg"); //$NON-NLS-1$ //$NON-NLS-2$
definesFile_ = new File (PreprocessorUtils.getInstance().getDefinesLocation( project ));
treeCacheFile_ = new File ( WorkspaceUtils.getProjectTemporaryFolder( project ) + "/_TREE_CACHE_.bin" );
ResourceUtils.createWesnothFile(wesnothFile_.getAbsolutePath(), false);
readProperties(true);
@ -82,7 +94,8 @@ public class ProjectCache
try
{
properties_.load(wesnothFile_.getAbsolutePath());
properties_.load( new InputStreamReader( new FileInputStream(
wesnothFile_.getAbsolutePath() ), "UTF-16" ) );
}
catch(ClassCastException ex)
{
@ -90,7 +103,7 @@ public class ProjectCache
// we already have an xml format used by Properties.
// convert it to DialogSettings
ResourceUtils.createWesnothFile(wesnothFile_.getAbsolutePath(), true);
properties_.load(wesnothFile_.getAbsolutePath());
properties_.load( wesnothFile_.getAbsolutePath() );
}
if (properties_.getSection("configs") != null) //$NON-NLS-1$
@ -116,6 +129,14 @@ public class ProjectCache
configFiles_.put(config.get("filename"), tmp); //$NON-NLS-1$
}
}
// unserialize the tree builder
if ( treeCacheFile_.exists( ) ) {
FileInputStream inStream = new FileInputStream( treeCacheFile_ );
ObjectInputStream deserializer = new ObjectInputStream( inStream );
dependTree_.deserialize( deserializer );
}
propertiesTimetamp_ = wesnothFile_.lastModified();
}
catch (Exception e)
@ -198,8 +219,15 @@ public class ProjectCache
}
// store properties
properties_.save(wesnothFile_.getAbsolutePath());
properties_.save( wesnothFile_.getAbsolutePath() );
propertiesTimetamp_ = wesnothFile_.lastModified();
// save the PDT tree
FileOutputStream outStream = new FileOutputStream( treeCacheFile_ );
ObjectOutputStream serializer = new ObjectOutputStream( outStream );
serializer.writeObject( dependTree_ );
serializer.close( );
return true;
}
catch (Exception e)
@ -256,4 +284,13 @@ public class ProjectCache
Constants.P_INST_NAME_PREFIX + project_.getName( ),
newInstallName );
}
/**
* Returns the current dependency tree builder for this project
* @return A dependency tree
*/
public DependencyTreeBuilder getDependencyTree()
{
return dependTree_;
}
}

View file

@ -192,7 +192,7 @@ public class SchemaParser
else if (tagStack.peek().equals("description")) //$NON-NLS-1$
{
String[] tokens = line.trim().split("="); //$NON-NLS-1$
StringBuilder value = new StringBuilder(); //$NON-NLS-1$
StringBuilder value = new StringBuilder();
// this *should* happen only in [description]
// multi-line string

View file

@ -110,7 +110,7 @@ public class TemplateProvider
if (tmpTemplate == null || parameters == null)
return null;
StringBuilder result = new StringBuilder(); //$NON-NLS-1$
StringBuilder result = new StringBuilder();
String[] template = StringUtils.getLines(tmpTemplate);
boolean skipLine;

View file

@ -45,10 +45,9 @@ import org.wesnoth.navigator.WesnothProjectsExplorer;
import org.wesnoth.preferences.Preferences;
import org.wesnoth.projects.ProjectUtils;
public class WorkspaceUtils
{
private static String temporaryFolder_ = null; //$NON-NLS-1$
private static String temporaryFolder_ = null;
/**
* Gets the selected project or or null if none selected
@ -277,6 +276,16 @@ public class WorkspaceUtils
return temporaryFolder_;
}
/**
* Gets the project's temporary folder
* @param project The project for which to compute the temporary folder
* @return
*/
public static String getProjectTemporaryFolder( IProject project )
{
return getTemporaryFolder( ) + project.getName( );
}
/**
* Returns a random fileName generated from current time
* @return