eclipse plugin: Create a simple wml parser that...

...parses the xtext's resource of a cfg file into a WMLConfig
structure
This commit is contained in:
Timotei Dolean 2011-07-26 15:26:04 +00:00
parent f7d2eac15b
commit c275ae48be
2 changed files with 98 additions and 61 deletions

View file

@ -26,8 +26,6 @@ import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.wesnoth.Constants;
import org.wesnoth.Logger;
import org.wesnoth.Messages;
@ -41,12 +39,9 @@ import org.wesnoth.utils.AntUtils;
import org.wesnoth.utils.ExternalToolInvoker;
import org.wesnoth.utils.ResourceUtils;
import org.wesnoth.utils.StringUtils;
import org.wesnoth.utils.WMLUtils;
import org.wesnoth.utils.WMLTools;
import org.wesnoth.utils.WorkspaceUtils;
import org.wesnoth.wml.WMLKey;
import org.wesnoth.wml.WMLRoot;
import org.wesnoth.wml.WMLTag;
import org.wesnoth.wml.core.SimpleWMLParser;
import org.wesnoth.wml.core.WMLConfig;
/**
@ -316,62 +311,9 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
monitor.subTask( String.format( Messages.WesnothProjectBuilder_22, filePath ) );
WMLConfig config = projectCache_.getWMLConfig( filePath );
WMLRoot root = ResourceUtils.getWMLRoot( file );
TreeIterator<EObject> itor = root.eAllContents( );
WMLTag currentTag = null;
String currentTagName = "";
SimpleWMLParser parser = new SimpleWMLParser( file, config );
parser.parse( );
while ( itor.hasNext( ) ) {
EObject object = itor.next( );
if ( object instanceof WMLTag ) {
currentTag = ( WMLTag ) object;
currentTagName = currentTag.getName( );
if ( currentTagName.equals( "scenario" ) )
config.IsScenario = true;
else if ( currentTagName.equals( "campaign" ) )
config.IsCampaign = true;
}
else if ( object instanceof WMLKey ) {
if ( currentTag != null ) {
WMLKey key = ( WMLKey ) object;
String keyName = key.getName( );
if ( keyName.equals( "id" ) ) {
if ( currentTagName.equals( "scenario" ) )
config.ScenarioId = WMLUtils.getKeyValue( key.getValue( ) );
else if ( currentTagName.equals( "campaign" ) )
config.CampaignId = WMLUtils.getKeyValue( key.getValue( ) );
}
}
}
}
System.out.println( "Config: " + config );
// WMLSaxHandler handler = (WMLSaxHandler) ResourceUtils.
// getWMLSAXHandlerFromResource(
// PreprocessorUtils.getInstance().getPreprocessedFilePath(file, false, false).toString(),
// new WMLSaxHandler(file.getLocation().toOSString()));
//
// if (handler != null)
// {
// WMLConfig cfg = handler.getConfigFile();
// projectCache_.getWMLConfigs().put( file.getProjectRelativePath( ).toString( ), cfg);
// if (cfg.IsScenario)
// {
// if ( StringUtils.isNullOrEmpty( cfg.ScenarioId ) )
// {
// Logger.getInstance().log("added scenarioId [" + cfg.ScenarioId + //$NON-NLS-1$
// "] for file: " + filePath ); //$NON-NLS-1$
// }
// else
// {
// projectCache_.getWMLConfigs().remove( filePath );
// }
// }
// }
monitor.worked(10);
} catch (Exception e)

View file

@ -0,0 +1,95 @@
/*******************************************************************************
* Copyright (c) 2011 by Timotei Dolean <timotei21@gmail.com>
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.wesnoth.wml.core;
import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.wesnoth.utils.ResourceUtils;
import org.wesnoth.utils.WMLUtils;
import org.wesnoth.wml.WMLKey;
import org.wesnoth.wml.WMLRoot;
import org.wesnoth.wml.WMLTag;
/**
* A simple WML Parser that parses a xtext WML Config file resource
*/
public class SimpleWMLParser
{
protected WMLConfig config_;
protected IFile file_;
/**
* Creates a new parser for the specified file
*/
public SimpleWMLParser( IFile file )
{
file_ = file;
config_ = new WMLConfig( file.getProjectRelativePath( ).toString( ) );
}
/**
* Creates a new parser and fills the specified config file
*/
public SimpleWMLParser( IFile file, WMLConfig config )
{
if ( config == null )
throw new IllegalArgumentException( "The config must not be null." );
config_ = config;
file_ = file;
}
/**
* Parses the config. The results will be available in {@link #getParsedConfig()}
*/
public void parse()
{
WMLRoot root = ResourceUtils.getWMLRoot( file_ );
TreeIterator<EObject> itor = root.eAllContents( );
WMLTag currentTag = null;
String currentTagName = "";
while ( itor.hasNext( ) ) {
EObject object = itor.next( );
if ( object instanceof WMLTag ) {
currentTag = ( WMLTag ) object;
currentTagName = currentTag.getName( );
if ( currentTagName.equals( "scenario" ) )
config_.IsScenario = true;
else if ( currentTagName.equals( "campaign" ) )
config_.IsCampaign = true;
}
else if ( object instanceof WMLKey ) {
if ( currentTag != null ) {
WMLKey key = ( WMLKey ) object;
String keyName = key.getName( );
if ( keyName.equals( "id" ) ) {
if ( currentTagName.equals( "scenario" ) )
config_.ScenarioId = WMLUtils.getKeyValue( key.getValue( ) );
else if ( currentTagName.equals( "campaign" ) )
config_.CampaignId = WMLUtils.getKeyValue( key.getValue( ) );
}
}
}
}
System.out.println( "parsed config: " + config_ );
}
public WMLConfig getParsedConfig()
{
return config_;
}
}