Package pdp.scrabble.game.impl

Source Code of pdp.scrabble.game.impl.DictionaryImpl

package pdp.scrabble.game.impl;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import pdp.scrabble.game.DicMap;
import pdp.scrabble.utility.Display;
import pdp.scrabble.game.Dictionary;
import static pdp.scrabble.Factory.FACTORY;

/**
*/
public class DictionaryImpl implements Dictionary {
    private static final boolean XML_EXPORT = false;

    /** Dictionary map. */
    private DicMap map = null;

    /** Create a new dictionary. */
    public DictionaryImpl() {
  this.map = FACTORY.createDicMap(true);
    }

    public void initWithLanguage(String language) {
  String filepath = new StringBuilder(
    PATH).append(language).append(EXTENSION).toString();

  Element root = null;
  Document dictionary = null;

  if (XML_EXPORT) {
      root = new Element("Dictionary");
      dictionary = new Document(root);
  }

  try {
      BufferedReader file = new BufferedReader(new FileReader(filepath));
      String line = file.readLine();

      while (line != null) {
    this.addWord(root, line);
    line = file.readLine();
      }
      file.close();
  }
  catch (FileNotFoundException e) {
      Display.error("Load letters", "File not found:", " " + filepath);
  }
  catch (IOException e) {
      Display.error("Load letters", "Can't read datas:", " " + filepath);
  }

  if (XML_EXPORT) {
      try {
    XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
    sortie.output(dictionary, new FileOutputStream(
      PATH + language + ".xml"));
      }
      catch (IOException e) {
    Display.error(
      "Save", "An error occured during saving dictionary !");
      }
  }
    }

    public void addWord(Element root, String word) {
  DicMap last = this.map;
  Element current = root;
  int len = word.length();

  // For each letter
  Character key = null;
  DicMap tree = null;
  for (int i = 0; i < len; i++) {

      // Get key (letter)
      key = word.charAt(i);

      // Search at this key
      tree = last.get(key);
      if (tree == null) {
    tree = FACTORY.createDicMap(true);

    // Declare as last for last letter
    if (i == (len - 1)) {
        tree.setLast();
    }
    last.put(key, tree);
      }

      // Build dictionary into xml
      // Not used because loading is not faster than
      // creation from original *.txt file !
      if (XML_EXPORT) {
    current = this.addKey(current, key);
      }
      last = tree;
  }
    }

    public boolean contains(StringBuilder word) {
  DicMap last = this.map;
  DicMap tree = null;
  int len = word.length();

  for (int i = 0; i < len; i++) {

      // Get key (letter)
      tree = last.get((Character) word.charAt(i));

      if (tree == null) {
    return false;
      }
      else {
    last = tree;
      }
  }

  if (last.isLast()) {
      return true;
  }
  return false;
    }

    private Element addKey(Element root, Character key) {
  String k = key.toString();
  Element child = root.getChild(k);

  if (child == null) {
      Element element = new Element(k);
      root.addContent(element);
      return element;
  }
  else {
      return child;
  }
    }

    public boolean isLast(String word) {
  DicMap last = this.map;
  DicMap tree = null;
  if (word == null) {
      return false;
  }

  int len = word.length();

  for (int i = 0; i < len; i++) {

      // Get key (letter)
      tree = last.get((Character) word.charAt(i));

      if (tree == null) {
    return false;
      }
      else {
    last = tree;
      }
  }

  if (last.isLast()) {
      for (int i = 0; i < 26; i++) {
    if (last.get(i) != null) {
        return false;
    }
      }
      return true;
  }
  return false;
    }

    public void load(String filename) {
  Document gamesave = null;
  SAXBuilder sxb = new SAXBuilder();
  Element root = null;

  try {
      gamesave = sxb.build(filename);
      root = gamesave.getRootElement();
  }
  catch (Exception e) {
      Display.error("Load", "An error occured during loading !");
  }
  @SuppressWarnings("unchecked")
  Iterator<Element> itr = root.getChildren().iterator();
  this.createMap(itr);
    }

    @SuppressWarnings("unchecked")
    private void createMap(Iterator<Element> itr) {
  while (itr.hasNext()) {
      Element element = itr.next();
      if (element.getContentSize() != 0) {
    this.createMap(element.getChildren().iterator());
      }
  }
    }
}
TOP

Related Classes of pdp.scrabble.game.impl.DictionaryImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.