Skip to content

Commit

Permalink
创建用来转换xml格式到unqualified风格的工具。
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Zhou committed Jan 2, 2013
1 parent aba07c3 commit 37b126f
Showing 1 changed file with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class ConvertToUnqualifiedStyle {
private final Logger log = LoggerFactory.getLogger(getClass());
private final File[] sources;
private final SpringExtSchemaSet schemas;
private final boolean forceConvert;
private final boolean backup;
private int convertedCount;

/** 必须运行在适当的classpath下,否则不能取得configuration points。 */
Expand All @@ -58,10 +60,16 @@ public static void main(String[] args) {
}

public ConvertToUnqualifiedStyle(File[] sources) {
this(sources, false, true);
}

public ConvertToUnqualifiedStyle(File[] sources, boolean forceConvert, boolean backup) {
LogConfigurator.getConfigurator().configureDefault();

this.sources = sources;
this.schemas = new SpringExtSchemaSet();
this.forceConvert = forceConvert;
this.backup = backup;
}

public void convert() {
Expand Down Expand Up @@ -98,7 +106,7 @@ private void convert(File source) {

boolean modified = new Converter(doc).doConvert();

if (modified) {
if (modified || forceConvert) {
File dir = source.getParentFile();
String fileName = source.getName();
int index = fileName.lastIndexOf(".");
Expand Down Expand Up @@ -133,20 +141,26 @@ private void convert(File source) {
if (failed) {
tmpFile.delete();
} else {
File backupFile;
File backupFile = null;

for (int i = 0; ; i++) {
backupFile = new File(dir, fileName + ".backup" + (i == 0 ? "" : "_" + i) + ext);
if (backup) {
for (int i = 0; ; i++) {
backupFile = new File(dir, fileName + ".backup" + (i == 0 ? "" : "_" + i) + ext);

if (!backupFile.exists()) {
break;
if (!backupFile.exists()) {
break;
}
}

source.renameTo(backupFile);
log.info(" ... converted, original content saved as {}", getRelativePath(backupFile));
} else {
source.delete();
log.info(" ... converted");
}

source.renameTo(backupFile);
tmpFile.renameTo(source);

log.info(" ... converted, original content saved as {}", getRelativePath(backupFile));
convertedCount++;
}
} else {
Expand Down

0 comments on commit 37b126f

Please sign in to comment.