Monday, May 28, 2012

First few Sub tasks.

My mentor for the project is Thomas of Catroid. First we communicated through mail and after we came to an agreement to use Skype. We have weekly meetings and additional meeting to discuss progress and problems.

First of all I was not very familiar with using Mercurial as a version control system. However I have used SVN before during my internship. Thomas introduced to me a very helpful tutorial by Joel Spolsky that helped me get the idea of Mercurial and how it differs from SVN. The tutorial is very easy to understand and fun to read.
Mercurial CVS is a distributed version control system (In contrast, SVN is a centralized version control) which means each contributor gets his own repository locally. i.e. the repo get's distributed. And the user can commit their local changed to their local repository before pushing them to the main external repository. Another distinctive change is that the changes in Mercurial are kept as changesets, which means information on what and where stuff changed gets captured and stored in revisions. There are a lot of specific characteristics to Mercurial you can learn from the tutorial.

My forst subtask related to the project was buiding a way of parsing a project related xml and extract important information from the header (first part) of the document.

Meanwhile Catroid is newly named as Catrobat programming language. And Catroid is the IDE for android devices.

The Catroid Project code XML:-


Catroid projects are saved in XML file named projectcode.xml in the device sd card. This project code xml file has the root <Content.Project>. And what follows are some general information regarding the project and the version. These are called hereafter by headertags. The headertags information can be helpful to list diplay the project metadata in the project listings. 


Extraction

There are several ways to parse an XML document. SAX and DOM are well known methods of doing so in Java. Because of the nature of extracting in this subtask in to obtain only the header information we have to look at efficiency in getting only the header information. For this I implemented two parsers, A SAX parser and a normal string parser.
String parser:-
The string parser uses a buffered reader to read the xml document. And for each line it will check for the known header tags. It will begin populating a String list with the values only after encountering the <Content.Project> tag. And will stop at the encounter of the <SpriteList> tag which marks the end of the header. The code for this is shown below. 
 
fReader = new FileReader(XMLFileStream);
   BufferedReader bReader = new BufferedReader(fReader);

   String currentLine = "";
   boolean inHeader = true;
   boolean tagCheck = false;

   while (inHeader) {
    currentLine = bReader.readLine();

    if (currentLine.trim().equals(OtherTags.CONTENTPROJECT.getOtherXMLTagString())) {
     tagCheck = true;
    }
    if (tagCheck) {
     if (currentLine.trim().equals(OtherTags.SPRITELIST.getOtherXMLTagString())) {
      inHeader = false;
      tagCheck = false;
     } else {
      for (int i = 0; i < 7; i++) {
       String currentTag = tagIndexes[i].getXmlTagString();
       if (currentLine.contains(currentTag)) {
        parsedStrings.add(currentLine.trim().replace("<" + currentTag + ">", "")
          .replace("", ""));
       }
      }
     }
    }

   }
 
This faces a problem if the elements are not seperated by new lines. That won't be a problem if the XML creating process adds new tags on new lines. HeaderTags is a enum declared to store the 7 headertag values for organized easy access anywhere.

SAX parser:

I also implemented a simple SAX parser that gets the values when the defined header tags are found in the parsed doucument in the endElement() method.

 
private List saxParser(File projectCodeXML) {
  parsedStrings = new ArrayList();
  SAXParserFactory parserFactory = SAXParserFactory.newInstance();

  try {
   SAXParser parser = parserFactory.newSAXParser();

   parser.parse(projectCodeXML, this);

  } catch (ParserConfigurationException e) {
   Log.e("SimpleParser.saxparser", "parserConfiguration exception");
   e.printStackTrace();
  } catch (SAXException e) {
   Log.e("SimpleParser.saxparser", "SAX exception");
   e.printStackTrace();
  } catch (IOException e) {
   Log.e("SimpleParser.saxparser", "IO exception");
   e.printStackTrace();
  }

  return parsedStrings;

 }

 @Override
 public void characters(char[] ch, int start, int length) throws SAXException {
  tempVal = new String(ch, start, length);
 }

 @Override
 public void endElement(String uri, String localName, String qName) throws SAXException {

  for (int i = 0; i < 7; i++) {
   String currentTag = tagIndexes[i].getXmlTagString();
   if (qName.equalsIgnoreCase(currentTag)) {
    parsedStrings.add(tempVal);
   }
  }

 }

An also a simple method was added to get the value of any header tag that was parsed by the SAX parser.
 
public String getvalueof(HeaderTags tag, File XMLFile) {
  List parsedValues = this.Parse(XMLFile);

  return parsedValues.get(tag.ordinal());

 }
The tag.ordinal returns the integer value of the HeaderTag enum.

Althought the first target was to create a Project pbject after parsing the document it has been proven difficult becuase of private attributes of that class and absence of a default constructor. So we are looking into using reflection to overcome this.

No comments:

Post a Comment