FormatXSD.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FormatXSD {

 private static FileWriter fw = null;

 private static String [] baseDir = {
“C:\\sherpartyV5”,
“C:\\sherpartyV5-New”,
};
private static String [] filePath = {
“xmlns\\sher\\net\\mdm\\types\\common\\v5_0”,
“xmlns\\sher\\net\\mdm\\types\\contract\\domain\\v5_0”,
“xmlns\\sher\\net\\mdm\\types\\chain\\domain\\v5_0”,
“xmlns\\sher\\net\\mdm\\types\\party\\domain\\v5_0”,
“xmlns\\sher\\net\\mdm\\types\\party\\service\\party\\v5_0”
};
private static String [] fileName = {
“common.xsd”,
“domain.xsd”,
“domain.xsd”,
“domain.xsd”,
“party.xsd”
};

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println(FormatXSD.class.getPackage());

for (int i = 0; i < baseDir.length; i++){
for (int j = 0; j < filePath.length; j++){
try {
File inputFile = new File(baseDir[i]+”\\”+filePath[j]+”\\”+fileName[j]);
File outFile = new File(baseDir[i]+”\\copy\\”+filePath[j]);
outFile.mkdirs();
fw = new FileWriter(outFile+”\\”+fileName[j]);
String input=””;
Map ctMap = new HashMap<String, StringBuilder>();
readALineAtATime(inputFile, ctMap);
fw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

   }
}
}

private static void readALineAtATime(File inputFile, Map<String, StringBuilder> ctMap) throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader(inputFile));
String line;
String ctName = null;
boolean firstLine = true;
while ((line = br.readLine()) != null) {
ctName = checkLineForComplexType(line, ctMap, ctName);
}
br.close();

// sort the keys
Set<String> keySet = ctMap.keySet();
Iterator<String> it = keySet.iterator();
List<String> keyList = new ArrayList<String>();
while (it.hasNext()){
keyList.add(it.next());
}
Collections.sort(keyList);

//print the keys
it = keyList.iterator();
while (it.hasNext()){
String key = it.next();
StringBuilder sb = ctMap.get(key);
//System.out.println(sb);
writeToFile(sb);
}

//close
fw.close();
}

 private static String checkLineForComplexType(String input, Map<String, StringBuilder> ctMap, String ctName) {
StringBuilder sb = new StringBuilder();
if (input.contains(“<?xml version=”)){
sb.append(input);
ctName = “1”;
ctMap.put(ctName, sb);
}else if (input.contains(“<xsd:complexType name=”)){
int start = input.indexOf(“\””);
int end = input.indexOf(“\”>”);
ctName = input.substring(start+1, end);
sb.append(input);
ctMap.put(ctName, sb);
}else{
sb = ctMap.get(ctName);
sb.append(“\n”);
sb.append(input);
ctMap.put(ctName, sb);
}
return ctName;
}

 private static void writeToFile(StringBuilder line) {
try {
fw.append(line);
fw.append(“\n”);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.