eclipse plugin: schema-based wizards: step 1 - schema parser
This commit is contained in:
parent
cff59652ff
commit
eb88b95683
3 changed files with 304 additions and 0 deletions
|
@ -0,0 +1,234 @@
|
|||
/**
|
||||
* @author Timotei Dolean
|
||||
*
|
||||
*/
|
||||
package wesnoth_eclipse_plugin.wizards.generator;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Stack;
|
||||
|
||||
import wesnoth_eclipse_plugin.utils.ResourceUtils;
|
||||
import wesnoth_eclipse_plugin.utils.StringUtils;
|
||||
|
||||
public class SchemaParser
|
||||
{
|
||||
private static SchemaParser instance_;
|
||||
|
||||
public static SchemaParser getInstance()
|
||||
{
|
||||
if (instance_ == null)
|
||||
instance_ = new SchemaParser();
|
||||
return instance_;
|
||||
}
|
||||
|
||||
private HashMap<String, String> primitives_ = new HashMap<String, String>();
|
||||
private HashMap<String, Tag> tags_ = new HashMap<String, Tag>();
|
||||
private boolean parsingDone_ = false;
|
||||
|
||||
public void parseSchema(boolean force)
|
||||
{
|
||||
if (parsingDone_ && !force)
|
||||
return;
|
||||
|
||||
String res = ResourceUtils.getFileContents(new File("D:\\timo\\gw\\data\\schema.cfg"));
|
||||
String[] lines = StringUtils.getLines(res);
|
||||
Stack<String> tagStack = new Stack<String>();
|
||||
|
||||
// temporarly add 'string' and 'tstring' primitives
|
||||
primitives_.put("string", "\"[\\d\\w\\s]\"");
|
||||
primitives_.put("tstring", "_\"[\\d\\w\\s]\"");
|
||||
|
||||
Tag currentTag = null;
|
||||
for (int index = 0; index < lines.length; index++)
|
||||
{
|
||||
String line = lines[index];
|
||||
// skip comments and empty lines
|
||||
if (StringUtils.startsWith(line, "#") || line.matches("^[\t ]*$"))
|
||||
continue;
|
||||
|
||||
if (StringUtils.startsWith(line, "["))
|
||||
{
|
||||
if (line.charAt(line.indexOf("[") + 1) == '/')
|
||||
{
|
||||
tagStack.pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
String tagName = line.substring(line.indexOf("[") + 1, line.indexOf("]"));
|
||||
String simpleTagName = tagName;
|
||||
String extendedTagName = "";
|
||||
if (tagName.split(":").length > 1)
|
||||
{
|
||||
simpleTagName = tagName.split(":")[0];
|
||||
extendedTagName = tagName.split(":")[1];
|
||||
}
|
||||
tagStack.push(simpleTagName);
|
||||
|
||||
if (!tagName.equals("description"))
|
||||
{
|
||||
System.out.println(simpleTagName);
|
||||
if (tags_.containsKey(simpleTagName))
|
||||
{
|
||||
currentTag = tags_.get(simpleTagName);
|
||||
currentTag.ExtendedTagName = extendedTagName;
|
||||
}
|
||||
else
|
||||
{
|
||||
Tag tag = new Tag(simpleTagName, extendedTagName);
|
||||
currentTag = tag;
|
||||
currentTag.NeedsExpanding = false;
|
||||
tags_.put(simpleTagName, tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// skip descriptions for now
|
||||
if (tagStack.peek().equals("description"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// top level - primitives defined
|
||||
if (tagStack.peek().equals("schema"))
|
||||
{
|
||||
String[] tokens = line.split("=");
|
||||
if (tokens.length != 2)
|
||||
{
|
||||
System.out.println("Error. invalid line :" + index);
|
||||
continue; //return;
|
||||
}
|
||||
primitives_.put(tokens[0].trim(), tokens[1].trim());
|
||||
//System.out.printf("[%s][%s]\n", tokens[0].trim(), tokens[1].trim());
|
||||
}
|
||||
else
|
||||
{
|
||||
String tmpLine = line.trim();
|
||||
if (line.contains("#"))
|
||||
tmpLine = line.substring(0, line.lastIndexOf("#")).trim();
|
||||
String[] tokens = tmpLine.split("=");
|
||||
|
||||
if (tokens.length != 2)
|
||||
{
|
||||
System.out.println("Error. invalid line :" + index);
|
||||
continue; //return;
|
||||
}
|
||||
|
||||
// // this *should* happen only in [description]
|
||||
// // multi-line string
|
||||
// String value = tokens[1];
|
||||
// if (StringUtils.countOf(value, '"') % 2 != 0)
|
||||
// {
|
||||
// ++index;
|
||||
// while (StringUtils.countOf(lines[index], '"') % 2 == 0 &&
|
||||
// !StringUtils.startsWith(lines[index], "#") &&
|
||||
// index < lines.length)
|
||||
// {
|
||||
// value += (lines[index] + "\n");
|
||||
// ++index;
|
||||
// }
|
||||
// value += lines[index];
|
||||
// }
|
||||
|
||||
String[] value = tokens[1].substring(1, tokens[1].length() - 1).split(" ");
|
||||
if (value.length != 2)
|
||||
{
|
||||
System.out.println("Error. invalid line :" + index);
|
||||
continue; //return;
|
||||
}
|
||||
|
||||
if (currentTag != null)//tags.containsKey(tagStack.peek()))
|
||||
{
|
||||
if (tokens[0].startsWith("_")) // reference to another tag
|
||||
{
|
||||
Tag targetTag = null;
|
||||
if (tags_.containsKey(value[1]))
|
||||
targetTag = tags_.get(value[1]);
|
||||
else
|
||||
// tag wasn't created yet
|
||||
{
|
||||
targetTag = new Tag(value[1]);
|
||||
System.err.println("creating missing tag: " + value[1]);
|
||||
tags_.put(value[1], targetTag);
|
||||
}
|
||||
|
||||
currentTag.addTag(targetTag);
|
||||
currentTag.NeedsExpanding = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(primitives_.containsKey(value[1])))
|
||||
currentTag.NeedsExpanding = true;
|
||||
currentTag.addKey(tokens[0], value[1], getCardinality(value[0]));
|
||||
}
|
||||
}
|
||||
else
|
||||
System.err.println("can't find entry for: " + tagStack.peek());
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("End parsing");
|
||||
parsingDone_ = true;
|
||||
//
|
||||
// try
|
||||
// {
|
||||
// BufferedWriter bw = new BufferedWriter(new PrintWriter(new File("D:\\timo\\gw\\data\\schema-out.cfg")));
|
||||
// // print primitives
|
||||
// for (Entry<String, String> primitive : primitives.entrySet())
|
||||
// {
|
||||
// bw.write(primitive.getKey() + ": " + primitive.getValue() + "\n");
|
||||
// }
|
||||
// // print tags
|
||||
// Tag root = tags.get("root");
|
||||
// for (Tag tag : root.TagChildren)
|
||||
// {
|
||||
// bw.write(getOutput(tag, ""));
|
||||
// }
|
||||
// bw.close();
|
||||
// } catch (Exception e)
|
||||
// {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// System.out.println("End writing result");
|
||||
}
|
||||
|
||||
public HashMap<String, Tag> getTags()
|
||||
{
|
||||
return tags_;
|
||||
}
|
||||
|
||||
public HashMap<String, String> getPrimitives()
|
||||
{
|
||||
return primitives_;
|
||||
}
|
||||
|
||||
public String getOutput(Tag tag, String indent)
|
||||
{
|
||||
String res = indent + "[" + tag.Name + "]\n";
|
||||
for (TagKey key : tag.KeyChildren)
|
||||
{
|
||||
res += (indent + "\t" + key.Name + "=" + key.Regex + "\n");
|
||||
}
|
||||
for (Tag tmpTag : tag.TagChildren)
|
||||
{
|
||||
res += (getOutput(tmpTag, indent + "\t"));
|
||||
}
|
||||
res += (indent + "[/" + tag.Name + "]\n");
|
||||
return res;
|
||||
}
|
||||
|
||||
public char getCardinality(String value)
|
||||
{
|
||||
if (value.equals("required"))
|
||||
return '1';
|
||||
else if (value.equals("optional"))
|
||||
return '?';
|
||||
else if (value.equals("repeated"))
|
||||
return '*';
|
||||
else if (value.equals("forbidden"))
|
||||
return '-';
|
||||
return 'a';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/**
|
||||
* @author Timotei Dolean
|
||||
*
|
||||
*/
|
||||
package wesnoth_eclipse_plugin.wizards.generator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Tag
|
||||
{
|
||||
public String Name;
|
||||
public String ExtendedTagName = "";
|
||||
public List<Tag> TagChildren;
|
||||
public List<TagKey> KeyChildren;
|
||||
|
||||
public boolean NeedsExpanding = false;
|
||||
|
||||
public Tag(String name, List<Tag> tagChildren, List<TagKey> keyChildren) {
|
||||
Name = name;
|
||||
TagChildren = tagChildren;
|
||||
KeyChildren = keyChildren;
|
||||
}
|
||||
|
||||
public Tag(String name) {
|
||||
Name = name;
|
||||
TagChildren = new ArrayList<Tag>();
|
||||
KeyChildren = new ArrayList<TagKey>();
|
||||
}
|
||||
|
||||
public Tag(String name, String extendedTagName) {
|
||||
Name = name;
|
||||
ExtendedTagName = extendedTagName;
|
||||
TagChildren = new ArrayList<Tag>();
|
||||
KeyChildren = new ArrayList<TagKey>();
|
||||
}
|
||||
|
||||
public void addTag(Tag tag)
|
||||
{
|
||||
TagChildren.add(tag);
|
||||
}
|
||||
|
||||
public void addKey(TagKey key)
|
||||
{
|
||||
KeyChildren.add(key);
|
||||
}
|
||||
|
||||
public void addKey(String name, String regex, char cardinality)
|
||||
{
|
||||
addKey(new TagKey(name, cardinality, regex));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
/**
|
||||
* @author Timotei Dolean
|
||||
*
|
||||
*/
|
||||
package wesnoth_eclipse_plugin.wizards.generator;
|
||||
|
||||
public class TagKey
|
||||
{
|
||||
public String Name;
|
||||
public char Cardinality;
|
||||
public String Regex;
|
||||
|
||||
public TagKey(String name, char cardinality, String regex) {
|
||||
Name = name;
|
||||
Cardinality = cardinality;
|
||||
Regex = regex;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue