Permalink
Browse files

Make some map files optional.

  • Loading branch information...
1 parent 73e79b9 commit bdbf9319ec8720fb128cb0c5c97aaa65268473d2 @ueno committed Apr 16, 2012
Showing with 50 additions and 25 deletions.
  1. +16 −16 libskk/map-file.vala
  2. +1 −1 libskk/rom-kana.vala
  3. +33 −8 libskk/rule.vala
View
@@ -34,19 +34,13 @@ namespace Skk {
}
}
- void load (string rule,
+ void load (RuleMetadata metadata,
string type,
string name,
Set<string> included) throws RuleParseError
{
- var metadata = Rule.find_rule (rule);
- if (metadata == null) {
- throw new RuleParseError.FAILED ("can't find rule %s", rule);
- }
- var filename = Path.build_filename (metadata.base_dir,
- type,
- name + ".json");
- if (!FileUtils.test (filename, FileTest.EXISTS)) {
+ var filename = metadata.locate_map_file (type, name);
+ if (filename == null) {
throw new RuleParseError.FAILED ("no such file %s", filename);
}
@@ -80,15 +74,21 @@ namespace Skk {
throw new RuleParseError.FAILED (
"found circular include of %s", parent);
}
+ string parent_rule, parent_name;
var index = parent.index_of ("/");
if (index < 0) {
- load (rule, type, parent, included);
+ parent_rule = metadata.name;
+ parent_name = parent;
} else {
- load (parent[0:index],
- type,
- parent[index + 1:parent.length],
- included);
+ parent_rule = parent[0:index];
+ parent_name = parent[index + 1:parent.length];
+ }
+ var parent_metadata = Rule.find_rule (parent_rule);
+ if (parent_metadata == null) {
+ throw new RuleParseError.FAILED ("can't find rule %s",
+ parent_rule);
}
+ load (parent_metadata, type, parent_name, included);
included.add (parent);
}
}
@@ -116,12 +116,12 @@ namespace Skk {
}
}
- internal MapFile (string rule,
+ internal MapFile (RuleMetadata metadata,
string type,
string name) throws RuleParseError
{
Set<string> included = new HashSet<string> ();
- load (rule, type, name, included);
+ load (metadata, type, name, included);
}
internal bool has_map (string name) {
View
@@ -192,7 +192,7 @@ namespace Skk {
public RomKanaConverter () {
try {
- _rule = new RomKanaMapFile ("default");
+ _rule = new RomKanaMapFile (Rule.find_rule ("default"));
current_node = _rule.root_node;
} catch (RuleParseError e) {
warning ("can't find default rom-kana rule: %s",
View
@@ -21,9 +21,9 @@ namespace Skk {
class KeymapMapFile : MapFile {
internal Keymap keymap;
- internal KeymapMapFile (string name, string mode) throws RuleParseError
+ internal KeymapMapFile (RuleMetadata metadata, string mode) throws RuleParseError
{
- base (name, "keymap", mode);
+ base (metadata, "keymap", mode);
if (has_map ("keymap")) {
var map = get ("keymap");
keymap = new Keymap ();
@@ -79,8 +79,8 @@ namespace Skk {
return node;
}
- public RomKanaMapFile (string name) throws RuleParseError {
- base (name, "rom-kana", "default");
+ public RomKanaMapFile (RuleMetadata metadata) throws RuleParseError {
+ base (metadata, "rom-kana", "default");
if (has_map ("rom-kana")) {
root_node = parse_rule (get ("rom-kana"));
} else {
@@ -121,6 +121,22 @@ namespace Skk {
* Name of key event filter.
*/
string filter;
+
+ /**
+ * Return the path of the map file.
+ *
+ * @param type type of the map file
+ * @param name name of the map file
+ *
+ * @return the absolute path of the map file
+ */
+ public string? locate_map_file (string type, string name) {
+ var filename = Path.build_filename (base_dir, type, name + ".json");
+ if (FileUtils.test (filename, FileTest.EXISTS)) {
+ return filename;
+ }
+ return null;
+ }
}
/**
@@ -134,7 +150,7 @@ namespace Skk {
internal KeymapMapFile[] keymaps = new KeymapMapFile[InputMode.LAST];
internal RomKanaMapFile rom_kana;
- static const Entry<InputMode,string>[] keymap_entries = {
+ static const Entry<InputMode,string>[] keymap_names = {
{ InputMode.HIRAGANA, "hiragana" },
{ InputMode.KATAKANA, "katakana" },
{ InputMode.HANKAKU_KATAKANA, "hankaku-katakana" },
@@ -236,11 +252,19 @@ namespace Skk {
}
this.metadata = metadata;
- foreach (var entry in keymap_entries) {
- keymaps[entry.key] = new KeymapMapFile (name, entry.value);
+ var default_metadata = find_rule ("default");
+ foreach (var entry in keymap_names) {
+ var _metadata = metadata;
+ if (metadata.locate_map_file ("keymap", entry.value) == null) {
+ _metadata = default_metadata;
+ }
+ keymaps[entry.key] = new KeymapMapFile (_metadata, entry.value);
}
- rom_kana = new RomKanaMapFile (name);
+ if (metadata.locate_map_file ("rom-kana", "default") == null) {
+ _metadata = default_metadata;
+ }
+ rom_kana = new RomKanaMapFile (_metadata);
}
~Rule () {
@@ -270,6 +294,7 @@ namespace Skk {
if (FileUtils.test (metadata_filename, FileTest.EXISTS)) {
try {
var metadata = load_metadata (metadata_filename);
+ metadata.name = name;
rule_cache.set (name, metadata);
return metadata;
} catch (RuleParseError e) {

0 comments on commit bdbf931

Please sign in to comment.