Skip to content

Commit

Permalink
add pre-line test
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberTianzun committed Jul 23, 2015
1 parent a9c7564 commit 79595a9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
34 changes: 34 additions & 0 deletions test/xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var expat = require('node-expat')

var parser = new expat.Parser('utf-8')

var content = '<html\n\n><head>\n<title>Hello World</title>\n</head>\n<body>\n<p>\nFoobar\n</p>\n</body>\n</html>'
var lines = content.split('\n')
var currentLineNumber

var temp = 0

parser.on('startElement', function (name, attrs) {
console.log(temp, currentLineNumber, name)
temp = currentLineNumber
})

parser.on('endElement', function (name) {
temp = currentLineNumber

})

parser.on('text', function (text) {
console.log(text)
})

parser.on('error', function (error) {
console.error(error)
})

for(var i in lines) {
currentLineNumber = i
parser.write(lines[i])
}
59 changes: 53 additions & 6 deletions utils/renamehandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ RenameHandler.prototype.parseResource = function (filename) {
'xmlns' : [ ]
}
var currentLineNumber
var preLineNumber = 0

// sax parse xml file
var parser = new expat.Parser(this.encoding)
Expand All @@ -94,11 +95,17 @@ RenameHandler.prototype.parseResource = function (filename) {
}
console.log('found xml define: ' + attrName + " => " + attrs[attrName])
modifyPoints.xmlns.push({
'preLine' : preLineNumber,
'line' : currentLineNumber,
'source' : attrs[attrName]
})
}
}
preLineNumber = currentLineNumber
})

parser.on('endElement', function (name) {
preLineNumber = currentLineNumber
})

parser.on('error', self.errorHandler)
Expand Down Expand Up @@ -126,43 +133,79 @@ RenameHandler.prototype.parseAndroidManifest = function (filename) {
filepath : filepath,
lines : lines,
providers : [ ],
actions : [ ]
actions : [ ],
processes : [ ]
}
var currentLineNumber
var preLineNumber = 0

// sax parse xml file
var parser = new expat.Parser(this.encoding)
parser.on('startElement', function (name, attrs) {
if (name.match(/provider/i)) {
if (name.match('provider')) {
var target
if (attrs['android:authorities'] !== undefined) {
console.log('found provider authorities need to modify => ' + attrs['android:authorities'])
modifyPoints.providers.push({
'preLine' : preLineNumber,
'line' : currentLineNumber,
'source' : attrs['android:authorities']
'source' : attrs['android:authorities'],
'target' : target
})
} else {
console.log('found provider need to add a special authorities => ' + attrs['android:name'])
modifyPoints.providers.push({
'line' : currentLineNumber,
'source' : '>',
'action' : 'add'
'action' : 'add',
'target' : target
})
}
} else if (name.match(/action/i)) {
} else if (name.match('action')) {
if (attrs['android:name'] !== undefined) {
// this action is not a system action
if (!attrs['android:name'].match(/^android\.intent\.action\./)) {
console.log('found action need to change => ' + attrs['android:name'])
var target
if (attrs['android:name'].match(self.oldPackageName)) {
target = attrs['android:name'].replace(self.oldPackageName, self.newPackageName)
} else {
target = self.newPackageName + '.' + attrs['android:name']
}
modifyPoints.actions.push({
'preLine' : preLineNumber,
'line' : currentLineNumber,
'source' : attrs['android:name']
'source' : attrs['android:name'],
'target' : target
})
}
}
} else if (name.match('service')) {
if (attrs['android:process'] !== undefined) {
console.log('found service process need to change => ' + attrs['android:process'])
var target
if (attrs['android:process'].match(self.oldPackageName)) {
target = attrs['android:process'].replace(self.oldPackageName, self.newPackageName)
} else {
target = self.newPackageName + '.' + attrs['android:process']
}
modifyPoints.processes.push({
'preLine' : preLineNumber,
'line' : currentLineNumber,
'source' : attrs['android:process'],
'target' : target
})
}
}
preLineNumber = currentLineNumber
})

parser.on('endElement', function (name) {
preLineNumber = currentLineNumber
})

parser.on('error', this.errorHandler)

for(var i in lines) {
currentLineNumber = i
parser.write(lines[i])
Expand All @@ -175,6 +218,10 @@ RenameHandler.prototype.parseAndroidManifest = function (filename) {
if (modifyPoints.actions.length == 0) {
delete modifyPoints.actions
}

if (modifyPoints.processes.length == 0) {
delete modifyPoints.processes
}

return modifyPoints
}
Expand Down

0 comments on commit 79595a9

Please sign in to comment.