'use strict';

var fs = require('fs'), 
    path = require('path'),
    expat = require('node-expat')

var RenameHandler = function (dir, encoding) {
    this.dir = dir
    this.encoding = encoding
    this.errorHandler = function (error) {
        console.error(error)
    }
}

RenameHandler.prototype.parsePackageName = function (filename) {
    var self = this
    var filepath = path.join(this.dir, filename)

    // sax parse xml file
    var parser = new expat.Parser(this.encoding)
    parser.on('startElement', function (name, attrs) {
        if (name.match(/manifest/i)) {
            console.log('found oldPackageName => ' + attrs.package)
            self.oldPackageName = attrs.package
        }
    })

    parser.on('error', this.errorHandler)
    parser.write(fs.readFileSync(filepath))
}

RenameHandler.prototype.parseResource = function (filename) {
    // cache file to memory
    var filepath = path.join(this.dir, filename)
    var content = fs.readFileSync(filepath)

    // sax parse xml file
    var parser = new expat.Parser(this.encoding)
    parser.on('startElement', function (name, attrs) {
        for(var attrName in attrs) {
            if (attrName.match(/^xmlns:/i)) {
                console.log('found xml define: ' + attrName + " => " + attrs[attrName])
            }
        }
    })

    parser.on('error', this.errorHandler)
    parser.write(content)
}

RenameHandler.prototype.parseAndroidManifest = function (filename) {
    // cache file to memory
    var filepath = path.join(this.dir, filename)
    var content = fs.readFileSync(filepath)

    // sax parse xml file
    var parser = new expat.Parser(this.encoding)
    parser.on('startElement', function (name, attrs) {
        for(var attrName in attrs) {
            if (attrName.match(/^xmlns:/i)) {
                console.log('found xml define: ' + attrName + " => " + attrs[attrName])
            }
        }
    })

    parser.on('error', this.errorHandler)
    parser.write(content)
}

RenameHandler.prototype.parseJava = function(filename) {

}

module.exports = RenameHandler

@@ -268,24 +268,120 @@
 r)%0A    %7D%0A%7D%0A%0A
+// parse package name from android manifest file%0A// then set the value to %60this.oldPackageName%60%0A
 RenameHandle
@@ -891,24 +891,136 @@
 lepath))%0A%7D%0A%0A
+// parse resource to list all the modify point about custom xmlns defines%0A// almost it's a layout resource file%0A
 RenameHandle
@@ -1587,32 +1587,128 @@
 ite(content)%0A%7D%0A%0A
+// parse android manifest to list all the modify point about authorities of provider and action%0A
 RenameHandler.pr
@@ -2085,90 +2085,684 @@
 ch(/
-%5Exmlns:/i)) %7B%0A                console.log('found xml define: ' + attrName + %22
+provider/i)) %7B%0A                if (attrs%5B'android:authorities'%5D !== undefined) %7B%0A                    console.log('found provider authorities need to modify =%3E ' + attrs%5B'android:authorities'%5D)%0A                %7D else %7B%0A                    console.log('found provider need to add a special authorities =%3E ' + attrs%5B'android:name'%5D)%0A                %7D%0A            %7D else if (attrName.match(/action/i)) %7B%0A                if (attrs%5B'android:name'%5D !== undefined) %7B%0A                    // this action is not a system action%0A                    if (attrs%5B'android:name'%5D.match(/%5Ecom%5C.android%5C.intent%5C.action%5C./)) %7B%0A                        console.log('found action need to change
  =%3E 
-%22
+'
  + a
@@ -2762,35 +2762,81 @@
 %3E ' + attrs%5B
-attrN
+'android:n
 ame
+'
 %5D)%0A
+                    %7D%0A                %7D%0A
             
@@ -2930,65 +2930,894 @@
 %0A%7D%0A%0A
-RenameHandler.prototype.parseJava = function(filename) %7B%0A
+// parse java file to list all the modify point about import line which is .R and .BuildConfig%0ARenameHandler.prototype.parseJava = function(filename) %7B%0A    // cache file to memory%0A    var filepath = path.join(this.dir, filename)%0A    var content = fs.readFileSync(filepath)%0A    var lines = content.split('%5Cn')%0A%0A    // read each line%0A    for(var i = 0; i %3C lines.count; i++) %7B%0A        // filter import define line%0A        var importClass = lines%5Bi%5D.match(/%5E%5Cs*import (.*);/)%0A        if (importClass) %7B%0A            if (importClass%5B1%5D.match(this.oldPackageName + '.R')) %7B%0A                console.log('found import line need to change =%3E ' + this.oldPackageName + '.R')%0A            %7D else if (importClass%5B1%5D.match(this.oldPackageName + '.BuildConfig')) %7B%0A                console.log('found import line need to change =%3E ' + this.oldPackageName + '.BuildConfig')%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A%0A

add comment and parseAndroidManifest and parseJava