From 77b3e42c12ea0a6403e683a8ed56f55e2302b5cb Mon Sep 17 00:00:00 2001 From: Timotei Dolean Date: Tue, 5 Jul 2011 08:54:09 +0000 Subject: [PATCH] eclipse plugin: Refactor the IFile to WMLRoot... ...conversion to its own method --- .../org.wesnoth.wml/META-INF/MANIFEST.MF | 5 +++- .../umc_dev/org.wesnoth/META-INF/MANIFEST.MF | 3 ++ .../builder/DependencyTreeBuilder.java | 6 ++-- .../src/org/wesnoth/handlers/TestHandler.java | 12 ++------ .../src/org/wesnoth/utils/ResourceUtils.java | 28 +++++++++++++++++++ 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/utils/umc_dev/org.wesnoth.wml/META-INF/MANIFEST.MF b/utils/umc_dev/org.wesnoth.wml/META-INF/MANIFEST.MF index d2fe7ae27a0..0d3ecf340bf 100644 --- a/utils/umc_dev/org.wesnoth.wml/META-INF/MANIFEST.MF +++ b/utils/umc_dev/org.wesnoth.wml/META-INF/MANIFEST.MF @@ -26,10 +26,13 @@ Require-Bundle: org.eclipse.xtext, Import-Package: org.apache.log4j Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Export-Package: org.wesnoth, - org.wesnoth.services, + org.wesnoth.formatting, org.wesnoth.parseTreeConstruction, org.wesnoth.parser.antlr, org.wesnoth.parser.antlr.internal, + org.wesnoth.scoping, + org.wesnoth.services, + org.wesnoth.tests, org.wesnoth.validation, org.wesnoth.wml, org.wesnoth.wml.impl, diff --git a/utils/umc_dev/org.wesnoth/META-INF/MANIFEST.MF b/utils/umc_dev/org.wesnoth/META-INF/MANIFEST.MF index 97535a0c2b7..624c78af451 100644 --- a/utils/umc_dev/org.wesnoth/META-INF/MANIFEST.MF +++ b/utils/umc_dev/org.wesnoth/META-INF/MANIFEST.MF @@ -57,3 +57,6 @@ Export-Package: org.wesnoth, org.wesnoth.wml.core, org.wesnoth.wml.schema, org.wesnoth.wml.schema.impl +Import-Package: org.wesnoth.wml, + org.wesnoth.wml.impl, + org.wesnoth.wml.util diff --git a/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/DependencyTreeBuilder.java b/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/DependencyTreeBuilder.java index 3efdeeef7eb..fdb1ff10964 100644 --- a/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/DependencyTreeBuilder.java +++ b/utils/umc_dev/org.wesnoth/src/org/wesnoth/builder/DependencyTreeBuilder.java @@ -24,6 +24,8 @@ import org.eclipse.core.runtime.CoreException; import org.wesnoth.Logger; import org.wesnoth.builder.WesnothProjectBuilder.WMLFilesComparator; import org.wesnoth.projects.ProjectDependencyNode; +import org.wesnoth.utils.ResourceUtils; +import org.wesnoth.wml.WMLRoot; public class DependencyTreeBuilder { @@ -53,9 +55,9 @@ public class DependencyTreeBuilder IResource main_cfg = container.findMember( "_main.cfg" ); if ( main_cfg != null ) { // add main.cfg to tree + WMLRoot root = ResourceUtils.getWMLRoot( ( IFile ) main_cfg ); - //TODO process the other children depending on the contents - // of the file + // iterate to find macro calls that include other dirs }else { List members = null; try { diff --git a/utils/umc_dev/org.wesnoth/src/org/wesnoth/handlers/TestHandler.java b/utils/umc_dev/org.wesnoth/src/org/wesnoth/handlers/TestHandler.java index f5b8977cfb3..03fddf15aa0 100644 --- a/utils/umc_dev/org.wesnoth/src/org/wesnoth/handlers/TestHandler.java +++ b/utils/umc_dev/org.wesnoth/src/org/wesnoth/handlers/TestHandler.java @@ -12,11 +12,7 @@ import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.resources.IFile; -import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.emf.ecore.resource.ResourceSet; -import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.wesnoth.utils.ResourceUtils; import org.wesnoth.utils.WorkspaceUtils; /** @@ -29,11 +25,7 @@ public class TestHandler extends AbstractHandler public Object execute(ExecutionEvent event) throws ExecutionException { IFile file = WorkspaceUtils.getSelectedFile( ); - URI uri = URI.createPlatformResourceURI( file.getFullPath( ).toString( ), true ); - ResourceSet resSet = new ResourceSetImpl( ); - Resource res = resSet.getResource( uri, true ); - EObject obj = res.getContents( ).get( 0 ); - System.out.println( obj); + System.out.println( ResourceUtils.getWMLRoot( file )); // IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new Path("E:\\work\\java\\runtime-EclipseApplication\\A_Simple_Campaign\\scenarios\\atemplate.cfg")); // try diff --git a/utils/umc_dev/org.wesnoth/src/org/wesnoth/utils/ResourceUtils.java b/utils/umc_dev/org.wesnoth/src/org/wesnoth/utils/ResourceUtils.java index 4a7bc745689..273fe0f90b2 100644 --- a/utils/umc_dev/org.wesnoth/src/org/wesnoth/utils/ResourceUtils.java +++ b/utils/umc_dev/org.wesnoth/src/org/wesnoth/utils/ResourceUtils.java @@ -34,6 +34,11 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.jface.dialogs.DialogSettings; import org.eclipse.swt.SWT; import org.wesnoth.Constants; @@ -43,6 +48,7 @@ import org.wesnoth.preprocessor.PreprocessorUtils; import org.wesnoth.projects.ProjectUtils; import org.wesnoth.templates.ReplaceableParameter; import org.wesnoth.templates.TemplateProvider; +import org.wesnoth.wml.WMLRoot; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; @@ -476,4 +482,26 @@ public class ResourceUtils return null; } } + + /** + * Gets the WML Grammar root of the specified file + * @param file The file to get the WML model from + * @return A WMLRoot instance or null if there is none + */ + public static WMLRoot getWMLRoot( IFile file ) + { + URI uri = URI.createPlatformResourceURI( file.getFullPath( ).toString( ), true ); + ResourceSet resourceSet = new ResourceSetImpl( ); + Resource resource = resourceSet.getResource( uri, true ); + if ( resource == null || + resource.getContents( ).isEmpty( ) ) + return null; + + EObject result = resource.getContents( ).get( 0 ); + + if ( result instanceof WMLRoot == false ) + return null; + + return ( WMLRoot ) result; + } } \ No newline at end of file