mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-19 14:49:07 +00:00
eclipse plugin: add parsing of macro defines from file
This commit is contained in:
parent
4ae7962271
commit
5ca89cbe27
@ -199,8 +199,10 @@ public class WesnothProjectBuilder extends IncrementalProjectBuilder
|
||||
|
||||
if (ResourceUtils.isScenarioFile(file.getLocation().toOSString()))
|
||||
{
|
||||
WMLSaxHandler handler = ResourceUtils.
|
||||
getParsedWMLFromResource(PreprocessorUtils.getPreprocessedFilePath(file, false, false).toString());
|
||||
WMLSaxHandler handler = (WMLSaxHandler) ResourceUtils.
|
||||
getWMLSAXHandlerFromResource(
|
||||
PreprocessorUtils.getPreprocessedFilePath(file, false, false).toString(),
|
||||
new WMLSaxHandler());
|
||||
if (handler == null || handler.ScenarioId == null)
|
||||
{
|
||||
projCache.getScenarios().remove(file.getName());
|
||||
|
@ -21,6 +21,10 @@ public class TestHandler extends AbstractHandler
|
||||
@Override
|
||||
public Object execute(ExecutionEvent event) throws ExecutionException
|
||||
{
|
||||
// DefinesSAXHandler handler = (DefinesSAXHandler) ResourceUtils.getWMLSAXHandlerFromResource(
|
||||
// "d:/tmp/wesnoth_plugin/a/_MACROS_.cfg",
|
||||
// new DefinesSAXHandler());
|
||||
// System.out.println(handler.getDefines().size());
|
||||
// try
|
||||
// {
|
||||
// BuildCommand c = ((BuildCommand)WorkspaceUtils.getSelectedProject().getDescription().getBuildSpec()[1]);
|
||||
|
@ -0,0 +1,59 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010 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 wesnoth_eclipse_plugin.preprocessor;
|
||||
|
||||
/**
|
||||
* This represents a WML preprocessor define:
|
||||
* [preproc_define]
|
||||
* name = ""
|
||||
* value = ""
|
||||
* textdomain = ""
|
||||
* linenum = ""
|
||||
* location = ""
|
||||
* [/preproc_define]
|
||||
*/
|
||||
public class Define
|
||||
{
|
||||
private String name_;
|
||||
private String value_;
|
||||
private String textdomain_;
|
||||
private int lineNum_;
|
||||
private String location_;
|
||||
|
||||
public Define(String name, String value, String textdomain,
|
||||
int linenum, String location)
|
||||
{
|
||||
name_ = name;
|
||||
value_ = value;
|
||||
textdomain_ = textdomain;
|
||||
lineNum_ = linenum;
|
||||
location_ = location;
|
||||
}
|
||||
|
||||
public int getLineNum()
|
||||
{
|
||||
return lineNum_;
|
||||
}
|
||||
public String getLocation()
|
||||
{
|
||||
return location_;
|
||||
}
|
||||
public String getName()
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
public String getTextdomain()
|
||||
{
|
||||
return textdomain_;
|
||||
}
|
||||
public String getValue()
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010 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 wesnoth_eclipse_plugin.preprocessor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import wesnoth_eclipse_plugin.utils.ResourceUtils;
|
||||
|
||||
public class DefinesCache
|
||||
{
|
||||
private static List<Define> defines_;
|
||||
|
||||
public static void readDefines(String file)
|
||||
{
|
||||
DefinesSAXHandler handler = (DefinesSAXHandler) ResourceUtils.
|
||||
getWMLSAXHandlerFromResource(file, new DefinesSAXHandler());
|
||||
defines_ = handler.getDefines();
|
||||
}
|
||||
|
||||
public static List<Define> getDefines()
|
||||
{
|
||||
return defines_;
|
||||
}
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2010 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 wesnoth_eclipse_plugin.preprocessor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
/**
|
||||
* A sax handler that parses files that contain [preproc_define]s
|
||||
*/
|
||||
public class DefinesSAXHandler extends DefaultHandler
|
||||
{
|
||||
private static Stack<String> stack_;
|
||||
private List<Define> defines_;
|
||||
|
||||
// indexes for different define properties
|
||||
private String name_;
|
||||
private String value_;
|
||||
private String textdomain_;
|
||||
private int linenum_;
|
||||
private String location_;
|
||||
|
||||
public DefinesSAXHandler()
|
||||
{
|
||||
stack_ = new Stack<String>();
|
||||
defines_ = new ArrayList<Define>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startElement(String uri, String localName, String qName,
|
||||
Attributes attributes) throws SAXException
|
||||
{
|
||||
super.startElement(uri, localName, qName, attributes);
|
||||
stack_.push(qName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endElement(String uri, String localName, String qName)
|
||||
throws SAXException
|
||||
{
|
||||
super.endElement(uri, localName, qName);
|
||||
stack_.pop();
|
||||
if (qName.equals("preproc_define"))
|
||||
{
|
||||
// create the define
|
||||
defines_.add(new Define(name_, value_, textdomain_, linenum_, location_));
|
||||
// reset values
|
||||
resetValues();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void characters(char[] ch, int start, int length)
|
||||
throws SAXException
|
||||
{
|
||||
super.characters(ch, start, length);
|
||||
if (stack_.peek().equals("name"))
|
||||
{
|
||||
name_ = new String(ch, start, length);
|
||||
}
|
||||
else if (stack_.peek().equals("value"))
|
||||
{
|
||||
value_ = new String(ch, start, length);
|
||||
}
|
||||
else if (stack_.peek().equals("textdomain"))
|
||||
{
|
||||
textdomain_ = new String(ch, start, length);
|
||||
}
|
||||
else if (stack_.peek().equals("linenum"))
|
||||
{
|
||||
linenum_ = Integer.valueOf(new String(ch, start, length));
|
||||
}
|
||||
else if (stack_.peek().equals("location"))
|
||||
{
|
||||
location_ = new String(ch, start, length);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* resets indexes for to be used by the next define
|
||||
*/
|
||||
private void resetValues()
|
||||
{
|
||||
name_ = "";
|
||||
value_ = "";
|
||||
linenum_ = 0;
|
||||
location_ = "";
|
||||
textdomain_ = "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of defines parsed
|
||||
* @return
|
||||
*/
|
||||
public List<Define> getDefines()
|
||||
{
|
||||
return defines_;
|
||||
}
|
||||
}
|
@ -18,7 +18,6 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.parsers.SAXParser;
|
||||
@ -36,6 +35,7 @@ import org.eclipse.core.runtime.Path;
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
import wesnoth_eclipse_plugin.Logger;
|
||||
import wesnoth_eclipse_plugin.templates.ReplaceableParameter;
|
||||
@ -352,9 +352,10 @@ public class ResourceUtils
|
||||
*/
|
||||
public static String getCampaignID(IResource resource)
|
||||
{
|
||||
WMLSaxHandler handler = getParsedWMLFromResource(
|
||||
WMLSaxHandler handler = (WMLSaxHandler) getWMLSAXHandlerFromResource(
|
||||
PreprocessorUtils.getPreprocessedFilePath(
|
||||
getMainConfigLocation(resource), false, true).toString());
|
||||
getMainConfigLocation(resource), false, true).toString(),
|
||||
new WMLSaxHandler());
|
||||
if (handler == null)
|
||||
return null;
|
||||
return handler.CampaignId;
|
||||
@ -367,31 +368,32 @@ public class ResourceUtils
|
||||
*/
|
||||
public static String getScenarioID(IFile file)
|
||||
{
|
||||
WMLSaxHandler handler = getParsedWMLFromResource(
|
||||
PreprocessorUtils.getPreprocessedFilePath(file, false, true).toString());
|
||||
WMLSaxHandler handler = (WMLSaxHandler) getWMLSAXHandlerFromResource(
|
||||
PreprocessorUtils.getPreprocessedFilePath(file, false, true).toString(),
|
||||
new WMLSaxHandler());
|
||||
if (handler == null)
|
||||
return null;
|
||||
return handler.ScenarioId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the WMLSaxHandler for the parsed specified resource
|
||||
* Returns the SaxHandler for the parsed specified wml resource
|
||||
* @param resourcePath The resourcepath to parse
|
||||
* @param saxHandler The SAX Handler used to handle the parsed wml
|
||||
* @return
|
||||
*/
|
||||
public static WMLSaxHandler getParsedWMLFromResource(String resourcePath)
|
||||
public static DefaultHandler getWMLSAXHandlerFromResource(String resourcePath,
|
||||
DefaultHandler saxHandler)
|
||||
{
|
||||
ExternalToolInvoker parser = WMLTools.runWMLParser2(resourcePath);
|
||||
if (parser == null)
|
||||
return null;
|
||||
try{
|
||||
parser.waitForTool();
|
||||
SAXParser saxparser;
|
||||
saxparser = SAXParserFactory.newInstance().newSAXParser();
|
||||
|
||||
WMLSaxHandler handler = new WMLSaxHandler();
|
||||
saxparser.parse(new InputSource(new StringReader(parser.getOutputContent())), handler);
|
||||
return handler;
|
||||
saxparser.parse(new InputSource(parser.getStdout()), saxHandler);
|
||||
return saxHandler;
|
||||
}
|
||||
catch (SAXException e) {
|
||||
Logger.getInstance().logException(e);
|
||||
|
@ -67,7 +67,7 @@ public class WMLTools
|
||||
}
|
||||
arguments.add(resourcePath);
|
||||
}
|
||||
return runPythonScript(arguments, stdin,stdout,stderr);
|
||||
return runPythonScript(arguments, stdin, true, true, stdout,stderr);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,7 +109,7 @@ public class WMLTools
|
||||
arguments.add("-i");
|
||||
arguments.add(resourcePath);
|
||||
|
||||
return runPythonScript(arguments, null, null, null);
|
||||
return runPythonScript(arguments, null, false, false, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -162,7 +162,7 @@ public class WMLTools
|
||||
Path.SEPARATOR + "data/core");
|
||||
arguments.add(resourcePath);
|
||||
|
||||
return runPythonScript(arguments, null, stdout,stderr);
|
||||
return runPythonScript(arguments, null, true, true, stdout,stderr);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -216,7 +216,7 @@ public class WMLTools
|
||||
Path.SEPARATOR + "data/core");
|
||||
arguments.add(resourcePath);
|
||||
|
||||
return runPythonScript(arguments, null, stdout, stderr);
|
||||
return runPythonScript(arguments, null, true, true, stdout, stderr);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -387,7 +387,7 @@ public class WMLTools
|
||||
|
||||
arguments.add("-u");
|
||||
arguments.add(containerPath);
|
||||
return runPythonScript(arguments, null, stdout,stderr);
|
||||
return runPythonScript(arguments, null, true, true, stdout, stderr);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -439,12 +439,15 @@ public class WMLTools
|
||||
* @return
|
||||
*/
|
||||
public static ExternalToolInvoker runPythonScript(List<String> arguments, String stdin,
|
||||
boolean stderrMonitoring, boolean stdoutMonitoring,
|
||||
final OutputStream[] stdout, final OutputStream[] stderr)
|
||||
{
|
||||
final ExternalToolInvoker pyscript = new ExternalToolInvoker("python", arguments);
|
||||
|
||||
pyscript.runTool();
|
||||
if (stderrMonitoring == true)
|
||||
pyscript.startErrorMonitor();
|
||||
if (stdoutMonitoring == true)
|
||||
pyscript.startOutputMonitor();
|
||||
if (stdin != null)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user